| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1bonuslevel.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created bonus.c |
| 10 | * - 3rd February 2009: Renamed bonus.c to bonus.cpp |
| 11 | * - 1st August 2012: Renamed bonus.cpp to jj1bonuslevel.cpp |
| 12 | * |
| 13 | * @par Licence: |
| 14 | * Copyright (c) 2005-2017 AJ Thomson |
| 15 | * |
| 16 | * OpenJazz is distributed under the terms of |
| 17 | * the GNU General Public License, version 2.0 |
| 18 | * |
| 19 | * @par Description: |
| 20 | * Deals with the loading, running and freeing of bonus levels. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include "jj1bonuslevelplayer.h" |
| 26 | #include "jj1bonuslevel.h" |
| 27 | |
| 28 | #include "game/game.h" |
| 29 | #include "game/gamemode.h" |
| 30 | #include "io/controls.h" |
| 31 | #include "io/file.h" |
| 32 | #include "io/gfx/font.h" |
| 33 | #include "io/gfx/paletteeffects.h" |
| 34 | #include "io/gfx/sprite.h" |
| 35 | #include "io/gfx/video.h" |
| 36 | #include "io/sound.h" |
| 37 | #include "io/log.h" |
| 38 | #include "util.h" |
| 39 | |
| 40 | #include <string.h> |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Load sprites. |
| 45 | * |
| 46 | * @return Error code |
| 47 | */ |
| 48 | int JJ1BonusLevel::loadSprites () { |
| 49 | |
| 50 | FilePtr file; |
| 51 | unsigned char* pixels; |
| 52 | |
| 53 | try { |
| 54 | |
| 55 | file = std::make_unique<File>("BONUS.000", PATH_TYPE_GAME); |
| 56 | |
| 57 | } catch (int e) { |
| 58 | |
| 59 | return e; |
| 60 | |
| 61 | } |
| 62 | |
| 63 | file->seek(2, true); |
| 64 | |
| 65 | sprites = file->loadShort(256); |
| 66 | spriteSet = new Sprite[sprites]; |
| 67 | |
| 68 | for (int i = 0; i < sprites; i++) { |
| 69 | |
| 70 | // Load dimensions |
| 71 | int width = file->loadShort(SW); |
| 72 | int height = file->loadShort(SH); |
| 73 | |
| 74 | int pixelsLength = file->loadShort(); |
| 75 | int maskLength = file->loadShort(); |
| 76 | |
| 77 | // Sprites can be either masked or not masked. |
| 78 | if (pixelsLength != 0xFFFF) { |
| 79 | |
| 80 | // Masked |
| 81 | width <<= 2; |
| 82 | |
| 83 | int pos = file->tell() + (pixelsLength << 2) + maskLength; |
| 84 | |
| 85 | // Read scrambled, masked pixel data |
| 86 | pixels = file->loadPixels(width * height, 0); |
| 87 | spriteSet[i].setPixels(pixels, width, height, 0); |
| 88 | |
| 89 | delete[] pixels; |
| 90 | |
| 91 | file->seek(pos, true); |
| 92 | |
| 93 | } else if (width) { |
| 94 | |
| 95 | // Not masked |
| 96 | |
| 97 | // Read pixel data |
| 98 | pixels = file->loadBlock(width * height); |
| 99 | spriteSet[i].setPixels(pixels, width, height, 0); |
| 100 | |
| 101 | delete[] pixels; |
| 102 | |
| 103 | } else { |
| 104 | |
| 105 | // Zero-length sprite |
| 106 | |
| 107 | // Create blank sprite |
| 108 | spriteSet[i].clearPixels(); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | return E_NONE; |
| 115 | |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /** |
| 120 | * Load the tileset. |
| 121 | * |
| 122 | * @param fileName Name of the file containing the tileset |
| 123 | * |
| 124 | * @return Error code |
| 125 | */ |
| 126 | int JJ1BonusLevel::loadTiles (char *fileName) { |
| 127 | |
| 128 | FilePtr file; |
| 129 | unsigned char *pixels; |
| 130 | unsigned char *sorted; |
| 131 | int count, x, y; |
| 132 | |
| 133 | direction = 0; |
| 134 | |
| 135 | try { |
| 136 | |
| 137 | file = std::make_unique<File>(fileName, PATH_TYPE_GAME); |
| 138 | |
| 139 | } catch (int e) { |
| 140 | |
| 141 | return e; |
| 142 | |
| 143 | } |
| 144 | |
| 145 | // Load background |
| 146 | pixels = file->loadRLE(832 * 20); |
| 147 | sorted = new unsigned char[512 * 20]; |
| 148 | |
| 149 | for (count = 0; count < 20; count++) memcpy(sorted + (count * 512), pixels + (count * 832), 512); |
| 150 | |
| 151 | background = video.createSurface(sorted, 512, 20); |
| 152 | |
| 153 | delete[] sorted; |
| 154 | delete[] pixels; |
| 155 | |
| 156 | // Load palette |
| 157 | file->loadPalette(palette); |
| 158 | |
| 159 | // Load tile graphics |
| 160 | pixels = file->loadRLE(1024 * 60); |
| 161 | tileSet = video.createSurface(pixels, 32, 32 * 60); |
| 162 | |
| 163 | // Create mask |
| 164 | for (count = 0; count < 60; count++) { |
| 165 | |
| 166 | memset(mask[count], 0, 64); |
| 167 | |
| 168 | for (y = 0; y < 32; y++) { |
| 169 | |
| 170 | for (x = 0; x < 32; x++) { |
| 171 | |
| 172 | if ((pixels[(count << 10) + (y << 5) + x] & 240) == 192) |
| 173 | mask[count][((y << 1) & 56) + ((x >> 2) & 7)] = 1; |
| 174 | |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |
| 179 | } |
| 180 | |
| 181 | delete[] pixels; |
| 182 | |
| 183 | return E_NONE; |
| 184 | |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /** |
| 189 | * Create a JJ1 bonus level. |
| 190 | * |
| 191 | * @param owner The current game |
| 192 | * @param fileName Name of the file containing the level data. |
| 193 | * @param multi Whether or not the level will be multi-player |
| 194 | */ |
| 195 | JJ1BonusLevel::JJ1BonusLevel (Game* owner, char * fileName, bool multi) : Level(owner) { |
| 196 | |
| 197 | Anim* pAnims[BPANIMS]; |
| 198 | unsigned short int soundRates[BSOUNDS]; |
| 199 | FilePtr file; |
| 200 | unsigned char *buffer; |
| 201 | char *string, *fileString; |
| 202 | int count, x, y; |
| 203 | |
| 204 | try { |
| 205 | |
| 206 | font = new Font(true); |
| 207 | |
| 208 | } catch (int e) { |
| 209 | |
| 210 | throw; |
| 211 | |
| 212 | } |
| 213 | #if DEBUG_FONTS |
| 214 | font->saveAtlasAsBMP("bonusfont.bmp"); |
| 215 | #endif |
| 216 | |
| 217 | try { |
| 218 | |
| 219 | file = std::make_unique<File>(fileName, PATH_TYPE_GAME); |
| 220 | |
| 221 | } catch (int e) { |
| 222 | |
| 223 | delete font; |
| 224 | |
| 225 | throw; |
| 226 | |
| 227 | } |
| 228 | |
| 229 | // Load sprites |
| 230 | count = loadSprites(); |
| 231 | |
| 232 | if (count < 0) { |
| 233 | |
| 234 | delete font; |
| 235 | |
| 236 | throw count; |
| 237 | |
| 238 | } |
| 239 | |
| 240 | // Skip Editor data files |
| 241 | file->seek(10 * 9, true); |
| 242 | |
| 243 | // Load tileset |
| 244 | string = file->loadTerminatedString(8); |
| 245 | fileString = createFileName(string, 0); |
| 246 | x = loadTiles(fileString); |
| 247 | delete[] string; |
| 248 | delete[] fileString; |
| 249 | |
| 250 | if (x != E_NONE) throw x; |
| 251 | |
| 252 | // Skip Editor tileset files |
| 253 | file->seek(9 + 13); |
| 254 | |
| 255 | // Load music |
| 256 | fileString = file->loadTerminatedString(12); |
| 257 | playMusic(fileString); |
| 258 | delete[] fileString; |
| 259 | |
| 260 | |
| 261 | // Load animations |
| 262 | buffer = file->loadBlock(BANIMS << 6); |
| 263 | |
| 264 | // Create animation set based on that data |
| 265 | for (count = 0; count < BANIMS; count++) { |
| 266 | |
| 267 | animSet[count].setData(buffer[(count << 6) + 6], |
| 268 | buffer[count << 6], buffer[(count << 6) + 1], |
| 269 | buffer[(count << 6) + 3], buffer[(count << 6) + 4], |
| 270 | buffer[(count << 6) + 2], buffer[(count << 6) + 5]); |
| 271 | |
| 272 | for (y = 0; y < buffer[(count << 6) + 6]; y++) { |
| 273 | |
| 274 | // Get frame |
| 275 | x = buffer[(count << 6) + 7 + y]; |
| 276 | if (x > sprites){ |
| 277 | LOG_DEBUG("Clipping Sprite index %d > %d", x, sprites); |
| 278 | x = sprites; |
| 279 | } |
| 280 | |
| 281 | // Assign sprite and vertical offset |
| 282 | animSet[count].setFrame(y, true); |
| 283 | animSet[count].setFrameData(spriteSet + x, |
| 284 | buffer[(count << 6) + 26 + y], buffer[(count << 6) + 45 + y]); |
| 285 | |
| 286 | } |
| 287 | |
| 288 | } |
| 289 | |
| 290 | delete[] buffer; |
| 291 | |
| 292 | |
| 293 | #if DEBUG_LOAD |
| 294 | // Show animation names |
| 295 | for (count = 0; count < BANIMS; count++) { |
| 296 | char *tmpName = file->loadTerminatedString(15); |
| 297 | if(strlen(tmpName)) { |
| 298 | LOG_MAX("Animation id %d is named \"%s\"", count, tmpName); |
| 299 | } |
| 300 | delete[] tmpName; |
| 301 | } |
| 302 | #else |
| 303 | // Skip animation names |
| 304 | file->seek(BANIMS * 16); |
| 305 | #endif |
| 306 | |
| 307 | |
| 308 | // Load tiles |
| 309 | buffer = file->loadRLE(BLW * BLH); |
| 310 | |
| 311 | for (y = 0; y < BLH; y++) { |
| 312 | for (x = 0; x < BLW; x++) { |
| 313 | |
| 314 | grid[y][x].tile = buffer[x + (y * BLW)]; |
| 315 | if (grid[y][x].tile > 59) { |
| 316 | LOG_DEBUG("Clipping Tile index %d > %d", grid[y][x].tile, 59); |
| 317 | grid[y][x].tile = 59; |
| 318 | } |
| 319 | |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | delete[] buffer; |
| 324 | |
| 325 | |
| 326 | // Load event properties |
| 327 | buffer = file->loadRLE(BEVENTS * 16); |
| 328 | for (count = 0; count < BEVENTS; count++) { |
| 329 | eventSet[count].anim = buffer[(count * BEVENTS) + 5]; |
| 330 | eventSet[count].type = buffer[(count * BEVENTS) + 7]; |
| 331 | eventSet[count].passable = buffer[(count * BEVENTS) + 8]; |
| 332 | eventSet[count].used = buffer[(count * BEVENTS) + 9]; |
| 333 | |
| 334 | auto se = static_cast<SE::Type>(buffer[(count * BEVENTS) + 13]); |
| 335 | if (!isValidSoundIndex(se)) { |
| 336 | eventSet[count].sound = SE::NONE; |
| 337 | LOG_WARN("Event %d has invalid sound effect %d.", count, se); |
| 338 | } else { |
| 339 | eventSet[count].sound = se; |
| 340 | } |
| 341 | |
| 342 | // This is a hack to make "exit signs" work in OJ |
| 343 | if(eventSet[count].type == 7) |
| 344 | eventSet[count].passable = 1; |
| 345 | |
| 346 | #if DEBUG_LOAD |
| 347 | //hexDump(nullptr, buffer + count * BEVENTS, 16); |
| 348 | LOG_MAX("Event %d: anim=%d, type=%d, passable=%d, sound=%d", |
| 349 | count, eventSet[count].anim, eventSet[count].type, |
| 350 | eventSet[count].passable, eventSet[count].sound); |
| 351 | #endif |
| 352 | } |
| 353 | delete[] buffer; |
| 354 | |
| 355 | |
| 356 | // Load event mapping |
| 357 | buffer = file->loadRLE(BLW * BLH); |
| 358 | |
| 359 | for (y = 0; y < BLW; y++) { |
| 360 | for (x = 0; x < BLH; x++) { |
| 361 | |
| 362 | grid[y][x].event = buffer[x + (y * BLW)]; |
| 363 | |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | delete[] buffer; |
| 368 | |
| 369 | |
| 370 | // Load sound map |
| 371 | for (count = 0; count < BSOUNDS; count++) { |
| 372 | soundRates[count] = file->loadShort(); |
| 373 | } |
| 374 | for (count = 0; count < BSOUNDS; count++) { |
| 375 | char *tmpName = file->loadTerminatedString(8); |
| 376 | resampleSound(count, tmpName, soundRates[count]); |
| 377 | delete[] tmpName; |
| 378 | } |
| 379 | |
| 380 | // Unknown marker |
| 381 | if(file->loadShort() != 0xFFFF) { |
| 382 | LOG_WARN("Invalid bonus level"); |
| 383 | throw E_FILE; |
| 384 | } |
| 385 | |
| 386 | // Set the tick at which the level will end |
| 387 | endTime = file->loadShort() * 1000; |
| 388 | |
| 389 | |
| 390 | // Number of gems to collect |
| 391 | items = file->loadShort(); |
| 392 | |
| 393 | |
| 394 | // The players' coordinates |
| 395 | x = file->loadShort(); |
| 396 | y = file->loadShort(); |
| 397 | |
| 398 | |
| 399 | // Generate player's animation set references |
| 400 | for (count = 0; count < BPANIMS; count++) |
| 401 | pAnims[count] = animSet + count; |
| 402 | |
| 403 | |
| 404 | createLevelPlayers(LT_JJ1BONUS, pAnims, NULL, false, x, y); |
| 405 | |
| 406 | |
| 407 | // Palette animations |
| 408 | |
| 409 | // Spinny whirly thing |
| 410 | paletteEffects = new RotatePaletteEffect(112, 16, F32, NULL); |
| 411 | |
| 412 | // Track sides |
| 413 | paletteEffects = new RotatePaletteEffect(192, 16, F32, paletteEffects); |
| 414 | |
| 415 | // Bouncy things |
| 416 | paletteEffects = new RotatePaletteEffect(240, 16, F32, paletteEffects); |
| 417 | |
| 418 | |
| 419 | // Adjust panelBigFont to use bonus level palette |
| 420 | panelBigFont->mapPalette(0, 32, 15, -16); |
| 421 | |
| 422 | |
| 423 | multiplayer = multi; |
| 424 | |
| 425 | video.setTitle("Bonus Level"); |
| 426 | |
| 427 | } |
| 428 | |
| 429 | |
| 430 | /** |
| 431 | * Delete the JJ1 bonus level. |
| 432 | */ |
| 433 | JJ1BonusLevel::~JJ1BonusLevel () { |
| 434 | |
| 435 | // Restore panelBigFont palette |
| 436 | panelBigFont->restorePalette(); |
| 437 | |
| 438 | video.destroySurface(tileSet); |
| 439 | video.destroySurface(background); |
| 440 | |
| 441 | delete[] spriteSet; |
| 442 | |
| 443 | delete font; |
| 444 | |
| 445 | resampleSounds(); |
| 446 | |
| 447 | video.setTitle(NULL); |
| 448 | |
| 449 | } |
| 450 | |
| 451 | |
| 452 | /** |
| 453 | * Determine whether or not the given point is in the event area of its tile. |
| 454 | * |
| 455 | * @param x X-coordinate |
| 456 | * @param y Y-coordinate |
| 457 | * |
| 458 | * @return True if in the event area |
| 459 | */ |
| 460 | bool JJ1BonusLevel::isEvent (fixed x, fixed y) { |
| 461 | |
| 462 | return ((x & 32767) > F12) && ((x & 32767) < F20) && |
| 463 | ((y & 32767) > F12) && ((y & 32767) < F20); |
| 464 | |
| 465 | } |
| 466 | |
| 467 | |
| 468 | /** |
| 469 | * Determine whether or not the given point is solid. |
| 470 | * |
| 471 | * @param x X-coordinate |
| 472 | * @param y Y-coordinate |
| 473 | * |
| 474 | * @return Solidity |
| 475 | */ |
| 476 | bool JJ1BonusLevel::checkMask (fixed x, fixed y) { |
| 477 | |
| 478 | JJ1BonusLevelGridElement *ge = grid[FTOT(y) & 255] + (FTOT(x) & 255); |
| 479 | |
| 480 | // Bounce back |
| 481 | if ((eventSet[ge->event].used) && !(eventSet[ge->event].passable) && isEvent(x, y)) |
| 482 | return true; |
| 483 | |
| 484 | // Check the mask in the tile in question |
| 485 | return mask[ge->tile][((y >> 9) & 56) + ((x >> 12) & 7)]; |
| 486 | |
| 487 | } |
| 488 | |
| 489 | |
| 490 | /** |
| 491 | * Interpret data received from client/server |
| 492 | * |
| 493 | * @param buffer Received data |
| 494 | */ |
| 495 | void JJ1BonusLevel::receive (unsigned char* buffer) { |
| 496 | |
| 497 | switch (buffer[1]) { |
| 498 | |
| 499 | case MT_L_PROP: |
| 500 | |
| 501 | if (buffer[2] == 2) { |
| 502 | |
| 503 | if (stage == LS_NORMAL) |
| 504 | endTime += buffer[3] * 1000; |
| 505 | |
| 506 | } |
| 507 | |
| 508 | break; |
| 509 | |
| 510 | case MT_L_GRID: |
| 511 | |
| 512 | if (buffer[4] == 0) grid[buffer[3]][buffer[2]].tile = buffer[5]; |
| 513 | else if (buffer[4] == 2) |
| 514 | grid[buffer[3]][buffer[2]].event = buffer[5]; |
| 515 | |
| 516 | break; |
| 517 | |
| 518 | case MT_L_STAGE: |
| 519 | |
| 520 | stage = LevelStage(buffer[2]); |
| 521 | |
| 522 | break; |
| 523 | |
| 524 | } |
| 525 | |
| 526 | } |
| 527 | |
| 528 | |
| 529 | /** |
| 530 | * Level iteration. |
| 531 | * |
| 532 | * @return Error code |
| 533 | */ |
| 534 | int JJ1BonusLevel::step () { |
| 535 | // Check if time has run out |
| 536 | if (ticks > endTime) { |
| 537 | playSound(SE::OW); |
| 538 | |
| 539 | return LOST; |
| 540 | } |
| 541 | |
| 542 | // Apply controls to local player |
| 543 | for (int i = 0; i < PCONTROLS; i++) |
| 544 | localPlayer->setControl(i, controls.getState(i)); |
| 545 | |
| 546 | // Process players |
| 547 | for (int i = 0; i < nPlayers; i++) { |
| 548 | |
| 549 | JJ1BonusLevelPlayer* bonusPlayer = players[i].getJJ1BonusLevelPlayer(); |
| 550 | |
| 551 | fixed playerX = bonusPlayer->getX(); |
| 552 | fixed playerY = bonusPlayer->getY(); |
| 553 | |
| 554 | bonusPlayer->step(ticks, 16, this); |
| 555 | |
| 556 | if ((bonusPlayer->getZ() < FH) && isEvent(playerX, playerY)) { |
| 557 | |
| 558 | int gridX = FTOT(playerX) & 255; |
| 559 | int gridY = FTOT(playerY) & 255; |
| 560 | |
| 561 | // Play sound if available |
| 562 | playSound(eventSet[grid[gridY][gridX].event].sound); |
| 563 | |
| 564 | switch (eventSet[grid[gridY][gridX].event].type) { |
| 565 | |
| 566 | case 1: // Extra time |
| 567 | |
| 568 | addTimer(60); |
| 569 | grid[gridY][gridX].event = 0; |
| 570 | |
| 571 | break; |
| 572 | |
| 573 | case 2: // Gem |
| 574 | |
| 575 | bonusPlayer->addGem(); |
| 576 | grid[gridY][gridX].event = 0; |
| 577 | |
| 578 | if (bonusPlayer->getGems() >= items) { |
| 579 | |
| 580 | players[i].addLife(); |
| 581 | |
| 582 | return WON; |
| 583 | |
| 584 | } |
| 585 | |
| 586 | break; |
| 587 | |
| 588 | case 5: // Hand |
| 589 | case 8: |
| 590 | |
| 591 | break; |
| 592 | |
| 593 | case 7: // Exit |
| 594 | |
| 595 | return LOST; |
| 596 | |
| 597 | default: |
| 598 | |
| 599 | // Do nothing |
| 600 | |
| 601 | break; |
| 602 | |
| 603 | } |
| 604 | |
| 605 | } |
| 606 | |
| 607 | } |
| 608 | |
| 609 | direction = localPlayer->getJJ1BonusLevelPlayer()->getDirection(); |
| 610 | |
| 611 | return E_NONE; |
| 612 | |
| 613 | } |
| 614 | |
| 615 | |
| 616 | /** |
| 617 | * Draw the level. |
| 618 | */ |
| 619 | void JJ1BonusLevel::draw () { |
| 620 | |
| 621 | JJ1BonusLevelPlayer *bonusPlayer; |
| 622 | Anim *anim; |
| 623 | SDL_Rect dst; |
| 624 | int x, y; |
| 625 | |
| 626 | // Draw the background |
| 627 | |
| 628 | for (x = -(direction & 1023); x < canvasW; x += background->w) { |
| 629 | |
| 630 | dst.x = x; |
| 631 | dst.y = (canvasH >> 1) - 4; |
| 632 | SDL_BlitSurface(background, NULL, canvas, &dst); |
| 633 | |
| 634 | } |
| 635 | |
| 636 | x = 171; |
| 637 | |
| 638 | for (y = (canvasH >> 1) - 5; (y >= 0) && (x > 128); y--) |
| 639 | video.drawRect(0, y, canvasW, 1, x--); |
| 640 | |
| 641 | if (y > 0) |
| 642 | video.drawRect(0, 0, canvasW, y + 1, 128); |
| 643 | |
| 644 | |
| 645 | bonusPlayer = localPlayer->getJJ1BonusLevelPlayer(); |
| 646 | |
| 647 | |
| 648 | // Draw the ground |
| 649 | |
| 650 | fixed playerX = bonusPlayer->getX(); |
| 651 | fixed playerY = bonusPlayer->getY(); |
| 652 | fixed playerSin = fSin(direction); |
| 653 | fixed playerCos = fCos(direction); |
| 654 | |
| 655 | if (SDL_MUSTLOCK(canvas)) SDL_LockSurface(canvas); |
| 656 | |
| 657 | for (y = 1; y <= (canvasH >> 1) - 15; y++) { |
| 658 | |
| 659 | fixed distance = DIV(ITOF(800), ITOF(92) - (ITOF(y * 84) / ((canvasH >> 1) - 16))); |
| 660 | fixed sideX = MUL(distance, playerCos); |
| 661 | fixed sideY = MUL(distance, playerSin); |
| 662 | fixed fwdX = playerX + MUL(distance - F16, playerSin) - (sideX >> 1); |
| 663 | fixed fwdY = playerY - MUL(distance - F16, playerCos) - (sideY >> 1); |
| 664 | |
| 665 | unsigned char* row = static_cast<unsigned char*>(canvas->pixels) + (canvas->pitch * (canvasH - y)); |
| 666 | |
| 667 | for (x = 0; x < canvasW; x++) { |
| 668 | |
| 669 | fixed nX = ITOF(x) / canvasW; |
| 670 | int levelX = FTOI(fwdX + MUL(nX, sideX)); |
| 671 | int levelY = FTOI(fwdY + MUL(nX, sideY)); |
| 672 | |
| 673 | row[x] = static_cast<unsigned char*>(tileSet->pixels) |
| 674 | [(grid[ITOT(levelY) & 255][ITOT(levelX) & 255].tile << 10) + |
| 675 | ((levelY & 31) * tileSet->pitch) + (levelX & 31)]; |
| 676 | |
| 677 | } |
| 678 | |
| 679 | } |
| 680 | |
| 681 | if (SDL_MUSTLOCK(canvas)) SDL_UnlockSurface(canvas); |
| 682 | |
| 683 | |
| 684 | // Draw nearby events |
| 685 | |
| 686 | for (y = -6; y < 6; y++) { |
| 687 | int evY = (((direction - FQ) & 512)? y: -y); |
| 688 | fixed sY = TTOF(evY) + F16 - (playerY & 32767); |
| 689 | |
| 690 | for (x = -6; x < 6; x++) { |
| 691 | int evX = ((direction & 512)? x: -x); |
| 692 | fixed sX = TTOF(evX) + F16 - (playerX & 32767); |
| 693 | |
| 694 | fixed divisor = F16 + MUL(sX, playerSin) - MUL(sY, playerCos); |
| 695 | |
| 696 | if (FTOI(divisor) > 8) { |
| 697 | unsigned char event = grid |
| 698 | [(evY + FTOT(playerY)) & 255][(evX + FTOT(playerX)) & 255].event; |
| 699 | |
| 700 | if (eventSet[event].type > 0) { |
| 701 | anim = animSet + eventSet[event].anim; |
| 702 | |
| 703 | anim->setFrame(ticks / 75, true); |
| 704 | fixed nX = DIV(MUL(sX, playerCos) + MUL(sY, playerSin), divisor); |
| 705 | anim->drawScaled(ITOF(FTOI(nX * canvasW) + (canvasW >> 1)), |
| 706 | ITOF(canvasH >> 1), DIV(F64 * canvasW / SW, divisor)); |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | |
| 713 | // Show the player |
| 714 | bonusPlayer->draw(ticks); |
| 715 | |
| 716 | |
| 717 | // Show gem count |
| 718 | font->showString("*", 0, 0); |
| 719 | font->showNumber(bonusPlayer->getGems() / 10, 50, 0); |
| 720 | font->showNumber(bonusPlayer->getGems() % 10, 68, 0); |
| 721 | font->showString("/", 65, 0); |
| 722 | font->showNumber(items, 124, 0); |
| 723 | |
| 724 | |
| 725 | // Show time remaining |
| 726 | if (endTime > ticks) x = (endTime - ticks) / 1000; |
| 727 | else x = 0; |
| 728 | font->showNumber(x / 60, 250, 0); |
| 729 | font->showString(":", 247, 0); |
| 730 | font->showNumber((x / 10) % 6, 274, 0); |
| 731 | font->showNumber(x % 10, 291, 0); |
| 732 | |
| 733 | } |
| 734 | |
| 735 | |
| 736 | /** |
| 737 | * Play the level. |
| 738 | * |
| 739 | * @return Error code |
| 740 | */ |
| 741 | int JJ1BonusLevel::play () { |
| 742 | |
| 743 | bool pmenu, pmessage; |
| 744 | int option; |
| 745 | unsigned int returnTime; |
| 746 | |
| 747 | tickOffset = globalTicks; |
| 748 | ticks = T_STEP; |
| 749 | steps = 0; |
| 750 | pmessage = pmenu = false; |
| 751 | option = 0; |
| 752 | returnTime = 0; |
| 753 | |
| 754 | video.setPalette(palette); |
| 755 | |
| 756 | while (true) { |
| 757 | |
| 758 | int ret = loop(pmenu, option, pmessage); |
| 759 | if (ret < 0) return ret; |
| 760 | |
| 761 | // Check if level has been won |
| 762 | if (returnTime && (ticks > returnTime)) { |
| 763 | |
| 764 | if (localPlayer->getJJ1BonusLevelPlayer()->getGems() >= items) { |
| 765 | |
| 766 | if (playScene("BONUS.0SC") == E_QUIT) return E_QUIT; |
| 767 | |
| 768 | return WON; |
| 769 | |
| 770 | } |
| 771 | |
| 772 | return LOST; |
| 773 | |
| 774 | } |
| 775 | |
| 776 | |
| 777 | // Process frame-by-frame activity |
| 778 | |
| 779 | while ((getTimeChange() >= T_STEP) && (stage == LS_NORMAL)) { |
| 780 | |
| 781 | ret = step(); |
| 782 | steps++; |
| 783 | if (ret < 0) return ret; |
| 784 | else if (ret) { |
| 785 | |
| 786 | stage = LS_END; |
| 787 | paletteEffects = new WhiteOutPaletteEffect(T_BONUS_END, paletteEffects); |
| 788 | returnTime = ticks + T_BONUS_END; |
| 789 | |
| 790 | } |
| 791 | |
| 792 | } |
| 793 | |
| 794 | |
| 795 | // Draw the graphics |
| 796 | |
| 797 | if ((ticks < returnTime) && !paused) direction += (ticks - prevTicks) * T_BONUS_END / (returnTime - ticks); |
| 798 | |
| 799 | draw(); |
| 800 | |
| 801 | // If paused, draw "PAUSE" |
| 802 | if (pmessage && !pmenu) |
| 803 | font->showString("pause", (canvasW >> 1) - 44, 32); |
| 804 | |
| 805 | |
| 806 | // Draw statistics, menu etc. |
| 807 | drawOverlay(0, pmenu, option, 0, 31, 16); |
| 808 | |
| 809 | } |
| 810 | |
| 811 | return E_NONE; |
| 812 | |
| 813 | } |