| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1sceneload.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created scene.c |
| 10 | * - 3rd February 2009: Created scene.h from parts of scene.c |
| 11 | * - 3rd February 2009: Renamed scene.c to scene.cpp |
| 12 | * - 27th March 2010: Created sceneload.cpp from parts of scene.cpp |
| 13 | * - 1st August 2012: Renamed sceneload.cpp to jj1sceneload.cpp |
| 14 | * |
| 15 | * @par Licence: |
| 16 | * Copyright (c) 2005-2012 AJ Thomson |
| 17 | * Copyright (c) 2015-2024 Carsten Teibes |
| 18 | * |
| 19 | * OpenJazz is distributed under the terms of |
| 20 | * the GNU General Public License, version 2.0 |
| 21 | * |
| 22 | * @par Description: |
| 23 | * Deals with the loading of cutscene data. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #include "jj1scene.h" |
| 29 | |
| 30 | #include "io/file.h" |
| 31 | #include "io/gfx/font.h" |
| 32 | #include "io/gfx/video.h" |
| 33 | #include "io/sound.h" |
| 34 | #include "util.h" |
| 35 | #include "io/log.h" |
| 36 | |
| 37 | #include <string.h> |
| 38 | |
| 39 | #define DEBUG_CM 0 |
| 40 | #define DEBUG_FF 0 |
| 41 | #define DEBUG_ANIM 0 |
| 42 | #define DEBUG_SCRIPT 0 |
| 43 | |
| 44 | #if DEBUG_CM |
| 45 | #define LOG_CM(...) LOG_MAX(__VA_ARGS__) |
| 46 | #else |
| 47 | #define LOG_CM(...) |
| 48 | #endif |
| 49 | #if DEBUG_FF |
| 50 | #define LOG_FF(...) LOG_MAX(__VA_ARGS__) |
| 51 | #else |
| 52 | #define LOG_FF(...) |
| 53 | #endif |
| 54 | #if DEBUG_ANIM |
| 55 | #define LOG_ANIM(...) LOG_MAX(__VA_ARGS__) |
| 56 | #else |
| 57 | #define LOG_ANIM(...) |
| 58 | #endif |
| 59 | #if DEBUG_SCRIPT |
| 60 | #define LOG_SCRIPT(...) LOG_MAX(__VA_ARGS__) |
| 61 | #else |
| 62 | #define LOG_SCRIPT(...) |
| 63 | #endif |
| 64 | |
| 65 | /** |
| 66 | * Load a short from a buffer and advance the pointer past it. |
| 67 | * |
| 68 | * @param data Pointer to data in a buffer |
| 69 | * |
| 70 | * @return The loaded short |
| 71 | */ |
| 72 | unsigned short int JJ1Scene::loadShortMem (unsigned char** data) { |
| 73 | |
| 74 | unsigned short int val; |
| 75 | |
| 76 | val = **data; |
| 77 | (*data)++; |
| 78 | val += ((unsigned short)(**data)) << 8; |
| 79 | (*data)++; |
| 80 | |
| 81 | return val; |
| 82 | |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Decompress JJ1 cutscene graphical data. |
| 88 | * |
| 89 | * @param size The size of the compressed data |
| 90 | * @param frameData The compressed data |
| 91 | * @param pixels Buffer to contain the decompressed data |
| 92 | */ |
| 93 | void JJ1Scene::loadFullFrameMem (int size, unsigned char* frameData, unsigned char* pixels) { |
| 94 | unsigned char* nextPixel = pixels; |
| 95 | unsigned char* nextData = frameData; |
| 96 | int fillWidth = 0; |
| 97 | bool trans = true; |
| 98 | |
| 99 | #if DEBUG_FF |
| 100 | FILE* out = fopen("ff-output.dat", "wb"); |
| 101 | fwrite(frameData, size, 1, out); |
| 102 | fclose(out); |
| 103 | #endif |
| 104 | |
| 105 | while ((nextData < frameData + size) && (nextPixel < pixels + (SW * SH))) { |
| 106 | unsigned char header = *nextData; |
| 107 | nextData++; |
| 108 | LOG_FF("PL FF Frame header: 0x%x", header); |
| 109 | |
| 110 | if ((header & 0x7F) == 0x7F) { |
| 111 | fillWidth = loadShortMem(&nextData); |
| 112 | if (trans) fillWidth += 255; |
| 113 | |
| 114 | LOG_FF("PL FF Skip %d pixels", fillWidth); |
| 115 | } else if (header) { |
| 116 | |
| 117 | if(trans) { |
| 118 | fillWidth = header; |
| 119 | LOG_FF("PL FF SKIP %d transparent pixels", fillWidth); |
| 120 | } else { |
| 121 | fillWidth = header & 0x1F; |
| 122 | |
| 123 | switch (header & 0x60) { |
| 124 | default: |
| 125 | LOG_FF("PL FF Unknown operation: 0x%x", header); |
| 126 | break; |
| 127 | |
| 128 | case 0x00: |
| 129 | LOG_FF("PL FF Copy %d pixels", fillWidth); |
| 130 | memcpy(nextPixel, nextData, fillWidth); |
| 131 | nextData += fillWidth; |
| 132 | break; |
| 133 | |
| 134 | case 0x20: |
| 135 | LOG_FF("PL FF Copy %d pixels of previous line", fillWidth); |
| 136 | if (nextPixel - SW >= pixels) |
| 137 | memcpy(nextPixel, nextPixel - SW, fillWidth); |
| 138 | break; |
| 139 | |
| 140 | case 0x60: |
| 141 | fillWidth = header&0x3F; |
| 142 | /* FALLTHROUGH */ |
| 143 | case 0x40: |
| 144 | LOG_FF("PL FF Fill %d pixels of row with color 0x%x", fillWidth, *nextData); |
| 145 | memset(nextPixel, *nextData, fillWidth); |
| 146 | nextData++; |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | } else { |
| 151 | LOG_FF("PL FF FAULTY END OF STREAM: %d", size); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | nextPixel += fillWidth; |
| 156 | |
| 157 | if (header & 0x80) trans = false; |
| 158 | else trans = !trans; |
| 159 | } |
| 160 | |
| 161 | LOG_FF("PL FF pixels: %d", (unsigned int)(nextPixel - pixels)); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /** |
| 166 | * Decompress JJ1 cutscene graphical data. |
| 167 | * |
| 168 | * @param size The size of the compressed data |
| 169 | * @param frameData The compressed data |
| 170 | * @param pixels Buffer to contain the decompressed data |
| 171 | */ |
| 172 | void JJ1Scene::loadCompactedMem (int size, unsigned char* frameData, unsigned char* pixels) { |
| 173 | |
| 174 | unsigned char* nextPixel = pixels; |
| 175 | unsigned char* endpixdata = pixels + (SW * SH); |
| 176 | unsigned char* fillstart = NULL; |
| 177 | int fillWidth; |
| 178 | |
| 179 | #if DEBUG_CM |
| 180 | FILE* out = fopen("cm-output.dat", "wb"); |
| 181 | fwrite(frameData, size, 1, out); |
| 182 | fclose(out); |
| 183 | #endif |
| 184 | |
| 185 | auto fillColumns = [&](unsigned char color, int columns) { |
| 186 | LOG_CM("PL CM Fill %d columns with color 0x%x", columns, color); |
| 187 | |
| 188 | fillstart = nextPixel; |
| 189 | while (fillstart + columns <= endpixdata) { |
| 190 | memset(fillstart, color, columns); |
| 191 | fillstart += SW; |
| 192 | } |
| 193 | if(fillstart < endpixdata) { |
| 194 | // Filling bottom row |
| 195 | memset(fillstart, color, endpixdata-fillstart); |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | while (size > 0) { |
| 200 | |
| 201 | unsigned char header = *frameData; |
| 202 | frameData++; |
| 203 | |
| 204 | if (header == 0x7F) { |
| 205 | fillWidth = loadShortMem(&frameData); |
| 206 | unsigned char fillColor = *frameData; |
| 207 | frameData++; |
| 208 | fillColumns(fillColor, fillWidth); |
| 209 | |
| 210 | size -= 3; |
| 211 | } else if (header == 0xFF) { |
| 212 | fillWidth = loadShortMem(&frameData); |
| 213 | LOG_CM("PL CM Skip %d pixels", fillWidth); |
| 214 | |
| 215 | size -= 2; |
| 216 | } else if (header & 0x80) { |
| 217 | fillWidth = (header - 0x80) + 1; // max 0xFE |
| 218 | LOG_CM("PL CM Skip %d pixels", fillWidth); |
| 219 | } else if (header & 0x40) { |
| 220 | unsigned char fillColor = *frameData; |
| 221 | frameData++; |
| 222 | fillWidth = (header - 0x40) + 1; // max 0x7E |
| 223 | fillColumns(fillColor, fillWidth); |
| 224 | |
| 225 | size--; |
| 226 | } else { |
| 227 | fillWidth = (header & 0x3F) + 1; // max 0x3F |
| 228 | LOG_CM("PL CM Fill %d columns with raw pixels", fillWidth); |
| 229 | |
| 230 | for (int col = 0; col < fillWidth; col++) { |
| 231 | unsigned char color = *frameData; |
| 232 | frameData++; |
| 233 | |
| 234 | if (color != 0xFF) { |
| 235 | fillstart = nextPixel + col; |
| 236 | |
| 237 | while (fillstart < endpixdata) { |
| 238 | *fillstart = color; |
| 239 | fillstart += SW; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | size--; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | nextPixel += fillWidth; |
| 248 | size--; |
| 249 | } |
| 250 | |
| 251 | LOG_CM("PL CM pixels: %d", (unsigned int)(nextPixel - pixels)); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /** |
| 256 | * Load JJ1 cutscene animation. |
| 257 | * |
| 258 | * @param scene Object to hold animation data |
| 259 | * @param f File from which to load animation |
| 260 | * @param dataIndex Index |
| 261 | */ |
| 262 | void JJ1Scene::loadAni (JJ1SceneAnimation &scene, File *f, int dataIndex) { |
| 263 | |
| 264 | LOG_ANIM("ParseAni DataLen: 0x%x", f->loadShort()); // should be 0x02 |
| 265 | LOG_ANIM("ParseAni Frames: %d", f->loadShort()); |
| 266 | unsigned short int type = 0; |
| 267 | int loop; |
| 268 | |
| 269 | while (type != EPlayListAniHeader) { |
| 270 | |
| 271 | type = f->loadShort(); |
| 272 | |
| 273 | if (type == ESoundListAniHeader) { |
| 274 | |
| 275 | /*unsigned short int offset =*/ f->loadShort(); |
| 276 | unsigned char nSounds = f->loadChar(); |
| 277 | |
| 278 | for(loop = 0; loop < nSounds; loop++) { |
| 279 | |
| 280 | char* soundName = f->loadTerminatedString(); |
| 281 | LOG_ANIM("Soundname: %s", soundName); |
| 282 | resampleSound(loop, soundName, 11025); |
| 283 | delete[] soundName; |
| 284 | |
| 285 | } |
| 286 | |
| 287 | } else if (type == EPlayListAniHeader) { |
| 288 | int nextPos = f->tell(); |
| 289 | LOG_ANIM("PL Read position: %d", nextPos); |
| 290 | f->loadShort(); // Length |
| 291 | |
| 292 | palettes.emplace_back(dataIndex); |
| 293 | auto &scenePalette = palettes.back(); |
| 294 | f->loadPalette(scenePalette.palette, false); |
| 295 | |
| 296 | #if DEBUG_ANIM |
| 297 | int items = 0; |
| 298 | #endif |
| 299 | LOG_ANIM("PL Read position start: %d", f->tell()); |
| 300 | |
| 301 | int validValue = true; |
| 302 | while (validValue) { |
| 303 | |
| 304 | unsigned short int value = f->loadShort(); |
| 305 | LOG_ANIM("PL Read block start tag: 0x%x", value); |
| 306 | int size = f->loadShort(); |
| 307 | LOG_ANIM("PL Anim block size: %d", size); |
| 308 | nextPos = f->tell(); |
| 309 | // next pos is intial position + size and four bytes header |
| 310 | nextPos += size; |
| 311 | |
| 312 | switch (value) { |
| 313 | |
| 314 | case EEndAniHeader: // END MARKER |
| 315 | validValue = false; |
| 316 | break; |
| 317 | |
| 318 | case E11AniHeader: |
| 319 | LOG_ANIM("PL 11 Background Type: 0"); |
| 320 | scene.background = f->loadSurface(SW, SH, false); |
| 321 | |
| 322 | // Use the most recently loaded palette |
| 323 | video.setPalette(scenePalette.palette); |
| 324 | break; |
| 325 | |
| 326 | case E1LAniHeader: |
| 327 | { |
| 328 | LOG_ANIM("PL 1L Background Type: 0"); |
| 329 | unsigned char* pixels = new unsigned char[SW* SH]; |
| 330 | memset(pixels, 0, SW*SH); |
| 331 | unsigned char* frameData = f->loadBlock(size); |
| 332 | loadCompactedMem(size, frameData, pixels); |
| 333 | delete[] frameData; |
| 334 | scene.background = video.createSurface(pixels, SW, SH); |
| 335 | delete[] pixels; |
| 336 | |
| 337 | // Use the most recently loaded palette |
| 338 | video.setPalette(scenePalette.palette); |
| 339 | } |
| 340 | break; |
| 341 | |
| 342 | case EFullFrameAniHeader: |
| 343 | { |
| 344 | unsigned char* blockData = f->loadBlock(size); |
| 345 | scene.addFrame(EFullFrameAniHeader, blockData, size); |
| 346 | } |
| 347 | break; |
| 348 | |
| 349 | case ERNAniHeader: |
| 350 | case ERBAniHeader: |
| 351 | case ERLAniHeader: |
| 352 | case EMXAniHeader: |
| 353 | LOG_DEBUG("PL Read Unsupported animation type 0x%x", value); |
| 354 | break; |
| 355 | |
| 356 | case EReverseAniHeader: |
| 357 | scene.reverseAnimation = 1; |
| 358 | break; |
| 359 | |
| 360 | case ERCAniHeader: |
| 361 | { |
| 362 | unsigned char* blockData = f->loadBlock(size); |
| 363 | scene.addFrame(ERCAniHeader, blockData, size); |
| 364 | } |
| 365 | break; |
| 366 | |
| 367 | case ECompactedAniHeader: |
| 368 | { |
| 369 | unsigned char* blockData = f->loadBlock(size); |
| 370 | scene.addFrame(ECompactedAniHeader, blockData, size); |
| 371 | } |
| 372 | break; |
| 373 | |
| 374 | case ESoundTagAniHeader: |
| 375 | { |
| 376 | auto se = static_cast<SE::Type>(f->loadChar()); |
| 377 | if (!isValidSoundIndex(se)) { |
| 378 | LOG_WARN("PL Audio tag with invalid index: %d", se); |
| 379 | scene.lastFrame->soundId = SE::NONE; |
| 380 | } else { |
| 381 | LOG_ANIM("PL Audio tag with index: %d", se); |
| 382 | scene.lastFrame->soundId = se; |
| 383 | } |
| 384 | LOG_ANIM("PL Audio tag play at: 0x%x", f->loadChar()); |
| 385 | LOG_ANIM("PL Audio tag play offset: 0x%x", f->loadChar()); |
| 386 | } |
| 387 | break; |
| 388 | |
| 389 | case E00AniHeader: // FIXME: wait command? |
| 390 | { |
| 391 | int longvalue = f->loadInt(); |
| 392 | |
| 393 | while (longvalue == 0) { |
| 394 | |
| 395 | longvalue = f->loadInt(); |
| 396 | nextPos += 4; |
| 397 | |
| 398 | } |
| 399 | |
| 400 | f->seek(-4, false); |
| 401 | value = longvalue; |
| 402 | |
| 403 | LOG_ANIM("PL Read Long: 0x%x", value); |
| 404 | } |
| 405 | break; |
| 406 | |
| 407 | default: |
| 408 | |
| 409 | LOG_DEBUG("PL Read Unknown type: 0x%x", value); |
| 410 | validValue = false; |
| 411 | |
| 412 | break; |
| 413 | |
| 414 | } |
| 415 | |
| 416 | LOG_ANIM("PL Read position after block should be: %d", nextPos); |
| 417 | f->seek(nextPos, true); |
| 418 | |
| 419 | #if DEBUG_ANIM |
| 420 | if(validValue) |
| 421 | items++; |
| 422 | #endif |
| 423 | } |
| 424 | |
| 425 | LOG_ANIM("PL Parsed through number of items skipping 0 items: %d", items); |
| 426 | LOG_ANIM("PL Read position after parsing anim blocks: %d", f->tell()); |
| 427 | |
| 428 | } |
| 429 | |
| 430 | } |
| 431 | |
| 432 | } |
| 433 | |
| 434 | |
| 435 | /** |
| 436 | * Load JJ1 cutscene data. |
| 437 | * |
| 438 | * @param f File from which to load the data |
| 439 | */ |
| 440 | void JJ1Scene::loadData (File *f) { |
| 441 | |
| 442 | for (int loop = 0; loop < dataItems; loop++) { |
| 443 | |
| 444 | f->seek(dataOffsets[loop], true); // Seek to data start |
| 445 | unsigned short int dataLen = f->loadShort(); // Get get the length of the datablock |
| 446 | LOG_MAX("Data dataLen: %d", dataLen); |
| 447 | |
| 448 | if (dataLen == EAniHeader) { |
| 449 | |
| 450 | LOG_MAX("Data Type: ANI"); |
| 451 | animations.emplace_back(loop); |
| 452 | auto &sceneAnimation = animations.back(); |
| 453 | loadAni(sceneAnimation, f, loop); |
| 454 | |
| 455 | } else { |
| 456 | |
| 457 | unsigned char type = f->loadChar(); |
| 458 | LOG_MAX("Data Type: %d", type); |
| 459 | |
| 460 | switch (type) { |
| 461 | |
| 462 | case 3: |
| 463 | case 4: // image |
| 464 | case 5: |
| 465 | case 6: |
| 466 | case 7: |
| 467 | { |
| 468 | LOG_MAX("Data Type: Image, index: %d", loop); |
| 469 | unsigned short int width = f->loadShort(SW); // Get width |
| 470 | unsigned short int height; |
| 471 | |
| 472 | // Get height |
| 473 | if (type == 3) height = f->loadChar(); |
| 474 | else height = f->loadShort(SH); |
| 475 | |
| 476 | images.emplace_back(loop); |
| 477 | auto& sceneImage = images.back(); |
| 478 | |
| 479 | if (type >= 5 && type <= 7) { |
| 480 | f->seek(-5, false); // account for metadata 2 + 2 |
| 481 | unsigned char* pixels = unpackRLE(f->loadBlock(dataLen), dataLen, width * height + 4); |
| 482 | sceneImage.image = video.createSurface(pixels + 4, width, height); |
| 483 | delete[] pixels; |
| 484 | } else { |
| 485 | sceneImage.image = f->loadSurface(width, height, false); |
| 486 | } |
| 487 | } |
| 488 | break; |
| 489 | |
| 490 | default: |
| 491 | |
| 492 | LOG_MAX("Data Type: Palette, index: %d", loop); |
| 493 | f->seek(-3, false); // fake an RLE block size marker and use first palette byte |
| 494 | |
| 495 | palettes.emplace_back(loop); |
| 496 | auto& palette = palettes.back(); |
| 497 | f->loadPalette(palette.palette); |
| 498 | |
| 499 | break; |
| 500 | |
| 501 | } |
| 502 | |
| 503 | } |
| 504 | |
| 505 | } |
| 506 | |
| 507 | } |
| 508 | |
| 509 | |
| 510 | /** |
| 511 | * Load JJ1 cutscene scripts. |
| 512 | * |
| 513 | * @param f File from which to load the scripts |
| 514 | */ |
| 515 | void JJ1Scene::loadScripts (File *f) { |
| 516 | |
| 517 | int textAlignment = 0; |
| 518 | int textFont = 0; |
| 519 | int textShadow = -1; |
| 520 | int textColour = 0; |
| 521 | int loop = 0; |
| 522 | |
| 523 | for(auto &page : pages) { |
| 524 | LOG_MAX("Parse Script: %d", loop); |
| 525 | |
| 526 | SDL_Rect textRect = { 0,0,0,0 }; |
| 527 | f->seek(scriptStarts[loop], true); // Seek to data start |
| 528 | |
| 529 | if (f->loadChar() == 0x50) { // Script tag |
| 530 | |
| 531 | LOG_MAX("Script id: 0x%x", f->loadShort()); |
| 532 | int palette = f->loadShort(); |
| 533 | LOG_SCRIPT("Script default palette: %d", palette); |
| 534 | page.paletteIndex = palette; |
| 535 | |
| 536 | bool breakloop = false; |
| 537 | int pos = f->tell(); |
| 538 | |
| 539 | int textPosX = -1; |
| 540 | int textPosY = -1; |
| 541 | int extraheight = -1; |
| 542 | bool textRectValid = false; |
| 543 | |
| 544 | while(!breakloop && pos < dataOffsets[0]) { |
| 545 | |
| 546 | unsigned char type = f->loadChar(); |
| 547 | |
| 548 | switch(type) { |
| 549 | |
| 550 | case ESceneYesNo: |
| 551 | { |
| 552 | page.askForYesNo = true; |
| 553 | LOG_SCRIPT("ESceneYesNo"); |
| 554 | } |
| 555 | break; |
| 556 | case ESceneStopMusic: |
| 557 | { |
| 558 | page.stopMusic = true; |
| 559 | LOG_SCRIPT("ESceneStopMusic"); |
| 560 | } |
| 561 | break; |
| 562 | case ESceneAnimation: |
| 563 | { |
| 564 | page.animLoops = f->loadInt(); |
| 565 | page.animSpeed = f->loadShort(); |
| 566 | page.animIndex = f->loadShort(); |
| 567 | |
| 568 | LOG_SCRIPT("ESceneAnimation: loops %d, speed %d, anim num %d", |
| 569 | page.animLoops, page.animSpeed, page.animIndex); |
| 570 | } |
| 571 | break; |
| 572 | |
| 573 | case ESceneAnimationPlayAndContinue: |
| 574 | { |
| 575 | page.nextPageAfterAnim = f->loadChar(); |
| 576 | LOG_SCRIPT("ESceneAnimationPlayAndContinue: %d", page.nextPageAfterAnim); |
| 577 | } |
| 578 | break; |
| 579 | |
| 580 | case ESceneTransitionType: |
| 581 | { |
| 582 | page.transitionType = f->loadChar(); |
| 583 | LOG_SCRIPT("ESceneTransitionType: %d", page.transitionType); |
| 584 | } |
| 585 | break; |
| 586 | |
| 587 | case ESceneBackground: |
| 588 | |
| 589 | page.bgX[page.backgrounds] = f->loadShort(); |
| 590 | page.bgY[page.backgrounds] = f->loadShort(); |
| 591 | page.bgIndex[page.backgrounds] = f->loadShort(); |
| 592 | |
| 593 | LOG_SCRIPT("ESceneBackground: x %d, y %d, index %d", page.bgX[page.backgrounds], |
| 594 | page.bgY[page.backgrounds], page.bgIndex[page.backgrounds]); |
| 595 | |
| 596 | page.backgrounds++; |
| 597 | |
| 598 | break; |
| 599 | |
| 600 | case ESceneMusic: |
| 601 | |
| 602 | page.musicFile = f->loadTerminatedString(); |
| 603 | LOG_SCRIPT("ESceneMusic: %s", page.musicFile); |
| 604 | |
| 605 | break; |
| 606 | |
| 607 | case ESceneMusicTransition: |
| 608 | { |
| 609 | char musicTransition = f->loadChar(); |
| 610 | // This value (-) is mentioned in the moddingwiki, though i encountered 0x45 (E) more often |
| 611 | if(musicTransition == 0x2D) { |
| 612 | page.stopMusic = true; // FIXME: maybe means actually fade? |
| 613 | LOG_SCRIPT("ESceneMusicTransition: off"); |
| 614 | } else |
| 615 | LOG_TRACE("Unknown ESceneMusicTransition: 0x%x", musicTransition); |
| 616 | } |
| 617 | break; |
| 618 | |
| 619 | case ESceneTextRect: |
| 620 | |
| 621 | textRect.x = f->loadShort(); |
| 622 | textRect.y = f->loadShort(); |
| 623 | textRect.w = f->loadShort() - textRect.x; |
| 624 | textRect.h = f->loadShort() - textRect.y; |
| 625 | textRectValid = true; |
| 626 | LOG_SCRIPT("ESceneTextRect: x %d, y %d, w %d, h %d", |
| 627 | textRect.x, textRect.y, textRect.w, textRect.h); |
| 628 | |
| 629 | break; |
| 630 | |
| 631 | case ESceneFontDefine: |
| 632 | |
| 633 | if (fonts.size() < 5) { |
| 634 | int id = f->loadShort(); |
| 635 | fonts.emplace_back(id); |
| 636 | auto &sceneFont = fonts.back(); |
| 637 | char *fontname = f->loadTerminatedString(); |
| 638 | |
| 639 | LOG_SCRIPT("ESceneFontDefine: %s with id %d", fontname, sceneFont.id); |
| 640 | |
| 641 | if (strcmp(fontname, "FONT2") == 0) |
| 642 | sceneFont.font = font2; |
| 643 | else if (strcmp(fontname, "FONTBIG") == 0) |
| 644 | sceneFont.font = fontbig; |
| 645 | else if (strcmp(fontname, "FONTINY") == 0) |
| 646 | sceneFont.font = fontiny; |
| 647 | else if (strcmp(fontname, "FONTMN1") == 0) |
| 648 | sceneFont.font = fontmn1; |
| 649 | else if (strcmp(fontname, "FONTMN2") == 0) |
| 650 | sceneFont.font = fontmn2; |
| 651 | else { |
| 652 | LOG_WARN("ESceneFontDefine: Unknown font name, using font2"); |
| 653 | sceneFont.font = font2; |
| 654 | } |
| 655 | |
| 656 | delete[] fontname; |
| 657 | } |
| 658 | break; |
| 659 | |
| 660 | case ESceneTextPosition: |
| 661 | |
| 662 | textPosX = f->loadShort(); |
| 663 | textPosY = f->loadShort(); |
| 664 | LOG_SCRIPT("ESceneTextPosition: x %d, y %d", textPosX, textPosY); |
| 665 | |
| 666 | break; |
| 667 | |
| 668 | case ESceneTextColour: |
| 669 | |
| 670 | textColour = f->loadShort(); |
| 671 | LOG_SCRIPT("ESceneTextColour: %d", textColour); |
| 672 | |
| 673 | break; |
| 674 | |
| 675 | case ESceneFontFun: |
| 676 | { |
| 677 | LOG_TRACE("Unimplemented ESceneFontFun len: %d", f->loadShort()); |
| 678 | |
| 679 | /*while (len) { |
| 680 | |
| 681 | unsigned char data = f->loadChar(); |
| 682 | len--; |
| 683 | |
| 684 | }*/ |
| 685 | } |
| 686 | break; |
| 687 | |
| 688 | case ESceneFontIndex: |
| 689 | |
| 690 | textFont = f->loadShort(); |
| 691 | LOG_SCRIPT("ESceneFontIndex: %d", textFont); |
| 692 | |
| 693 | break; |
| 694 | |
| 695 | case ESceneTextVAdjust: |
| 696 | |
| 697 | extraheight = f->loadShort(); |
| 698 | LOG_SCRIPT("ESceneTextVAdjust: %d", extraheight); |
| 699 | |
| 700 | break; |
| 701 | |
| 702 | case ESceneBackgroundFade: |
| 703 | { |
| 704 | page.backgroundFade = f->loadShort(); |
| 705 | LOG_SCRIPT("ESceneBackgroundFade: %d", page.backgroundFade); |
| 706 | } |
| 707 | break; |
| 708 | |
| 709 | case ESceneTextShadow: |
| 710 | { |
| 711 | char enableShadow = f->loadChar(); |
| 712 | if(enableShadow) { |
| 713 | textShadow = f->loadChar(); |
| 714 | } else { |
| 715 | f->loadChar(); // Skip this value since shadows are turned off |
| 716 | textShadow = -1; // Turn off shadow , -1 means no shadow colour |
| 717 | } |
| 718 | |
| 719 | LOG_SCRIPT("ESceneTextShadow: %d", textShadow); |
| 720 | } |
| 721 | break; |
| 722 | |
| 723 | case ESceneTextAlign: |
| 724 | |
| 725 | textAlignment = f->loadChar(); |
| 726 | LOG_SCRIPT("ESceneTextAlign: %d", textAlignment); |
| 727 | |
| 728 | break; |
| 729 | |
| 730 | case ESceneTextAlign2: |
| 731 | { |
| 732 | char a = f->loadChar(); |
| 733 | int b = f->loadShort(); |
| 734 | LOG_TRACE("Unimplemented ESceneTextAlign2: a 0x%x, b 0x%x", a, b); |
| 735 | } |
| 736 | break; |
| 737 | |
| 738 | case ESceneTextSomething: |
| 739 | { |
| 740 | char a = f->loadChar(); |
| 741 | int b = f->loadShort(); |
| 742 | LOG_TRACE("Unimplemented ESceneTextSomething: a 0x%x, b 0x%x", a, b); |
| 743 | } |
| 744 | break; |
| 745 | |
| 746 | case ESceneTextLine: |
| 747 | case ESceneTextBlock: |
| 748 | { |
| 749 | unsigned char datalen = f->loadChar(); |
| 750 | LOG_SCRIPT("Text len: %d", datalen); |
| 751 | |
| 752 | page.texts.emplace_back(); |
| 753 | auto &sceneText = page.texts.back(); |
| 754 | |
| 755 | if (datalen > 0) { |
| 756 | sceneText.text = reinterpret_cast<unsigned char *>(f->loadString(datalen)); |
| 757 | |
| 758 | // Convert number placeholders |
| 759 | if (type == ESceneTextLine) { |
| 760 | for (int textPos = 1; textPos < datalen; textPos++) { |
| 761 | |
| 762 | if (sceneText.text[textPos] == 0x8B) { |
| 763 | // Current page |
| 764 | if (loop >= 9) |
| 765 | sceneText.text[textPos - 1] = ((loop + 1) / 10) + 53; |
| 766 | |
| 767 | sceneText.text[textPos] = ((loop + 1) % 10) + 53; |
| 768 | } else if (sceneText.text[textPos] == 0x8A) { |
| 769 | // Number of pages |
| 770 | if (scriptItems >= 10) |
| 771 | sceneText.text[textPos - 1] = (scriptItems / 10) + 53; |
| 772 | |
| 773 | sceneText.text[textPos] = (scriptItems % 10) + 53; |
| 774 | } else if (sceneText.text[textPos] == 0x8C) { |
| 775 | // FIXME: seems not used at all |
| 776 | sceneText.text[textPos-2] = '0'; |
| 777 | sceneText.text[textPos-1] = '0'; |
| 778 | sceneText.text[textPos] = '0'; |
| 779 | } else if (sceneText.text[textPos] > 0x8C) { |
| 780 | // internal |
| 781 | sceneText.text[textPos] = '0'; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | sceneText.alignment = textAlignment; |
| 788 | sceneText.fontId = textFont; |
| 789 | sceneText.textColour = textColour; |
| 790 | sceneText.shadowColour = textShadow; |
| 791 | |
| 792 | if(textPosX != -1) { |
| 793 | |
| 794 | sceneText.x = textPosX; |
| 795 | sceneText.y = textPosY; |
| 796 | textPosX = -1; |
| 797 | textPosY = -1; |
| 798 | |
| 799 | } |
| 800 | |
| 801 | if(textRectValid) { |
| 802 | |
| 803 | sceneText.textRect = textRect; |
| 804 | textRectValid = false; |
| 805 | |
| 806 | } |
| 807 | |
| 808 | if(extraheight != -1) { |
| 809 | |
| 810 | sceneText.extraLineHeight = extraheight; |
| 811 | extraheight = -1; |
| 812 | |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | break; |
| 817 | |
| 818 | case ESceneTime: |
| 819 | |
| 820 | page.pageTime = (f->loadShort()); |
| 821 | LOG_SCRIPT("Scene time: %d", page.pageTime); |
| 822 | page.pageTime&=255; |
| 823 | break; |
| 824 | |
| 825 | case ESceneBreaker: |
| 826 | case 0x3e: |
| 827 | |
| 828 | pos = f->tell(); |
| 829 | LOG_SCRIPT("Parse script end at position: %d, with: 0x%x", pos, type); |
| 830 | breakloop = true; |
| 831 | f->loadChar(); |
| 832 | |
| 833 | break; |
| 834 | |
| 835 | default: |
| 836 | |
| 837 | pos = f->tell(); |
| 838 | LOG_SCRIPT("Parse script end at position: %d, breaker: 0x%x", pos, type); |
| 839 | breakloop = true; |
| 840 | |
| 841 | break; |
| 842 | |
| 843 | } |
| 844 | |
| 845 | pos = f->tell(); |
| 846 | |
| 847 | } |
| 848 | |
| 849 | } |
| 850 | |
| 851 | loop++; |
| 852 | } |
| 853 | |
| 854 | } |