| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file anim.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 "anim.h" |
| 27 | #include "sprite.h" |
| 28 | |
| 29 | #include "jj1/level/jj1level.h" |
| 30 | |
| 31 | #ifdef OJ_SDL3 |
| 32 | #include <SDL3/SDL.h> |
| 33 | #else |
| 34 | #include <SDL.h> |
| 35 | #endif |
| 36 | |
| 37 | /** |
| 38 | * Create empty animation. |
| 39 | */ |
| 40 | Anim::Anim () { |
| 41 | |
| 42 | sprites = new Sprite *[19]; |
| 43 | xOffsets = new signed char[19]; |
| 44 | yOffsets = new signed char[19]; |
| 45 | |
| 46 | shootX = shootY = 0; |
| 47 | accessoryX = accessoryY = 0; |
| 48 | yOffset = 0; |
| 49 | frames = 19; |
| 50 | frame = 0; |
| 51 | accessory = 0; |
| 52 | |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * Delete animation. |
| 58 | */ |
| 59 | Anim::~Anim () { |
| 60 | |
| 61 | delete[] sprites; |
| 62 | delete[] xOffsets; |
| 63 | delete[] yOffsets; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Set overall animation data. |
| 70 | * |
| 71 | * @param length Number of frames |
| 72 | * @param sX Bullet generation x-coordinate |
| 73 | * @param sY Bullet generation y-coordinate |
| 74 | * @param aX Accessory animation x-coordinate |
| 75 | * @param aY Accessory animation y-coordinate |
| 76 | * @param a Accessory animation index |
| 77 | * @param y Vertical offset |
| 78 | */ |
| 79 | void Anim::setData (int length, signed char sX, signed char sY, signed char aX, signed char aY, unsigned char a, signed char y) { |
| 80 | |
| 81 | if (length > 19) { |
| 82 | |
| 83 | delete[] sprites; |
| 84 | delete[] xOffsets; |
| 85 | delete[] yOffsets; |
| 86 | |
| 87 | sprites = new Sprite *[length]; |
| 88 | xOffsets = new signed char[length]; |
| 89 | yOffsets = new signed char[length]; |
| 90 | |
| 91 | } |
| 92 | |
| 93 | frames = length; |
| 94 | shootX = sX; |
| 95 | shootY = sY; |
| 96 | accessoryX = aX; |
| 97 | accessoryY = aY; |
| 98 | accessory = a; |
| 99 | yOffset = y; |
| 100 | |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * Set current frame. |
| 106 | * |
| 107 | * @param nextFrame The frame to use |
| 108 | * @param looping Whether the animation should stop at the end or loop |
| 109 | */ |
| 110 | void Anim::setFrame (int nextFrame, bool looping) { |
| 111 | |
| 112 | if (looping) frame = nextFrame % frames; |
| 113 | else frame = (nextFrame >= frames)? frames - 1: nextFrame; |
| 114 | |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * Set the data for the current frame. |
| 120 | * |
| 121 | * @param sprite Sprite to use |
| 122 | * @param x Horizontal offset |
| 123 | * @param y Vertical offset |
| 124 | */ |
| 125 | void Anim::setFrameData (Sprite *sprite, signed char x, signed char y) { |
| 126 | |
| 127 | sprites[frame] = sprite; |
| 128 | xOffsets[frame] = x; |
| 129 | yOffsets[frame] = y; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Determine the width of the current frame. |
| 136 | * |
| 137 | * @return The width of the current frame |
| 138 | */ |
| 139 | int Anim::getWidth () { |
| 140 | |
| 141 | return sprites[frame]->getWidth(); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * Determine the height of the current frame. |
| 148 | * |
| 149 | * @return The height of the current frame |
| 150 | */ |
| 151 | int Anim::getHeight () { |
| 152 | |
| 153 | return sprites[frame]->getHeight(); |
| 154 | |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * Determine the length of the animation. |
| 160 | * |
| 161 | * @return The length of the animation |
| 162 | */ |
| 163 | int Anim::getLength () { |
| 164 | |
| 165 | return frames; |
| 166 | |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /** |
| 171 | * Determine the bullet generation x-coordinate of the current frame. |
| 172 | * |
| 173 | * @return The bullet generation x-coordinate |
| 174 | */ |
| 175 | fixed Anim::getShootX () { |
| 176 | |
| 177 | return ITOF(shootX + xOffsets[frame]) << 2; |
| 178 | |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /** |
| 183 | * Determine the bullet generation y-coordinate of the current frame. |
| 184 | * |
| 185 | * @return The bullet generation y-coordinate |
| 186 | */ |
| 187 | fixed Anim::getShootY () { |
| 188 | |
| 189 | return ITOF(shootY + yOffsets[frame]); |
| 190 | |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /** |
| 195 | * Determine the accessory bullet generation x-coordinate of the current frame. |
| 196 | * |
| 197 | * @return The accessory bullet generation x-coordinate |
| 198 | */ |
| 199 | fixed Anim::getAccessoryShootX () { |
| 200 | |
| 201 | return ITOF(shootX + accessoryX + xOffsets[frame]) << 2; |
| 202 | |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /** |
| 207 | * Determine the accessory bullet generation y-coordinate of the current frame. |
| 208 | * |
| 209 | * @return The accessory bullet generation y-coordinate |
| 210 | */ |
| 211 | fixed Anim::getAccessoryShootY () { |
| 212 | |
| 213 | return ITOF(shootY + accessoryY + yOffsets[frame]); |
| 214 | |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /** |
| 219 | * Determine the vertical offset. |
| 220 | * |
| 221 | * @return The vertical offset |
| 222 | */ |
| 223 | fixed Anim::getOffset () { |
| 224 | |
| 225 | return -ITOF(yOffset); |
| 226 | |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /** |
| 231 | * Determine the horizontal offset of the current frame. |
| 232 | * |
| 233 | * @return The horizontal offset |
| 234 | */ |
| 235 | fixed Anim::getXOffset () { |
| 236 | |
| 237 | return ITOF(sprites[frame]->getXOffset() + (xOffsets[frame] << 2) + 1); |
| 238 | |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /** |
| 243 | * Determine the vertical offset of the current frame. |
| 244 | * |
| 245 | * @return The vertical offset |
| 246 | */ |
| 247 | fixed Anim::getYOffset () { |
| 248 | |
| 249 | return ITOF(sprites[frame]->getYOffset() + yOffsets[frame] + 1); |
| 250 | |
| 251 | } |
| 252 | |
| 253 | |
| 254 | /** |
| 255 | * Draw current frame. |
| 256 | * |
| 257 | * @param x X-coordinate at which to draw |
| 258 | * @param y Y-coordinate at which to draw |
| 259 | * @param accessories Number of accessory animations to draw |
| 260 | */ |
| 261 | void Anim::draw (fixed x, fixed y, int accessories) { |
| 262 | |
| 263 | sprites[frame]->draw( |
| 264 | FTOI(x) + (xOffsets[frame] << 2), |
| 265 | FTOI(y) + yOffsets[frame] - yOffset); |
| 266 | |
| 267 | |
| 268 | if (accessories && accessory) { |
| 269 | |
| 270 | Anim* anim = level->getAnim(accessory); |
| 271 | anim->setFrame(frame, true); |
| 272 | anim->draw( |
| 273 | x + ITOF(accessoryX << 2), |
| 274 | y + ITOF(accessoryY - yOffset) - anim->getOffset(), |
| 275 | accessories - 1); |
| 276 | |
| 277 | } |
| 278 | |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /** |
| 283 | * Draw current frame scaled. |
| 284 | * |
| 285 | * @param x X-coordinate at which to draw |
| 286 | * @param y Y-coordinate at which to draw |
| 287 | * @param scale Scaling factor |
| 288 | */ |
| 289 | void Anim::drawScaled (fixed x, fixed y, fixed scale) { |
| 290 | |
| 291 | // Used to draw bonus level player, so no offset |
| 292 | sprites[frame]->drawScaled(FTOI(x), FTOI(y), scale); |
| 293 | |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /** |
| 298 | * Set the current frame's palette. |
| 299 | * |
| 300 | * @param palette The new palette to use |
| 301 | * @param start The first entry to use |
| 302 | * @param amount The number of entries to use |
| 303 | */ |
| 304 | void Anim::setPalette (SDL_Color *palette, int start, int amount) { |
| 305 | |
| 306 | sprites[frame]->setPalette(palette, start, amount); |
| 307 | |
| 308 | } |
| 309 | |
| 310 | |
| 311 | /** |
| 312 | * Turn the whole of the current frame a single colour. |
| 313 | * |
| 314 | * @param index The index of the colour to use |
| 315 | */ |
| 316 | void Anim::flashPalette (int index) { |
| 317 | |
| 318 | sprites[frame]->flashPalette(index); |
| 319 | |
| 320 | } |
| 321 | |
| 322 | |
| 323 | /** |
| 324 | * Restore the current frame's original palette. |
| 325 | */ |
| 326 | void Anim::restorePalette () { |
| 327 | |
| 328 | sprites[frame]->restorePalette(); |
| 329 | |
| 330 | } |