| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj2layer.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created level.c |
| 10 | * - 3rd February 2009: Renamed level.c to level.cpp |
| 11 | * - 19th July 2009: Created levelframe.cpp from parts of level.cpp |
| 12 | * - 29th June 2010: Created jj2levelframe.cpp from parts of levelframe.cpp |
| 13 | * - 30th June 2010: Created jj2layer.cpp from parts of jj2levelframe.cpp |
| 14 | * |
| 15 | * @par Licence: |
| 16 | * Copyright (c) 2005-2012 AJ Thomson |
| 17 | * |
| 18 | * OpenJazz is distributed under the terms of |
| 19 | * the GNU General Public License, version 2.0 |
| 20 | * |
| 21 | * @par Description: |
| 22 | * Handles JJ2 level layers. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include "jj2level.h" |
| 28 | |
| 29 | #include "io/gfx/video.h" |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Create a blank 1-by-1 layer. |
| 34 | */ |
| 35 | JJ2Layer::JJ2Layer () { |
| 36 | |
| 37 | width = height = 1; |
| 38 | tileX = tileY = 0; |
| 39 | limit = true; |
| 40 | warp = false; |
| 41 | xSpeed = ySpeed = 0; |
| 42 | |
| 43 | grid = new JJ2Tile *[1]; |
| 44 | *grid = new JJ2Tile[1]; |
| 45 | |
| 46 | (*grid)->tile = 0; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Create a blank layer. |
| 53 | * |
| 54 | * @param newWidth The width of the layer (in tiles) |
| 55 | * @param newHeight The height of the layer (in tiles) |
| 56 | * @param newXSpeed The relative horizontal speed of the layer |
| 57 | * @param newYSpeed The relative vertical speed of the layer |
| 58 | * @param flags Layer flags |
| 59 | */ |
| 60 | JJ2Layer::JJ2Layer (int flags, int newWidth, int newHeight, fixed newXSpeed, fixed newYSpeed) { |
| 61 | |
| 62 | int row; |
| 63 | |
| 64 | width = newWidth; |
| 65 | height = newHeight; |
| 66 | |
| 67 | grid = new JJ2Tile *[height]; |
| 68 | *grid = new JJ2Tile[width * height]; |
| 69 | |
| 70 | for (row = 0; row < height; row++) grid[row] = *grid + (row * width); |
| 71 | |
| 72 | tileX = flags & 1; |
| 73 | tileY = flags & 2; |
| 74 | limit = flags & 4; |
| 75 | warp = flags & 8; |
| 76 | |
| 77 | xSpeed = newXSpeed; |
| 78 | ySpeed = newYSpeed; |
| 79 | |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /** |
| 84 | * Delete the layer. |
| 85 | */ |
| 86 | JJ2Layer::~JJ2Layer () { |
| 87 | |
| 88 | delete[] *grid; |
| 89 | delete[] grid; |
| 90 | |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * Get flipped. We aim to offend! |
| 96 | * |
| 97 | * @param x The x-coordinate of the tile (in tiles) |
| 98 | * @param y The y-coordinate of the tile (in tiles) |
| 99 | * |
| 100 | * @return Whether or not the tile is flipped horizontally |
| 101 | */ |
| 102 | bool JJ2Layer::getFlipped (int x, int y) { |
| 103 | |
| 104 | if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) return false; |
| 105 | |
| 106 | return grid[y][x].flipped; |
| 107 | |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * Get the height of the layer. |
| 113 | * |
| 114 | * @return The height of the layer (in tiles) |
| 115 | */ |
| 116 | int JJ2Layer::getHeight () { |
| 117 | |
| 118 | return height; |
| 119 | |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /** |
| 124 | * Get the tile for the given co-ordinates. |
| 125 | * |
| 126 | * @param x The x-coordinate of the tile (in tiles) |
| 127 | * @param y The y-coordinate of the tile (in tiles) |
| 128 | * |
| 129 | * @return The number of the tile |
| 130 | */ |
| 131 | int JJ2Layer::getTile (int x, int y) { |
| 132 | |
| 133 | if ((x < 0) || (y < 0)) return 0; |
| 134 | |
| 135 | if ((x >= width) && !tileX) return 0; |
| 136 | if ((y >= height) && !tileY) return 0; |
| 137 | |
| 138 | return grid[y % height][x % width].tile; |
| 139 | |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * Get the width of the layer. |
| 145 | * |
| 146 | * @return The width of the layer (in tiles) |
| 147 | */ |
| 148 | int JJ2Layer::getWidth () { |
| 149 | |
| 150 | return width; |
| 151 | |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | * Set the frame of the tile at the given co-ordinates. |
| 157 | * |
| 158 | * @param x The x-coordinate of the tile (in tiles) |
| 159 | * @param y The y-coordinate of the tile (in tiles) |
| 160 | * @param frame The new frame |
| 161 | */ |
| 162 | void JJ2Layer::setFrame (int x, int y, unsigned char frame) { |
| 163 | |
| 164 | grid[y][x].frame = frame; |
| 165 | |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * Set the tile at the given co-ordinates. |
| 171 | * |
| 172 | * @param x The x-coordinate of the tile (in tiles) |
| 173 | * @param y The y-coordinate of the tile (in tiles) |
| 174 | * @param tile The number of the tile |
| 175 | * @param TSF Whether or not this is a TSF tile |
| 176 | * @param tiles The total number of tiles |
| 177 | */ |
| 178 | void JJ2Layer::setTile (int x, int y, unsigned short int tile, bool TSF, int tiles) { |
| 179 | |
| 180 | JJ2Tile* ge; |
| 181 | |
| 182 | ge = grid[y] + x; |
| 183 | |
| 184 | if (TSF) { |
| 185 | |
| 186 | ge->flipped = tile & 0x1000; |
| 187 | ge->tile = tile & 0xFFF; |
| 188 | |
| 189 | } else { |
| 190 | |
| 191 | ge->flipped = tile & 0x400; |
| 192 | ge->tile = tile & 0x3FF; |
| 193 | |
| 194 | } |
| 195 | |
| 196 | if (ge->tile > tiles) ge->tile = 0; |
| 197 | |
| 198 | ge->frame = 0; |
| 199 | |
| 200 | } |
| 201 | |
| 202 | |
| 203 | /** |
| 204 | * Draw the layer. |
| 205 | * |
| 206 | * @param tileSet The tile set to use for non-flipped tiles |
| 207 | * @param flippedTileSet The tile set to use for flipped tiles |
| 208 | */ |
| 209 | void JJ2Layer::draw (SDL_Surface* tileSet, SDL_Surface* flippedTileSet) { |
| 210 | |
| 211 | SDL_Rect src, dst; |
| 212 | int vX, vY; |
| 213 | int x, y; |
| 214 | |
| 215 | // Set tile drawing dimensions |
| 216 | src.w = TTOI(1); |
| 217 | src.h = TTOI(1); |
| 218 | src.x = 0; |
| 219 | |
| 220 | |
| 221 | // Calculate the layer view |
| 222 | vX = FTOI(FTOI(viewX) * xSpeed); |
| 223 | vY = FTOI(FTOI(viewY) * ySpeed); |
| 224 | |
| 225 | if (limit) { |
| 226 | |
| 227 | if (!tileX) { |
| 228 | |
| 229 | if (vX + canvasW > TTOI(width)) vX = TTOI(width) - canvasW; |
| 230 | |
| 231 | } |
| 232 | |
| 233 | if (!tileY) { |
| 234 | |
| 235 | vY -= canvasH - SH; |
| 236 | if (vY + canvasH > TTOI(height)) vY = TTOI(height) - canvasH; |
| 237 | |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
| 242 | for (y = 0; y <= ITOT(canvasH - 1) + 1; y++) { |
| 243 | |
| 244 | for (x = 0; x <= ITOT(canvasW - 1) + 1; x++) { |
| 245 | |
| 246 | dst.x = TTOI(x) - (vX & 31); |
| 247 | dst.y = TTOI(y) - (vY & 31); |
| 248 | src.y = TTOI(getTile(x + ITOT(vX), y + ITOT(vY))); |
| 249 | if (src.y) SDL_BlitSurface(getFlipped(x + ITOT(vX), y + ITOT(vY))? flippedTileSet: tileSet, &src, canvas, &dst); |
| 250 | |
| 251 | } |
| 252 | |
| 253 | } |
| 254 | |
| 255 | } |