| 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"cxx . . $ `<file>/src/io/gfx/video.h`/ |
| 27 | #include "sprite.h"cxx . . $ `<file>/src/io/gfx/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 | Spritecxx . . $ Sprite#::Spritecxx . . $ Sprite#Sprite(49f6e7a06ebc5aa8). () { |
| 40 | |
| 41 | pixelscxx . . $ Sprite#pixels. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 42 | xOffsetcxx . . $ Sprite#xOffset. = 0; |
| 43 | yOffsetcxx . . $ Sprite#yOffset. = 0; |
| 44 | |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Delete the sprite. |
| 50 | */ |
| 51 | Spritecxx . . $ Sprite#::~cxx . . $ Sprite#`~Sprite`(49f6e7a06ebc5aa8).Spritecxx . . $ Sprite# () { |
| 52 | |
| 53 | videocxx . . $ video..destroySurfacecxx . . $ Video#destroySurface(60a0b30a00b4fc6d).(pixelscxx . . $ Sprite#pixels.); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Make the sprite blank. |
| 60 | */ |
| 61 | void Spritecxx . . $ Sprite#::clearPixelscxx . . $ Sprite#clearPixels(49f6e7a06ebc5aa8). () { |
| 62 | |
| 63 | unsigned char data; |
| 64 | |
| 65 | videocxx . . $ video..destroySurfacecxx . . $ Video#destroySurface(60a0b30a00b4fc6d).(pixelscxx . . $ Sprite#pixels.); |
| 66 | |
| 67 | data = 0; |
| 68 | pixelscxx . . $ Sprite#pixels. = videocxx . . $ video..createSurfacecxx . . $ Video#createSurface(5ec4649223a2dfef).(&data, 1, 1); |
| 69 | videocxx . . $ video..enableColorKeycxx . . $ Video#enableColorKey(905504f46e4e09c1).(pixelscxx . . $ Sprite#pixels., 0); |
| 70 | |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void Spritecxx . . $ Sprite#::setOffsetcxx . . $ Sprite#setOffset(af780a7c10c7990f). (short int x, short int y) { |
| 75 | |
| 76 | xOffsetcxx . . $ Sprite#xOffset. = x; |
| 77 | yOffsetcxx . . $ Sprite#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 Spritecxx . . $ Sprite#::setPixelscxx . . $ Sprite#setPixels(3f9b724c895640a0). (unsigned char *data, int width, int height, unsigned char key) { |
| 91 | |
| 92 | videocxx . . $ video..destroySurfacecxx . . $ Video#destroySurface(60a0b30a00b4fc6d).(pixelscxx . . $ Sprite#pixels.); |
| 93 | |
| 94 | pixelscxx . . $ Sprite#pixels. = videocxx . . $ video..createSurfacecxx . . $ Video#createSurface(5ec4649223a2dfef).(data, width, height); |
| 95 | videocxx . . $ video..enableColorKeycxx . . $ Video#enableColorKey(905504f46e4e09c1).(pixelscxx . . $ Sprite#pixels., key); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * Get the width of the sprite. |
| 102 | * |
| 103 | * @return The width |
| 104 | */ |
| 105 | int Spritecxx . . $ Sprite#::getWidthcxx . . $ Sprite#getWidth(b126dc7c1de90089). () { |
| 106 | |
| 107 | return pixelscxx . . $ Sprite#pixels.->wcxx . . $ SDL_Surface#w.; |
| 108 | |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Get the height of the sprite. |
| 114 | * |
| 115 | * @return The height |
| 116 | */ |
| 117 | int Spritecxx . . $ Sprite#::getHeightcxx . . $ Sprite#getHeight(b126dc7c1de90089).() { |
| 118 | |
| 119 | return pixelscxx . . $ Sprite#pixels.->hcxx . . $ SDL_Surface#h.; |
| 120 | |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Get the horizontal offset of the sprite. |
| 126 | * |
| 127 | * @return The horizontal offset |
| 128 | */ |
| 129 | int Spritecxx . . $ Sprite#::getXOffsetcxx . . $ Sprite#getXOffset(b126dc7c1de90089). () { |
| 130 | |
| 131 | return xOffsetcxx . . $ Sprite#xOffset.; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * Get the vertical offset of the sprite. |
| 138 | * |
| 139 | * @return The vertical offset |
| 140 | */ |
| 141 | int Spritecxx . . $ Sprite#::getYOffsetcxx . . $ Sprite#getYOffset(b126dc7c1de90089). () { |
| 142 | |
| 143 | return yOffsetcxx . . $ Sprite#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 Spritecxx . . $ Sprite#::setPalettecxx . . $ Sprite#setPalette(acbc52fef2632da7). (SDL_Color *palette, int start, int amount) { |
| 156 | |
| 157 | videocxx . . $ video..setSurfacePalettecxx . . $ Video#setSurfacePalette(b59f7f041c83eb39).(pixelscxx . . $ Sprite#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 Spritecxx . . $ Sprite#::flashPalettecxx . . $ Sprite#flashPalette(d4f767463ce0a6b3). (int index) { |
| 168 | |
| 169 | SDL_Color palette[MAX_PALETTE_COLORScxx . . $ `src/OpenJazz.h:71:9`!]; |
| 170 | for (int i = 0; i < MAX_PALETTE_COLORScxx . . $ `src/OpenJazz.h:71:9`!; i++) { |
| 171 | palette[i].rcxx . . $ SDL_Color#r. = palette[i].gcxx . . $ SDL_Color#g. = palette[i].bcxx . . $ SDL_Color#b. = index; |
| 172 | #if OJ_SDL3 || OJ_SDL2cxx . . $ `<built-in>:465:9`! |
| 173 | palette[i].acxx . . $ SDL_Color#a. = 0xFF; |
| 174 | #endif |
| 175 | } |
| 176 | |
| 177 | videocxx . . $ video..setSurfacePalettecxx . . $ Video#setSurfacePalette(b59f7f041c83eb39).(pixelscxx . . $ Sprite#pixels., palette, 0, MAX_PALETTE_COLORScxx . . $ `src/OpenJazz.h:71:9`!); |
| 178 | |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /** |
| 183 | * Restore the sprite's palette to its original state. |
| 184 | */ |
| 185 | void Spritecxx . . $ Sprite#::restorePalettecxx . . $ Sprite#restorePalette(49f6e7a06ebc5aa8). () { |
| 186 | |
| 187 | videocxx . . $ video..restoreSurfacePalettecxx . . $ Video#restoreSurfacePalette(60a0b30a00b4fc6d).(pixelscxx . . $ Sprite#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 Spritecxx . . $ Sprite#::drawcxx . . $ Sprite#draw(6908015b96d379c0). (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 += xOffsetcxx . . $ Sprite#xOffset.; |
| 209 | dst.y += yOffsetcxx . . $ Sprite#yOffset.; |
| 210 | |
| 211 | } |
| 212 | |
| 213 | SDL_BlitSurfacecxx . . $ `<external>/18043084669712134795/SDL_surface.h:873:9`!(pixelscxx . . $ Sprite#pixels., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, canvascxx . . $ 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 Spritecxx . . $ Sprite#::drawScaledcxx . . $ Sprite#drawScaled(b1146eedae1e1d0b). (int x, int y, fixedcxx . . $ fixed# scale) { |
| 226 | |
| 227 | int width, height, fullWidth, fullHeight; |
| 228 | int dstX, dstY; |
| 229 | int srcX, srcY; |
| 230 | |
| 231 | unsigned char key = videocxx . . $ video..getColorKeycxx . . $ Video#getColorKey(77e0245589bafc78).(pixelscxx . . $ Sprite#pixels.); |
| 232 | |
| 233 | fullWidth = FTOIcxx . . $ `src/types.h:52:9`!(pixels->w * scale); |
| 234 | if (x < -(fullWidth >> 1)) return; // Off-screen |
| 235 | if (x + (fullWidth >> 1) > canvasWcxx . . $ canvasW.) width = canvasWcxx . . $ canvasW. + (fullWidth >> 1) - x; |
| 236 | else width = fullWidth; |
| 237 | |
| 238 | fullHeight = FTOIcxx . . $ `src/types.h:52:9`!(pixels->h * scale); |
| 239 | if (y < -(fullHeight >> 1)) return; // Off-screen |
| 240 | if (y + (fullHeight >> 1) > canvasHcxx . . $ canvasH.) height = canvasHcxx . . $ canvasH. + (fullHeight >> 1) - y; |
| 241 | else height = fullHeight; |
| 242 | |
| 243 | if (SDL_MUSTLOCKcxx . . $ `<external>/18043084669712134795/SDL_surface.h:62:9`!(canvas)) SDL_LockSurface(canvascxx . . $ 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*>(pixelscxx . . $ Sprite#pixels.->pixelscxx . . $ SDL_Surface#pixels.) + (pixelscxx . . $ Sprite#pixels.->pitchcxx . . $ SDL_Surface#pitch. * DIVcxx . . $ `src/types.h:55:9`!(srcY, scale)); |
| 260 | unsigned char* dstRow = static_cast<unsigned char*>(canvascxx . . $ canvas.->pixelscxx . . $ SDL_Surface#pixels.) + (canvascxx . . $ canvas.->pitchcxx . . $ SDL_Surface#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[DIVcxx . . $ `src/types.h:55:9`!(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_MUSTLOCKcxx . . $ `<external>/18043084669712134795/SDL_surface.h:62:9`!(canvas)) SDL_UnlockSurface(canvascxx . . $ canvas.); |
| 290 | |
| 291 | } |