| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file sprite.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created level.c |
| 10 | * - 1st January 2006: Created events.c from parts of level.c |
| 11 | * - 3rd February 2009: Renamed events.c to events.cpp and level.c to level.cpp, |
| 12 | * created player.cpp |
| 13 | * - 5th February 2009: Added parts of events.cpp and level.cpp to player.cpp |
| 14 | * - 19th March 2009: Created sprite.cpp from parts of event.cpp and player.cpp |
| 15 | * - 26th July 2009: Created anim.cpp from parts of sprite.cpp |
| 16 | * |
| 17 | * @par Licence: |
| 18 | * Copyright (c) 2005-2013 AJ Thomson |
| 19 | * |
| 20 | * OpenJazz is distributed under the terms of |
| 21 | * the GNU General Public License, version 2.0 |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "video.h" |
| 27 | #include "sprite.h" |
| 28 | |
| 29 | #ifdef OJ_SDL3 |
| 30 | #include <SDL3/SDL.h> |
| 31 | #else |
| 32 | #include <SDL.h> |
| 33 | #endif |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Create a sprite. |
| 38 | */ |
| 39 | Sprite::Sprite () { |
| 40 | |
| 41 | pixels = NULL; |
| 42 | xOffset = 0; |
| 43 | yOffset = 0; |
| 44 | |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Delete the sprite. |
| 50 | */ |
| 51 | Sprite::~Sprite () { |
| 52 | |
| 53 | video.destroySurface(pixels); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Make the sprite blank. |
| 60 | */ |
| 61 | void Sprite::clearPixels () { |
| 62 | |
| 63 | unsigned char data; |
| 64 | |
| 65 | video.destroySurface(pixels); |
| 66 | |
| 67 | data = 0; |
| 68 | pixels = video.createSurface(&data, 1, 1); |
| 69 | video.enableColorKey(pixels, 0); |
| 70 | |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void Sprite::setOffset (short int x, short int y) { |
| 75 | |
| 76 | xOffset = x; |
| 77 | yOffset = y; |
| 78 | |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Set new pixel data for the sprite. |
| 84 | * |
| 85 | * @param data The new pixel data |
| 86 | * @param width The width of the sprite image |
| 87 | * @param height The height of the sprite image |
| 88 | * @param key The transparent pixel value |
| 89 | */ |
| 90 | void Sprite::setPixels (unsigned char *data, int width, int height, unsigned char key) { |
| 91 | |
| 92 | video.destroySurface(pixels); |
| 93 | |
| 94 | pixels = video.createSurface(data, width, height); |
| 95 | video.enableColorKey(pixels, key); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * Get the width of the sprite. |
| 102 | * |
| 103 | * @return The width |
| 104 | */ |
| 105 | int Sprite::getWidth () { |
| 106 | |
| 107 | return pixels->w; |
| 108 | |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Get the height of the sprite. |
| 114 | * |
| 115 | * @return The height |
| 116 | */ |
| 117 | int Sprite::getHeight() { |
| 118 | |
| 119 | return pixels->h; |
| 120 | |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Get the horizontal offset of the sprite. |
| 126 | * |
| 127 | * @return The horizontal offset |
| 128 | */ |
| 129 | int Sprite::getXOffset () { |
| 130 | |
| 131 | return xOffset; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * Get the vertical offset of the sprite. |
| 138 | * |
| 139 | * @return The vertical offset |
| 140 | */ |
| 141 | int Sprite::getYOffset () { |
| 142 | |
| 143 | return yOffset; |
| 144 | |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * Set the sprite's palette, or a portion thereof. |
| 150 | * |
| 151 | * @param palette New palette |
| 152 | * @param start First colour to change |
| 153 | * @param amount Number of colours to change |
| 154 | */ |
| 155 | void Sprite::setPalette (SDL_Color *palette, int start, int amount) { |
| 156 | |
| 157 | video.setSurfacePalette(pixels, palette + start, start, amount); |
| 158 | |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * Map the whole of the sprite's palette to one index. |
| 164 | * |
| 165 | * @param index The index to use |
| 166 | */ |
| 167 | void Sprite::flashPalette (int index) { |
| 168 | |
| 169 | SDL_Color palette[MAX_PALETTE_COLORS]; |
| 170 | for (int i = 0; i < MAX_PALETTE_COLORS; i++) { |
| 171 | palette[i].r = palette[i].g = palette[i].b = index; |
| 172 | #if OJ_SDL3 || OJ_SDL2 |
| 173 | palette[i].a = 0xFF; |
| 174 | #endif |
| 175 | } |
| 176 | |
| 177 | video.setSurfacePalette(pixels, palette, 0, MAX_PALETTE_COLORS); |
| 178 | |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /** |
| 183 | * Restore the sprite's palette to its original state. |
| 184 | */ |
| 185 | void Sprite::restorePalette () { |
| 186 | |
| 187 | video.restoreSurfacePalette(pixels); |
| 188 | |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * Draw the sprite |
| 194 | * |
| 195 | * @param x The x-coordinate at which to draw the sprite |
| 196 | * @param y The y-coordinate at which to draw the sprite |
| 197 | * @param includeOffsets Whether or not to include the sprite's offsets |
| 198 | */ |
| 199 | void Sprite::draw (int x, int y, bool includeOffsets) { |
| 200 | |
| 201 | SDL_Rect dst; |
| 202 | |
| 203 | dst.x = x; |
| 204 | dst.y = y; |
| 205 | |
| 206 | if (includeOffsets) { |
| 207 | |
| 208 | dst.x += xOffset; |
| 209 | dst.y += yOffset; |
| 210 | |
| 211 | } |
| 212 | |
| 213 | SDL_BlitSurface(pixels, NULL, canvas, &dst); |
| 214 | |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /** |
| 219 | * Draw the sprite scaled |
| 220 | * |
| 221 | * @param x The x-coordinate at which to draw the sprite |
| 222 | * @param y The y-coordinate at which to draw the sprite |
| 223 | * @param scale The amount by which to scale the sprite |
| 224 | */ |
| 225 | void Sprite::drawScaled (int x, int y, fixed scale) { |
| 226 | |
| 227 | int width, height, fullWidth, fullHeight; |
| 228 | int dstX, dstY; |
| 229 | int srcX, srcY; |
| 230 | |
| 231 | unsigned char key = video.getColorKey(pixels); |
| 232 | |
| 233 | fullWidth = FTOI(pixels->w * scale); |
| 234 | if (x < -(fullWidth >> 1)) return; // Off-screen |
| 235 | if (x + (fullWidth >> 1) > canvasW) width = canvasW + (fullWidth >> 1) - x; |
| 236 | else width = fullWidth; |
| 237 | |
| 238 | fullHeight = FTOI(pixels->h * scale); |
| 239 | if (y < -(fullHeight >> 1)) return; // Off-screen |
| 240 | if (y + (fullHeight >> 1) > canvasH) height = canvasH + (fullHeight >> 1) - y; |
| 241 | else height = fullHeight; |
| 242 | |
| 243 | if (SDL_MUSTLOCK(canvas)) SDL_LockSurface(canvas); |
| 244 | |
| 245 | if (y < (fullHeight >> 1)) { |
| 246 | |
| 247 | srcY = (fullHeight >> 1) - y; |
| 248 | dstY = 0; |
| 249 | |
| 250 | } else { |
| 251 | |
| 252 | srcY = 0; |
| 253 | dstY = y - (fullHeight >> 1); |
| 254 | |
| 255 | } |
| 256 | |
| 257 | while (srcY < height) { |
| 258 | |
| 259 | unsigned char* srcRow = static_cast<unsigned char*>(pixels->pixels) + (pixels->pitch * DIV(srcY, scale)); |
| 260 | unsigned char* dstRow = static_cast<unsigned char*>(canvas->pixels) + (canvas->pitch * dstY); |
| 261 | |
| 262 | if (x < (fullWidth >> 1)) { |
| 263 | |
| 264 | srcX = (fullWidth >> 1) - x; |
| 265 | dstX = 0; |
| 266 | |
| 267 | } else { |
| 268 | |
| 269 | srcX = 0; |
| 270 | dstX = x - (fullWidth >> 1); |
| 271 | |
| 272 | } |
| 273 | |
| 274 | while (srcX < width) { |
| 275 | |
| 276 | unsigned char pixel = srcRow[DIV(srcX, scale)]; |
| 277 | if (pixel != key) dstRow[dstX] = pixel; |
| 278 | |
| 279 | srcX++; |
| 280 | dstX++; |
| 281 | |
| 282 | } |
| 283 | |
| 284 | srcY++; |
| 285 | dstY++; |
| 286 | |
| 287 | } |
| 288 | |
| 289 | if (SDL_MUSTLOCK(canvas)) SDL_UnlockSurface(canvas); |
| 290 | |
| 291 | } |