├── .gitignore ├── ADSFile.cpp ├── ADSFile.h ├── BMPFile.cpp ├── BMPFile.h ├── BaseFile.cpp ├── BaseFile.h ├── CMakeLists.txt ├── CommandlineParser.h ├── CompressedBaseFile.cpp ├── CompressedBaseFile.h ├── GraphicBaseFile.cpp ├── GraphicBaseFile.h ├── LICENCE_LinBiolinum.txt ├── LICENSE ├── PALFile.cpp ├── PALFile.h ├── README.md ├── RESFile.cpp ├── RESFile.h ├── RIFFPlayer.cpp ├── RIFFPlayer.h ├── Robinson.cpp ├── Robinson.h ├── RobinsonCompositor.cpp ├── RobinsonCompositor.h ├── RobinsonMenu.cpp ├── RobinsonMenu.h ├── SCRFile.cpp ├── SCRFile.h ├── TTMFile.cpp ├── TTMFile.h ├── TTMPlayer.cpp ├── TTMPlayer.h ├── VINFile.cpp ├── VINFile.h ├── defines.h ├── font.ttf ├── main.cpp └── types.h /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeCache.txt 2 | CMakeFiles 3 | CMakeScripts 4 | Makefile 5 | cmake_install.cmake 6 | install_manifest.txt 7 | CMakeLists.txt.user 8 | *.kdev4 9 | build/ 10 | -------------------------------------------------------------------------------- /ADSFile.cpp: -------------------------------------------------------------------------------- 1 | #include "ADSFile.h" 2 | 3 | SCRANTIC::ADSFile::ADSFile(const std::string &name, v8 &data) 4 | : CompressedBaseFile(name) { 5 | 6 | initMnemonics(); 7 | parseFile(data); 8 | 9 | parseRawScript(); 10 | findLabels(); 11 | } 12 | 13 | SCRANTIC::ADSFile::ADSFile(const std::string &filename) 14 | : CompressedBaseFile(filename) { 15 | 16 | initMnemonics(); 17 | 18 | std::ifstream in; 19 | 20 | in.open(filename, std::ios::in); 21 | if (!in.is_open()) { 22 | std::cerr << "ADSFile: Could not open " << filename << std::endl; 23 | return; 24 | } 25 | 26 | u16 i, j, k, l; 27 | 28 | std::string line; 29 | std::string tag; 30 | std::string mnemonic; 31 | u16 id, opcode; 32 | int count; 33 | 34 | std::string mode; 35 | 36 | while (getline(in, line)) { 37 | if (line.substr(0, 1) == "#" || line == "") { 38 | continue; 39 | } 40 | 41 | if (line == "VERSION") { 42 | getline(in, line); 43 | version = line; 44 | continue; 45 | } 46 | 47 | if (line == "RESOURCES") { 48 | mode = line; 49 | continue; 50 | } 51 | 52 | if (line == "TAGS") { 53 | mode = line; 54 | continue; 55 | } 56 | 57 | if (line == "SCRIPT") { 58 | mode = line; 59 | continue; 60 | } 61 | 62 | if (mode == "RESOURCES") { 63 | std::istringstream iss(line); 64 | if (!(iss >> std::hex >> id)) { 65 | break; 66 | } 67 | tag = line.substr(line.find(" ")+1); 68 | resList.insert({id, tag}); 69 | } else if (mode == "TAGS") { 70 | std::istringstream iss(line); 71 | if (!(iss >> std::hex >> id)) { 72 | break; 73 | } 74 | tag = line.substr(line.find(" ")+1); 75 | tagList.insert({id, tag}); 76 | } else if (mode == "SCRIPT") { 77 | mnemonic = line.substr(0, line.find(" ")); 78 | opcode = getOpcodeFromMnemonic(mnemonic); 79 | count = getParamCount(opcode); 80 | 81 | if (opcode != CMD_SET_SCENE) { 82 | writeUintLE(rawScript, opcode); 83 | } 84 | 85 | std::istringstream iss(line); 86 | switch (count) { 87 | case 1: 88 | iss >> mnemonic >> std::hex >> i; 89 | writeUintLE(rawScript, i); 90 | break; 91 | case 2: 92 | iss >> mnemonic >> std::hex >> i >> j; 93 | writeUintLE(rawScript, i); 94 | writeUintLE(rawScript, j); 95 | break; 96 | case 3: 97 | iss >> mnemonic >> std::hex >> i >> j >> k; 98 | writeUintLE(rawScript, i); 99 | writeUintLE(rawScript, j); 100 | writeUintLE(rawScript, k); 101 | break; 102 | case 4: 103 | iss >> mnemonic >> std::hex >> i >> j >> k >> l; 104 | writeUintLE(rawScript, i); 105 | writeUintLE(rawScript, j); 106 | writeUintLE(rawScript, k); 107 | writeUintLE(rawScript, l); 108 | break; 109 | default: 110 | break; 111 | } 112 | } 113 | } 114 | 115 | in.close(); 116 | 117 | parseRawScript(); 118 | findLabels(); 119 | } 120 | 121 | void SCRANTIC::ADSFile::parseFile(v8 &data) { 122 | v8::iterator it = data.begin(); 123 | 124 | assertString(it, "VER:"); 125 | 126 | readUintLE(it, verSize); 127 | version = readString(it, verSize-1); 128 | 129 | assertString(it, "ADS:"); 130 | 131 | readUintLE(it, resScrTagSize); 132 | readUintLE(it, magic); 133 | 134 | assertString(it, "RES:"); 135 | 136 | readUintLE(it, resSize); 137 | readUintLE(it, resCount); 138 | 139 | u16 id; 140 | std::string desc; 141 | 142 | for (u16 i = 0; i < resCount; ++i) { 143 | readUintLE(it, id); 144 | desc = readString(it); 145 | resList.insert(std::pair(id, desc)); 146 | } 147 | 148 | assertString(it, "SCR:"); 149 | 150 | if (!handleDecompression(data, it, rawScript)) { 151 | return; 152 | } 153 | 154 | std::advance(it, compressedSize); 155 | 156 | assertString(it, "TAG:"); 157 | 158 | readUintLE(it, tagSize); 159 | readUintLE(it, tagCount); 160 | 161 | for (u16 i = 0; i < tagCount; ++i) { 162 | readUintLE(it, id); 163 | desc = readString(it); 164 | tagList.insert(std::pair(id, desc)); 165 | } 166 | } 167 | 168 | void SCRANTIC::ADSFile::parseRawScript() { 169 | if (!rawScript.size()) { 170 | return; 171 | } 172 | 173 | script.clear(); 174 | 175 | v8::iterator it = rawScript.begin(); 176 | u16 opcode, word, listOpcode; 177 | 178 | u16 maxTagId = tagList.rbegin()->first; 179 | u16 currentMovie; 180 | 181 | while (it != rawScript.end()) { 182 | readUintLE(it, opcode); 183 | size_t length = getParamCount(opcode); 184 | 185 | Command c; 186 | c.opcode = opcode; 187 | 188 | if (opcode <= maxTagId) { 189 | c.opcode = CMD_SET_SCENE; 190 | c.data.push_back(opcode); 191 | c.name = tagList.find(opcode)->second; 192 | currentMovie = opcode; 193 | script[currentMovie].push_back(c); 194 | continue; 195 | } 196 | 197 | for (size_t i = 0; i < length; ++i) { 198 | readUintLE(it, word); 199 | c.data.push_back(word); 200 | } 201 | 202 | if (opcode == CMD_AFTER_SCENE || opcode == CMD_SKIP_IF_PLAYED) { 203 | if (opcode == CMD_AFTER_SCENE) { 204 | listOpcode = CMD_OR_AFTER; 205 | } else { 206 | listOpcode = CMD_OR_SKIP; 207 | } 208 | 209 | readUintLE(it, word); 210 | while (word == listOpcode) { 211 | readUintLE(it, word); 212 | if (word != opcode) { 213 | std::cerr << filename << ": Error parsing label list "<< word << std::endl; 214 | break; 215 | } 216 | readUintLE(it, word); 217 | c.data.push_back(word); 218 | readUintLE(it, word); 219 | c.data.push_back(word); 220 | readUintLE(it, word); 221 | } 222 | it -= 2; 223 | } 224 | 225 | script[currentMovie].push_back(c); 226 | } 227 | } 228 | 229 | u16 SCRANTIC::ADSFile::getMovieNumberFromOrder(size_t pos) { 230 | size_t count = 0; 231 | 232 | for (auto it = tagList.begin(); it != tagList.end(); ++it) { 233 | if (pos == count) { 234 | return it->first; 235 | } 236 | ++count; 237 | } 238 | 239 | return 0; 240 | } 241 | 242 | size_t SCRANTIC::ADSFile::getMoviePosFromNumber(u16 number) { 243 | size_t count = 0; 244 | 245 | for (auto it = tagList.begin(); it != tagList.end(); ++it) { 246 | if (number == it->first) { 247 | return count; 248 | } 249 | ++count; 250 | } 251 | 252 | return 0; 253 | } 254 | 255 | 256 | 257 | void SCRANTIC::ADSFile::findLabels() { 258 | u16 currentMovie; 259 | u16 currentHash = 0; 260 | size_t playCount = 1; 261 | 262 | for (auto movieIt = script.begin(); movieIt != script.end(); ++movieIt) { 263 | currentMovie = movieIt->first; 264 | playCount = 1; 265 | for (size_t pos = 0; pos < movieIt->second.size(); ++pos) { 266 | Command c = movieIt->second[pos]; 267 | if (c.opcode == CMD_AFTER_SCENE) { 268 | if (playCount != 0) { 269 | std::cout << "PlayCount mismatch!" << std::endl; 270 | } 271 | playCount = 1; 272 | for (size_t i = 0; i < c.data.size(); i += 2) { 273 | currentHash = makeHash(c.data.at(i), c.data.at(i+1)); 274 | labelsAfter[currentMovie][currentHash].push_back(pos); 275 | } 276 | } else if (c.opcode == CMD_PLAY_MOVIE) { 277 | playCount--; 278 | } else if ((c.opcode == CMD_SKIP_IF_PLAYED) || (c.opcode == CMD_ONLY_IF_PLAYED)) { 279 | playCount++; 280 | } else if ((c.opcode == CMD_ONLY_IF_PLAYED) && (playCount == 0)) { 281 | playCount = 1; 282 | currentHash = makeHash(c.data.at(0), c.data.at(1)); 283 | labelsTogether[currentMovie][currentHash].push_back(pos); 284 | } 285 | } 286 | } 287 | } 288 | 289 | size_t SCRANTIC::ADSFile::getLabelCountAfter(u16 movie, u16 hash) { 290 | if (labelsAfter.find(movie) == labelsAfter.end()) { 291 | return 0; 292 | } 293 | 294 | if (labelsAfter[movie].find(hash) == labelsAfter[movie].end()) { 295 | return 0; 296 | } 297 | 298 | return labelsAfter[movie][hash].size(); 299 | } 300 | 301 | size_t SCRANTIC::ADSFile::getLabelCountTogether(u16 movie, u16 hash) { 302 | if (labelsTogether.find(movie) == labelsTogether.end()) { 303 | return 0; 304 | } 305 | 306 | if (labelsTogether[movie].find(hash) == labelsTogether[movie].end()) { 307 | return 0; 308 | } 309 | 310 | return labelsTogether[movie][hash].size(); 311 | } 312 | 313 | std::vector SCRANTIC::ADSFile::getInitialBlock(u16 movie) { 314 | std::vector block; 315 | size_t playCount = 1; 316 | 317 | for (size_t pos = 1; pos < script[movie].size(); ++pos) { 318 | Command c = script[movie][pos]; 319 | if ((c.opcode == CMD_SKIP_IF_PLAYED) || (c.opcode == CMD_ONLY_IF_PLAYED)) { 320 | playCount++; 321 | } 322 | 323 | if (c.opcode == CMD_PLAY_MOVIE) { 324 | playCount--; 325 | } 326 | 327 | block.push_back(c); 328 | 329 | if (playCount == 0) { 330 | break; 331 | } 332 | } 333 | 334 | return block; 335 | } 336 | 337 | std::vector SCRANTIC::ADSFile::getBlockAfterMovie(u16 movie, u16 hash, u16 num) { 338 | std::vector block; 339 | 340 | if (num >= getLabelCountAfter(movie, hash)) { 341 | return block; 342 | } 343 | 344 | size_t playCount = 1; 345 | size_t pos = labelsAfter[movie][hash][num] + 1; 346 | 347 | for (; pos < script[movie].size(); ++pos) { 348 | Command c = script[movie][pos]; 349 | if ((c.opcode == CMD_SKIP_IF_PLAYED) || (c.opcode == CMD_ONLY_IF_PLAYED)) { 350 | playCount++; 351 | } 352 | 353 | if (c.opcode == CMD_PLAY_MOVIE) { 354 | playCount--; 355 | } 356 | 357 | block.push_back(c); 358 | 359 | if (playCount == 0) { 360 | break; 361 | } 362 | } 363 | 364 | return block; 365 | } 366 | 367 | std::vector SCRANTIC::ADSFile::getBlockTogetherWithMovie(u16 movie, u16 hash, u16 num) { 368 | std::vector block; 369 | 370 | if (num >= getLabelCountTogether(movie, hash)) { 371 | return block; 372 | } 373 | 374 | size_t playCount = 1; 375 | size_t pos = labelsTogether[movie][hash][num] + 1; 376 | 377 | for (; pos < script[movie].size(); ++pos) { 378 | Command c = script[movie][pos]; 379 | if (c.opcode == CMD_SKIP_IF_PLAYED) { 380 | playCount++; 381 | } 382 | 383 | if (c.opcode == CMD_PLAY_MOVIE) { 384 | playCount--; 385 | } 386 | 387 | block.push_back(c); 388 | 389 | if (playCount == 0) { 390 | break; 391 | } 392 | } 393 | 394 | return block; 395 | } 396 | 397 | v8 SCRANTIC::ADSFile::repackIntoResource() { 398 | std::string strings[5] = { "VER:", "ADS:", "RES:", "SCR:", "TAG:" }; 399 | 400 | compressionFlag = 2; 401 | v8 compressedData = LZCCompress(rawScript); 402 | uncompressedSize = rawScript.size(); 403 | compressedSize = compressedData.size() + 5; 404 | 405 | verSize = version.size() + 1; 406 | magic = 0x8000; 407 | 408 | resSize = 2; 409 | resCount = resList.size(); 410 | for (auto it = resList.begin(); it != resList.end(); ++it) { 411 | resSize += 2 + it->second.size() + 1; 412 | } 413 | 414 | tagCount = tagList.size(); 415 | tagSize = 2; 416 | for (auto it = tagList.begin(); it != tagList.end(); ++it) { 417 | tagSize += 2 + it->second.size() + 1; 418 | } 419 | 420 | resScrTagSize = tagSize + resSize + compressedSize + 24; 421 | 422 | v8 rawData(strings[0].begin(), strings[0].end()); 423 | BaseFile::writeUintLE(rawData, verSize); 424 | std::copy(version.begin(), version.end(), std::back_inserter(rawData)); 425 | rawData.push_back(0); 426 | 427 | std::copy(strings[1].begin(), strings[1].end(), std::back_inserter(rawData)); 428 | BaseFile::writeUintLE(rawData, resScrTagSize); 429 | BaseFile::writeUintLE(rawData, magic); 430 | 431 | std::copy(strings[2].begin(), strings[2].end(), std::back_inserter(rawData)); 432 | BaseFile::writeUintLE(rawData, resSize); 433 | BaseFile::writeUintLE(rawData, resCount); 434 | for (auto it = resList.begin(); it != resList.end(); ++it) { 435 | BaseFile::writeUintLE(rawData, it->first); 436 | std::copy(it->second.begin(), it->second.end(), std::back_inserter(rawData)); 437 | rawData.push_back(0); 438 | } 439 | 440 | std::copy(strings[3].begin(), strings[3].end(), std::back_inserter(rawData)); 441 | BaseFile::writeUintLE(rawData, compressedSize); 442 | BaseFile::writeUintLE(rawData, compressionFlag); 443 | BaseFile::writeUintLE(rawData, uncompressedSize); 444 | std::copy(compressedData.begin(), compressedData.end(), std::back_inserter(rawData)); 445 | compressedSize -= 5; 446 | 447 | std::copy(strings[4].begin(), strings[4].end(), std::back_inserter(rawData)); 448 | BaseFile::writeUintLE(rawData, tagSize); 449 | BaseFile::writeUintLE(rawData, tagCount); 450 | 451 | for (auto it = tagList.begin(); it != tagList.end(); ++it) { 452 | BaseFile::writeUintLE(rawData, it->first); 453 | std::copy(it->second.begin(), it->second.end(), std::back_inserter(rawData)); 454 | rawData.push_back(0); 455 | } 456 | 457 | return rawData; 458 | } 459 | 460 | std::string SCRANTIC::ADSFile::getResource(u16 num) { 461 | auto it = resList.find(num); 462 | if (it == resList.end()) { 463 | return ""; 464 | } else { 465 | return it->second; 466 | } 467 | } 468 | 469 | void SCRANTIC::ADSFile::saveFile(const std::string &path) { 470 | std::stringstream output; 471 | 472 | output << "# SCRANTIC ADS file" << std::endl; 473 | output << "VERSION" << std::endl; 474 | output << version << std::endl << std::endl; 475 | 476 | output << "RESOURCES" << std::endl; 477 | for (auto it = resList.begin(); it != resList.end(); ++it) { 478 | output << hexToString(it->first, std::hex, 4) << " " << it->second << std::endl; 479 | } 480 | 481 | output << std::endl << "TAGS" << std::endl; 482 | for (auto it = tagList.begin(); it != tagList.end(); ++it) { 483 | output << hexToString(it->first, std::hex, 4) << " " << it->second << std::endl; 484 | } 485 | 486 | output << std::endl << "SCRIPT" << std::endl; 487 | 488 | v8::iterator it = rawScript.begin(); 489 | u16 opcode; 490 | u8 length; 491 | u16 word; 492 | 493 | while (it != rawScript.end()) { 494 | readUintLE(it, opcode); 495 | Command command; 496 | 497 | if (opcode <= 0x100) { 498 | output << "# new movie" << std::endl; 499 | command.opcode = CMD_SET_SCENE; 500 | command.data.push_back(opcode); 501 | } else { 502 | length = getParamCount(opcode); 503 | command.opcode = opcode; 504 | for (u8 i = 0; i < length; ++i) { 505 | readUintLE(it, word); 506 | command.data.push_back(word); 507 | } 508 | } 509 | 510 | output << getMnemoic(command) << std::endl; 511 | } 512 | 513 | writeFile(output.str(), filename, path); 514 | } 515 | 516 | 517 | void SCRANTIC::ADSFile::initMnemonics() { 518 | // fake 519 | mnemonics.insert({CMD_SET_SCENE, "SCENE"}); 520 | //no params 521 | mnemonics.insert({CMD_OR_SKIP, "OR"}); 522 | mnemonics.insert({CMD_OR_AFTER, "AND"}); 523 | mnemonics.insert({CMD_PLAY_MOVIE, "PLAYMOVIE"}); 524 | mnemonics.insert({CMD_UNK_1520, "UNK1520"}); 525 | mnemonics.insert({CMD_RANDOM_START, "RANDSTART"}); 526 | mnemonics.insert({CMD_RANDOM_END, "RANDEND"}); 527 | mnemonics.insert({CMD_UNK_LABEL, "UNKLABEL"}); 528 | mnemonics.insert({CMD_UNK_F010, "UNKF010"}); 529 | mnemonics.insert({CMD_END_SCRIPT, "ENDSCRIPT"}); 530 | //1 param 531 | mnemonics.insert({CMD_PLAY_ADS_MOVIE, "PLAYADS"}); 532 | mnemonics.insert({CMD_ZERO_CHANCE, "ZEROCHANCE"}); 533 | //2 params 534 | mnemonics.insert({CMD_UNK_1070, "UNK1070"}); 535 | mnemonics.insert({CMD_ADD_INIT_TTM, "INITTTM"}); 536 | mnemonics.insert({CMD_AFTER_SCENE, "CONTINUEAFTER"}); 537 | mnemonics.insert({CMD_SKIP_IF_PLAYED, "SKIPAFTER"}); 538 | mnemonics.insert({CMD_ONLY_IF_PLAYED, "TOGETHERWITH"}); 539 | //3 params 540 | mnemonics.insert({CMD_KILL_TTM, "KILLTTM"}); 541 | //4 params 542 | mnemonics.insert({CMD_ADD_TTM, "ADDTTM"}); 543 | } 544 | 545 | 546 | std::string SCRANTIC::ADSFile::getMnemoic(SCRANTIC::Command c) { 547 | std::string result = mnemonics[c.opcode]; 548 | 549 | int count = getParamCount(c.opcode); 550 | 551 | if (!count) { 552 | return result; 553 | } 554 | 555 | for (int i = 0; i < count; ++i) { 556 | std::string data = hexToString(c.data.at(i), std::hex, 4); 557 | result += " " + data; 558 | } 559 | 560 | return result; 561 | } 562 | 563 | u16 SCRANTIC::ADSFile::getOpcodeFromMnemonic(std::string &mnemonic) { 564 | for (auto it = mnemonics.begin(); it != mnemonics.end(); ++it) { 565 | if (it->second == mnemonic) { 566 | return it->first; 567 | } 568 | } 569 | return 0; 570 | } 571 | 572 | 573 | int SCRANTIC::ADSFile::getParamCount(u16 opcode) { 574 | switch (opcode) { 575 | case CMD_OR_SKIP: 576 | case CMD_OR_AFTER: 577 | case CMD_PLAY_MOVIE: 578 | case CMD_UNK_1520: 579 | case CMD_RANDOM_START: 580 | case CMD_RANDOM_END: 581 | case CMD_UNK_F010: 582 | case CMD_END_SCRIPT: 583 | return 0; 584 | case CMD_SET_SCENE: // beware fake 585 | case CMD_PLAY_ADS_MOVIE: 586 | case CMD_ZERO_CHANCE: 587 | return 1; 588 | case CMD_UNK_1070: 589 | case CMD_ADD_INIT_TTM: 590 | case CMD_AFTER_SCENE: 591 | case CMD_SKIP_IF_PLAYED: 592 | case CMD_ONLY_IF_PLAYED: 593 | return 2; 594 | case CMD_KILL_TTM: 595 | case CMD_UNK_LABEL: 596 | return 3; 597 | case CMD_ADD_TTM: 598 | return 4; 599 | default: 600 | if (opcode <= 0x100) { 601 | return 1; // CMD_SET_SCENE 602 | } 603 | std::cerr << "Unknown command found: " << opcode << std::endl; 604 | return 0; 605 | } 606 | } 607 | 608 | u16 SCRANTIC::ADSFile::makeHash(u16 ttm, u16 scene) { 609 | return ((ttm & 0xFF) << 8) | (scene & 0xFF); 610 | } 611 | 612 | -------------------------------------------------------------------------------- /ADSFile.h: -------------------------------------------------------------------------------- 1 | #ifndef ADSFILE_H 2 | #define ADSFILE_H 3 | 4 | #include "CompressedBaseFile.h" 5 | #include 6 | #include 7 | 8 | namespace SCRANTIC { 9 | 10 | class ADSFile : public CompressedBaseFile 11 | { 12 | private: 13 | void parseRawScript(); 14 | void findLabels(); 15 | void parseFile(v8 &data); 16 | 17 | std::map mnemonics; 18 | 19 | int getParamCount(u16 opcode); 20 | u16 getOpcodeFromMnemonic(std::string &mnemonic); 21 | void initMnemonics(); 22 | std::string getMnemoic(Command c); 23 | 24 | protected: 25 | u32 verSize; 26 | std::string version; 27 | u16 resScrTagSize; 28 | u16 magic; 29 | u32 resSize; 30 | u16 resCount; 31 | std::map resList; 32 | v8 rawScript; 33 | u32 tagSize; 34 | u16 tagCount; 35 | std::map, size_t> > labels; 36 | std::map > script; 37 | 38 | std::map>> labelsAfter; 39 | std::map>> labelsTogether; 40 | 41 | #ifdef DUMP_ADS 42 | friend class Robinson; 43 | #endif 44 | 45 | public: 46 | std::map tagList; 47 | 48 | ADSFile(const std::string &name, v8 &data); 49 | explicit ADSFile(const std::string &filename); 50 | std::string getResource(u16 num); 51 | 52 | v8 repackIntoResource() override; 53 | void saveFile(const std::string &path) override; 54 | 55 | std::vector getBlockAfterMovie(u16 movie, u16 hash, u16 num = 0); 56 | std::vector getBlockTogetherWithMovie(u16 movie, u16 hash, u16 num = 0); 57 | std::vector getInitialBlock(u16 movie); 58 | size_t getLabelCountAfter(u16 movie, u16 hash); 59 | size_t getLabelCountTogether(u16 movie, u16 hash); 60 | 61 | u16 getMovieNumberFromOrder(size_t pos); 62 | size_t getMoviePosFromNumber(u16 number); 63 | 64 | static u16 makeHash(u16 ttm, u16 scene); 65 | }; 66 | 67 | } 68 | 69 | #endif // ADSFILE_H 70 | -------------------------------------------------------------------------------- /BMPFile.cpp: -------------------------------------------------------------------------------- 1 | #include "BMPFile.h" 2 | 3 | #include 4 | #include 5 | namespace fs = std::experimental::filesystem; 6 | 7 | SCRANTIC::BMPFile::BMPFile(const std::string &name, v8 &data) 8 | : CompressedBaseFile(name), 9 | GraphicBaseFile(), 10 | overview(NULL), 11 | ovTexture(NULL) { 12 | 13 | v8::iterator it = data.begin(); 14 | 15 | assertString(it, "BMP:"); 16 | 17 | readUintLE(it, infBinSize); 18 | readUintLE(it, magic); 19 | 20 | assertString(it, "INF:"); 21 | 22 | readUintLE(it, infSize); 23 | readUintLE(it, imageCount); 24 | 25 | u16 word; 26 | for (int i = 0; i < imageCount; ++i) { 27 | readUintLE(it, word); 28 | widthList.push_back(word); 29 | } 30 | 31 | for (int i = 0; i < imageCount; ++i) { 32 | readUintLE(it, word); 33 | heightList.push_back(word); 34 | } 35 | 36 | assertString(it, "BIN:"); 37 | 38 | if (!handleDecompression(data, it, uncompressedData)) { 39 | return; 40 | } 41 | 42 | size_t z = 0; 43 | for (u16 i = 0; i < imageCount; ++i) { 44 | SDL_Surface *image = createSdlSurface(uncompressedData, widthList.at(i), heightList.at(i), z); 45 | z += widthList.at(i) * heightList.at(i) / 2; 46 | imageList.push_back(image); 47 | } 48 | 49 | createOverview(); 50 | } 51 | 52 | SCRANTIC::BMPFile::BMPFile(const std::string &path) 53 | : CompressedBaseFile(path), 54 | GraphicBaseFile(), 55 | overview(NULL), 56 | ovTexture(NULL) { 57 | 58 | std::string basename = filename; 59 | size_t pos = basename.rfind("/"); 60 | if (pos != std::string::npos) { 61 | basename = basename.substr(pos+1); 62 | } 63 | basename = basename.substr(0, basename.rfind('.')); 64 | size_t length = basename.size(); 65 | v8 bitmapData; 66 | u16 width, height; 67 | std::vector filelist; 68 | 69 | for (const auto & entry : fs::directory_iterator(path)) { 70 | if (entry.path().filename().string().substr(0, length) == basename) { 71 | filelist.push_back(entry.path()); 72 | } 73 | } 74 | 75 | std::sort(filelist.begin(), filelist.end(), [](std::string a, std::string b) { return a < b; }); 76 | 77 | for (auto it = filelist.begin(); it != filelist.end(); ++it) { 78 | bitmapData = readRGBABitmapData(*it, width, height); 79 | std::copy(bitmapData.begin(), bitmapData.end(), std::back_inserter(uncompressedData)); 80 | widthList.push_back(width); 81 | heightList.push_back(height); 82 | } 83 | 84 | imageCount = widthList.size(); 85 | 86 | size_t z = 0; 87 | for (u16 i = 0; i < imageCount; ++i) { 88 | SDL_Surface *image = createSdlSurface(uncompressedData, widthList.at(i), heightList.at(i), z); 89 | z += widthList.at(i) * heightList.at(i) / 2; 90 | imageList.push_back(image); 91 | } 92 | 93 | createOverview(); 94 | } 95 | 96 | SCRANTIC::BMPFile::~BMPFile() { 97 | for (auto i = std::begin(imageList); i != std::end(imageList); ++i) { 98 | SDL_FreeSurface(*i); 99 | } 100 | 101 | SDL_FreeSurface(overview); 102 | SDL_DestroyTexture(ovTexture); 103 | } 104 | 105 | void SCRANTIC::BMPFile::saveFile(const std::string &path) { 106 | size_t z = 0; 107 | for (u16 i = 0; i < imageCount; ++i) { 108 | v8 bmpFile = createRGBABitmapData(uncompressedData, widthList.at(i), heightList.at(i), z); 109 | z += (widthList.at(i) * heightList.at(i)) / 2; 110 | 111 | std::string num = hexToString(i, std::dec); 112 | for (size_t j = num.size(); j < 3; ++j) { 113 | num = "0" + num; 114 | } 115 | 116 | std::string newFilename = filename.substr(0, filename.rfind('.') + 1) + num + ".BMP"; 117 | 118 | SCRANTIC::BaseFile::writeFile(bmpFile, newFilename, path); 119 | } 120 | } 121 | 122 | v8 SCRANTIC::BMPFile::repackIntoResource() { 123 | 124 | std::string strings[3] = { "BMP:", "INF:", "BIN:" }; 125 | 126 | magic = 0x8000; 127 | 128 | compressionFlag = 2; 129 | v8 compressedData = LZCCompress(uncompressedData); 130 | uncompressedSize = uncompressedData.size(); 131 | compressedSize = compressedData.size() + 5; 132 | 133 | imageCount = widthList.size(); 134 | infSize = imageCount * 4 + 2; 135 | 136 | infBinSize = infSize + compressedSize + 16; 137 | 138 | v8 rawData(strings[0].begin(), strings[0].end()); 139 | BaseFile::writeUintLE(rawData, infBinSize); 140 | BaseFile::writeUintLE(rawData, magic); 141 | std::copy(strings[1].begin(), strings[1].end(), std::back_inserter(rawData)); 142 | BaseFile::writeUintLE(rawData, infSize); 143 | BaseFile::writeUintLE(rawData, imageCount); 144 | for (u16 i = 0; i < imageCount; ++i) { 145 | BaseFile::writeUintLE(rawData, widthList.at(i)); 146 | } 147 | for (u16 i = 0; i < imageCount; ++i) { 148 | BaseFile::writeUintLE(rawData, heightList.at(i)); 149 | } 150 | std::copy(strings[2].begin(), strings[2].end(), std::back_inserter(rawData)); 151 | BaseFile::writeUintLE(rawData, compressedSize); 152 | BaseFile::writeUintLE(rawData, compressionFlag); 153 | BaseFile::writeUintLE(rawData, uncompressedSize); 154 | std::copy(compressedData.begin(), compressedData.end(), std::back_inserter(rawData)); 155 | 156 | compressedSize -= 5; 157 | 158 | return rawData; 159 | } 160 | 161 | SDL_Texture *SCRANTIC::BMPFile::getImage(SDL_Renderer *renderer, u16 num, SDL_Rect &rect) { 162 | if ((num >= imageList.size()) || (imageList.at(num) == NULL)) { 163 | return NULL; 164 | } 165 | 166 | if (overview == NULL) { 167 | createOverview(); 168 | } 169 | 170 | if (ovTexture == NULL) { 171 | ovTexture = SDL_CreateTextureFromSurface(renderer, overview); 172 | if (ovTexture == NULL) { 173 | std::cerr << filename << ": Error creating SDL_Texture" << std::endl; 174 | return NULL; 175 | } 176 | SDL_SetTextureBlendMode(ovTexture, SDL_BLENDMODE_BLEND); 177 | } 178 | 179 | rect = imageRect.at(num); 180 | return ovTexture; 181 | } 182 | 183 | SDL_Rect SCRANTIC::BMPFile::getRect(u16 num) { 184 | if ((num >= imageList.size()) || (imageList.at(num) == NULL)) { 185 | return SDL_Rect(); 186 | } 187 | 188 | return imageRect.at(num); 189 | } 190 | 191 | void SCRANTIC::BMPFile::createOverview() { 192 | u16 currentWidth = 0; 193 | u16 lineHeight = 0; 194 | u16 maxWidth = 640; 195 | u16 imgWidth, imgHeight; 196 | u16 currentY = 0; 197 | 198 | for (size_t i = 0; i < imageList.size(); ++i) { 199 | imgWidth = imageList.at(i)->w; 200 | imgHeight = imageList.at(i)->h; 201 | if (currentWidth + imgWidth < maxWidth) { 202 | currentWidth += imgWidth; 203 | if (lineHeight < imgHeight) { 204 | lineHeight = imgHeight; 205 | } 206 | } else { 207 | currentY += lineHeight; 208 | lineHeight = imgHeight; 209 | currentWidth = imgWidth; 210 | } 211 | } 212 | 213 | SDL_FreeSurface(overview); 214 | 215 | imageRect.clear(); 216 | 217 | overview = SDL_CreateRGBSurface(0, maxWidth, currentY + lineHeight, 8, 0, 0, 0, 0); 218 | SDL_SetPaletteColors(overview->format->palette, defaultPalette, 0, 256); 219 | currentWidth = 0; 220 | currentY = 0; 221 | lineHeight = 0; 222 | 223 | SDL_Rect rect, target; 224 | rect.x = 0; 225 | rect.y = 0; 226 | 227 | for (size_t i = 0; i < imageList.size(); ++i) { 228 | imgWidth = imageList.at(i)->w; 229 | imgHeight = imageList.at(i)->h; 230 | rect.w = imgWidth; 231 | rect.h = imgHeight; 232 | target.w = imgWidth; 233 | target.h = imgHeight; 234 | 235 | if (currentWidth + imgWidth < maxWidth) { 236 | target.x = currentWidth; 237 | target.y = currentY; 238 | SDL_BlitSurface(imageList.at(i), &rect, overview, &target); 239 | currentWidth += imgWidth; 240 | if (lineHeight < imgHeight) 241 | lineHeight = imgHeight; 242 | } else { 243 | currentY += lineHeight; 244 | target.x = 0; 245 | target.y = currentY; 246 | SDL_BlitSurface(imageList.at(i), &rect, overview, &target); 247 | lineHeight = imgHeight; 248 | currentWidth = imgWidth; 249 | } 250 | imageRect.push_back(target); 251 | } 252 | 253 | } 254 | 255 | SDL_Texture *SCRANTIC::BMPFile::getOverviewImage(SDL_Renderer *renderer, SDL_Rect &rect) { 256 | if (overview == NULL) { 257 | createOverview(); 258 | if (overview == NULL) { 259 | return NULL; 260 | } 261 | } 262 | 263 | if (ovTexture == NULL) { 264 | ovTexture = SDL_CreateTextureFromSurface(renderer, overview); 265 | SDL_SetTextureBlendMode(ovTexture, SDL_BLENDMODE_BLEND); 266 | if (ovTexture == NULL) { 267 | std::cerr << filename << ": Error creating SDL_Texture" << std::endl; 268 | return NULL; 269 | } 270 | } 271 | 272 | rect.w = overview->w; 273 | rect.h = overview->h; 274 | 275 | return ovTexture; 276 | } 277 | 278 | void SCRANTIC::BMPFile::setPalette(SDL_Color color[], u16 count) { 279 | if (overview == NULL) { 280 | return; 281 | } 282 | 283 | SDL_SetPaletteColors(overview->format->palette, color, 0, 256); 284 | SDL_DestroyTexture(ovTexture); 285 | ovTexture = NULL; 286 | } 287 | -------------------------------------------------------------------------------- /BMPFile.h: -------------------------------------------------------------------------------- 1 | #ifndef BMPFILE_H 2 | #define BMPFILE_H 3 | 4 | #include "CompressedBaseFile.h" 5 | #include "GraphicBaseFile.h" 6 | 7 | namespace SCRANTIC { 8 | 9 | class BMPFile : public CompressedBaseFile, public GraphicBaseFile { 10 | protected: 11 | u16 infBinSize; 12 | u16 magic; //0x8000 13 | u32 infSize; 14 | u16 imageCount; 15 | v16 widthList; 16 | v16 heightList; 17 | v8 uncompressedData; 18 | std::vector imageList; 19 | std::vector imageRect; 20 | SDL_Surface *overview; 21 | SDL_Texture *ovTexture; 22 | 23 | void createOverview(); 24 | 25 | public: 26 | BMPFile(const std::string &name, v8 &data); 27 | explicit BMPFile(const std::string &path); 28 | ~BMPFile(); 29 | 30 | void saveFile(const std::string &path) override; 31 | v8 repackIntoResource() override; 32 | 33 | SDL_Texture *getImage(SDL_Renderer *renderer, u16 num, SDL_Rect &rect); 34 | SDL_Texture *getOverviewImage(SDL_Renderer *renderer, SDL_Rect &rect); 35 | void setPalette(SDL_Color color[], u16 count); 36 | SDL_Rect getRect(u16 num); 37 | 38 | size_t getImageCount() { 39 | return imageList.size(); 40 | } 41 | }; 42 | 43 | } 44 | 45 | #endif // BMPFILE_H 46 | -------------------------------------------------------------------------------- /BaseFile.cpp: -------------------------------------------------------------------------------- 1 | #include "BaseFile.h" 2 | 3 | #include 4 | 5 | SCRANTIC::BaseFile::BaseFile(const std::string &name) : 6 | filename(name) { 7 | 8 | size_t pos = filename.rfind('/'); 9 | if (pos != std::string::npos) { 10 | filename = filename.substr(pos+1); 11 | } 12 | } 13 | 14 | SCRANTIC::BaseFile::~BaseFile() { 15 | 16 | } 17 | 18 | v8 SCRANTIC::BaseFile::readFile(const std::string &filename) { 19 | std::ifstream in; 20 | in.open(filename, std::ios::binary | std::ios::in); 21 | in.unsetf(std::ios::skipws); 22 | 23 | u8 byte; 24 | v8 data; 25 | 26 | while (in.read((char*)&byte, 1)) { 27 | data.push_back(byte); 28 | } 29 | 30 | in.close(); 31 | return data; 32 | } 33 | 34 | 35 | std::string SCRANTIC::BaseFile::readString(std::ifstream *in, u8 length, char delimiter) { 36 | if (!in->is_open()) { 37 | return ""; 38 | } 39 | 40 | std::streampos pos = in->tellg(); 41 | std::streampos i = 0; 42 | std::string str = ""; 43 | char c; 44 | 45 | in->read(&c, 1); 46 | i += 1; 47 | while (c != delimiter) { 48 | str += c; 49 | in->read(&c, 1); 50 | i += 1; 51 | if ((length) && (i > length)) { 52 | break; 53 | } 54 | } 55 | 56 | if (length && (str.length() != length)) { 57 | in->seekg(pos + static_cast(length + 1), std::ios::beg); 58 | } 59 | 60 | return str; 61 | } 62 | 63 | std::string SCRANTIC::BaseFile::readString(v8::iterator &it, u8 length, char delimiter) { 64 | std::string str = ""; 65 | u8 i = 0; 66 | 67 | char c = (char)*it; 68 | ++it; 69 | ++i; 70 | 71 | while (c != delimiter) { 72 | str += c; 73 | c = (char)*it; 74 | ++i; 75 | ++it; 76 | 77 | if (length && (i > length)) { 78 | break; 79 | } 80 | } 81 | 82 | if (length) { 83 | for ( ; i < length; ++i) { 84 | ++it; 85 | } 86 | } 87 | 88 | return str; 89 | } 90 | 91 | std::string SCRANTIC::BaseFile::readConstString(std::ifstream *in, u8 length) { 92 | if (!in->is_open()) { 93 | return ""; 94 | } 95 | 96 | char c; 97 | std::string str = ""; 98 | 99 | for (u8 i = 0; i < length; ++i) { 100 | in->read(&c, 1); 101 | str += c; 102 | } 103 | 104 | return str; 105 | } 106 | 107 | std::string SCRANTIC::BaseFile::readConstString(v8::iterator &it, u8 length) { 108 | std::string str = ""; 109 | 110 | for (u8 i = 0; i < length; ++i) { 111 | str += (char)*it; 112 | ++it; 113 | } 114 | 115 | return str; 116 | } 117 | 118 | void SCRANTIC::BaseFile::assertString(v8::iterator &it, std::string expectedString) { 119 | std::string actualString = readConstString(it, expectedString.size()); 120 | if (actualString != expectedString) { 121 | std::cerr << filename << ": \"" << expectedString << "\" expected; got \"" << actualString << "\"" << std::endl; 122 | throw BaseFileException(); 123 | } 124 | } 125 | 126 | void SCRANTIC::BaseFile::writeFile(const std::vector &data, std::string &name, std::string path) { 127 | std::ofstream out; 128 | 129 | if (path.length() && (path[path.length()-1] != '/')) { 130 | path += "/"; 131 | } 132 | 133 | out.open(path + name, std::ios::binary | std::ios::out); 134 | out.unsetf(std::ios::skipws); 135 | 136 | if (!out.is_open()) { 137 | std::cerr << "BaseFile: Could not open " << path + name << std::endl; 138 | return; 139 | } 140 | 141 | for (auto const &i: data) { 142 | out.write((char*)&i, 1); 143 | } 144 | 145 | out.close(); 146 | } 147 | 148 | void SCRANTIC::BaseFile::writeFile(const std::string &data, std::string &name, std::string path) { 149 | std::ofstream out; 150 | 151 | if (path.length() && (path[path.length()-1] != '/')) { 152 | path += "/"; 153 | } 154 | 155 | out.open(path + name, std::ios::out); 156 | 157 | if (!out.is_open()) { 158 | std::cerr << "BaseFile: Could not open " << path + name << std::endl; 159 | return; 160 | } 161 | 162 | out << data; 163 | 164 | out.close(); 165 | } 166 | 167 | std::string SCRANTIC::BaseFile::commandToString(Command cmd, bool ads) { 168 | std::string ret = " "; 169 | std::string hex; 170 | size_t len = 4; 171 | 172 | for (size_t i = 0; i < cmd.data.size(); ++i) { 173 | hex = hexToString(cmd.data.at(i), std::hex); 174 | for (size_t j = hex.size(); j < len; ++j) 175 | hex = "0" + hex; 176 | ret += hex + " "; 177 | } 178 | 179 | if (cmd.name.length()) { 180 | ret += cmd.name; 181 | } 182 | 183 | if (!ads) { 184 | switch (cmd.opcode) { 185 | case CMD_CLEAR_IMGSLOT: 186 | return "Clear Image Slot" + ret; 187 | case CMD_PURGE: 188 | return "Purge" + ret; 189 | case CMD_UPDATE: 190 | return "Update" + ret; 191 | case CMD_DELAY: 192 | return "Delay" + ret; 193 | case CMD_SEL_SLOT_IMG: 194 | return "Select Image Slot" + ret; 195 | case CMD_SEL_SLOT_PAL: 196 | return "Select Palette Slot" + ret; 197 | case CMD_SET_SCENE_LABEL: 198 | return "Label Scene" + ret; 199 | case CMD_SET_SCENE: 200 | return "New Scene" + ret; 201 | case CMD_UNK_1120: 202 | return "Unkown 0x1120" + ret; 203 | case CMD_JMP_SCENE: 204 | return "Jump to Scene" + ret; 205 | case CMD_SET_COLOR: 206 | return "Set Color" + ret; 207 | case CMD_UNK_2010: 208 | return "Unkown 0x2010" + ret; 209 | case CMD_TIMER: 210 | return "Timer" + ret; 211 | case CMD_CLIP_REGION: 212 | return "Clip Region" + ret; 213 | case CMD_SAVE_IMAGE: 214 | return "Save Image" + ret; 215 | case CMD_SAVE_IMAGE_NEW: 216 | return "Save New Image" + ret; 217 | case CMD_DRAW_PIXEL: 218 | return "Draw Pixel" + ret; 219 | case CMD_UNK_A050: 220 | return "Unkown 0xA050" + ret; 221 | case CMD_UNK_A060: 222 | return "Unkown 0xA060" + ret; 223 | case CMD_DRAW_LINE: 224 | return "Draw Line" + ret; 225 | case CMD_DRAW_RECTANGLE: 226 | return "Draw Rectangle" + ret; 227 | case CMD_DRAW_ELLIPSE: 228 | return "Draw Ellipse" + ret; 229 | case CMD_DRAW_SPRITE: 230 | return "Draw Sprite (normal)" + ret; 231 | case CMD_DRAW_SPRITE_MIRROR: 232 | return "Draw Sprite (mirror)" + ret; 233 | case CMD_CLEAR_RENDERER: 234 | return "Clear Renderer" + ret; 235 | case CMD_UNK_B600: 236 | return "Unkown 0xB600" + ret; 237 | case CMD_PLAY_SOUND: 238 | return "Play Sound" + ret; 239 | case CMD_LOAD_SCREEN: 240 | return "Load Screen" + ret; 241 | case CMD_LOAD_BITMAP: 242 | return "Load Bitmap" + ret; 243 | case CMD_LOAD_PALETTE: 244 | return "Load Palette" + ret; 245 | default: 246 | return "DEFAULT 0x" + hexToString(cmd.opcode, std::hex) + ret; 247 | } 248 | } else { 249 | switch (cmd.opcode) { 250 | // ADS instructions 251 | case CMD_SET_SCENE: 252 | return "New ADS Movie" + ret; 253 | case CMD_UNK_1070: 254 | return "Unkown 0x1070" + ret; 255 | case CMD_ADD_INIT_TTM: 256 | return "Add Init TTM" + ret; 257 | case CMD_AFTER_SCENE: 258 | return "Label" + ret; 259 | case CMD_SKIP_IF_PLAYED: 260 | return "Skip Next If Played" + ret; 261 | case CMD_ONLY_IF_PLAYED: 262 | return "Play only with" + ret; 263 | case CMD_OR_SKIP: 264 | return "Or (Skip)" + ret; 265 | case CMD_OR_AFTER: 266 | return "Or (After)" + ret; 267 | case CMD_PLAY_MOVIE: 268 | return "Play Movie" + ret; 269 | case CMD_UNK_1520: 270 | return "Unkown 0x1520" + ret; 271 | case CMD_ADD_TTM: 272 | return "Add TTM" + ret; 273 | case CMD_KILL_TTM: 274 | return "Kill TTM" + ret; 275 | case CMD_RANDOM_START: 276 | return "Random Start" + ret; 277 | case CMD_ZERO_CHANCE: 278 | return "Zero chance" + ret; 279 | case CMD_RANDOM_END: 280 | return "Random End" + ret; 281 | case CMD_UNK_LABEL: 282 | return "Unkown 0x4000" + ret; 283 | case CMD_UNK_F010: 284 | return "Unkown 0xF010" + ret; 285 | case CMD_PLAY_ADS_MOVIE: 286 | return "Play ADS Movie" + ret; 287 | case CMD_END_SCRIPT: 288 | return "End script" + ret; 289 | 290 | default: 291 | return "DEFAULT 0x" + hexToString(cmd.opcode, std::hex) + ret; 292 | } 293 | } 294 | } 295 | -------------------------------------------------------------------------------- /BaseFile.h: -------------------------------------------------------------------------------- 1 | #ifndef BASEFILE_H 2 | #define BASEFILE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "types.h" 10 | 11 | namespace SCRANTIC { 12 | 13 | // TTM instructions 14 | #define CMD_CLEAR_IMGSLOT 0x0080 // only after "Select Image Slot" - clear slot?! -- old define CMD_DRAW_BACKGROUND / no params => seems right 15 | #define CMD_PURGE 0x0110 // still not sure what really gets "purged" / no params 16 | #define CMD_UPDATE 0x0FF0 // no params 17 | 18 | #define CMD_DELAY 0x1020 // how long is delay 0 ? / 1 param: delay 19 | #define CMD_SEL_SLOT_IMG 0x1050 // 1 param: slot number 20 | #define CMD_SEL_SLOT_PAL 0x1060 // 1 param: slot number 21 | #define CMD_SET_SCENE_LABEL 0x1100 // called 5 times always before 0xA600 also SET_SCENE_LABEL / 1 param: scene number 22 | #define CMD_SET_SCENE 0x1110 // 1 param: scene number 23 | #define CMD_UNK_1120 0x1120 // always(?) before "Save New Image" parm 0/1 (once 2 - mistake?) does this actually clear the saved image? / 1 param 24 | #define CMD_JMP_SCENE 0x1200 // param very often own scene number - not always / 1 param scene number 25 | #define CMD_CLEAR_RENDERER 0xA600 // param 0/1/2 (count: 6126/673/15) empty sprite list?! / 1 param 26 | #define CMD_PLAY_SOUND 0xC050 // not all sounds used?! / 1 param 27 | #define CMD_LOAD_SCREEN 0xF010 // 1 string param 28 | #define CMD_LOAD_BITMAP 0xF020 // 1 string param 29 | #define CMD_LOAD_PALETTE 0xF050 // 1 string param 30 | 31 | #define CMD_SET_COLOR 0x2000 // Set Bg/Fg color? (once 0xcf 0xcf - mistake?) / 2 params fore/background colour 32 | #define CMD_UNK_2010 0x2010 // param always 0x0 0x0 usually before Select Image Slot/Load Bitmap -- old define CMD_SET_FRAME_1 / 2 params 33 | #define CMD_TIMER 0x2020 // called often in "xyz timer" / 2 params wait count & delay 34 | #define CMD_DRAW_PIXEL 0xA000 // draw pixel at x,y / 2 params 35 | 36 | #define CMD_CLIP_REGION 0x4000 // clip region for sprites ATTENTION x1, y1, x2, y2 - not width/height! / 4 params 37 | #define CMD_SAVE_IMAGE 0x4200 // save to last image? / 4 params x,y,w,h 38 | #define CMD_SAVE_IMAGE_NEW 0x4210 // save to new image / 4 params x,y,w,h 39 | #define CMD_UNK_A050 0xA050 // called only once in LILIPUTS ROW ASHORE (2: Unkown 0xA050 00c2 007b 00af 008e) freeze part of screen? / 4 params => removing this command the ship is "kept" after sailing away 40 | #define CMD_UNK_A060 0xA060 // called only once (5: Unkown 0xA060 00c2 007b 00af 008e) unfreeze screen --- probably wrong / 4 params => removing this command seems to do nothing?! 41 | #define CMD_DRAW_LINE 0xA0A0 // 4 params 42 | #define CMD_DRAW_RECTANGLE 0xA100 // draw rectangle?! (colors from set frame?!) / 4 params 43 | #define CMD_DRAW_ELLIPSE 0xA400 // 4 params 44 | #define CMD_DRAW_SPRITE 0xA500 // 4 params 45 | //#define CMD_DRAW_SPRITE_VMIRROR 0xA510 // 4 params 46 | #define CMD_DRAW_SPRITE_MIRROR 0xA520 // mirrored / 4 params 47 | //#define CMD_DRAW_SPRITE_HVMIRROR 0xA510 // 4 params ? 48 | 49 | #define CMD_UNK_B600 0xB600 // called 6 times params: rect + 0x2 + 0x1 ?! -- old define CMD_DRAW_SCREEN / 6 params 50 | 51 | // ADS instructions 52 | #define CMD_UNK_1070 0x1070 // called only once before 0x1520 (0x1520 might be third param?) 53 | // don't follow to label for ttm/scene ? 54 | // 36: Unkown 0x1070 0004 0005 55 | // 37: Unkown 0x1520 56 | #define CMD_ADD_INIT_TTM 0x1330 // Init for TTM Res $1 Scene $2 - why is scene needed? 57 | #define CMD_AFTER_SCENE 0x1350 // more like "do while ttm/scene last played" 58 | // play the following only, but always, after res/scene 59 | #define CMD_SKIP_IF_PLAYED 0x1360 // this seems like an actual skip if res/scene was lastplayed 60 | #define CMD_ONLY_IF_PLAYED 0x1370 // 2 Params: TTM and Scene ? ==> Next command only with or after TTM/scene 61 | #define CMD_OR_SKIP 0x1420 // always after/between SKIPNEXT2 OR condition FOR CMD_SKIP_NEXT_IF_2 ? 62 | #define CMD_OR_AFTER 0x1430 // already attached to CMD_COND_MOVIE 63 | #define CMD_PLAY_MOVIE 0x1510 // play one movie from rand list OR play movie list 64 | #define CMD_UNK_1520 0x1520 // only called once 65 | // no params; Add TTM follows 2005 0004 0016 0000 0001 66 | #define CMD_ADD_TTM 0x2005 // $1: res $2: scene $3: repeat $4: ??? --- does no longer force init scene 0 67 | #define CMD_KILL_TTM 0x2010 // kill TTM 68 | #define CMD_RANDOM_START 0x3010 // add following movies to random list 69 | #define CMD_ZERO_CHANCE 0x3020 70 | #define CMD_RANDOM_END 0x30FF // rand list end 71 | #define CMD_UNK_LABEL 0x4000 // another kind of label/jump mark? 72 | #define CMD_UNK_F010 0xF010 // called 67 times ==> 0xF010 0xFFFF end current script 73 | #define CMD_PLAY_ADS_MOVIE 0xF200 // 0: Select Scene 0001 MUN. AMB. POS.A SW 74 | // 1: Unkown 0xF200 000e <-- play movie no. 0x000e ? 75 | #define CMD_END_SCRIPT 0xFFFF // end movie? 76 | 77 | 78 | struct BaseFileException : public std::exception { 79 | const char * what () const throw () { 80 | return "Magic string not found!"; 81 | } 82 | }; 83 | 84 | struct Command { 85 | u16 opcode; 86 | v16 data; 87 | std::string name; 88 | }; 89 | 90 | class BaseFile { 91 | protected: 92 | void assertString(v8::iterator &it, std::string expectedString); 93 | 94 | public: 95 | explicit BaseFile(const std::string &name); 96 | virtual ~BaseFile(); 97 | 98 | std::string filename; 99 | virtual void saveFile(const std::string &path) {}; 100 | virtual v8 repackIntoResource() { return v8{}; }; 101 | 102 | static std::string commandToString(Command cmd, bool ads = false); 103 | 104 | static std::string readString(std::ifstream *in, u8 length = 0, char delimiter = '\0'); 105 | static std::string readString(v8::iterator &it, u8 length = 0, char delimiter = '\0'); 106 | static std::string readConstString(std::ifstream *in, u8 length); 107 | static std::string readConstString(v8::iterator &it, u8 length); 108 | 109 | static void writeFile(const v8 &data, std::string &name, std::string path = ""); 110 | static void writeFile(const std::string &data, std::string &name, std::string path = ""); 111 | 112 | static v8 readFile(const std::string &filename); 113 | 114 | template < typename T > static void readUintLE(std::ifstream *in, T &var); 115 | template < typename T > static void readUintLE(v8::iterator &it, T &var); 116 | template < typename T > static std::string hexToString(T t, std::ios_base & (*f)(std::ios_base&),int pad = 0); 117 | 118 | template < typename T > static void writeUintLE(v8 &data, T &var); 119 | template < typename T > static void writeUintLE(std::ofstream *out, T &var); 120 | }; 121 | 122 | } 123 | 124 | template < typename T > 125 | void SCRANTIC::BaseFile::readUintLE(std::ifstream *in, T &var) { 126 | if (!in->is_open()) { 127 | return; 128 | } 129 | 130 | size_t size = sizeof(var); 131 | u8 byte; 132 | var = 0; 133 | 134 | for (u8 i = 0; i < size; ++i) { 135 | in->read((char*)&byte, 1); 136 | var |= (byte << (i * 8)); 137 | } 138 | } 139 | 140 | template < typename T > 141 | void SCRANTIC::BaseFile::readUintLE(v8::iterator &it, T &var) { 142 | size_t size = sizeof(var); 143 | var = 0; 144 | 145 | for (u8 i = 0; i < size; ++i) { 146 | var |= (*it << (i * 8)); 147 | ++it; 148 | } 149 | } 150 | 151 | template 152 | std::string SCRANTIC::BaseFile::hexToString(T t, std::ios_base & (*f)(std::ios_base&), int pad) { 153 | std::ostringstream oss; 154 | oss << f << t; 155 | if (pad == 0) { 156 | return oss.str(); 157 | } 158 | std::string result = oss.str(); 159 | for (int i = result.size(); i < pad; ++i) { 160 | result = "0" + result; 161 | } 162 | return result; 163 | } 164 | 165 | template < typename T > 166 | void SCRANTIC::BaseFile::writeUintLE(v8 &data, T &var) { 167 | size_t size = sizeof(var); 168 | for (u8 i = 0; i < size; ++i) { 169 | data.push_back((var >> (8*i)) & 0xFF); 170 | } 171 | } 172 | 173 | template < typename T > 174 | void SCRANTIC::BaseFile::writeUintLE(std::ofstream *out, T &var) { 175 | if (!out->is_open()) { 176 | return; 177 | } 178 | 179 | size_t size = sizeof(var); 180 | u8 byte; 181 | var = 0; 182 | 183 | for (u8 i = 0; i < size; ++i) { 184 | out->write((char*)&byte, 1); 185 | var |= (byte << (i * 8)); 186 | } 187 | } 188 | 189 | #endif // BASEFILE_H 190 | 191 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(Johnny) 2 | cmake_minimum_required(VERSION 2.8) 3 | aux_source_directory(. SRC_LIST) 4 | add_executable(${PROJECT_NAME} ${SRC_LIST}) 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 6 | 7 | INCLUDE(FindPkgConfig) 8 | 9 | PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) 10 | #PKG_SEARCH_MODULE(SDL2GFX REQUIRED SDL2_gfx) 11 | PKG_SEARCH_MODULE(SDL2MIXER REQUIRED SDL2_mixer>=2.0.0) 12 | PKG_SEARCH_MODULE(SDL2TTF REQUIRED SDL2_ttf>=2.0.0) 13 | set(CMAKE_BUILD_TYPE Debug) 14 | 15 | INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS}) 16 | set(SDL2GFX_LIBRARY "-lSDL2_gfx") 17 | set(CPPFS_LIBRARY "-lstdc++fs") 18 | TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2GFX_LIBRARY} ${SDL2MIXER_LIBRARIES} ${SDL2TTF_LIBRARIES} ${CPPFS_LIBRARY}) 19 | -------------------------------------------------------------------------------- /CommandlineParser.h: -------------------------------------------------------------------------------- 1 | #ifndef INPUTPARSER_H 2 | #define INPUTPARSER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class CommandlineParser { 9 | private: 10 | std::map parameters; 11 | 12 | public: 13 | CommandlineParser (char **begin, char **end) { 14 | std::string lastArg = ""; 15 | std::string currentArg = ""; 16 | 17 | for (char **it = ++begin; it != end; ++it) { 18 | currentArg = *it; 19 | 20 | if (currentArg.substr(0, 2) == "--") { 21 | if (lastArg != "") { 22 | parameters.insert({lastArg, ""}); 23 | lastArg = currentArg; 24 | continue; 25 | } 26 | lastArg = currentArg; 27 | continue; 28 | } 29 | 30 | if (lastArg == "") { 31 | std::cout << "Invalid parameter: " << currentArg << std::endl; 32 | continue; 33 | } 34 | 35 | parameters.insert({lastArg, currentArg}); 36 | lastArg = ""; 37 | } 38 | 39 | if (lastArg != "") { 40 | parameters.insert({lastArg, ""}); 41 | } 42 | } 43 | 44 | std::string getCmdOption(const std::string &option) { 45 | auto it = parameters.find(option); 46 | if (it == parameters.end()) { 47 | return ""; 48 | } 49 | return it->second; 50 | } 51 | 52 | bool cmdOptionExists(const std::string &option) { 53 | auto it = parameters.find(option); 54 | return it != parameters.end(); 55 | } 56 | }; 57 | 58 | #endif // INPUTPARSER_H 59 | -------------------------------------------------------------------------------- /CompressedBaseFile.cpp: -------------------------------------------------------------------------------- 1 | #include "CompressedBaseFile.h" 2 | 3 | #include 4 | 5 | v8 SCRANTIC::CompressedBaseFile::RLECompress(v8 const &uncompressedData) { 6 | v8 compressedData; 7 | size_t pos = 0; 8 | size_t differenceStart; 9 | u8 length = 0; 10 | u8 byte; 11 | 12 | while (pos < uncompressedData.size()) { 13 | byte = uncompressedData[pos++]; 14 | 15 | while ((uncompressedData[pos] == byte) 16 | && (length < 0x7E) && (pos < uncompressedData.size())) { 17 | length++; 18 | pos++; 19 | } 20 | 21 | if (length) { 22 | compressedData.push_back((length+1) | 0x80); 23 | compressedData.push_back(byte); 24 | length = 0; 25 | continue; 26 | } 27 | 28 | differenceStart = pos - 1; 29 | while ((uncompressedData[pos] != byte) 30 | && (length < 0x7E) && (pos < uncompressedData.size())) { 31 | byte = uncompressedData[pos++]; 32 | length++; 33 | } 34 | 35 | compressedData.push_back(length+1); 36 | length = 0; 37 | for (; differenceStart < pos; ++differenceStart) { 38 | compressedData.push_back(uncompressedData[differenceStart]); 39 | } 40 | } 41 | 42 | return compressedData; 43 | } 44 | 45 | v8 SCRANTIC::CompressedBaseFile::RLEDecompress(v8 const &compressedData, size_t offset, u32 size) { 46 | v8 decompressedData; 47 | u8 byte, length; 48 | 49 | while ((offset < compressedData.size()) && ((size == 0) || (decompressedData.size() < size))) { 50 | byte = compressedData[offset++]; 51 | if ((byte & 0x80) == 0x80) { 52 | length = (u8)(byte & 0x7F); 53 | byte = compressedData[offset++]; 54 | for (u8 i = 0; i < length; ++i) { 55 | decompressedData.push_back(byte); 56 | } 57 | } else { 58 | for (u8 i = 0; i < byte; ++i) { 59 | decompressedData.push_back(compressedData[offset++]); 60 | } 61 | } 62 | } 63 | 64 | return decompressedData; 65 | } 66 | 67 | v8 SCRANTIC::CompressedBaseFile::RLE2Decompress(v8 const &compressedData, size_t offset, u32 size) { 68 | v8 decompressedData; 69 | u8 byte, length; 70 | 71 | while ((offset < compressedData.size()) && ((size == 0) || (decompressedData.size() < size))) { 72 | byte = compressedData[offset++]; 73 | if ((byte & 0x80) == 0x80) { 74 | length = compressedData[offset++]; 75 | for (u8 i = 0; i < length; ++i) { 76 | decompressedData.push_back(byte & 0x7F); 77 | } 78 | } else { 79 | for (u8 i = 0; i < byte; ++i) { 80 | decompressedData.push_back(compressedData[offset++]); 81 | } 82 | } 83 | } 84 | 85 | return decompressedData; 86 | } 87 | 88 | void SCRANTIC::CompressedBaseFile::writeBits(v8 &data, u8 &bitPos, u16 bits, u8 bitLength) { 89 | bool partialByte = (bitPos != 0); 90 | u32 shiftedData = bits << bitPos; 91 | size_t byteCount = (bitLength + bitPos) / 8; 92 | bitPos = (bitLength + bitPos) % 8; 93 | if (bitPos) { 94 | ++byteCount; 95 | } 96 | 97 | for (size_t i = 0; i < byteCount; ++i) { 98 | u8 currentByte = (shiftedData >> (8*i)) & 0xFF; 99 | if ((i == 0) && (partialByte)) { 100 | data[data.size() - 1] |= currentByte; 101 | } else { 102 | data.push_back(currentByte); 103 | } 104 | } 105 | 106 | } 107 | 108 | u16 SCRANTIC::CompressedBaseFile::readBits(v8 const &data, size_t &bytePos, u8 &bitPos, u16 bits) { 109 | u16 byte = 0x00; 110 | for (u16 i = 0; i < bits; ++i) { 111 | if (bytePos >= data.size()) { 112 | return byte; 113 | } 114 | 115 | byte |= ((data[bytePos] >> bitPos) & 1) << i; 116 | 117 | if (bitPos >= 7) { 118 | bitPos = 0; 119 | ++bytePos; 120 | } else { 121 | ++bitPos; 122 | } 123 | } 124 | 125 | return byte; 126 | } 127 | 128 | bool SCRANTIC::CompressedBaseFile::findInDictionary(std::vector &dictionary, v8 currentBlock, u16 &dictPos) { 129 | if (currentBlock.size() == 1) { 130 | dictPos = currentBlock[0]; 131 | return true; 132 | } 133 | 134 | for (u16 i = 0; i < dictionary.size(); ++i) { 135 | if (currentBlock.size() != dictionary[i].size()) { 136 | continue; 137 | } 138 | 139 | bool mismatch = false; 140 | for (size_t j = 0; j < dictionary[i].size(); ++j) { 141 | if (dictionary[i][j] != currentBlock[j]) { 142 | mismatch = true; 143 | break; 144 | } 145 | } 146 | 147 | if (!mismatch) { 148 | dictPos = i + 257; 149 | return true; 150 | } 151 | } 152 | 153 | return false; 154 | } 155 | 156 | v8 SCRANTIC::CompressedBaseFile::LZCCompress(v8 const &uncompressedData) { 157 | v8 compressedData; 158 | v8 currentBlock; 159 | 160 | std::vector dictionary; 161 | 162 | u8 bitLength = 9; 163 | u8 bitPos = 0; 164 | size_t bytePos = 0; 165 | u32 bitCounter = 0; 166 | 167 | u8 nextByte; 168 | u16 dictPos = 0; 169 | 170 | while (bytePos < uncompressedData.size()) { 171 | nextByte = uncompressedData[bytePos++]; 172 | currentBlock.push_back(nextByte); 173 | 174 | if (!findInDictionary(dictionary, currentBlock, dictPos)) { 175 | if (dictionary.size() + 256 < 4095) { 176 | dictionary.push_back(currentBlock); 177 | } 178 | 179 | currentBlock.pop_back(); 180 | if (currentBlock.size() == 1) { 181 | writeBits(compressedData, bitPos, currentBlock[0], bitLength); 182 | } else { 183 | writeBits(compressedData, bitPos, dictPos, bitLength); 184 | } 185 | bitCounter += bitLength; 186 | currentBlock = { nextByte }; 187 | 188 | if (dictionary.size() + 256 >= (1 << bitLength)) { 189 | ++bitLength; 190 | bitCounter = 0; 191 | } 192 | 193 | if (dictionary.size() + 256 >= 4095) { 194 | writeBits(compressedData, bitPos, 256, bitLength); 195 | bitCounter += bitLength; 196 | u16 nskip = ((bitLength*8) - ((bitCounter - 1) % (bitLength*8))) - 1; 197 | writeBits(compressedData, bitPos, 0, nskip); 198 | bitCounter = 0; 199 | bitLength = 9; 200 | dictionary.clear(); 201 | } 202 | } 203 | } 204 | 205 | for (size_t i = 0; i < currentBlock.size(); ++i) { 206 | writeBits(compressedData, bitPos, currentBlock[i], bitLength); 207 | } 208 | 209 | return compressedData; 210 | } 211 | 212 | v8 SCRANTIC::CompressedBaseFile::LZCDecompress(v8 const &compressedData, size_t offset, u32 size) { 213 | v8 decompressedData; 214 | std::pair dictionary[4096]; 215 | v8 decodeStack; 216 | 217 | u8 bitLength = 9; 218 | u16 freeDictPos = 257; 219 | 220 | u16 oldcode, newcode, code; 221 | size_t bytePos = offset; 222 | u8 bitPos = 0; 223 | u8 lastbyte; 224 | u32 bitCounter = 0; 225 | 226 | oldcode = readBits(compressedData, bytePos, bitPos, bitLength); 227 | lastbyte = oldcode; 228 | 229 | decompressedData.push_back((u8)oldcode); 230 | 231 | while ((bytePos < compressedData.size()-1) && ((size == 0) || (decompressedData.size() < size))) { 232 | newcode = readBits(compressedData, bytePos, bitPos, bitLength); 233 | bitCounter += bitLength; 234 | 235 | if (newcode == 256) { 236 | u16 nskip = ((bitLength*8) - ((bitCounter - 1) % (bitLength*8))) - 1; 237 | readBits(compressedData, bytePos, bitPos, nskip); 238 | bitLength = 9; 239 | freeDictPos = 256; 240 | bitCounter = 0; 241 | continue; 242 | } 243 | 244 | code = newcode; 245 | if (code >= freeDictPos) { 246 | if (decodeStack.size() >= 4096) { 247 | break; 248 | } 249 | decodeStack.push_back(lastbyte); 250 | code = oldcode; 251 | } 252 | 253 | while (code >= 256) { 254 | if (code > 4095) { 255 | break; 256 | } 257 | decodeStack.push_back(dictionary[code].second); 258 | code = dictionary[code].first; 259 | } 260 | 261 | decodeStack.push_back((u8)code); 262 | lastbyte = (u8)code; 263 | 264 | for (size_t i = decodeStack.size(); i > 0; --i) { 265 | decompressedData.push_back(decodeStack[i-1]); 266 | } 267 | decodeStack.clear(); 268 | 269 | if (freeDictPos < 4096) { 270 | dictionary[freeDictPos].first = oldcode; 271 | dictionary[freeDictPos].second = lastbyte; 272 | ++freeDictPos; 273 | 274 | if ((bitLength < 12) && (freeDictPos >= (1 << bitLength))) { 275 | ++bitLength; 276 | bitCounter = 0; 277 | } 278 | } 279 | 280 | oldcode = newcode; 281 | } 282 | 283 | return decompressedData; 284 | } 285 | bool SCRANTIC::CompressedBaseFile::handleDecompression(v8 &data, v8::iterator &it, v8 &uncompressedData) { 286 | readUintLE(it, compressedSize); 287 | compressedSize -= 5; // substract compressionFlag and uncompressedSize 288 | readUintLE(it, compressionFlag); 289 | readUintLE(it, uncompressedSize); 290 | 291 | size_t i = std::distance(data.begin(), it); 292 | 293 | switch (compressionFlag) { 294 | case 0x00: 295 | uncompressedData = v8(it, (it+uncompressedSize)); 296 | break; 297 | case 0x01: 298 | uncompressedData = RLEDecompress(data, i, uncompressedSize); 299 | // { 300 | // v8 recompressedData = RLECompress(uncompressedData); 301 | // v8 derecompressedData = RLEDecompress(recompressedData, 0, uncompressedSize); 302 | // std::cout << filename << ": compression size " << (u32)compressedSize << std::endl; 303 | // std::cout << filename << ": recompression size " << recompressedData.size() << std::endl; 304 | // for (u32 j = 0; j < uncompressedSize; ++j) { 305 | // if (derecompressedData[j] != uncompressedData[j]) { 306 | // std::cout << filename << ": >>>>>>>>>>>>>>>>>>>>>>>>>> Recompression did not work" << std::endl; 307 | // break; 308 | // } 309 | // } 310 | // } 311 | break; 312 | case 0x02: 313 | uncompressedData = LZCDecompress(data, i, uncompressedSize); 314 | // { 315 | // std::cout << filename << ": Compression flag: " << (i16)compressionFlag << std::endl; 316 | // v8 recompressedData = LZCCompress(uncompressedData); 317 | // v8 derecompressedData = LZCDecompress(recompressedData, 0, uncompressedSize); 318 | // //std::cout << filename << ": compression size " << (u32)compressedSize << std::endl; 319 | // //std::cout << filename << ": recompression size " << recompressedData.size() << std::endl; 320 | // bool mismatch = false; 321 | // for (u32 j = 0; j < uncompressedSize; ++j) { 322 | // if (derecompressedData[j] != uncompressedData[j]) { 323 | // std::cout << filename << ": >>>>>>>>>>>>>>>>>>>>>>>>>> Recompression did not work" << std::endl; 324 | // mismatch = true; 325 | // break; 326 | // } 327 | // } 328 | // if (!mismatch) { 329 | // std::cout << filename << ": compression size difference " << (i32)(recompressedData.size() - compressedSize) << std::endl; 330 | // } 331 | // } 332 | 333 | break; 334 | case 0x03: 335 | uncompressedData = RLE2Decompress(data, i, uncompressedSize); 336 | break; 337 | default: 338 | std::cerr << filename << ": unhandled compression type: " 339 | << (i16)compressionFlag << std::endl; 340 | } 341 | 342 | if (uncompressedSize != (u32)uncompressedData.size()) { 343 | std::cerr << filename << ": decompression error: expected size: " 344 | << (size_t)uncompressedSize << " - got " << uncompressedData.size() 345 | << " type " << (i16)compressionFlag << std::endl; 346 | return false; 347 | } 348 | 349 | if (!uncompressedData.size()) { 350 | return false; 351 | } 352 | return true; 353 | } 354 | -------------------------------------------------------------------------------- /CompressedBaseFile.h: -------------------------------------------------------------------------------- 1 | #ifndef COMPRESSEDBASEFILE_H 2 | #define COMPRESSEDBASEFILE_H 3 | 4 | #include "BaseFile.h" 5 | 6 | namespace SCRANTIC { 7 | 8 | class CompressedBaseFile : public BaseFile { 9 | protected: 10 | u32 compressedSize; 11 | u8 compressionFlag; 12 | u32 uncompressedSize; 13 | 14 | bool handleDecompression(v8 &data, v8::iterator &it, v8 &uncompressedData); 15 | 16 | static v8 RLECompress(v8 const &compressedData); 17 | static v8 RLEDecompress(v8 const &compressedData, size_t offset = 0, u32 size = 0); 18 | static v8 RLE2Decompress(v8 const &compressedData, size_t offset = 0, u32 size = 0); 19 | static bool findInDictionary(std::vector &dictionary, v8 currentBlock, u16 &dictPos); 20 | static void writeBits(v8 &data, u8 &bitPos, u16 bits, u8 bitLength); 21 | static u16 readBits(v8 const &data, size_t &bytePos, u8 &bitPos, u16 bits); 22 | static v8 LZCCompress(v8 const &uncompressedData); 23 | static v8 LZCDecompress(v8 const &compressedData, size_t offset = 0, u32 size = 0); 24 | 25 | public: 26 | explicit CompressedBaseFile(const std::string &name) : BaseFile(name) {}; 27 | ~CompressedBaseFile() {}; 28 | }; 29 | 30 | } 31 | 32 | #endif // COMPRESSEDBASEFILE_H 33 | -------------------------------------------------------------------------------- /GraphicBaseFile.cpp: -------------------------------------------------------------------------------- 1 | #include "GraphicBaseFile.h" 2 | 3 | #include "BaseFile.h" 4 | 5 | SCRANTIC::GraphicBaseFile::GraphicBaseFile() { 6 | defaultPalette[0] = { 168, 0, 168, 0}; 7 | defaultPalette[1] = { 0, 0, 168, 255}; 8 | defaultPalette[2] = { 0, 168, 0, 255}; 9 | defaultPalette[3] = { 0, 168, 168, 255}; 10 | defaultPalette[4] = { 168, 0, 0, 255}; 11 | defaultPalette[5] = { 0, 0, 0, 255}; 12 | defaultPalette[6] = { 168, 168, 0, 255}; 13 | defaultPalette[7] = { 212, 212, 212, 255}; 14 | defaultPalette[8] = { 128, 128, 128, 255}; 15 | defaultPalette[9] = { 0, 0, 255, 255}; 16 | defaultPalette[10] = { 0, 255, 0, 255}; 17 | defaultPalette[11] = { 0, 255, 255, 255}; 18 | defaultPalette[12] = { 255, 0, 0, 255}; 19 | defaultPalette[13] = { 255, 0, 255, 255}; 20 | defaultPalette[14] = { 255, 255, 0, 255}; 21 | defaultPalette[15] = { 255, 255, 255, 255}; 22 | } 23 | 24 | SDL_Surface* SCRANTIC::GraphicBaseFile::createSdlSurface(v8 &data, u16 width, u16 height, size_t offset) { 25 | SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 8, 0, 0, 0, 0); 26 | SDL_SetPaletteColors(surface->format->palette, defaultPalette, 0, 256); 27 | 28 | size_t z = offset; 29 | bool high = false; 30 | u8 idx; 31 | 32 | unsigned char *p = (unsigned char*)surface->pixels; 33 | 34 | for (int y = 0; y < surface->h; ++y) { 35 | for (int x = 0; x < surface->w; ++x) { 36 | if (high) { 37 | high = false; 38 | idx = data[z] & 0xF; 39 | z++; 40 | } else { 41 | high = true; 42 | idx = data[z] >> 4; 43 | } 44 | p[y * surface->w + x] = idx; 45 | } 46 | } 47 | 48 | return surface; 49 | } 50 | 51 | i8 SCRANTIC::GraphicBaseFile::matchSdlColorToPaletteNumber(SDL_Color& color) { 52 | for (u8 i = 0; i < 16; ++i) { 53 | if ((defaultPalette[i].r == color.r) 54 | && (defaultPalette[i].g == color.g) 55 | && (defaultPalette[i].b == color.b) 56 | && (defaultPalette[i].a == color.a)) { 57 | return i; 58 | } 59 | } 60 | return -1; 61 | } 62 | 63 | v8 SCRANTIC::GraphicBaseFile::createRGBABitmapData(v8 &data, u16 width, u16 height, size_t offset) { 64 | BitmapFileHeader header; 65 | BitmapInfoHeader info; 66 | 67 | header.bfType = 0x4D42; 68 | header.bfSize = width * height * 4 + 124; 69 | header.bfReserved = 0; 70 | header.bfOffsetBits = 124; 71 | 72 | info.biSize = sizeof(BitmapInfoHeader); 73 | info.biWidth = width; 74 | info.biHeight = height; 75 | info.biPlanes = 1; 76 | info.biBitCount = 32; 77 | info.biCompression = 3;//0; 78 | info.biSizeImage = width * height; 79 | info.biXPelsPerMeter = 0xB13; 80 | info.biYPelsPerMeter = 0xB13; 81 | info.biClrUsed = 0x0; //16; 82 | info.biClrImportant = 0; 83 | info.biRedMask = 0xFF;//0; 84 | info.biGreenMask = 0xFF00;//0; 85 | info.biBlueMask = 0xFF0000;//0; 86 | info.biAlphaMask = 0xFF000000;//0; 87 | info.biColourSpace = 0; 88 | info.biCsEndpoints[0] = 0; 89 | info.biCsEndpoints[1] = 0; 90 | info.biCsEndpoints[2] = 0; 91 | info.biCsEndpoints[3] = 0; 92 | info.biCsEndpoints[4] = 0; 93 | info.biCsEndpoints[5] = 0; 94 | info.biCsEndpoints[6] = 0; 95 | info.biCsEndpoints[7] = 0; 96 | info.biCsEndpoints[8] = 0; 97 | info.biRedGamma = 0; 98 | info.biGreenGamma = 0; 99 | info.biBlueGamma = 0; 100 | 101 | //16 * SDL_Color (4x u8 => 64 bytes) 102 | // u16 padding 103 | //image data: width*height 104 | 105 | v8 bitmapData; 106 | BaseFile::writeUintLE(bitmapData, header.bfType); 107 | BaseFile::writeUintLE(bitmapData, header.bfSize); 108 | BaseFile::writeUintLE(bitmapData, header.bfReserved); 109 | BaseFile::writeUintLE(bitmapData, header.bfOffsetBits); 110 | 111 | BaseFile::writeUintLE(bitmapData, info.biSize); 112 | BaseFile::writeUintLE(bitmapData, info.biWidth); 113 | BaseFile::writeUintLE(bitmapData, info.biHeight); 114 | BaseFile::writeUintLE(bitmapData, info.biPlanes); 115 | BaseFile::writeUintLE(bitmapData, info.biBitCount); 116 | BaseFile::writeUintLE(bitmapData, info.biCompression); 117 | BaseFile::writeUintLE(bitmapData, info.biSizeImage); 118 | BaseFile::writeUintLE(bitmapData, info.biXPelsPerMeter); 119 | BaseFile::writeUintLE(bitmapData, info.biYPelsPerMeter); 120 | BaseFile::writeUintLE(bitmapData, info.biClrUsed); 121 | BaseFile::writeUintLE(bitmapData, info.biClrImportant); 122 | BaseFile::writeUintLE(bitmapData, info.biRedMask); 123 | BaseFile::writeUintLE(bitmapData, info.biGreenMask); 124 | BaseFile::writeUintLE(bitmapData, info.biBlueMask); 125 | BaseFile::writeUintLE(bitmapData, info.biAlphaMask); 126 | 127 | u32 fillHeader = 0; 128 | for (int i = 0; i < 13; ++i) { 129 | BaseFile::writeUintLE(bitmapData, fillHeader); 130 | } 131 | 132 | u16 pad = 0; 133 | BaseFile::writeUintLE(bitmapData, pad); 134 | 135 | SDL_Color color; 136 | for (i32 i = height - 1; i >= 0; --i) { 137 | for (u16 j = 0; j < width/2; ++j) { 138 | size_t pos = offset + i*width/2 + j; 139 | color = defaultPalette[data[pos] >> 4]; 140 | bitmapData.push_back(color.r); 141 | bitmapData.push_back(color.g); 142 | bitmapData.push_back(color.b); 143 | bitmapData.push_back(color.a); 144 | 145 | color = defaultPalette[data[pos] & 0xF]; 146 | bitmapData.push_back(color.r); 147 | bitmapData.push_back(color.g); 148 | bitmapData.push_back(color.b); 149 | bitmapData.push_back(color.a); 150 | } 151 | } 152 | 153 | return bitmapData; 154 | } 155 | 156 | 157 | v8 SCRANTIC::GraphicBaseFile::readRGBABitmapData(const std::string &filename, u16 &width, u16 &height) { 158 | v8 data = BaseFile::readFile(filename); 159 | 160 | v8 scrData; 161 | 162 | if (data.size() < 122) { 163 | std::cout << "ERROR: only 32bit RGBA bitmaps are supported" << std::endl; 164 | width = 0; 165 | height = 0; 166 | return scrData; 167 | } 168 | 169 | v8::iterator it = data.begin(); 170 | 171 | BitmapFileHeader header; 172 | BitmapInfoHeader info; 173 | 174 | BaseFile::readUintLE(it, header.bfType); 175 | BaseFile::readUintLE(it, header.bfSize); 176 | BaseFile::readUintLE(it, header.bfReserved); 177 | BaseFile::readUintLE(it, header.bfOffsetBits); 178 | 179 | BaseFile::readUintLE(it, info.biSize); 180 | BaseFile::readUintLE(it, info.biWidth); 181 | BaseFile::readUintLE(it, info.biHeight); 182 | BaseFile::readUintLE(it, info.biPlanes); 183 | BaseFile::readUintLE(it, info.biBitCount); 184 | BaseFile::readUintLE(it, info.biCompression); 185 | BaseFile::readUintLE(it, info.biSizeImage); 186 | BaseFile::readUintLE(it, info.biXPelsPerMeter); 187 | BaseFile::readUintLE(it, info.biYPelsPerMeter); 188 | BaseFile::readUintLE(it, info.biClrUsed); 189 | BaseFile::readUintLE(it, info.biClrImportant); 190 | BaseFile::readUintLE(it, info.biRedMask); 191 | BaseFile::readUintLE(it, info.biGreenMask); 192 | BaseFile::readUintLE(it, info.biBlueMask); 193 | BaseFile::readUintLE(it, info.biAlphaMask); 194 | 195 | if ((header.bfType != 0x4D42) || (info.biBitCount != 32) || (info.biCompression != 3) || (info.biRedMask != 0xFF) 196 | || (info.biGreenMask != 0xFF00) || (info.biBlueMask != 0xFF0000) || (info.biAlphaMask != 0xFF000000)) { 197 | std::cout << "ERROR: only 32bit RGBA bitmaps are supported" << std::endl; 198 | width = 0; 199 | height = 0; 200 | return scrData; 201 | } 202 | 203 | width = info.biWidth; 204 | height = info.biHeight; 205 | 206 | SDL_Color color; 207 | i8 palIndex; 208 | bool high = false; 209 | u8 byte; 210 | 211 | for (i32 i = height - 1; i >= 0; --i) { 212 | for (u16 j = 0; j < width; ++j) { 213 | size_t pos = header.bfOffsetBits + i*width*4 + j*4; 214 | color = { data[pos], data[pos+1], data[pos+2], data[pos+3] }; 215 | 216 | palIndex = matchSdlColorToPaletteNumber(color); 217 | if (palIndex != -1) { 218 | if (!high) { 219 | byte = palIndex << 4; 220 | high = true; 221 | } else { 222 | byte |= palIndex; 223 | scrData.push_back(byte); 224 | high = false; 225 | } 226 | } 227 | } 228 | } 229 | 230 | return scrData; 231 | } 232 | -------------------------------------------------------------------------------- /GraphicBaseFile.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHICBASEFILE_H 2 | #define GRAPHICBASEFILE_H 3 | 4 | #include "types.h" 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #ifdef WIN32 11 | #include 12 | #else 13 | #include 14 | #endif 15 | 16 | namespace SCRANTIC { 17 | 18 | struct BitmapFileHeader { 19 | u16 bfType; 20 | u32 bfSize; 21 | u32 bfReserved; 22 | u32 bfOffsetBits; 23 | }; 24 | 25 | struct BitmapInfoHeader { 26 | u32 biSize; 27 | i32 biWidth; 28 | i32 biHeight; 29 | u16 biPlanes; 30 | u16 biBitCount; 31 | u32 biCompression; 32 | u32 biSizeImage; 33 | i32 biXPelsPerMeter; 34 | i32 biYPelsPerMeter; 35 | u32 biClrUsed; 36 | u32 biClrImportant; 37 | u32 biRedMask; 38 | u32 biGreenMask; 39 | u32 biBlueMask; 40 | u32 biAlphaMask; 41 | u32 biColourSpace; 42 | u32 biCsEndpoints[9]; 43 | u32 biRedGamma; 44 | u32 biGreenGamma; 45 | u32 biBlueGamma; 46 | }; 47 | 48 | class GraphicBaseFile { 49 | private: 50 | i8 matchSdlColorToPaletteNumber(SDL_Color &color); 51 | 52 | protected: 53 | SDL_Surface* createSdlSurface(v8 &data, u16 width, u16 height, size_t offset = 0); 54 | v8 createRGBABitmapData(v8 &data, u16 width, u16 height, size_t offset = 0); 55 | v8 readRGBABitmapData(const std::string &filename, u16 &width, u16 &height); 56 | 57 | SDL_Color defaultPalette[256]; 58 | 59 | public: 60 | GraphicBaseFile(); 61 | ~GraphicBaseFile() {}; 62 | }; 63 | 64 | } 65 | 66 | #endif // GRAPHICBASEFILE_H 67 | -------------------------------------------------------------------------------- /LICENCE_LinBiolinum.txt: -------------------------------------------------------------------------------- 1 | - Lizenz / Licence - 2 | 3 | Unsere Schriften sind frei im Sinne der GPL, d.h. (stark vereinfacht) dass Veränderungen an der Schriftart erlaubt sind unter der Bedingung, dass diese wieder der Öffentlichkeit unter gleicher Lizenz freigegeben werden. Querdenker behaupten oft, dass bei der Verwendung einer GPL-Schrift eingebettet in beispielsweise eine PDF auch diese freigestellt werden müsse. Deshalb gibt es die sogenannte "Font-exception" der GPL (welche diesem Lizenztext hinzugefügt wurde). Weitere Informationen zur GPL (Lizenztext mit Font-Exzeption als GPL.txt in diesem Paket). 4 | Zusätzlich stehen die Schriften unter der Open Font License (siehe OFL.txt). 5 | 6 | Our fonts are free in the sense of the GPL. In short: Changing the font is allowed as long as the derivative work is published under the same licence again. Pedantics keep claiming that the embedded use of GPL-fonts in i.e. PDFs requires the free publication of the PDF as well. This is why our GPL contains the so called "font exception". Further information about the GPL (licence text with font exception see GPL.txt in this package). 7 | Additionally our fonts are licensed under the Open Fonts License (see OFL.txt). -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /PALFile.cpp: -------------------------------------------------------------------------------- 1 | #include "PALFile.h" 2 | 3 | SCRANTIC::PALFile::PALFile(const std::string &name, v8 &data) 4 | : BaseFile(name) { 5 | 6 | v8::iterator it = data.begin(); 7 | 8 | assertString(it, "PAL:"); 9 | 10 | readUintLE(it, vgaSize); 11 | readUintLE(it, magic); 12 | 13 | assertString(it, "VGA:"); 14 | 15 | readUintLE(it, palCount); 16 | if (palCount > 256*3) { 17 | std::cerr << filename << ": Palette count too large! " << palCount << std::endl; 18 | palCount = 256*3; 19 | } 20 | 21 | u8 r,g,b; 22 | SDL_Color color; 23 | color.a = 0; 24 | 25 | for (u32 i = 0; i < palCount/3; i++) { 26 | readUintLE(it, r); 27 | readUintLE(it, g); 28 | readUintLE(it, b); 29 | color.r = r*4; 30 | color.g = g*4; 31 | color.b = b*4; 32 | palette.push_back(color); 33 | color.a = 255; 34 | } 35 | } 36 | 37 | SCRANTIC::PALFile::PALFile(const std::string &filename) 38 | : BaseFile(filename) { 39 | 40 | std::ifstream in; 41 | 42 | in.open(filename, std::ios::in); 43 | if (!in.is_open()) { 44 | std::cerr << "PALFile: Could not open " << filename << std::endl; 45 | return; 46 | } 47 | 48 | int r,g,b; 49 | char c1, c2; 50 | std::string line; 51 | palCount = 0; 52 | u8 a = 0; 53 | 54 | while (getline(in, line)) { 55 | if (line.substr(0, 1) == "#") { 56 | continue; 57 | } 58 | 59 | std::istringstream iss(line); 60 | if (!(iss >> r >> c1 >> g >> c2 >> b)) { 61 | break; 62 | } 63 | palette.push_back({ (u8)r, (u8)g, (u8)b, (u8)a}); 64 | a = 255; 65 | palCount += 3; 66 | } 67 | 68 | in.close(); 69 | } 70 | 71 | v8 SCRANTIC::PALFile::repackIntoResource() { 72 | std::string strings[2] = { "PAL:", "VGA:" }; 73 | 74 | magic = 0x8000; 75 | vgaSize = palCount + 8; 76 | 77 | v8 rawData(strings[0].begin(), strings[0].end()); 78 | BaseFile::writeUintLE(rawData, vgaSize); 79 | BaseFile::writeUintLE(rawData, magic); 80 | std::copy(strings[1].begin(), strings[1].end(), std::back_inserter(rawData)); 81 | BaseFile::writeUintLE(rawData, palCount); 82 | 83 | u8 r,g,b; 84 | 85 | for (size_t i = 0; i < palette.size(); ++i) { 86 | r = palette[i].r/4; 87 | g = palette[i].g/4; 88 | b = palette[i].b/4; 89 | BaseFile::writeUintLE(rawData, r); 90 | BaseFile::writeUintLE(rawData, g); 91 | BaseFile::writeUintLE(rawData, b); 92 | } 93 | 94 | return rawData; 95 | } 96 | 97 | void SCRANTIC::PALFile::saveFile(const std::string &path) { 98 | std::stringstream output; 99 | 100 | output << "# SCRANTIC palette file" << std::endl 101 | << "# format: r,g,b" << std::endl 102 | << "# first entry determines transparent colour" << std::endl; 103 | 104 | for (size_t i = 0; i < palette.size(); ++i) { 105 | output << (u16)palette[i].r << "," 106 | << (u16)palette[i].g << "," 107 | << (u16)palette[i].b << std::endl; 108 | } 109 | 110 | writeFile(output.str(), filename, path); 111 | } 112 | -------------------------------------------------------------------------------- /PALFile.h: -------------------------------------------------------------------------------- 1 | #ifndef PALFILE_H 2 | #define PALFILE_H 3 | 4 | #include "BaseFile.h" 5 | #include 6 | 7 | #ifdef WIN32 8 | #include 9 | #else 10 | #include 11 | #endif 12 | 13 | namespace SCRANTIC { 14 | 15 | class PALFile : public BaseFile 16 | { 17 | protected: 18 | u16 vgaSize; 19 | u16 magic; // 0x8000 20 | u32 palCount; 21 | std::vector palette; 22 | 23 | public: 24 | v8 repackIntoResource() override; 25 | void saveFile(const std::string &path) override; 26 | 27 | SDL_Color *getPalette() { return &palette[0]; } 28 | 29 | PALFile(const std::string &name, v8 &data); 30 | explicit PALFile(const std::string &filename); 31 | }; 32 | 33 | } 34 | 35 | #endif // PALFILE_H 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Johnny's World 2 | 3 | Open Source C++/SDL implementation of the Screen Antics' Johnny Castaway screensaver 4 | 5 | This is an attempt to recreate the old 16bit screensaver by Sierra/Screen Antics "Johnny Castaway" using the original resource files. You have to provide `RESOURCE.MAP` `RESOUCRE.001` and `SCRANTICS.SCR` in the same directory as the executable or set commandline options accordingly (see below). 6 | 7 | Most of the file parsing code is ported from Hans Millings C# project: https://github.com/nivs1978/Johnny-Castaway-Open-Source 8 | 9 | ## Status 10 | 11 | The program plays a few scenes almost perfect and many scenes okay. Some scenes break, because the timing between individual clips is off and of course because of unimplemented commands. Combining the individual movies into a smooth story has not been started at all, yet. 12 | 13 | ## Controls 14 | 15 | * Space pauses/unpauses 16 | * Return opens a menu to select a movie using left/right/up/down and return to select 17 | * Escape closes the menu or exists the program 18 | 19 | ## Resources (un)packing 20 | 21 | The application is able to unpack all resources to "normal" file types and repack them into a `RESOUCRE.MAP/.001` archive. There are some blobs in `RESOURCE.MAP` which are not understood and just copied over. However, repacked resources seem to work fine with the original executable. 22 | 23 | Scrantic BMP and SCR files are unpacked to 32bit RGBA bitmaps, other resources result in plain text files. The text parser for repacking the resources is not very robust, so make sure to only feed it correctly formatted files. Sound samples will be extract to the subdirectory `RIFF`, but they will not be repacked! 24 | 25 | ## Commandline options 26 | 27 | There are a few commandline options: 28 | 29 | argument | help 30 | ------------ | ------------- 31 | `--resourcePath folder/` | set location of resource files 32 | `--unpackedResources` | use unpacked resources 33 | `--unpack` | only unpack resource archive and exit 34 | `--onlyFiles` | unpack only "raw" files from resource archive 35 | `--repack` | only repack resource into resource archive and exit 36 | `--prepackedPath folder/` | point to a folder containing "raw" resources files
If a file is found in this directory it will be copied
into the resource archive, otherwise the unpacked file
from the resourcePath is repacked.
This is useful as BMP files take a few seconds each
to repack (because of my slow compression implementation) 37 | `--outputPath folder/` | set output directory for --repack and --unpack 38 | 39 | ## Compilation 40 | 41 | This has only been tested on Linux so far, but should work on every system with a C++11 compiler and SDL2. The includes might be incorrect for other environments. The C++11 experimental filesystem library is also used and its library is added manually to `CMakeLists.txt`. 42 | The project depends on SDL2/Mixer/TTF/GFX. The library for the SDL2_gfx is added manually, too. 43 | 44 | Compile using: 45 | 46 | cmake . && make 47 | -------------------------------------------------------------------------------- /RESFile.cpp: -------------------------------------------------------------------------------- 1 | #include "RESFile.h" 2 | #include "PALFile.h" 3 | #include "BMPFile.h" 4 | #include "SCRFile.h" 5 | #include "TTMFile.h" 6 | #include "ADSFile.h" 7 | #include "VINFile.h" 8 | 9 | #include 10 | #include 11 | namespace fs = std::experimental::filesystem; 12 | 13 | SCRANTIC::RESFile::RESFile(const std::string &name, bool readFromFile) 14 | : BaseFile(name) { 15 | 16 | std::string path = ""; 17 | size_t pathPos = name.rfind('/'); 18 | if (pathPos != std::string::npos) { 19 | path = name.substr(0, pathPos+1); 20 | } 21 | 22 | if (readFromFile) { 23 | readFromFiles(path); 24 | } else { 25 | readFromRes(path); 26 | } 27 | } 28 | 29 | void SCRANTIC::RESFile::readFromRes(const std::string &path) { 30 | std::ifstream in; 31 | in.open(path + filename, std::ios::binary | std::ios::in); 32 | in.unsetf(std::ios::skipws); 33 | 34 | header.reserve(6); 35 | 36 | u8 byte; 37 | for (int i = 0; i < 6; ++i) { 38 | readUintLE(&in, byte); 39 | header.push_back(byte); 40 | } 41 | 42 | resFilename = readString(&in, 12); 43 | 44 | if (resFilename.length() != 12) { 45 | std::cerr << "RESFile: Resource filename corrupt? " << resFilename << std::endl; 46 | } 47 | 48 | std::ifstream res; 49 | res.open(path + resFilename, std::ios::binary | std::ios::in); 50 | res.unsetf(std::ios::skipws); 51 | 52 | if (!res.is_open()) { 53 | std::cerr << "RESFile: Could not open resource file: " << path + resFilename << std::endl; 54 | } 55 | 56 | readUintLE(&in, resCount); 57 | 58 | u32 offset; 59 | u16 blob1, blob2; 60 | resource newRes; 61 | 62 | for (u16 i = 0; i < resCount; ++i) { 63 | newRes.data.clear(); 64 | newRes.handle = NULL; 65 | readUintLE(&in, blob1); 66 | readUintLE(&in, blob2); 67 | readUintLE(&in, offset); 68 | 69 | res.seekg(offset, std::ios::beg); 70 | 71 | newRes.num = i; 72 | newRes.filename = readString(&res, 12); 73 | newRes.filetype = newRes.filename.substr(newRes.filename.rfind('.')+1); 74 | newRes.blob1 = blob1; 75 | newRes.blob2 = blob2; 76 | newRes.offset = offset; 77 | readUintLE(&res, newRes.size); 78 | 79 | for (u32 j = 0; j < newRes.size; ++j) { 80 | readUintLE(&res, byte); 81 | newRes.data.push_back(byte); 82 | } 83 | 84 | if (newRes.filetype == "PAL") { 85 | PALFile *resfile = new PALFile(newRes.filename, newRes.data); 86 | newRes.handle = static_cast(resfile); 87 | } else if (newRes.filetype == "SCR") { 88 | SCRFile *resfile = new SCRFile(newRes.filename, newRes.data); 89 | newRes.handle = static_cast(resfile); 90 | } else if (newRes.filetype == "BMP") { 91 | BMPFile *resfile = new BMPFile(newRes.filename, newRes.data); 92 | newRes.handle = static_cast(resfile); 93 | } else if (newRes.filetype == "TTM") { 94 | TTMFile *resfile = new TTMFile(newRes.filename, newRes.data); 95 | newRes.handle = static_cast(resfile); 96 | } else if (newRes.filetype == "VIN") { 97 | VINFile *resfile = new VINFile(newRes.filename, newRes.data); 98 | newRes.handle = static_cast(resfile); 99 | } else if (newRes.filetype == "ADS") { 100 | ADSFile *resfile = new ADSFile(newRes.filename, newRes.data); 101 | newRes.handle = static_cast(resfile); 102 | } 103 | 104 | resourceMap.insert(std::pair(i, newRes)); 105 | } 106 | 107 | res.close(); 108 | in.close(); 109 | } 110 | 111 | void SCRANTIC::RESFile::readFromFiles(const std::string &path) { 112 | std::ifstream in; 113 | 114 | in.open(path + filename, std::ios::in); 115 | if (!in.is_open()) { 116 | std::cerr << "RESFile: Could not open " << path << filename << std::endl; 117 | return; 118 | } 119 | 120 | std::string line; 121 | std::string resname; 122 | u16 blob1, blob2; 123 | int count = 0; 124 | 125 | std::string mode = "fname"; 126 | 127 | while (getline(in, line)) { 128 | if (line.substr(0, 1) == "#" || line == "") { 129 | continue; 130 | } 131 | 132 | if (mode == "fname") { 133 | resFilename = line; 134 | mode = "header"; 135 | continue; 136 | } 137 | 138 | if (mode == "header") { 139 | for (int i = 0; i < 6; ++i) { 140 | std::string tmp = line.substr(3*i, 2); 141 | header.push_back(std::stoi(tmp, 0, 16)); 142 | } 143 | mode = ""; 144 | continue; 145 | } 146 | 147 | std::istringstream iss(line); 148 | if (!(iss >> std::hex >> blob1 >> blob2 >> resname)) { 149 | break; 150 | } 151 | 152 | resource newRes; 153 | newRes.num = count; 154 | newRes.filename = resname; 155 | newRes.filetype = newRes.filename.substr(newRes.filename.rfind('.')+1); 156 | newRes.blob1 = blob1; 157 | newRes.blob2 = blob2; 158 | newRes.offset = 0; 159 | newRes.size = 0; 160 | 161 | if (newRes.filetype == "PAL") { 162 | PALFile *resfile = new PALFile(path + newRes.filename); 163 | newRes.handle = static_cast(resfile); 164 | } else if (newRes.filetype == "SCR") { 165 | SCRFile *resfile = new SCRFile(path + newRes.filename); 166 | newRes.handle = static_cast(resfile); 167 | } else if (newRes.filetype == "BMP") { 168 | BMPFile *resfile = new BMPFile(path + newRes.filename); 169 | newRes.handle = static_cast(resfile); 170 | } else if (newRes.filetype == "TTM") { 171 | TTMFile *resfile = new TTMFile(path + newRes.filename); 172 | newRes.handle = static_cast(resfile); 173 | } else if (newRes.filetype == "VIN") { 174 | VINFile *resfile = new VINFile(path + newRes.filename); 175 | newRes.handle = static_cast(resfile); 176 | } else if (newRes.filetype == "ADS") { 177 | ADSFile *resfile = new ADSFile(path + newRes.filename); 178 | newRes.handle = static_cast(resfile); 179 | } 180 | 181 | resourceMap.insert(std::pair(count++, newRes)); 182 | } 183 | 184 | resCount = count; 185 | 186 | in.close(); 187 | } 188 | 189 | void SCRANTIC::RESFile::repackResources(const std::string &path, const std::string &prepackedPath) { 190 | fs::create_directories(path); 191 | 192 | std::cout << "Johnny will repack his resources :)" << std::endl; 193 | std::cout << "===================================" << std::endl << std::endl; 194 | 195 | std::string maxFiles = hexToString(resourceMap.size(), std::dec); 196 | int pad = maxFiles.size(); 197 | int i = 1; 198 | 199 | v8 data; 200 | v8 resFile; 201 | 202 | for (int i = 0; i < 6; ++i) { 203 | data.push_back(header[i]); 204 | } 205 | 206 | std::copy(resFilename.begin(), resFilename.end(), std::back_inserter(data)); 207 | for (size_t i = resFilename.size(); i < 13; ++i) { 208 | data.push_back(0); 209 | } 210 | 211 | writeUintLE(data, resCount); 212 | 213 | u32 offset = 0; 214 | u32 newSize; 215 | 216 | for (auto it = resourceMap.begin(); it != resourceMap.end(); ++it) { 217 | resource currentResource = it->second; 218 | 219 | std::cout << "[" << hexToString(i, std::dec, pad) << "/" << maxFiles << "]: " << currentResource.filename << " "; 220 | writeUintLE(data, currentResource.blob1); 221 | writeUintLE(data, currentResource.blob2); 222 | writeUintLE(data, offset); 223 | 224 | v8 newData; 225 | if ((prepackedPath != "") && fs::exists(prepackedPath + currentResource.filename)) { 226 | newData = BaseFile::readFile(prepackedPath + currentResource.filename); 227 | std::cout << "prepacked file used" << std::endl; 228 | } else { 229 | newData = (currentResource.handle)->repackIntoResource(); 230 | std::cout << "repacked" << std::endl; 231 | } 232 | 233 | std::copy(currentResource.filename.begin(), currentResource.filename.end(), std::back_inserter(resFile)); 234 | for (size_t i = currentResource.filename.size(); i < 13; ++i) { 235 | resFile.push_back(0); 236 | } 237 | newSize = newData.size(); 238 | writeUintLE(resFile, newSize); 239 | std::copy(newData.begin(), newData.end(), std::back_inserter(resFile)); 240 | offset += newSize + 17; 241 | ++i; 242 | } 243 | 244 | std::cout << "Writing " << filename << std::endl; 245 | SCRANTIC::BaseFile::writeFile(data, filename, path); 246 | 247 | std::cout << "Writing " << resFilename << std::endl; 248 | SCRANTIC::BaseFile::writeFile(resFile, resFilename, path); 249 | 250 | std::cout << "Done." << std::endl; 251 | } 252 | 253 | void SCRANTIC::RESFile::unpackResources(const std::string& path, bool onlyFiles) { 254 | fs::create_directories(path); 255 | fs::create_directories(path + "RIFF/"); 256 | 257 | std::cout << "Johnny will unpack his resources :)" << std::endl; 258 | std::cout << "===================================" << std::endl << std::endl; 259 | 260 | std::string maxFiles = hexToString(resourceMap.size(), std::dec); 261 | int pad = maxFiles.size(); 262 | int i = 1; 263 | 264 | std::stringstream output; 265 | 266 | output << "# RESOURCES.MAP" << std::endl 267 | << "# resource filename and header followed by file list" << std::endl; 268 | 269 | output << resFilename << std::endl; 270 | for (size_t z = 0; z < header.size(); ++z) { 271 | output << hexToString((u16)header[z], std::hex, 2) << " "; 272 | } 273 | output << std::endl << std::endl; 274 | 275 | for (auto it = resourceMap.begin(); it != resourceMap.end(); ++it) { 276 | std::string fname = (it->second).filename; 277 | std::cout << "[" << hexToString(i, std::dec, pad) << "/" << maxFiles << "]: " << fname << std::endl; 278 | 279 | if (onlyFiles) { 280 | BaseFile::writeFile(it->second.data, fname, path); 281 | } else { 282 | if (it->second.filetype == "BMP") { 283 | fs::create_directory(path + fname); 284 | (*it->second.handle).saveFile(path +fname); 285 | } else { 286 | (*it->second.handle).saveFile(path); 287 | } 288 | } 289 | 290 | output << hexToString(it->second.blob1, std::hex, 4) << " " 291 | << hexToString(it->second.blob2, std::hex, 4) << " " 292 | << fname << std::endl; 293 | 294 | ++i; 295 | } 296 | 297 | if (!onlyFiles) { 298 | writeFile(output.str(), filename, path); 299 | } 300 | 301 | std::cout << "Done." << std::endl; 302 | } 303 | 304 | SCRANTIC::RESFile::~RESFile() { 305 | for (auto i = std::begin(resourceMap); i != std::end(resourceMap); ++i) { 306 | if (i->second.handle != NULL) { 307 | delete i->second.handle; 308 | } 309 | } 310 | } 311 | 312 | 313 | SDL_Color* SCRANTIC::RESFile::setPaletteForAllGraphicResources(const std::string &palFile) { 314 | PALFile *pal = static_cast(getResource(palFile)); 315 | 316 | for (auto it = resourceMap.begin(); it != resourceMap.end(); ++it) { 317 | if (it->second.filetype == "BMP") { 318 | static_cast(it->second.handle)->setPalette(pal->getPalette(), 256); 319 | } else if (it->second.filetype == "SCR") { 320 | static_cast(it->second.handle)->setPalette(pal->getPalette(), 256); 321 | } 322 | } 323 | 324 | return pal->getPalette(); 325 | } 326 | 327 | 328 | SCRANTIC::BaseFile *SCRANTIC::RESFile::getResource(const std::string &name) { 329 | for (auto i = std::begin(resourceMap); i != std::end(resourceMap); ++i) { 330 | if (i->second.filename == name) { 331 | return i->second.handle; 332 | } 333 | } 334 | 335 | return NULL; 336 | } 337 | 338 | std::map > SCRANTIC::RESFile::getMovieList() { 339 | std::map> items; 340 | 341 | for (auto it = resourceMap.begin(); it != resourceMap.end(); ++it) { 342 | if (it->second.filetype == "ADS") { 343 | ADSFile * ads = static_cast(it->second.handle); 344 | std::string page = ads->filename; 345 | for (auto item : ads->tagList) { 346 | items[page].push_back(item.second); 347 | } 348 | } 349 | } 350 | 351 | return items; 352 | } 353 | 354 | 355 | -------------------------------------------------------------------------------- /RESFile.h: -------------------------------------------------------------------------------- 1 | #ifndef RESFILE_H 2 | #define RESFILE_H 3 | 4 | #include "BaseFile.h" 5 | #include 6 | #include "GraphicBaseFile.h" 7 | 8 | namespace SCRANTIC { 9 | 10 | struct resource { 11 | u16 num; 12 | std::string filename; 13 | u16 blob1; 14 | u16 blob2; 15 | u32 offset; 16 | u32 size; 17 | v8 data; 18 | std::string filetype; 19 | BaseFile *handle; 20 | }; 21 | 22 | class RESFile : public BaseFile { 23 | private: 24 | void readFromRes(const std::string &path); 25 | void readFromFiles(const std::string &path); 26 | 27 | protected: 28 | v8 header; 29 | u16 resCount; 30 | std::string resFilename; 31 | std::map resourceMap; 32 | 33 | public: 34 | RESFile(const std::string &name, bool readFromFile = false); 35 | ~RESFile(); 36 | 37 | SDL_Color *setPaletteForAllGraphicResources(const std::string &palFile); 38 | BaseFile *getResource(const std::string &name); 39 | std::map> getMovieList(); 40 | 41 | void repackResources(const std::string &path, const std::string &prepackedPath); 42 | void unpackResources(const std::string &path, bool onlyFiles = false); 43 | }; 44 | 45 | } 46 | 47 | #endif // RESFILE_H 48 | -------------------------------------------------------------------------------- /RIFFPlayer.cpp: -------------------------------------------------------------------------------- 1 | #include "RIFFPlayer.h" 2 | 3 | #include 4 | #include 5 | 6 | #include "BaseFile.h" 7 | 8 | SCRANTIC::RIFFPlayer::RIFFPlayer(const std::string &path, bool readFromFiles) { 9 | 10 | std::vector rawData; 11 | 12 | if (readFromFiles) { 13 | rawData = readRIFFFiles(path); 14 | } else { 15 | rawData = extractRIFFFiles(path + "SCRANTIC.SCR", "", false); 16 | if (rawData.empty()) { 17 | rawData = readRIFFFiles(path); 18 | } 19 | } 20 | 21 | for (size_t i = 0; i < rawData.size(); ++i) { 22 | SDL_RWops* rwops = SDL_RWFromMem((unsigned char*)&rawData[i].front(), rawData[i].size()); 23 | audioSamples[i] = Mix_LoadWAV_RW(rwops, 1); 24 | } 25 | } 26 | 27 | 28 | SCRANTIC::RIFFPlayer::~RIFFPlayer() { 29 | for (u8 i = 0; i < MAX_AUDIO; ++i) { 30 | Mix_FreeChunk(audioSamples[i]); 31 | } 32 | } 33 | 34 | 35 | void SCRANTIC::RIFFPlayer::play(u8 num, bool stopAllOther) { 36 | if (stopAllOther) { 37 | Mix_HaltChannel(-1); 38 | } 39 | 40 | Mix_PlayChannel(-1, audioSamples[num], 0); 41 | } 42 | 43 | std::vector SCRANTIC::RIFFPlayer::readRIFFFiles(const std::string& path) { 44 | std::vector riffs; 45 | 46 | for (size_t i = 0; i < MAX_AUDIO; ++i) { 47 | std::string fname = path + "RIFF/RIFF" + SCRANTIC::BaseFile::hexToString(i, std::dec, 2) + ".WAV"; 48 | riffs.push_back(SCRANTIC::BaseFile::readFile(fname)); 49 | } 50 | 51 | return riffs; 52 | } 53 | 54 | 55 | std::vector SCRANTIC::RIFFPlayer::extractRIFFFiles(const std::string &filename, const std::string &path, bool writeFiles) { 56 | 57 | size_t offsets[] = { 58 | 0x1DC00, 0x20800, 0x20E00, 0x22C00, 0x24000, 0x24C00, 59 | 0x28A00, 0x2C600, 0x2D000, 0x2DE00, 0x34400, 0x32E00, 60 | 0x39C00, 0x43400, 0x37200, 0x37E00, 0x45A00, 0x3AE00, 61 | 0x3E600, 0x3F400, 0x41200, 0x42600, 0x42C00, 0x43400 62 | }; 63 | 64 | u32 size; 65 | u8 byte; 66 | 67 | std::vector rawRIFFs; 68 | 69 | std::ifstream in; 70 | in.open(filename, std::ios::binary | std::ios::in); 71 | in.unsetf(std::ios::skipws); 72 | 73 | if (!in.is_open()) { 74 | std::cerr << "RIFFPlayer: Error opening " << filename << std::endl; 75 | return rawRIFFs; 76 | } 77 | 78 | for (u8 i = 0; i < MAX_AUDIO; ++i) { 79 | v8 rawAudio; 80 | in.seekg(offsets[i]+4, std::ios_base::beg); 81 | SCRANTIC::BaseFile::readUintLE(&in, size); 82 | size += 8; 83 | 84 | rawAudio.clear(); 85 | rawAudio.reserve(size); 86 | 87 | in.seekg(offsets[i], std::ios_base::beg); 88 | 89 | for (u32 j = 0; j < size; ++j) { 90 | in.read((char*)&byte, 1); 91 | rawAudio.push_back(byte); 92 | } 93 | 94 | if (writeFiles) { 95 | std::string fname ="RIFF" + SCRANTIC::BaseFile::hexToString((u16)i, std::dec, 2) + ".WAV"; 96 | SCRANTIC::BaseFile::writeFile(rawAudio, fname, path); 97 | } else { 98 | rawRIFFs.push_back(rawAudio); 99 | } 100 | } 101 | 102 | in.close(); 103 | 104 | return rawRIFFs; 105 | } 106 | -------------------------------------------------------------------------------- /RIFFPlayer.h: -------------------------------------------------------------------------------- 1 | #ifndef RIFFPLAYER_H 2 | #define RIFFPLAYER_H 3 | 4 | #ifdef WIN32 5 | #include 6 | #else 7 | #include 8 | #endif 9 | 10 | #include 11 | #include "types.h" 12 | 13 | #define MAX_AUDIO 24 14 | 15 | namespace SCRANTIC { 16 | 17 | class RIFFPlayer 18 | { 19 | private: 20 | std::vector readRIFFFiles(const std::string &path); 21 | 22 | protected: 23 | Mix_Chunk *audioSamples[MAX_AUDIO]; 24 | 25 | public: 26 | explicit RIFFPlayer(const std::string &path, bool readFromFiles); 27 | ~RIFFPlayer(); 28 | void play(u8 num, bool stopAllOther = true); 29 | 30 | static std::vector extractRIFFFiles(const std::string &filename, const std::string &path, bool writeFiles); 31 | }; 32 | 33 | } 34 | 35 | #endif // RIFFPLAYER_H 36 | -------------------------------------------------------------------------------- /Robinson.cpp: -------------------------------------------------------------------------------- 1 | #include "Robinson.h" 2 | 3 | #include 4 | #include 5 | 6 | #include "defines.h" 7 | 8 | #ifdef WIN32 9 | #include 10 | #else 11 | #include 12 | #endif 13 | 14 | SCRANTIC::Robinson::Robinson(const std::string &path, bool readUnpacked) 15 | : res(NULL), 16 | audioPlayer(NULL), 17 | menu(NULL), 18 | compositor(NULL), 19 | renderer(NULL), 20 | ads(NULL), 21 | currentMovie(0), 22 | movieRunning(false), 23 | movieFirstRun(true), 24 | movieLastRun(false), 25 | delay(0), 26 | delayTicks(0), 27 | renderMenu(false) { 28 | 29 | std::cout << "--------------- Hello from Robinson Crusoe!---------------" << std::endl; 30 | 31 | res = new RESFile(path + "RESOURCE.MAP", readUnpacked); 32 | audioPlayer = new RIFFPlayer(path, readUnpacked); 33 | 34 | std::srand(std::time(0)); 35 | 36 | palette = res->setPaletteForAllGraphicResources("JOHNCAST.PAL"); 37 | 38 | #ifdef DUMP_ADS 39 | std::string adsstring; 40 | std::string num; 41 | std::map> items = res->getMovieList(); 42 | ADSFile *dump; 43 | for (auto it = items.begin(); it != items.end(); ++it) { 44 | adsstring = it->first; 45 | dump = static_cast(res->getResource(adsstring)); 46 | 47 | std::cout << "Filename: " << dump->filename << std::endl; 48 | 49 | for (auto it = dump->tagList.begin(); it != dump->tagList.end(); ++it) { 50 | num = SCRANTIC::BaseFile::hexToString(it->first, std::dec); 51 | for (size_t j = num.size(); j < 3; ++j) { 52 | num = " " + num; 53 | } 54 | std::cout << "TAG ID " << num << ": " << it->second << std::endl; 55 | } 56 | std::cout << std::endl; 57 | 58 | std::string cmdString; 59 | std::string ttmName; 60 | Command *cmd; 61 | TTMFile *ttm; 62 | for (auto it = dump->script.begin(); it != dump->script.end(); ++it) { 63 | std::cout << "Movie number: " << it->first << " - 0x" << SCRANTIC::BaseFile::hexToString(it->first, std::hex) << std::endl; 64 | for (size_t pos = 0; pos < it->second.size(); ++pos) { 65 | num = SCRANTIC::BaseFile::hexToString(pos, std::dec); 66 | for (size_t j = num.size(); j < 3; ++j) { 67 | num = " " + num; 68 | } 69 | 70 | cmdString = SCRANTIC::BaseFile::commandToString(it->second[pos], true); 71 | cmd = &(it->second[pos]); 72 | switch (cmd->opcode) { 73 | case CMD_ADD_INIT_TTM: 74 | case CMD_ADD_TTM: 75 | case CMD_KILL_TTM: 76 | case CMD_UNK_1370: 77 | ttmName = dump->getResource(cmd->data.at(0)); 78 | ttm = static_cast(res->getResource(ttmName)); 79 | //cmdString += "| " + ttmName + " - " + ttm->getTag(cmd->data.at(1)); 80 | cmdString += "| " + ttm->getTag(cmd->data.at(1)); 81 | break; 82 | case CMD_TTM_LABEL: 83 | case CMD_SKIP_IF_LAST: 84 | for (size_t j = 0; j < cmd->data.size(); j+=2) { 85 | ttmName = dump->getResource(cmd->data.at(j)); 86 | ttm = static_cast(res->getResource(ttmName)); 87 | cmdString += "| " + ttm->getTag(cmd->data.at(j+1)) + " "; 88 | } 89 | break; 90 | 91 | } 92 | 93 | std::cout << num << ": " << cmdString << std::endl; 94 | 95 | } 96 | 97 | std::cout << std::endl; 98 | } 99 | 100 | std::cout << std::endl; 101 | std::cout << std::endl; 102 | 103 | } 104 | #endif 105 | 106 | } 107 | 108 | SCRANTIC::Robinson::~Robinson() 109 | { 110 | if (menu != NULL) { 111 | delete menu; 112 | } 113 | 114 | if (compositor != NULL) { 115 | delete compositor; 116 | } 117 | 118 | delete palette; 119 | 120 | if (audioPlayer != NULL) { 121 | delete audioPlayer; 122 | } 123 | 124 | if (res != NULL) { 125 | delete res; 126 | } 127 | 128 | std::cout << "-------------- Goodbye from Robinson Crusoe!--------------" << std::endl; 129 | } 130 | 131 | void SCRANTIC::Robinson::displaySplash() { 132 | compositor->displaySplash(); 133 | } 134 | 135 | void SCRANTIC::Robinson::initRenderer(SDL_Renderer *rendererSDL, TTF_Font *font) { 136 | renderer = rendererSDL; 137 | 138 | compositor = new RobinsonCompositor(renderer, SCREEN_WIDTH, SCREEN_HEIGHT, res); 139 | 140 | menu = new RobinsonMenu(renderer, SCREEN_WIDTH, SCREEN_HEIGHT); 141 | menu->initMenu(res->getMovieList(), font); 142 | } 143 | 144 | bool SCRANTIC::Robinson::navigateMenu(SDL_Keycode key) { 145 | std::string newPage; 146 | size_t newPos; 147 | 148 | if (menu->navigateMenu(key, newPage, newPos)) { 149 | ADSFile *ads = static_cast(res->getResource(newPage)); 150 | loadMovie(newPage, ads->getMovieNumberFromOrder(newPos)); 151 | renderMenu = false; 152 | movieRunning = true; 153 | return true; 154 | } 155 | 156 | return false; 157 | } 158 | 159 | 160 | void SCRANTIC::Robinson::render() { 161 | compositor->render(ttmScenes.begin(), ttmScenes.end()); 162 | 163 | for (auto it = ttmScenes.begin(); it != ttmScenes.end(); ++it) { 164 | i16 audio = (*it)->getSample(); 165 | if (audio != -1) { 166 | audioPlayer->play(audio); 167 | } 168 | } 169 | 170 | if (renderMenu) { 171 | menu->render(); 172 | } 173 | 174 | } 175 | 176 | void SCRANTIC::Robinson::resetPlayer() { 177 | for (auto scene : ttmScenes) { 178 | delete scene; 179 | } 180 | 181 | ttmScenes.clear(); 182 | 183 | compositor->reset(); 184 | 185 | delay = 0; 186 | delayTicks = 0; 187 | 188 | for (int i = 0; i < MAX_IMAGES; ++i) { 189 | images[i] = NULL; 190 | } 191 | } 192 | 193 | void SCRANTIC::Robinson::addTTM(Command cmd) { 194 | 195 | //check if a TTM matching this hash already exists - if it does do nothing 196 | u16 hash = SCRANTIC::ADSFile::makeHash(cmd.data.at(0), cmd.data.at(1)); 197 | for (auto scene : ttmScenes) { 198 | if (scene->getHash() == hash) { 199 | return; 200 | } 201 | } 202 | 203 | u16 sceneNum = 0; 204 | i16 repeat = 0; 205 | 206 | if (cmd.opcode != CMD_ADD_INIT_TTM) { 207 | sceneNum = cmd.data.at(1); 208 | repeat = cmd.data.at(2); 209 | 210 | if (repeat) { 211 | --repeat; 212 | } 213 | } else { 214 | if (ads->getResource(cmd.data.at(0)) == "WOULDBE.TTM") { 215 | sceneNum = 3; 216 | } 217 | } 218 | 219 | TTMPlayer *ttm = new TTMPlayer(ads->getResource(cmd.data.at(0)), cmd.data.at(0), sceneNum, repeat, res, images, palette, renderer); 220 | 221 | if (cmd.opcode != CMD_ADD_INIT_TTM) { 222 | ttmScenes.push_back(ttm); 223 | return; 224 | } 225 | 226 | // this assumes the only relavant action in the init scripts 227 | // is to load a SCR! all other actions are lost 228 | std::string scrName; 229 | 230 | do { 231 | ttm->advanceScript(); 232 | scrName = ttm->getSCRName(); 233 | if (scrName != "") { 234 | compositor->setScreen(scrName); 235 | } 236 | } while (!ttm->isFinished()); 237 | 238 | delete ttm; 239 | } 240 | 241 | void SCRANTIC::Robinson::runTTMs() { 242 | TTMPlayer *ttm; 243 | u16 newDelay = 100; 244 | u16 oldDelay = delay; 245 | u32 ticks; 246 | 247 | if (!oldDelay) { 248 | oldDelay = 100; 249 | } 250 | 251 | auto it = ttmScenes.begin(); 252 | while (it != ttmScenes.end()) { 253 | ttm = (*it); 254 | ticks = ttm->getRemainigDelay(oldDelay); 255 | 256 | if (!ticks) { 257 | ttm->advanceScript(); 258 | 259 | newDelay = ttm->getDelay(); 260 | 261 | if (newDelay && (newDelay < delay)) { 262 | delay = newDelay; 263 | } 264 | } 265 | 266 | if (ttm->isFinished()) { 267 | lastHashes.push_back(ttm->getHash()); 268 | delete ttm; 269 | it = ttmScenes.erase(it); 270 | continue; 271 | } else { 272 | ++it; 273 | } 274 | } 275 | } 276 | 277 | bool SCRANTIC::Robinson::loadMovie(const std::string &adsName, u16 num) { 278 | ads = static_cast(res->getResource(adsName)); 279 | 280 | if (!ads) { 281 | return false; 282 | } 283 | 284 | adsFileName = ads->filename; 285 | menu->setMenuPostion(adsFileName, ads->getMoviePosFromNumber(num)); 286 | currentMovie = num; 287 | 288 | std::cout << "Playing ADS movie " << ads->tagList[num] << std::endl; 289 | 290 | movieRunning = false; 291 | movieFirstRun = true; 292 | movieLastRun = false; 293 | lastHashes.clear(); 294 | killedHashes.clear(); 295 | 296 | resetPlayer(); 297 | 298 | //SDL_Point pos = { ISLAND_TEMP_X, ISLAND_TEMP_Y }; 299 | //compositor->setAbsoluteIslandPos(&pos); 300 | 301 | return true; 302 | } 303 | 304 | void SCRANTIC::Robinson::startMovie() { 305 | movieRunning = true; 306 | renderMenu = false; 307 | } 308 | 309 | void SCRANTIC::Robinson::advanceScripts() { 310 | if (!movieRunning) { 311 | return; 312 | } 313 | 314 | if (movieFirstRun) { 315 | movieFirstRun = false; 316 | runADSBlock(false, currentMovie, 0, 0); 317 | return; 318 | } 319 | 320 | if (!ttmScenes.empty()) { 321 | runTTMs(); 322 | if (lastHashes.size() && !movieLastRun) { 323 | auto it = lastHashes.begin(); 324 | while (it != lastHashes.end()) { 325 | u16 hash = *it; 326 | size_t countAfter = ads->getLabelCountAfter(currentMovie, hash); 327 | 328 | for (size_t i = 0; i < countAfter; ++i) { 329 | runADSBlock(false, currentMovie, hash, i); 330 | } 331 | ++it; 332 | } 333 | lastHashes.clear(); 334 | } 335 | return; 336 | } 337 | 338 | std::cout << adsFileName << ": Finished ADS Movie: " << currentMovie << std::endl; 339 | 340 | if (movieQueue.empty()) { 341 | movieRunning = false; 342 | return; 343 | } 344 | 345 | std::pair nextItem = *movieQueue.begin(); 346 | bool skipToPlayAds = nextItem.second; 347 | currentMovie = nextItem.first; 348 | 349 | movieQueue.erase(movieQueue.begin()); 350 | 351 | runADSBlock(false, currentMovie, 0, 0, skipToPlayAds); 352 | } 353 | 354 | void SCRANTIC::Robinson::runADSBlock(bool togetherWith, u16 movie, u16 hash, u16 num, bool skipToPlayAds) { 355 | std::vector block; 356 | 357 | if (hash == 0) { 358 | block = ads->getInitialBlock(movie); 359 | } else if (togetherWith) { 360 | block = ads->getBlockTogetherWithMovie(movie, hash, num); 361 | } else { 362 | block = ads->getBlockAfterMovie(movie, hash, num); 363 | } 364 | 365 | bool done = false; 366 | bool found; 367 | u16 lastHash = 0; 368 | bool skipToPlayMovie = false; 369 | bool isRandom = false; 370 | size_t randomChoice; 371 | 372 | std::vector randomBlock; 373 | std::vector ttmsToAdd; 374 | std::vector ttmsToAddHashes; 375 | 376 | for (auto cmd : block) { 377 | 378 | if (skipToPlayAds) { 379 | if (cmd.opcode == CMD_PLAY_ADS_MOVIE) { 380 | skipToPlayAds = false; 381 | } 382 | continue; 383 | } 384 | 385 | if (skipToPlayMovie) { 386 | if (cmd.opcode == CMD_PLAY_MOVIE) { 387 | skipToPlayMovie = false; 388 | } 389 | continue; 390 | } 391 | 392 | switch (cmd.opcode) { 393 | 394 | case CMD_ADD_INIT_TTM: 395 | addTTM(cmd); 396 | break; 397 | 398 | case CMD_SKIP_IF_PLAYED: 399 | skipToPlayMovie = false; 400 | for (size_t i = 0; i < cmd.data.size(); i += 2) { 401 | lastHash = SCRANTIC::ADSFile::makeHash(cmd.data.at(i), cmd.data.at(i+1)); 402 | if (std::find(lastHashes.begin(), lastHashes.end(), lastHash) != lastHashes.end()) { 403 | skipToPlayMovie = true; 404 | break; 405 | } 406 | if (std::find(ttmsToAddHashes.begin(), ttmsToAddHashes.end(), lastHash) != ttmsToAddHashes.end()) { 407 | skipToPlayMovie = true; 408 | break; 409 | } 410 | } 411 | break; 412 | 413 | case CMD_ONLY_IF_PLAYED: 414 | skipToPlayMovie = true; 415 | for (size_t i = 0; i < cmd.data.size(); i += 2) { 416 | lastHash = SCRANTIC::ADSFile::makeHash(cmd.data.at(i), cmd.data.at(i+1)); 417 | if (std::find(ttmsToAddHashes.begin(), ttmsToAddHashes.end(), lastHash) != ttmsToAddHashes.end()) { 418 | skipToPlayMovie = false; 419 | break; 420 | } 421 | for (auto scene : ttmScenes) { 422 | if (scene->getHash() == lastHash) { 423 | skipToPlayMovie = false; 424 | break; 425 | } 426 | } 427 | } 428 | break; 429 | 430 | case CMD_PLAY_MOVIE: 431 | done = true; 432 | break; 433 | 434 | case CMD_ADD_TTM: //TTM Scene ??? Repeat 435 | if (isRandom) { 436 | for (u16 j = 0; j < cmd.data.at(3); j++) { 437 | randomBlock.push_back(cmd); 438 | } 439 | } else { 440 | ttmsToAdd.push_back(cmd); 441 | ttmsToAddHashes.push_back(SCRANTIC::ADSFile::makeHash(cmd.data.at(0), cmd.data.at(1))); 442 | } 443 | break; 444 | 445 | case CMD_ZERO_CHANCE: 446 | if (!isRandom) { 447 | std::cerr << "ERROR: CMD_ZERO_CHANCE found outside of random block!" << std::endl; 448 | } 449 | for (u16 j = 0; j < cmd.data.at(0); j++) { 450 | randomBlock.push_back(cmd); 451 | } 452 | break; 453 | 454 | case CMD_RANDOM_START: 455 | isRandom = true; 456 | randomBlock.clear(); 457 | break; 458 | 459 | case CMD_RANDOM_END: 460 | isRandom = false; 461 | randomChoice = std::rand() % randomBlock.size(); 462 | if (randomBlock[randomChoice].opcode != CMD_ZERO_CHANCE) { 463 | ttmsToAdd.push_back(randomBlock[randomChoice]); 464 | ttmsToAddHashes.push_back(SCRANTIC::ADSFile::makeHash(randomBlock[randomChoice].data.at(0), randomBlock[randomChoice].data.at(1))); 465 | } 466 | break; 467 | 468 | case CMD_KILL_TTM: 469 | lastHash = SCRANTIC::ADSFile::makeHash(cmd.data.at(0), cmd.data.at(1)); 470 | killedHashes.push_back(lastHash); 471 | found = false; 472 | for (auto scene : ttmScenes) { 473 | if (lastHash == scene->getHash()) { 474 | scene->kill(); 475 | found = true; 476 | break; 477 | } 478 | } 479 | 480 | if (!found) { 481 | std::cout << "ADS Command: Kill movie not found ! " << (u16)cmd.data.at(0) << " " << cmd.data.at(1) << std::endl; 482 | } 483 | break; 484 | 485 | case CMD_UNK_LABEL: 486 | lastHash = SCRANTIC::ADSFile::makeHash(cmd.data.at(0), cmd.data.at(1)); 487 | if (std::find(killedHashes.begin(), killedHashes.end(), lastHash) != killedHashes.end()) { 488 | skipToPlayMovie = true; 489 | break; 490 | } 491 | break; 492 | 493 | case CMD_PLAY_ADS_MOVIE: 494 | if (currentMovie != cmd.data.at(0)) { 495 | movieQueue.push_front(std::make_pair(currentMovie, true)); 496 | movieQueue.push_front(std::make_pair(cmd.data.at(0), false)); 497 | movieLastRun = true; 498 | return; 499 | } 500 | break; 501 | 502 | case CMD_SET_SCENE: 503 | std::cout << "Play ADS Movie: " << cmd.name << std::endl; 504 | break; 505 | 506 | case CMD_END_SCRIPT: 507 | movieLastRun = true; 508 | killedHashes.clear(); 509 | break; 510 | 511 | default: 512 | std::cout << "ADS Command: " << SCRANTIC::BaseFile::commandToString(cmd, true) << std::endl; 513 | break; 514 | } 515 | 516 | if (done) { 517 | break; 518 | } 519 | } 520 | 521 | for (auto ttm : ttmsToAdd) { 522 | addTTM(ttm); 523 | lastHash = SCRANTIC::ADSFile::makeHash(ttm.data.at(0), ttm.data.at(1)); 524 | size_t count = ads->getLabelCountTogether(movie, lastHash); 525 | for (size_t j = 0; j < count; ++j) { 526 | runADSBlock(true, movie, lastHash, j); 527 | } 528 | } 529 | 530 | runTTMs(); 531 | } 532 | -------------------------------------------------------------------------------- /Robinson.h: -------------------------------------------------------------------------------- 1 | #ifndef ROBINSON_H 2 | #define ROBINSON_H 3 | 4 | #include "RESFile.h" 5 | #include "ADSFile.h" 6 | #include "SCRFile.h" 7 | #include "BMPFile.h" 8 | #include "PALFile.h" 9 | #include "TTMPlayer.h" 10 | #include "RIFFPlayer.h" 11 | #include "RobinsonMenu.h" 12 | #include "RobinsonCompositor.h" 13 | 14 | #ifdef WIN32 15 | #include 16 | #else 17 | #include 18 | #endif 19 | 20 | namespace SCRANTIC { 21 | 22 | class Robinson 23 | { 24 | private: 25 | Robinson(Robinson &C); 26 | 27 | RESFile *res; 28 | ADSFile *ads; 29 | 30 | RIFFPlayer *audioPlayer; 31 | RobinsonMenu *menu; 32 | RobinsonCompositor *compositor; 33 | 34 | SDL_Renderer *renderer; 35 | 36 | SDL_Color *palette; 37 | 38 | bool movieRunning; 39 | bool movieFirstRun; 40 | bool movieLastRun; 41 | bool renderMenu; 42 | 43 | //u16 currentMovie; 44 | std::string adsFileName; 45 | 46 | std::list ttmScenes; 47 | std::list> movieQueue; 48 | std::list lastHashes; 49 | std::list killedHashes; 50 | 51 | BMPFile *images[MAX_IMAGES]; 52 | 53 | u16 delay; 54 | u16 currentMovie; 55 | 56 | u32 delayTicks; 57 | 58 | void resetPlayer(); 59 | 60 | void addTTM(Command cmd); 61 | void runTTMs(); 62 | void runADSBlock(bool togetherWith, u16 movie, u16 hash, u16 num = 0, bool skipToPlayAds = false); 63 | 64 | public: 65 | Robinson(const std::string &path, bool readUnpacked); 66 | ~Robinson(); 67 | 68 | bool navigateMenu(SDL_Keycode key); 69 | 70 | void initRenderer(SDL_Renderer *rendererSDL, TTF_Font *font); 71 | 72 | void advanceScripts(); 73 | void render(); 74 | 75 | bool isMenuOpen() { return renderMenu; } 76 | bool isMovieRunning() { return movieRunning; } 77 | 78 | bool loadMovie(const std::string &adsName, u16 num); 79 | void startMovie(); 80 | 81 | u32 getCurrentDelay() { return delay; } 82 | void displayMenu(bool show) { renderMenu = show; } 83 | void displaySplash(); 84 | }; 85 | 86 | } 87 | 88 | #endif // ROBINSON_H 89 | -------------------------------------------------------------------------------- /RobinsonCompositor.cpp: -------------------------------------------------------------------------------- 1 | #include "RobinsonCompositor.h" 2 | #include "defines.h" 3 | 4 | SCRANTIC::RobinsonCompositor::RobinsonCompositor(SDL_Renderer* renderer, int width, int height, RESFile* resources) : 5 | renderer(renderer), 6 | width(width), 7 | height(height), 8 | resources(resources), 9 | fullScreenRect({0, 0, width, height}), 10 | animationCycle(0), 11 | islandPos(NO_ISLAND), 12 | islandTrunk({ ISLAND_TEMP_X, ISLAND_TEMP_Y }), 13 | absoluteIslandTrunkPos({ 0, 0 }), 14 | isNight(false), 15 | isLargeIsland(false), 16 | screenSCR(NULL) { 17 | 18 | backgroundBMP = static_cast(resources->getResource("BACKGRND.BMP")); 19 | raftBMP = static_cast(resources->getResource("MRAFT.BMP")); 20 | holidayBMP = static_cast(resources->getResource("HOLIDAY.BMP")); 21 | cloudsBMP = static_cast(resources->getResource("CLOUDS.BMP")); 22 | 23 | rendererTarget = SDL_GetRenderTarget(renderer); 24 | 25 | oceanSCRList = { 26 | static_cast(resources->getResource("OCEAN00.SCR")), 27 | static_cast(resources->getResource("OCEAN01.SCR")), 28 | static_cast(resources->getResource("OCEAN02.SCR")), 29 | }; 30 | 31 | oceanNightSCR = static_cast(resources->getResource("NIGHT.SCR")); 32 | oceanSCR = oceanSCRList[0]; 33 | 34 | splashSCR = static_cast(resources->getResource("INTRO.SCR")); 35 | 36 | // create background and foreground texture 37 | // better pixel format? 38 | oceanTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, width, height); 39 | bgTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, width, height); 40 | fgTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, width, height); 41 | saveTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, width, height); 42 | SDL_SetTextureBlendMode(bgTexture, SDL_BLENDMODE_BLEND); 43 | SDL_SetTextureBlendMode(fgTexture, SDL_BLENDMODE_BLEND); 44 | SDL_SetTextureBlendMode(saveTexture, SDL_BLENDMODE_BLEND); 45 | } 46 | 47 | SCRANTIC::RobinsonCompositor::~RobinsonCompositor() { 48 | SDL_DestroyTexture(oceanTexture); 49 | SDL_DestroyTexture(bgTexture); 50 | SDL_DestroyTexture(fgTexture); 51 | SDL_DestroyTexture(saveTexture); 52 | } 53 | 54 | void SCRANTIC::RobinsonCompositor::displaySplash() { 55 | renderFullScreenSCR(splashSCR); 56 | SDL_RenderPresent(renderer); 57 | SDL_Delay(2000); 58 | } 59 | 60 | void SCRANTIC::RobinsonCompositor::renderFullScreenSCR(SCRFile* scr) { 61 | SDL_Rect src; 62 | SDL_Texture *tex = scr->getImage(renderer, src); 63 | SDL_Rect dst = { 0, 0, src.w, src.h }; 64 | SDL_RenderCopy(renderer, tex, &src, &dst); 65 | } 66 | 67 | void SCRANTIC::RobinsonCompositor::renderSpriteNumAtPos(BMPFile *bmp, u16 num, i32 x, i32 y) { 68 | SDL_Rect src, dest; 69 | SDL_Texture *tex = bmp->getImage(renderer, num, src); 70 | dest = { x, y, src.w, src.h }; 71 | SDL_RenderCopy(renderer, tex, &src, &dest); 72 | } 73 | 74 | void SCRANTIC::RobinsonCompositor::animateBackground() { 75 | if (islandPos == NO_ISLAND) { 76 | return; 77 | } 78 | 79 | i8 spriteOffset = animationCycle/12; 80 | int trunkX = absoluteIslandTrunkPos.x != 0 ? absoluteIslandTrunkPos.x : islandTrunk.x; 81 | int trunkY = absoluteIslandTrunkPos.y != 0 ? absoluteIslandTrunkPos.y : islandTrunk.y; 82 | 83 | if (isLargeIsland) { 84 | renderSpriteNumAtPos(backgroundBMP, (SPRITE_WAVE_L_LEFT + spriteOffset), WAVE_L_LEFT_X + trunkX, WAVE_L_LEFT_Y + trunkY); 85 | renderSpriteNumAtPos(backgroundBMP, (SPRITE_WAVE_L_MID + ((spriteOffset + 1) % 3)), WAVE_L_MID_X + trunkX, WAVE_L_MID_Y + trunkY); 86 | renderSpriteNumAtPos(backgroundBMP, (SPRITE_WAVE_L_RIGHT + ((spriteOffset + 2) % 3)), WAVE_L_RIGHT_X + trunkX, WAVE_L_RIGHT_Y + trunkY); 87 | renderSpriteNumAtPos(backgroundBMP, (SPRITE_WAVE_STONE + spriteOffset), WAVE_STONE_X + trunkX, WAVE_STONE_Y + trunkY); 88 | } else { 89 | renderSpriteNumAtPos(backgroundBMP, (SPRITE_WAVE_LEFT + spriteOffset), WAVE_LEFT_X + trunkX, WAVE_LEFT_Y + trunkY); 90 | renderSpriteNumAtPos(backgroundBMP, (SPRITE_WAVE_MID + ((spriteOffset + 1) % 3)), WAVE_MID_X + trunkX, WAVE_MID_Y + trunkY); 91 | renderSpriteNumAtPos(backgroundBMP, (SPRITE_WAVE_RIGHT + ((spriteOffset + 2) % 3)), WAVE_RIGHT_X + trunkX, WAVE_RIGHT_Y + trunkY); 92 | } 93 | 94 | //weather is missing 95 | 96 | ++animationCycle; 97 | if (animationCycle >= 36) { 98 | animationCycle = 0; 99 | } 100 | } 101 | 102 | void SCRANTIC::RobinsonCompositor::render(std::list::iterator begin, std::list::iterator end) { 103 | // first render background 104 | SDL_SetRenderTarget(renderer, bgTexture); 105 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); 106 | SDL_RenderClear(renderer); 107 | 108 | SDL_SetRenderTarget(renderer, oceanTexture); 109 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 110 | SDL_RenderClear(renderer); 111 | 112 | if (islandPos != NO_ISLAND) { 113 | if (isNight) { 114 | renderFullScreenSCR(oceanNightSCR); 115 | } else { 116 | renderFullScreenSCR(oceanSCR); 117 | } 118 | 119 | int trunkX = absoluteIslandTrunkPos.x != 0 ? absoluteIslandTrunkPos.x : islandTrunk.x; 120 | int trunkY = absoluteIslandTrunkPos.y != 0 ? absoluteIslandTrunkPos.y : islandTrunk.y; 121 | 122 | SDL_SetRenderTarget(renderer, bgTexture); 123 | if (isLargeIsland) { 124 | renderSpriteNumAtPos(backgroundBMP, SPRITE_L_ISLAND, L_ISLAND_X + trunkX, L_ISLAND_Y + trunkY); 125 | renderSpriteNumAtPos(backgroundBMP, SPRITE_STONE, STONE_X + trunkX, STONE_Y + trunkY); 126 | } 127 | 128 | renderSpriteNumAtPos(backgroundBMP, SPRITE_ISLAND, ISLAND_X + trunkX, ISLAND_Y + trunkY); 129 | renderSpriteNumAtPos(backgroundBMP, SPRITE_TOP_SHADOW, TOP_SHADOW_X + trunkX, TOP_SHADOW_Y + trunkY); 130 | renderSpriteNumAtPos(backgroundBMP, SPRITE_TRUNK, trunkX, trunkY); 131 | renderSpriteNumAtPos(backgroundBMP, SPRITE_TOP, TOP_X + trunkX, TOP_Y + trunkY); 132 | renderSpriteNumAtPos(raftBMP, 0, RAFT_X + trunkX, RAFT_Y + trunkY); 133 | 134 | animateBackground(); 135 | } else { 136 | if (screenSCR != NULL) { 137 | renderFullScreenSCR(screenSCR); 138 | } 139 | } 140 | 141 | int shiftX = absoluteIslandTrunkPos.x != 0 ? absoluteIslandTrunkPos.x - islandTrunk.x : 0; 142 | int shiftY = absoluteIslandTrunkPos.y != 0 ? absoluteIslandTrunkPos.y - islandTrunk.y : 0; 143 | SDL_Rect targetRect; 144 | u8 save; 145 | std::string scrName; 146 | 147 | // saved image 148 | SDL_SetRenderTarget(renderer, saveTexture); 149 | for (auto it = begin; it != end; ++it) { 150 | save = (*it)->needsSave(); 151 | if (save != SAVE_NOSAVE) { 152 | if (save == SAVE_NEWIMAGE) { 153 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); 154 | SDL_RenderClear(renderer); 155 | } 156 | SDL_RenderCopy(renderer, (*it)->savedImage, &fullScreenRect, &fullScreenRect); 157 | } 158 | 159 | //pre render foreground 160 | (*it)->renderForeground(shiftX, shiftY); 161 | } 162 | // background end 163 | 164 | // render everything to screen 165 | SDL_SetRenderTarget(renderer, rendererTarget); 166 | SDL_RenderClear(renderer); 167 | 168 | targetRect = { 0, 0, width, height}; 169 | shiftRect(&targetRect, shiftX, shiftY); 170 | 171 | SDL_RenderCopy(renderer, oceanTexture, &fullScreenRect, &fullScreenRect); 172 | SDL_RenderCopy(renderer, bgTexture, &fullScreenRect, &fullScreenRect); 173 | SDL_RenderCopy(renderer, saveTexture, &fullScreenRect, &fullScreenRect); 174 | 175 | for (auto it = begin; it != end; ++it) { 176 | if ((*it)->isClipped()) { 177 | SDL_Rect clipRect = (*it)->getClipRect(); 178 | shiftRect(&clipRect, shiftX, shiftY); 179 | SDL_RenderCopy(renderer, (*it)->fg, &clipRect, &clipRect); 180 | } else { 181 | SDL_RenderCopy(renderer, (*it)->fg, &fullScreenRect, &fullScreenRect); 182 | } 183 | 184 | scrName = (*it)->getSCRName(); 185 | if (scrName != "") { 186 | setScreen(scrName); 187 | } 188 | } 189 | } 190 | 191 | void SCRANTIC::RobinsonCompositor::reset() { 192 | islandPos = NO_ISLAND; 193 | screenSCR = NULL; 194 | 195 | absoluteIslandTrunkPos = { 0, 0 }; 196 | 197 | SDL_SetRenderTarget(renderer, saveTexture); 198 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); 199 | SDL_RenderClear(renderer); 200 | SDL_SetRenderTarget(renderer, rendererTarget); 201 | } 202 | 203 | void SCRANTIC::RobinsonCompositor::setScreen(const std::string &screen) { 204 | screenSCR = static_cast(resources->getResource(screen)); 205 | if (screen == "ISLETEMP.SCR") { 206 | islandPos = ISLAND_RIGHT; 207 | islandTrunk.x = ISLAND_TEMP_X; 208 | islandTrunk.y = ISLAND_TEMP_Y; 209 | } else if (screen == "ISLAND2.SCR") { 210 | islandPos = ISLAND_LEFT; 211 | islandTrunk.x = ISLAND2_X; 212 | islandTrunk.y = ISLAND2_Y; 213 | } else { 214 | islandPos = NO_ISLAND; 215 | } 216 | } 217 | 218 | void SCRANTIC::RobinsonCompositor::shiftRect(SDL_Rect* rect, int x, int y) { 219 | rect->x += x; 220 | rect->y += y; 221 | if (rect->x < 0) { 222 | rect->w += rect->x; 223 | rect->x = 0; 224 | } 225 | if (rect->y < 0) { 226 | rect->h += rect->y; 227 | rect->y = 0; 228 | } 229 | if (rect->x + rect->w > width) { 230 | rect->w = width - rect->x; 231 | } 232 | if (rect->y + rect->h > height) { 233 | rect->h = height - rect->y; 234 | } 235 | } 236 | -------------------------------------------------------------------------------- /RobinsonCompositor.h: -------------------------------------------------------------------------------- 1 | #ifndef ROBINSONCOMPOSITOR_H 2 | #define ROBINSONCOMPOSITOR_H 3 | 4 | #include "types.h" 5 | 6 | #include "SCRFile.h" 7 | #include "BMPFile.h" 8 | #include "TTMPlayer.h" 9 | 10 | #ifdef WIN32 11 | #include 12 | #else 13 | #include 14 | #endif 15 | 16 | namespace SCRANTIC { 17 | 18 | class RobinsonCompositor 19 | { 20 | private: 21 | SDL_Renderer *renderer; 22 | 23 | int width; 24 | int height; 25 | 26 | bool isLargeIsland; 27 | bool isNight; 28 | 29 | RESFile *resources; 30 | 31 | BMPFile *backgroundBMP; 32 | BMPFile *holidayBMP; 33 | BMPFile *raftBMP; 34 | BMPFile *cloudsBMP; 35 | 36 | SCRFile *splashSCR; 37 | std::vector oceanSCRList; 38 | SCRFile *oceanNightSCR; 39 | SCRFile *oceanSCR; 40 | SCRFile *screenSCR; 41 | 42 | i8 animationCycle; 43 | i8 islandPos; 44 | SDL_Point islandTrunk; 45 | SDL_Point absoluteIslandTrunkPos; 46 | 47 | SDL_Texture *oceanTexture; 48 | SDL_Texture *bgTexture; 49 | SDL_Texture *fgTexture; 50 | SDL_Texture *saveTexture; 51 | SDL_Texture *rendererTarget; 52 | 53 | SDL_Rect fullScreenRect; 54 | 55 | RobinsonCompositor(const RobinsonCompositor& other); 56 | 57 | void renderSpriteNumAtPos(BMPFile *bmp, u16 num, i32 x, i32 y); 58 | void renderFullScreenSCR(SCRFile *scr); 59 | void animateBackground(); 60 | void shiftRect(SDL_Rect *rect, int x, int y); 61 | 62 | public: 63 | RobinsonCompositor(SDL_Renderer* renderer, int width, int height, RESFile *resources); 64 | ~RobinsonCompositor(); 65 | 66 | void displaySplash(); 67 | void reset(); 68 | void render(std::list::iterator begin, std::list::iterator end); 69 | void setScreen(const std::string &screen); 70 | 71 | void setNight(bool night) { isNight = night; } 72 | void setOcean(int num) { oceanSCR = oceanSCRList[num]; } 73 | void setLargeIsland(bool large) { isLargeIsland = large; } 74 | void setAbsoluteIslandPos(SDL_Point *pos) { absoluteIslandTrunkPos = *pos; } 75 | 76 | bool getNight() { return isNight; } 77 | bool getLargeIsland() { return isLargeIsland; } 78 | }; 79 | 80 | } 81 | 82 | #endif // ROBINSONCOMPOSITOR_H 83 | -------------------------------------------------------------------------------- /RobinsonMenu.cpp: -------------------------------------------------------------------------------- 1 | #include "RobinsonMenu.h" 2 | 3 | #include "types.h" 4 | 5 | #include 6 | 7 | SCRANTIC::RobinsonMenu::RobinsonMenu(SDL_Renderer* renderer, int width, int height) 8 | : renderer(renderer), 9 | width(width), 10 | height(height), 11 | currentPage(""), 12 | currentItem(0) { 13 | } 14 | 15 | SCRANTIC::RobinsonMenu::~RobinsonMenu() { 16 | for (auto it = menuScreen.begin(); it != menuScreen.end(); ++it) { 17 | SDL_DestroyTexture(it->second); 18 | } 19 | } 20 | 21 | void SCRANTIC::RobinsonMenu::initMenu(std::map> items, TTF_Font *font) { 22 | std::string page; 23 | 24 | SDL_Color c1 = { 255, 0, 0, 255 }; 25 | SDL_Color c2 = { 255, 255, 255, 255 }; 26 | SDL_Rect rect = { 20, 0, 0, 0 }; 27 | 28 | for (auto it = items.begin(); it != items.end(); ++it) { 29 | page = it->first; 30 | 31 | SDL_Surface *menu = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0); 32 | 33 | rect.y = 10; 34 | 35 | TTF_SizeText(font, page.c_str(), &rect.w, &rect.h); 36 | SDL_Surface *tmpSurface = TTF_RenderText_Blended(font, page.c_str(), c1); 37 | 38 | if (tmpSurface == NULL) { 39 | std::cerr << "ERROR: Renderer: Could not render text: " << page << std::endl; 40 | } else { 41 | SDL_BlitSurface(tmpSurface, NULL, menu, &rect); 42 | SDL_FreeSurface(tmpSurface); 43 | } 44 | 45 | for (auto item : it->second) { 46 | rect.y = rect.y + rect.h + 10; 47 | 48 | TTF_SizeText(font, item.c_str(), &rect.w, &rect.h); 49 | tmpSurface = TTF_RenderText_Blended(font, item.c_str(), c2); 50 | 51 | if (tmpSurface == NULL) { 52 | std::cerr << "ERROR: Renderer: Could not render text: " << item << std::endl; 53 | } else { 54 | SDL_BlitSurface(tmpSurface, NULL, menu, &rect); 55 | SDL_FreeSurface(tmpSurface); 56 | rect.w += 6; 57 | rect.x -= 3; 58 | rect.h += 6; 59 | rect.y -= 3; 60 | menuRects[page].push_back(rect); 61 | rect.x += 3; 62 | rect.y += 3; 63 | rect.h -= 6; 64 | } 65 | } 66 | 67 | SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, menu); 68 | 69 | if (tex == NULL) { 70 | std::cerr << "ERROR: Renderer: Could not convert menu surface to to texture: " << page << std::endl; 71 | } else { 72 | SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND); 73 | SDL_SetTextureAlphaMod(tex, 200); 74 | } 75 | 76 | SDL_FreeSurface(menu); 77 | 78 | menuScreen[page] = tex; 79 | } 80 | } 81 | 82 | void SCRANTIC::RobinsonMenu::render() { 83 | auto it = menuScreen.find(currentPage); 84 | if (it == menuScreen.end()) { 85 | std::cerr << "Menu screen not found! " << currentPage << std::endl; 86 | std::cerr << "Resetting menu position!" << std::endl; 87 | currentPage = menuScreen.begin()->first; 88 | currentItem = 0; 89 | return; 90 | } 91 | 92 | SDL_SetRenderDrawColor(renderer, 127, 127, 127, 255); 93 | SDL_RenderFillRect(renderer, &menuRects[currentPage][currentItem]); 94 | SDL_RenderCopy(renderer, it->second, NULL, NULL); 95 | } 96 | 97 | bool SCRANTIC::RobinsonMenu::navigateMenu(SDL_Keycode key, std::string &newPage, size_t &newPosition) { 98 | std::map::iterator it; 99 | 100 | switch (key) { 101 | case SDLK_LEFT: 102 | case SDLK_RIGHT: 103 | it = menuScreen.find(currentPage); 104 | if (it == menuScreen.end()) { 105 | std::cerr << "Menu screen not found! " << currentPage << std::endl; 106 | return false; 107 | } 108 | 109 | currentItem = 0; 110 | 111 | if (key == SDLK_LEFT) { 112 | if (it == menuScreen.begin()) { 113 | it = menuScreen.end(); 114 | } 115 | --it; 116 | } else { 117 | ++it; 118 | if (it == menuScreen.end()) { 119 | it = menuScreen.begin(); 120 | } 121 | } 122 | currentPage = it->first; 123 | break; 124 | 125 | case SDLK_UP: 126 | if (currentItem == 0) { 127 | currentItem = menuRects[currentPage].size() - 1; 128 | } else { 129 | --currentItem; 130 | } 131 | break; 132 | 133 | case SDLK_DOWN: 134 | ++currentItem; 135 | if (currentItem == menuRects[currentPage].size()) { 136 | currentItem = 0; 137 | } 138 | break; 139 | 140 | case SDLK_RETURN: 141 | newPage = currentPage; 142 | newPosition = currentItem; 143 | return true; 144 | } 145 | 146 | return false; 147 | } 148 | 149 | void SCRANTIC::RobinsonMenu::setMenuPostion(std::string page, size_t pos) { 150 | if (menuScreen.find(page) != menuScreen.end()) { 151 | currentPage = page; 152 | } 153 | if (pos < menuRects[currentPage].size()) { 154 | currentItem = pos; 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /RobinsonMenu.h: -------------------------------------------------------------------------------- 1 | #ifndef ROBINSONMENU_H 2 | #define ROBINSONMENU_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #ifdef WIN32 9 | #include 10 | #else 11 | #include 12 | #endif 13 | 14 | namespace SCRANTIC { 15 | 16 | class RobinsonMenu 17 | { 18 | private: 19 | std::map menuScreen; 20 | std::map> menuRects; 21 | 22 | SDL_Renderer *renderer; 23 | 24 | int width; 25 | int height; 26 | 27 | std::string currentPage; 28 | size_t currentItem; 29 | 30 | RobinsonMenu(const RobinsonMenu& C); 31 | 32 | public: 33 | RobinsonMenu(SDL_Renderer *renderer, int width, int height); 34 | ~RobinsonMenu(); 35 | 36 | void render(); 37 | void initMenu(std::map> items, TTF_Font *font); 38 | void setMenuPostion(std::string page, size_t pos); 39 | bool navigateMenu(SDL_Keycode key, std::string &newPage, size_t &newPosition); 40 | }; 41 | 42 | } 43 | #endif // ROBINSONMENU_H 44 | -------------------------------------------------------------------------------- /SCRFile.cpp: -------------------------------------------------------------------------------- 1 | #include "SCRFile.h" 2 | 3 | SCRANTIC::SCRFile::SCRFile(const std::string &name, v8 &data) : 4 | CompressedBaseFile(name), 5 | image(NULL), 6 | texture(NULL) { 7 | 8 | v8::iterator it = data.begin(); 9 | 10 | assertString(it, "SCR:"); 11 | 12 | readUintLE(it, dimBinSize); 13 | readUintLE(it, magic); 14 | 15 | assertString(it, "DIM:"); 16 | 17 | readUintLE(it, dimSize); 18 | readUintLE(it, width); 19 | readUintLE(it, height); 20 | 21 | assertString(it, "BIN:"); 22 | 23 | if (!handleDecompression(data, it, uncompressedData)) { 24 | return; 25 | } 26 | 27 | image = createSdlSurface(uncompressedData, width, height); 28 | } 29 | 30 | v8 SCRANTIC::SCRFile::repackIntoResource() { 31 | 32 | std::string strings[3] = { "SCR:", "DIM:", "BIN:" }; 33 | 34 | magic = 0x8000; 35 | 36 | compressionFlag = 2; 37 | v8 compressedData = LZCCompress(uncompressedData); 38 | uncompressedSize = uncompressedData.size(); 39 | compressedSize = compressedData.size() + 5; 40 | 41 | dimSize = 4; 42 | dimBinSize = dimSize + compressedSize + 16; 43 | 44 | v8 rawData(strings[0].begin(), strings[0].end()); 45 | BaseFile::writeUintLE(rawData, dimBinSize); 46 | BaseFile::writeUintLE(rawData, magic); 47 | std::copy(strings[1].begin(), strings[1].end(), std::back_inserter(rawData)); 48 | BaseFile::writeUintLE(rawData, dimSize); 49 | BaseFile::writeUintLE(rawData, width); 50 | BaseFile::writeUintLE(rawData, height); 51 | std::copy(strings[2].begin(), strings[2].end(), std::back_inserter(rawData)); 52 | BaseFile::writeUintLE(rawData, compressedSize); 53 | BaseFile::writeUintLE(rawData, compressionFlag); 54 | BaseFile::writeUintLE(rawData, uncompressedSize); 55 | std::copy(compressedData.begin(), compressedData.end(), std::back_inserter(rawData)); 56 | 57 | compressedSize -= 5; 58 | 59 | return rawData; 60 | } 61 | 62 | SCRANTIC::SCRFile::SCRFile(const std::string &bmpFilename) 63 | : CompressedBaseFile(bmpFilename), 64 | image(NULL), 65 | texture(NULL) { 66 | 67 | std::string actualFilename = bmpFilename.substr(0, bmpFilename.rfind('.')) + ".BMP"; 68 | 69 | uncompressedData = readRGBABitmapData(actualFilename, width, height); 70 | 71 | image = createSdlSurface(uncompressedData, width, height); 72 | } 73 | 74 | void SCRANTIC::SCRFile::saveFile(const std::string &path) { 75 | v8 bmpFile = createRGBABitmapData(uncompressedData, width, height); 76 | 77 | std::string newFilename = filename.substr(0, filename.rfind('.')) + ".BMP"; 78 | 79 | SCRANTIC::BaseFile::writeFile(bmpFile, newFilename, path); 80 | } 81 | 82 | SCRANTIC::SCRFile::~SCRFile() { 83 | SDL_DestroyTexture(texture); 84 | SDL_FreeSurface(image); 85 | } 86 | 87 | void SCRANTIC::SCRFile::setPalette(SDL_Color color[], u16 count) { 88 | if (image == NULL) { 89 | return; 90 | } 91 | 92 | SDL_SetPaletteColors(image->format->palette, color, 0, 256); 93 | SDL_DestroyTexture(texture); 94 | texture = NULL; 95 | } 96 | 97 | SDL_Texture *SCRANTIC::SCRFile::getImage(SDL_Renderer *renderer, SDL_Rect &rect) { 98 | if (image == NULL) { 99 | return NULL; 100 | } 101 | 102 | if (texture == NULL) { 103 | texture = SDL_CreateTextureFromSurface(renderer, image); 104 | if (texture == NULL) { 105 | std::cerr << filename << ": Error creating SDL_Texture" << std::endl; 106 | return NULL; 107 | } 108 | } 109 | 110 | rect = { 0, 0, image->w, image->h }; 111 | 112 | return texture; 113 | } 114 | -------------------------------------------------------------------------------- /SCRFile.h: -------------------------------------------------------------------------------- 1 | #ifndef SCRFILE_H 2 | #define SCRFILE_H 3 | 4 | #include "CompressedBaseFile.h" 5 | #include "GraphicBaseFile.h" 6 | 7 | namespace SCRANTIC { 8 | 9 | class SCRFile : public CompressedBaseFile, public GraphicBaseFile { 10 | protected: 11 | u16 dimBinSize; // 12 | u16 magic; //0x8000 13 | u32 dimSize; 14 | u16 imageCount; 15 | u16 width; 16 | u16 height; 17 | v8 uncompressedData; 18 | SDL_Surface *image; 19 | SDL_Texture *texture; 20 | 21 | public: 22 | SCRFile(const std::string &name, v8 &data); 23 | explicit SCRFile(const std::string &bmpFilename); 24 | ~SCRFile(); 25 | 26 | void saveFile(const std::string &path) override; 27 | v8 repackIntoResource() override; 28 | 29 | SDL_Texture *getImage(SDL_Renderer *renderer, SDL_Rect &rect); 30 | void setPalette(SDL_Color color[], u16 count); 31 | }; 32 | 33 | } 34 | 35 | #endif // SCRFILE_H 36 | 37 | -------------------------------------------------------------------------------- /TTMFile.cpp: -------------------------------------------------------------------------------- 1 | #include "TTMFile.h" 2 | 3 | SCRANTIC::TTMFile::TTMFile(const std::string &name, v8 &data) 4 | : CompressedBaseFile(name) { 5 | 6 | initMnemonics(); 7 | 8 | v8::iterator it = data.begin(); 9 | 10 | assertString(it, "VER:"); 11 | 12 | readUintLE(it, verSize); 13 | version = readString(it, verSize-1); 14 | 15 | assertString(it, "PAG:"); 16 | 17 | readUintLE(it, pagSize); 18 | readUintLE(it, pag); 19 | 20 | assertString(it, "TT3:"); 21 | 22 | if (!handleDecompression(data, it, rawScript)) { 23 | return; 24 | } 25 | 26 | std::advance(it, compressedSize); 27 | 28 | assertString(it, "TTI:"); 29 | 30 | readUintLE(it, fullTagSize); 31 | readUintLE(it, magic); 32 | 33 | assertString(it, "TAG:"); 34 | 35 | readUintLE(it, tagSize); 36 | readUintLE(it, tagCount); 37 | 38 | u16 id; 39 | std::string desc; 40 | 41 | for (u16 i = 0; i < tagCount; ++i) { 42 | readUintLE(it, id); 43 | desc = readString(it); 44 | tagList.insert(std::pair(id, desc)); 45 | } 46 | 47 | if (!rawScript.size()) { 48 | return; 49 | } 50 | 51 | parseRawScript(); 52 | } 53 | 54 | SCRANTIC::TTMFile::TTMFile(const std::string& filename) 55 | : CompressedBaseFile(filename) { 56 | 57 | initMnemonics(); 58 | 59 | std::ifstream in; 60 | 61 | in.open(filename, std::ios::in); 62 | if (!in.is_open()) { 63 | std::cerr << "TTMFile: Could not open " << filename << std::endl; 64 | return; 65 | } 66 | 67 | u16 i, j, k, l, m, n; 68 | 69 | std::string line; 70 | std::string tag; 71 | std::string mnemonic; 72 | u16 id, opcode; 73 | int count; 74 | 75 | std::string mode; 76 | 77 | while (getline(in, line)) { 78 | if (line.substr(0, 1) == "#" || line == "") { 79 | continue; 80 | } 81 | 82 | if (line == "VERSION") { 83 | getline(in, line); 84 | version = line; 85 | continue; 86 | } 87 | 88 | if (line == "TAGS") { 89 | mode = line; 90 | continue; 91 | } 92 | 93 | if (line == "SCRIPT") { 94 | mode = line; 95 | continue; 96 | } 97 | 98 | if (mode == "TAGS") { 99 | std::istringstream iss(line); 100 | if (!(iss >> std::hex >> id)) { 101 | break; 102 | } 103 | tag = line.substr(line.find(" ")+1); 104 | tagList.insert({id, tag}); 105 | } else if (mode == "SCRIPT") { 106 | mnemonic = line.substr(0, line.find(" ")); 107 | opcode = getOpcodeFromMnemonic(mnemonic); 108 | count = getParamCount(opcode); 109 | 110 | opcode |= count; 111 | writeUintLE(rawScript, opcode); 112 | 113 | std::istringstream iss(line); 114 | switch (count) { 115 | case 1: 116 | iss >> mnemonic >> std::hex >> i; 117 | writeUintLE(rawScript, i); 118 | break; 119 | case 2: 120 | iss >> mnemonic >> std::hex >> i >> j; 121 | writeUintLE(rawScript, i); 122 | writeUintLE(rawScript, j); 123 | break; 124 | case 4: 125 | iss >> mnemonic >> std::hex >> i >> j >> k >> l; 126 | writeUintLE(rawScript, i); 127 | writeUintLE(rawScript, j); 128 | writeUintLE(rawScript, k); 129 | writeUintLE(rawScript, l); 130 | break; 131 | case 6: 132 | iss >> mnemonic >> std::hex >> i >> j >> k >> l >> m >> n; 133 | writeUintLE(rawScript, i); 134 | writeUintLE(rawScript, j); 135 | writeUintLE(rawScript, k); 136 | writeUintLE(rawScript, l); 137 | writeUintLE(rawScript, m); 138 | writeUintLE(rawScript, n); 139 | break; 140 | case 0xF: 141 | iss >> mnemonic >> tag; 142 | std::copy(tag.begin(), tag.end(), std::back_inserter(rawScript)); 143 | rawScript.push_back(0); 144 | if (tag.size() % 2 == 0) { 145 | rawScript.push_back(0); 146 | } 147 | break; 148 | default: 149 | break; 150 | } 151 | } 152 | } 153 | 154 | in.close(); 155 | 156 | parseRawScript(); 157 | } 158 | 159 | v8 SCRANTIC::TTMFile::repackIntoResource() { 160 | std::string strings[5] = { "VER:", "PAG:", "TT3:", "TTI:", "TAG:" }; 161 | 162 | magic = 0x8000; 163 | 164 | compressionFlag = 1; 165 | v8 compressedData = RLECompress(rawScript); 166 | uncompressedSize = rawScript.size(); 167 | compressedSize = compressedData.size() + 5; 168 | 169 | verSize = version.size() + 1; 170 | 171 | pagSize = 2; 172 | pag = countUpdateInScript(); 173 | 174 | tagSize = 2; 175 | for (auto it = tagList.begin(); it != tagList.end(); ++it) { 176 | tagSize += 2 + it->second.size() + 1; 177 | } 178 | 179 | fullTagSize = tagSize + 8; // TAG to end 180 | tagCount = tagList.size(); 181 | 182 | v8 rawData(strings[0].begin(), strings[0].end()); 183 | BaseFile::writeUintLE(rawData, verSize); 184 | std::copy(version.begin(), version.end(), std::back_inserter(rawData)); 185 | rawData.push_back(0); 186 | 187 | std::copy(strings[1].begin(), strings[1].end(), std::back_inserter(rawData)); 188 | BaseFile::writeUintLE(rawData, pagSize); 189 | BaseFile::writeUintLE(rawData, pag); 190 | 191 | std::copy(strings[2].begin(), strings[2].end(), std::back_inserter(rawData)); 192 | BaseFile::writeUintLE(rawData, compressedSize); 193 | BaseFile::writeUintLE(rawData, compressionFlag); 194 | BaseFile::writeUintLE(rawData, uncompressedSize); 195 | std::copy(compressedData.begin(), compressedData.end(), std::back_inserter(rawData)); 196 | compressedSize -= 5; 197 | 198 | std::copy(strings[3].begin(), strings[3].end(), std::back_inserter(rawData)); 199 | BaseFile::writeUintLE(rawData, fullTagSize); 200 | BaseFile::writeUintLE(rawData, magic); 201 | 202 | std::copy(strings[4].begin(), strings[4].end(), std::back_inserter(rawData)); 203 | BaseFile::writeUintLE(rawData, tagSize); 204 | BaseFile::writeUintLE(rawData, tagCount); 205 | 206 | for (auto it = tagList.begin(); it != tagList.end(); ++it) { 207 | BaseFile::writeUintLE(rawData, it->first); 208 | std::copy(it->second.begin(), it->second.end(), std::back_inserter(rawData)); 209 | rawData.push_back(0); 210 | } 211 | 212 | return rawData; 213 | } 214 | 215 | void SCRANTIC::TTMFile::parseRawScript() { 216 | v8::iterator it = rawScript.begin(); 217 | 218 | u16 opcode; 219 | u16 word, scene; 220 | u8 length; 221 | std::map::iterator tagIt; 222 | 223 | scene = 0; 224 | 225 | while (it != rawScript.end()) { 226 | readUintLE(it, opcode); 227 | length = (opcode & 0x000F); 228 | Command command; 229 | command.opcode = (opcode & 0xFFF0); 230 | 231 | if ((command.opcode == CMD_SET_SCENE) || (command.opcode == CMD_SET_SCENE_LABEL)) { // && (length == 1)) // tag 232 | readUintLE(it, word); 233 | command.data.push_back(word); 234 | tagIt = tagList.find(word); 235 | if (tagIt != tagList.end()) { 236 | command.name = tagIt->second; 237 | } 238 | } else if (length == 0xF) { //string 239 | command.name = readString(it); 240 | if ((command.name.length() % 2) == 0) { // align to word 241 | readUintLE(it, length); 242 | } 243 | } else { 244 | for (u8 i = 0; i < length; ++i) { 245 | readUintLE(it, word); 246 | command.data.push_back(word); 247 | } 248 | } 249 | 250 | if (command.opcode == CMD_SET_SCENE_LABEL) { 251 | Command c; 252 | c.opcode = CMD_JMP_SCENE; 253 | c.data.push_back(command.data.at(0)); 254 | c.data.push_back(0xFFFF); 255 | script[scene].push_back(c); 256 | } 257 | 258 | if ((command.opcode == CMD_SET_SCENE) || (command.opcode == CMD_SET_SCENE_LABEL)) { 259 | scene = command.data.at(0); 260 | } 261 | 262 | script[scene].push_back(command); 263 | } 264 | } 265 | 266 | void SCRANTIC::TTMFile::saveFile(const std::string &path) { 267 | std::stringstream output; 268 | 269 | output << "# SCRANTIC TTM file" << std::endl; 270 | output << "VERSION" << std::endl; 271 | output << version << std::endl << std::endl; 272 | 273 | output << "TAGS" << std::endl; 274 | for (auto it = tagList.begin(); it != tagList.end(); ++it) { 275 | output << hexToString(it->first, std::hex, 4) << " " << it->second << std::endl; 276 | } 277 | 278 | output << std::endl << "SCRIPT" << std::endl; 279 | 280 | v8::iterator it = rawScript.begin(); 281 | u16 opcode; 282 | u8 length; 283 | u16 word; 284 | 285 | while (it != rawScript.end()) { 286 | readUintLE(it, opcode); 287 | length = (opcode & 0x000F); 288 | Command command; 289 | command.opcode = (opcode & 0xFFF0); 290 | if (command.opcode == CMD_SET_SCENE || command.opcode == CMD_SET_SCENE_LABEL) { 291 | output << "# new scene" << std::endl; 292 | } 293 | 294 | if (length == 0xF) { //string 295 | command.name = readString(it); 296 | if ((command.name.length() % 2) == 0) { // align to word 297 | readUintLE(it, length); 298 | } 299 | } else { 300 | for (u8 i = 0; i < length; ++i) { 301 | readUintLE(it, word); 302 | command.data.push_back(word); 303 | } 304 | } 305 | 306 | output << getMnemoic(command) << std::endl; 307 | } 308 | 309 | writeFile(output.str(), filename, path); 310 | } 311 | 312 | std::vector SCRANTIC::TTMFile::getFullScene(u16 num) 313 | { 314 | std::map >::iterator it = script.find(num); 315 | if (it == script.end()) { 316 | return std::vector(); 317 | } 318 | return it->second; 319 | } 320 | 321 | std::string SCRANTIC::TTMFile::getTag(u16 num) 322 | { 323 | std::map::iterator it = tagList.find(num); 324 | if (it == tagList.end()) { 325 | return std::string(); 326 | } 327 | return it->second; 328 | } 329 | 330 | 331 | bool SCRANTIC::TTMFile::hasInit() 332 | { 333 | std::map >::iterator it; 334 | it = script.find(0); 335 | 336 | if (it == script.end()) { 337 | return false; 338 | } 339 | return true; 340 | } 341 | 342 | u16 SCRANTIC::TTMFile::countUpdateInScript() { 343 | u16 count = 0; 344 | for (auto it = script.begin(); it != script.end(); ++it) { 345 | for (size_t i = 0; i < it->second.size(); ++i) { 346 | if (it->second[i].opcode == CMD_UPDATE) { 347 | ++count; 348 | } 349 | } 350 | } 351 | return count; 352 | } 353 | 354 | void SCRANTIC::TTMFile::initMnemonics() { 355 | //no params 356 | mnemonics.insert({CMD_CLEAR_IMGSLOT, "CLRIMGSLOT"}); 357 | mnemonics.insert({CMD_PURGE, "PURGE"}); 358 | mnemonics.insert({CMD_UPDATE, "UPDATE"}); 359 | //1 param 360 | mnemonics.insert({CMD_DELAY, "DELAY"}); 361 | mnemonics.insert({CMD_SEL_SLOT_IMG, "IMGSLOT"}); 362 | mnemonics.insert({CMD_SEL_SLOT_PAL, "PALSLOT"}); 363 | mnemonics.insert({CMD_SET_SCENE_LABEL, "LABEL"}); 364 | mnemonics.insert({CMD_SET_SCENE, "SCENE"}); 365 | mnemonics.insert({CMD_UNK_1120, "UNK1120"}); 366 | mnemonics.insert({CMD_JMP_SCENE, "JUMP"}); 367 | mnemonics.insert({CMD_CLEAR_RENDERER, "CLEAR"}); 368 | mnemonics.insert({CMD_PLAY_SOUND, "PLAYSND"}); 369 | //string param 370 | mnemonics.insert({CMD_LOAD_SCREEN, "SCREEN"}); 371 | mnemonics.insert({CMD_LOAD_BITMAP, "BITMAP"}); 372 | mnemonics.insert({CMD_LOAD_PALETTE, "PALETTE"}); 373 | //2 params 374 | mnemonics.insert({CMD_SET_COLOR, "SELCOLOR"}); 375 | mnemonics.insert({CMD_UNK_2010, "UNK2010"}); 376 | mnemonics.insert({CMD_TIMER, "TIMER"}); 377 | mnemonics.insert({CMD_DRAW_PIXEL, "PIXEL"}); 378 | //4 params 379 | mnemonics.insert({CMD_CLIP_REGION, "SETCLIP"}); 380 | mnemonics.insert({CMD_SAVE_IMAGE, "SAVEIMG"}); 381 | mnemonics.insert({CMD_SAVE_IMAGE_NEW, "SAVENEWIMG"}); 382 | mnemonics.insert({CMD_UNK_A050, "UNKA050"}); 383 | mnemonics.insert({CMD_UNK_A060, "UNKA060"}); 384 | mnemonics.insert({CMD_DRAW_LINE, "LINE"}); 385 | mnemonics.insert({CMD_DRAW_RECTANGLE, "RECTANGLE"}); 386 | mnemonics.insert({CMD_DRAW_ELLIPSE, "ELLIPSE"}); 387 | mnemonics.insert({CMD_DRAW_SPRITE, "SPRITE"}); 388 | mnemonics.insert({CMD_DRAW_SPRITE_MIRROR, "SPRITEHINV"}); 389 | //6 params 390 | mnemonics.insert({CMD_UNK_B600, "UNKB600"}); 391 | } 392 | 393 | 394 | std::string SCRANTIC::TTMFile::getMnemoic(SCRANTIC::Command c) { 395 | std::string result = mnemonics[c.opcode]; 396 | 397 | int count = getParamCount(c.opcode); 398 | 399 | if (!count) { 400 | return result; 401 | } 402 | 403 | if (count == 0xF) { 404 | return result + " " + c.name; 405 | } 406 | 407 | for (int i = 0; i < count; ++i) { 408 | std::string data = hexToString(c.data.at(i), std::hex, 4); 409 | result += " " + data; 410 | } 411 | 412 | return result; 413 | } 414 | 415 | u16 SCRANTIC::TTMFile::getOpcodeFromMnemonic(std::string &mnemonic) { 416 | for (auto it = mnemonics.begin(); it != mnemonics.end(); ++it) { 417 | if (it->second == mnemonic) { 418 | return it->first; 419 | } 420 | } 421 | return 0; 422 | } 423 | 424 | 425 | int SCRANTIC::TTMFile::getParamCount(u16 opcode) { 426 | switch (opcode) { 427 | case CMD_CLEAR_IMGSLOT: 428 | case CMD_PURGE: 429 | case CMD_UPDATE: 430 | return 0; 431 | case CMD_DELAY: 432 | case CMD_SEL_SLOT_IMG: 433 | case CMD_SEL_SLOT_PAL: 434 | case CMD_SET_SCENE_LABEL: 435 | case CMD_SET_SCENE: 436 | case CMD_UNK_1120: 437 | case CMD_JMP_SCENE: 438 | case CMD_CLEAR_RENDERER: 439 | case CMD_PLAY_SOUND: 440 | return 1; 441 | case CMD_LOAD_SCREEN: 442 | case CMD_LOAD_BITMAP: 443 | case CMD_LOAD_PALETTE: 444 | return 0xF; 445 | case CMD_SET_COLOR: 446 | case CMD_UNK_2010: 447 | case CMD_TIMER: 448 | case CMD_DRAW_PIXEL: 449 | return 2; 450 | case CMD_CLIP_REGION: 451 | case CMD_SAVE_IMAGE: 452 | case CMD_SAVE_IMAGE_NEW: 453 | case CMD_UNK_A050: 454 | case CMD_UNK_A060: 455 | case CMD_DRAW_LINE: 456 | case CMD_DRAW_RECTANGLE: 457 | case CMD_DRAW_ELLIPSE: 458 | case CMD_DRAW_SPRITE: 459 | case CMD_DRAW_SPRITE_MIRROR: 460 | return 4; 461 | case CMD_UNK_B600: 462 | return 6; 463 | default: 464 | std::cerr << "Unknown command found: " << opcode << std::endl; 465 | return 0; 466 | } 467 | } 468 | -------------------------------------------------------------------------------- /TTMFile.h: -------------------------------------------------------------------------------- 1 | #ifndef TTMFILE_H 2 | #define TTMFILE_H 3 | 4 | #include "CompressedBaseFile.h" 5 | 6 | #include 7 | #include 8 | 9 | namespace SCRANTIC { 10 | 11 | class TTMFile : public CompressedBaseFile 12 | { 13 | private: 14 | std::map mnemonics; 15 | 16 | int getParamCount(u16 opcode); 17 | u16 getOpcodeFromMnemonic(std::string &mnemonic); 18 | 19 | u16 countUpdateInScript(); 20 | 21 | void parseRawScript(); 22 | 23 | void initMnemonics(); 24 | std::string getMnemoic(Command c); 25 | 26 | protected: 27 | u32 verSize; 28 | std::string version; 29 | u32 pagSize; // always 2 ? 30 | u16 pag; 31 | u16 fullTagSize; 32 | u16 magic; // 0x8000 33 | u32 tagSize; 34 | u16 tagCount; 35 | std::map tagList; 36 | v8 rawScript; 37 | std::map > script; 38 | 39 | public: 40 | TTMFile(const std::string &name, v8 &data); 41 | explicit TTMFile(const std::string &filename); 42 | 43 | std::vector getFullScene(u16 num); 44 | std::string getTag(u16 num); 45 | bool hasInit(); 46 | 47 | void saveFile(const std::string &path) override; 48 | v8 repackIntoResource() override; 49 | 50 | }; 51 | 52 | } 53 | 54 | #endif // TTMFILE_H 55 | -------------------------------------------------------------------------------- /TTMPlayer.cpp: -------------------------------------------------------------------------------- 1 | #include "TTMPlayer.h" 2 | 3 | #ifdef WIN32 4 | #include 5 | #else 6 | #include 7 | #endif 8 | 9 | SCRANTIC::TTMPlayer::TTMPlayer(const std::string &ttmName, u16 resNum, u16 scene, i16 repeatCount, RESFile *resFile, BMPFile **BMPs, SDL_Color *pal, SDL_Renderer *rendererContext) 10 | : resNo(resNum), 11 | sceneNo(scene), 12 | originalScene(scene), 13 | repeat(repeatCount), 14 | delay(0), 15 | remainingDelay(0), 16 | waitCount(0), 17 | waitDelay(0), 18 | imgSlot(0), 19 | audioSample(-1), 20 | jumpToScript(-1), 21 | renderer(rendererContext), 22 | clipRegion(false), 23 | alreadySaved(true), 24 | saveNewImage(false), 25 | palette(pal), 26 | maxTicks(0), 27 | selfDestruct(false), 28 | selfDestructActive(false), 29 | saveImage(false), 30 | isDone(false), 31 | toBeKilled(false), 32 | images(BMPs), 33 | res(resFile), 34 | screen(""), 35 | ttm(NULL), 36 | savedImage(NULL), 37 | fg(NULL) { 38 | 39 | for (int i = 0; i < MAX_IMAGES; ++i) { 40 | oldImages[i] = images[i]; 41 | } 42 | 43 | ttm = static_cast(res->getResource(ttmName)); 44 | 45 | if (!ttm) { 46 | return; 47 | } 48 | 49 | script = ttm->getFullScene(scene); 50 | if (script.size()) { 51 | scriptPos = script.begin(); 52 | } 53 | 54 | 55 | name = ttm->filename + " - " + ttm->getTag(scene); 56 | 57 | if (name == "MEANWHIL.TTM - quarky watch") { 58 | for (auto &c : script) { 59 | if (c.opcode == CMD_SEL_SLOT_IMG) { 60 | c.data[0] = 7; 61 | } 62 | if ((c.opcode == CMD_DRAW_SPRITE) || (c.opcode == CMD_DRAW_SPRITE_MIRROR)) { 63 | c.data[3] = 7; 64 | } 65 | } 66 | } 67 | 68 | savedImage = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT); 69 | fg = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT); 70 | SDL_SetTextureBlendMode(savedImage, SDL_BLENDMODE_BLEND); 71 | SDL_SetTextureBlendMode(fg, SDL_BLENDMODE_BLEND); 72 | 73 | saveRect = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT }; 74 | 75 | if (repeat < 0) { 76 | maxTicks = -DELAY_MS*repeat; 77 | repeat = 0; 78 | selfDestructActive = true; 79 | } 80 | 81 | } 82 | 83 | SCRANTIC::TTMPlayer::~TTMPlayer() { 84 | SDL_DestroyTexture(savedImage); 85 | SDL_DestroyTexture(fg); 86 | } 87 | 88 | u16 SCRANTIC::TTMPlayer::getRemainigDelay(u32 ticks) { 89 | if (selfDestructActive) { 90 | maxTicks -= ticks; 91 | 92 | if (maxTicks < 1) { 93 | selfDestruct = true; 94 | } 95 | } 96 | 97 | if (ticks < remainingDelay) { 98 | remainingDelay -= ticks; 99 | } else { 100 | remainingDelay = 0; 101 | } 102 | 103 | return remainingDelay; 104 | } 105 | 106 | void SCRANTIC::TTMPlayer::advanceScript() { 107 | if (toBeKilled) { 108 | isDone = true; 109 | return; 110 | } 111 | 112 | if (waitCount) { 113 | remainingDelay = waitDelay/DELAY_MS; 114 | waitCount--; 115 | return; 116 | } 117 | 118 | remainingDelay = delay; 119 | 120 | if ((jumpToScript >= 0) && !selfDestruct) { 121 | if (jumpToScript == sceneNo) { 122 | scriptPos = script.begin(); 123 | } else { 124 | std::cout << "Jump to different sceneNo! From " << sceneNo << " to Scene " << jumpToScript << std::endl; 125 | sceneNo = jumpToScript; 126 | script.clear(); 127 | script = ttm->getFullScene(sceneNo); 128 | if (script.size()) { 129 | scriptPos = script.begin(); 130 | } 131 | 132 | name = ttm->filename + " - " + ttm->getTag(sceneNo); 133 | } 134 | 135 | jumpToScript = -1; 136 | } 137 | 138 | 139 | if (scriptPos == script.end()) { 140 | if (repeat) { 141 | --repeat; 142 | scriptPos = script.begin(); 143 | } else if (selfDestructActive && !selfDestruct) { 144 | scriptPos = script.begin(); 145 | } else { 146 | isDone = true; 147 | return; 148 | } 149 | } 150 | 151 | Command cmd; 152 | SceneItem item; 153 | 154 | bool stop = false; 155 | audioSample = -1; 156 | saveImage = false; 157 | saveNewImage = false; 158 | screen = ""; 159 | 160 | for (; scriptPos != script.end(); ++scriptPos) { 161 | cmd = (*scriptPos); 162 | 163 | switch (cmd.opcode) { 164 | case CMD_PURGE: 165 | clipRegion = false; 166 | break; 167 | 168 | case CMD_UPDATE: 169 | items.splice(items.end(), queuedItems); 170 | stop = true; 171 | break; 172 | 173 | case CMD_DELAY: 174 | delay = cmd.data.at(0) * DELAY_MS; 175 | remainingDelay = delay; 176 | break; 177 | 178 | case CMD_SEL_SLOT_IMG: 179 | imgSlot = cmd.data.at(0); 180 | break; 181 | 182 | case CMD_SEL_SLOT_PAL: 183 | // ignored - there is only one palette... 184 | //palSlot = cmd.data.at(0); 185 | break; 186 | 187 | case CMD_CLEAR_IMGSLOT: 188 | images[imgSlot] = oldImages[imgSlot]; 189 | break; 190 | 191 | case CMD_SET_SCENE: 192 | case CMD_SET_SCENE_LABEL: 193 | std::cout << "TTM Scene: " << cmd.name << std::endl; 194 | break; 195 | 196 | case CMD_JMP_SCENE: 197 | jumpToScript = cmd.data.at(0); 198 | break; 199 | 200 | case CMD_TIMER: 201 | waitCount = cmd.data.at(0); 202 | waitDelay = cmd.data.at(1); 203 | break; 204 | 205 | case CMD_CLIP_REGION: 206 | clipRect.x = (i16)cmd.data.at(0); 207 | clipRect.y = (i16)cmd.data.at(1); 208 | clipRect.w = cmd.data.at(2) - clipRect.x; 209 | clipRect.h = cmd.data.at(3) - clipRect.y; 210 | if (clipRect.x + clipRect.w >= SCREEN_WIDTH) { 211 | clipRect.w = SCREEN_WIDTH - clipRect.x; 212 | } 213 | if (clipRect.y + clipRect.h >= SCREEN_HEIGHT) { 214 | clipRect.h = SCREEN_HEIGHT - clipRect.y; 215 | } 216 | clipRegion = true; 217 | break; 218 | 219 | case CMD_SAVE_IMAGE_NEW: 220 | saveNewImage = true; 221 | case CMD_SAVE_IMAGE: 222 | alreadySaved = false; 223 | saveImage = true; 224 | saveRect.x = (i16)cmd.data.at(0); 225 | saveRect.y = (i16)cmd.data.at(1); 226 | saveRect.w = cmd.data.at(2); 227 | saveRect.h = cmd.data.at(3); 228 | items.splice(items.end(), queuedItems); 229 | renderForeground(); 230 | break; 231 | 232 | case CMD_DRAW_PIXEL: 233 | item.dest.x = (i16)cmd.data.at(0); 234 | item.dest.y = (i16)cmd.data.at(1); 235 | item.dest.w = 2; 236 | item.dest.h = 2; 237 | item.color = currentColor; 238 | item.itemType = RENDERITEM_RECT; 239 | queuedItems.push_back(item); 240 | break; 241 | 242 | case CMD_DRAW_LINE: 243 | item.dest.x = (i16)cmd.data.at(0); 244 | item.dest.y = (i16)cmd.data.at(1); 245 | item.dest.w = (i16)cmd.data.at(2); 246 | item.dest.h = (i16)cmd.data.at(3); 247 | item.color = currentColor; 248 | item.itemType = RENDERITEM_LINE; 249 | queuedItems.push_back(item); 250 | break; 251 | 252 | case CMD_SET_COLOR: 253 | currentColor = std::make_pair(cmd.data.at(0), cmd.data.at(1)); 254 | break; 255 | 256 | case CMD_DRAW_RECTANGLE: 257 | item.dest.x = (i16)cmd.data.at(0); 258 | item.dest.y = (i16)cmd.data.at(1); 259 | item.dest.w = cmd.data.at(2); 260 | item.dest.h = cmd.data.at(3); 261 | item.color = currentColor; 262 | item.itemType = RENDERITEM_RECT; 263 | queuedItems.push_back(item); 264 | break; 265 | case CMD_DRAW_ELLIPSE: 266 | item.dest.w = cmd.data.at(2)/2; 267 | item.dest.h = cmd.data.at(3)/2; 268 | item.dest.x = (i16)cmd.data.at(0) + item.dest.w; 269 | item.dest.y = (i16)cmd.data.at(1) + item.dest.h; 270 | item.color = currentColor; 271 | item.itemType = RENDERITEM_ELLIPSE; 272 | queuedItems.push_back(item); 273 | break; 274 | 275 | case CMD_DRAW_SPRITE_MIRROR: 276 | case CMD_DRAW_SPRITE: 277 | if (images[cmd.data.at(3)] != NULL) { 278 | item.dest.x = (i16)cmd.data.at(0); 279 | item.dest.y = (i16)cmd.data.at(1); 280 | item.num = cmd.data.at(2); 281 | item.tex = images[cmd.data.at(3)]->getImage(renderer, item.num, item.src); 282 | if (item.tex == NULL) { 283 | std::cerr << name << ": Error tried to access non existing sprite number!" << std::endl; 284 | std::cout << ">>> TTM Command: " << SCRANTIC::BaseFile::commandToString(cmd) << std::endl; 285 | break; 286 | } 287 | item.dest.w = item.src.w; 288 | item.dest.h = item.src.h; 289 | item.flags = 0; 290 | if (cmd.opcode == CMD_DRAW_SPRITE_MIRROR) { 291 | item.flags |= RENDERFLAG_MIRROR; 292 | } 293 | item.itemType = RENDERITEM_SPRITE; 294 | queuedItems.push_back(item); 295 | } else { 296 | std::cerr << name << ": Error tried to access unloaded image slot!" << std::endl; 297 | std::cout << ">>> TTM Command: " << SCRANTIC::BaseFile::commandToString(cmd) << std::endl; 298 | } 299 | break; 300 | 301 | case CMD_CLEAR_RENDERER: 302 | items.clear(); 303 | break; 304 | 305 | case CMD_PLAY_SOUND: 306 | if ((cmd.data.at(0) < 1) || (cmd.data.at(0) > MAX_AUDIO)) { 307 | break; 308 | } 309 | audioSample = cmd.data.at(0) - 1; 310 | break; 311 | 312 | case CMD_LOAD_SCREEN: 313 | screen = cmd.name; 314 | items.clear(); 315 | break; 316 | 317 | case CMD_LOAD_BITMAP: 318 | images[imgSlot] = static_cast(res->getResource(cmd.name)); 319 | break; 320 | 321 | case CMD_LOAD_PALETTE: 322 | // ignored - there is only one palette... 323 | break; 324 | 325 | default: 326 | std::cout << "TTM Command: " << SCRANTIC::BaseFile::commandToString(cmd) << std::endl; 327 | break; 328 | } 329 | 330 | if (stop) { 331 | break; 332 | } 333 | } 334 | 335 | if (scriptPos == script.end()) { 336 | if (repeat) { 337 | --repeat; 338 | scriptPos = script.begin(); 339 | return; 340 | } else { 341 | if (jumpToScript == -1) { 342 | isDone = true; 343 | } 344 | return; 345 | } 346 | } 347 | 348 | ++scriptPos; 349 | 350 | return; 351 | } 352 | 353 | 354 | void SCRANTIC::TTMPlayer::renderForeground(i16 shiftX, i16 shiftY) { 355 | SDL_SetRenderTarget(renderer, fg); 356 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); 357 | SDL_RenderClear(renderer); 358 | 359 | SDL_Rect target; 360 | 361 | Uint32 c1, c2; 362 | 363 | for (auto item : items) { 364 | switch (item.itemType) { 365 | case RENDERITEM_SPRITE: 366 | target = { item.dest.x + shiftX, item.dest.y + shiftY, item.dest.w, item.dest.h }; 367 | SDL_RenderCopyEx(renderer, item.tex, &item.src, &target, 0, NULL, (SDL_RendererFlip)item.flags); 368 | break; 369 | 370 | case RENDERITEM_LINE: 371 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 372 | SDL_RenderDrawLine(renderer, item.dest.x + shiftX, item.dest.y + shiftY, item.dest.w + shiftX, item.dest.h + shiftY); 373 | break; 374 | 375 | case RENDERITEM_RECT: 376 | target = { item.dest.x + shiftX, item.dest.y + shiftY, item.dest.w, item.dest.h }; 377 | SDL_SetRenderDrawColor(renderer, palette[item.color.first].r, palette[item.color.first].g, palette[item.color.first].b, 255); 378 | SDL_RenderDrawRect(renderer, &target); 379 | SDL_SetRenderDrawColor(renderer, palette[item.color.second].r, palette[item.color.second].g, palette[item.color.second].b, 255); 380 | SDL_RenderFillRect(renderer, &target); 381 | break; 382 | 383 | case RENDERITEM_ELLIPSE: 384 | c1 = palette[item.color.first].r * 0x10000 385 | + palette[item.color.first].g * 0x100 386 | + palette[item.color.first].b + 0xFF000000; 387 | c2 = palette[item.color.second].r * 0x10000 388 | + palette[item.color.second].g * 0x100 389 | + palette[item.color.second].b + 0xFF000000; 390 | filledEllipseColor(renderer, item.dest.x + shiftX, item.dest.y + shiftY, item.dest.w, item.dest.h, c2); 391 | ellipseColor(renderer, item.dest.x + shiftX, item.dest.y + shiftY, item.dest.w, item.dest.h, c1); 392 | break; 393 | 394 | default: 395 | std::cerr << "ERROR: Renderer: Unkown render item type!" << std::endl; 396 | break; 397 | } 398 | } 399 | 400 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 401 | 402 | // save foreground rect 403 | if (!alreadySaved) { 404 | if (saveImage) { 405 | target = { saveRect.x + shiftX, saveRect.y + shiftY, saveRect.w, saveRect.h }; 406 | SDL_SetRenderTarget(renderer, savedImage); 407 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); 408 | SDL_RenderClear(renderer); 409 | SDL_RenderCopy(renderer, fg, &saveRect, &target); 410 | alreadySaved = true; 411 | } 412 | } 413 | 414 | SDL_SetRenderTarget(renderer, fg); 415 | } 416 | 417 | u8 SCRANTIC::TTMPlayer::needsSave() { 418 | if (!saveImage) { 419 | return SAVE_NOSAVE; 420 | } else if (saveNewImage) { 421 | return SAVE_NEWIMAGE; 422 | } else { 423 | return SAVE_IMAGE; 424 | } 425 | } 426 | -------------------------------------------------------------------------------- /TTMPlayer.h: -------------------------------------------------------------------------------- 1 | #ifndef TTMPLAYER_H 2 | #define TTMPLAYER_H 3 | 4 | #include "TTMFile.h" 5 | #include "BMPFile.h" 6 | #include "RESFile.h" 7 | #include "ADSFile.h" 8 | 9 | #include "defines.h" 10 | 11 | #include 12 | 13 | #ifdef WIN32 14 | #include 15 | #else 16 | #include 17 | #endif 18 | 19 | namespace SCRANTIC { 20 | 21 | struct SceneItem { 22 | SDL_Texture *tex; 23 | SDL_Rect src; 24 | SDL_Rect dest; 25 | u16 num; 26 | u8 flags; 27 | std::pair color; 28 | i8 itemType; 29 | }; 30 | 31 | class TTMPlayer { 32 | 33 | protected: 34 | std::vector script; 35 | std::vector::iterator scriptPos; 36 | 37 | std::list items; 38 | std::list queuedItems; 39 | std::list::iterator itemPos; 40 | 41 | SDL_Color *palette; 42 | std::pair currentColor; 43 | 44 | std::string name; 45 | 46 | u16 resNo; 47 | u16 sceneNo; 48 | u16 originalScene; 49 | u16 delay; 50 | u16 remainingDelay; 51 | u16 waitCount; 52 | u16 waitDelay; 53 | u16 imgSlot; 54 | i16 audioSample; 55 | i16 repeat; 56 | i16 maxTicks; 57 | 58 | i32 jumpToScript; 59 | 60 | SDL_Renderer *renderer; 61 | 62 | bool clipRegion; 63 | bool alreadySaved; 64 | bool saveNewImage; 65 | bool saveImage; 66 | bool isDone; 67 | bool toBeKilled; 68 | bool selfDestruct; 69 | bool selfDestructActive; 70 | 71 | SDL_Rect clipRect; 72 | SDL_Rect saveRect; 73 | 74 | BMPFile **images; 75 | BMPFile *oldImages[MAX_IMAGES]; 76 | RESFile *res; 77 | TTMFile *ttm; 78 | 79 | std::string screen; 80 | 81 | public: 82 | u16 getDelay() { return delay; }; 83 | u16 getRemainigDelay(u32 ticks); 84 | SDL_Rect getClipRect() { return clipRect; } 85 | i16 getSample() { i16 tmp = audioSample; audioSample = -1; return tmp; } 86 | std::string getSCRName() { return screen; } 87 | bool isFinished() { return isDone; } 88 | bool isClipped() { return clipRegion; } 89 | u8 needsSave(); 90 | 91 | void kill() { toBeKilled = true; } 92 | u16 getHash() { return SCRANTIC::ADSFile::makeHash(resNo, originalScene); } 93 | 94 | //Needs to be freed 95 | SDL_Texture *savedImage; 96 | SDL_Texture *fg; 97 | 98 | void advanceScript(); 99 | void renderForeground(i16 shiftX = 0, i16 shiftY = 0); 100 | 101 | TTMPlayer(const std::string &ttmName, u16 resNo, u16 scene, i16 repeatCount, RESFile *resFile, BMPFile **images, SDL_Color *pal, SDL_Renderer *rendererContext); 102 | ~TTMPlayer(); 103 | }; 104 | 105 | } 106 | 107 | #endif // TTMPLAYER_H 108 | -------------------------------------------------------------------------------- /VINFile.cpp: -------------------------------------------------------------------------------- 1 | #include "VINFile.h" 2 | 3 | SCRANTIC::VINFile::VINFile(const std::string &name, v8 &data) 4 | : BaseFile(name), 5 | rawData(data.begin(), data.end()) { 6 | } 7 | 8 | SCRANTIC::VINFile::VINFile(const std::string &filename) 9 | : BaseFile(filename), 10 | rawData(BaseFile::readFile(filename)) { 11 | 12 | } 13 | 14 | v8 SCRANTIC::VINFile::repackIntoResource() { 15 | return rawData; 16 | } 17 | 18 | void SCRANTIC::VINFile::saveFile(const std::string &path) { 19 | writeFile(rawData, filename, path); 20 | } 21 | -------------------------------------------------------------------------------- /VINFile.h: -------------------------------------------------------------------------------- 1 | #ifndef VINFILE_H 2 | #define VINFILE_H 3 | 4 | #include "BaseFile.h" 5 | 6 | namespace SCRANTIC { 7 | 8 | class VINFile : public BaseFile 9 | { 10 | protected: 11 | v8 rawData; 12 | 13 | public: 14 | VINFile(const std::string &name, v8 &data); 15 | explicit VINFile(const std::string &filename); 16 | 17 | v8 repackIntoResource() override; 18 | void saveFile(const std::string &path) override; 19 | }; 20 | 21 | } 22 | 23 | #endif // VINFILE_H 24 | -------------------------------------------------------------------------------- /defines.h: -------------------------------------------------------------------------------- 1 | #ifndef DEFINES_H 2 | #define DEFINES_H 3 | 4 | #define SCREEN_WIDTH 640 5 | #define SCREEN_HEIGHT 480 6 | 7 | #define WINDOW_WIDTH 640 8 | #define WINDOW_HEIGHT 480 9 | 10 | #define DELAY_MS 20 11 | 12 | #define ISLAND_RIGHT 0x1 13 | #define ISLAND_LEFT 0x2 14 | #define NO_ISLAND 0x0 15 | 16 | /*relative: 17 | 0 0 trunk 18 | -77 -26 top 19 | -154 131 island 20 | -172 158 wave left 21 | -78 171 wave middle 22 | 78 155 wave right 23 | -193 155 large island 24 | -292 180 stone 25 | -313 192 wave stone 26 | -209 266 large wave left 27 | -75 208 large wave middle (might be off) 28 | 116 175 large wave right 29 | 73 118 raft 30 | -46 131 top shadow*/ 31 | 32 | #define TOP_X -77 33 | #define TOP_Y -26 34 | #define ISLAND_X -154 35 | #define ISLAND_Y 131 36 | #define WAVE_LEFT_X -172 37 | #define WAVE_LEFT_Y 158 38 | #define WAVE_MID_X -78 39 | #define WAVE_MID_Y 171 40 | #define WAVE_RIGHT_X 78 41 | #define WAVE_RIGHT_Y 155 42 | #define STONE_X -292 43 | #define STONE_Y 180 44 | #define WAVE_STONE_X -313 45 | #define WAVE_STONE_Y 192 46 | #define L_ISLAND_X -193 47 | #define L_ISLAND_Y 155 48 | #define WAVE_L_LEFT_X -209 49 | #define WAVE_L_LEFT_Y 175 50 | #define WAVE_L_MID_X -75 51 | #define WAVE_L_MID_Y 208 52 | #define WAVE_L_RIGHT_X 116 53 | #define WAVE_L_RIGHT_Y 175 54 | #define RAFT_X 73 55 | #define RAFT_Y 118 56 | #define TOP_SHADOW_X -46 57 | #define TOP_SHADOW_Y 131 58 | 59 | #define ISLAND_RIGHT_X 443 60 | #define ISLAND_RIGHT_Y 168 61 | 62 | #define ISLAND_LEFT_X 171 63 | #define ISLAND_LEFT_Y 178 64 | 65 | #define ISLAND_TEMP_X 442 66 | #define ISLAND_TEMP_Y 148 67 | 68 | #define ISLAND2_X 170 69 | #define ISLAND2_Y 148 70 | 71 | #define SPRITE_ISLAND 0 72 | #define SPRITE_L_ISLAND 1 73 | #define SPRITE_STONE 2 74 | #define SPRITE_WAVE_LEFT 3 75 | #define SPRITE_WAVE_MID 6 76 | #define SPRITE_WAVE_RIGHT 9 77 | #define SPRITE_TOP 12 78 | #define SPRITE_TRUNK 13 79 | #define SPRITE_TOP_SHADOW 14 80 | #define SPRITE_WAVE_L_LEFT 30 81 | #define SPRITE_WAVE_L_MID 33 82 | #define SPRITE_WAVE_L_RIGHT 36 83 | #define SPRITE_WAVE_STONE 39 84 | 85 | #define RENDERITEM_SPRITE 0 86 | #define RENDERITEM_LINE 1 87 | #define RENDERITEM_RECT 2 88 | #define RENDERITEM_ELLIPSE 3 89 | #define RENDERITEM_NONE -1 90 | 91 | #define RENDERFLAG_MIRROR 0x1 92 | 93 | #define SAVE_NOSAVE 0 94 | #define SAVE_IMAGE 1 95 | #define SAVE_NEWIMAGE 2 96 | 97 | #define MAX_IMAGES 10 98 | #define MAX_AUDIO 24 99 | 100 | #endif // DEFINES_H 101 | -------------------------------------------------------------------------------- /font.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bailli/Johnny/363f10cbe579423ccf156f8cc9d458c90e6d3187/font.ttf -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "RESFile.h" 4 | #include "SCRFile.h" 5 | #include "PALFile.h" 6 | #include "BMPFile.h" 7 | #include "Robinson.h" 8 | 9 | #include "CommandlineParser.h" 10 | #include "RIFFPlayer.h" 11 | 12 | #ifdef WIN32 13 | #include 14 | #include 15 | #else 16 | #include 17 | #include 18 | #endif 19 | 20 | #define ticksPerFrame 1000/30 21 | 22 | using namespace std; 23 | 24 | SDL_Window *g_Mainwindow = NULL; 25 | SDL_Renderer *g_Renderer = NULL; 26 | TTF_Font *g_Font = NULL; 27 | std::string g_path = ""; 28 | bool g_readUnpackedRes = false; 29 | 30 | void cleanup() { 31 | if (g_Renderer != NULL) { 32 | SDL_DestroyRenderer(g_Renderer); 33 | } 34 | 35 | if (g_Mainwindow != NULL) { 36 | SDL_DestroyWindow(g_Mainwindow); 37 | } 38 | 39 | TTF_CloseFont(g_Font); 40 | 41 | Mix_Quit(); 42 | TTF_Quit(); 43 | SDL_Quit(); 44 | } 45 | 46 | bool init() { 47 | //Initialize SDL 48 | if(SDL_Init(SDL_INIT_VIDEO) < 0) { 49 | std::cerr << "SDL could not initialize VIDEO! SDL_Error: " << SDL_GetError() << std::endl; 50 | return false; 51 | } 52 | 53 | if (TTF_Init() < 0) { 54 | std::cerr << "SDL could not initialize TTF! SDL_Error: " << SDL_GetError() << std::endl; 55 | return false; 56 | } 57 | 58 | g_Font = TTF_OpenFont("font.ttf", 14); 59 | 60 | if (SDL_Init(SDL_INIT_AUDIO) < 0) { 61 | std::cerr << "SDL could not initialize AUDIO! SDL_Error: " << SDL_GetError() << std::endl; 62 | return false; 63 | } 64 | 65 | if (Mix_OpenAudio(11025, AUDIO_U8, 1, 2048) < 0) { 66 | printf( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() ); 67 | return false; 68 | } 69 | 70 | //Create window 71 | g_Mainwindow = SDL_CreateWindow( 72 | "Johnny's World", 73 | SDL_WINDOWPOS_UNDEFINED, 74 | SDL_WINDOWPOS_UNDEFINED, 75 | WINDOW_WIDTH, 76 | WINDOW_HEIGHT, 77 | SDL_WINDOW_SHOWN 78 | ); 79 | 80 | if (g_Mainwindow == NULL) { 81 | std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl; 82 | cleanup(); 83 | return false; 84 | } 85 | 86 | g_Renderer = SDL_CreateRenderer(g_Mainwindow, -1, 0); 87 | if (g_Renderer == NULL) { 88 | std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl; 89 | cleanup(); 90 | return false; 91 | } 92 | 93 | // Set size of renderer to the same as window 94 | SDL_RenderSetLogicalSize(g_Renderer, SCREEN_WIDTH, SCREEN_HEIGHT); 95 | 96 | return true; 97 | } 98 | 99 | bool handleCommandline(char **begin, char **end) { 100 | 101 | CommandlineParser commandlineParser(begin, end); 102 | 103 | std::string path = ""; 104 | std::string outputPath = ""; 105 | std::string prepackedPath = ""; 106 | 107 | if (commandlineParser.cmdOptionExists("--resourcePath")) { 108 | path = commandlineParser.getCmdOption("--resourcePath"); 109 | if (path.substr(path.size() - 1) != "/") { 110 | path += "/"; 111 | } 112 | g_path = path; 113 | } 114 | 115 | if (commandlineParser.cmdOptionExists("--outputPath")) { 116 | outputPath = commandlineParser.getCmdOption("--outputPath"); 117 | if (outputPath.substr(outputPath.size() - 1) != "/") { 118 | outputPath += "/"; 119 | } 120 | } 121 | 122 | if (commandlineParser.cmdOptionExists("--prepackedPath")) { 123 | prepackedPath = commandlineParser.getCmdOption("--prepackedPath"); 124 | if (prepackedPath.substr(prepackedPath.size() - 1) != "/") { 125 | prepackedPath += "/"; 126 | } 127 | } 128 | 129 | if (commandlineParser.cmdOptionExists("--unpack")) { 130 | bool onlyFiles = commandlineParser.cmdOptionExists("--onlyFiles"); 131 | SCRANTIC::RESFile res(path + "RESOURCE.MAP", false); 132 | res.unpackResources(outputPath, onlyFiles); 133 | SCRANTIC::RIFFPlayer::extractRIFFFiles(path + "SCRANTIC.SCR", outputPath + "RIFF/", true); 134 | return true; 135 | } 136 | 137 | if (commandlineParser.cmdOptionExists("--repack")) { 138 | SCRANTIC::RESFile res(path + "RESOURCE.MAP", true); 139 | res.repackResources(outputPath, prepackedPath); 140 | return true; 141 | } 142 | 143 | if (commandlineParser.cmdOptionExists("--unpackedResources")) { 144 | std::cout << "Unpacked resources will be read from " << path << std::endl; 145 | g_readUnpackedRes = true; 146 | } 147 | 148 | return false; 149 | } 150 | 151 | int main(int argc, char **argv) { 152 | if (handleCommandline(argv, argv + argc)) { 153 | return 0; 154 | } 155 | 156 | if (!init()) { 157 | std::cerr << "Error in init()!" << std::endl; 158 | return 1; 159 | } 160 | 161 | cout << "Hello Johnny's World!" << endl; 162 | SCRANTIC::Robinson *crusoe = new SCRANTIC::Robinson(g_path, g_readUnpackedRes); 163 | 164 | crusoe->initRenderer(g_Renderer, g_Font); 165 | 166 | crusoe->loadMovie("VISITOR.ADS", 3); 167 | crusoe->startMovie(); 168 | 169 | bool quit = false; 170 | SDL_Event e; 171 | u16 delay; 172 | Uint32 ticks, newTicks, frameTicks, waitTicks; 173 | ticks = SDL_GetTicks(); 174 | bool waiting = false; 175 | bool pause = false; 176 | 177 | while (!quit) { 178 | newTicks = SDL_GetTicks(); 179 | frameTicks = newTicks - ticks; 180 | 181 | if (!waiting) { 182 | crusoe->advanceScripts(); 183 | if (!crusoe->isMovieRunning()) { 184 | crusoe->displayMenu(true); 185 | } 186 | delay = crusoe->getCurrentDelay(); 187 | if (delay == 0) { 188 | delay = 100; 189 | } 190 | } 191 | 192 | if (!pause) { 193 | if (delay > frameTicks) { 194 | delay -= frameTicks; 195 | } else { 196 | delay = 0; 197 | } 198 | } 199 | 200 | if (frameTicks < ticksPerFrame) { 201 | waitTicks = ticksPerFrame - frameTicks; 202 | } else { 203 | waitTicks = 0; 204 | } 205 | 206 | ticks = newTicks; 207 | waiting = (delay > waitTicks); 208 | SDL_Delay(waitTicks); 209 | crusoe->render(); 210 | SDL_RenderPresent(g_Renderer); 211 | 212 | while(SDL_PollEvent(&e) != 0) { 213 | switch(e.type) { 214 | case SDL_KEYDOWN: 215 | //case SDL_KEYUP: 216 | if (e.key.keysym.sym == SDLK_SPACE && !e.key.repeat) { 217 | pause = !pause; 218 | } else if (e.key.keysym.sym == SDLK_ESCAPE && !e.key.repeat) { 219 | if (crusoe->isMenuOpen()) { 220 | crusoe->displayMenu(false); 221 | pause = false; 222 | } else { 223 | quit = true; 224 | } 225 | } else if (!e.key.repeat && crusoe->isMenuOpen()) { 226 | if (crusoe->navigateMenu(e.key.keysym.sym)) { 227 | pause = false; 228 | } 229 | } else if (e.key.keysym.sym == SDLK_RETURN && !e.key.repeat) { 230 | pause = true; 231 | crusoe->displayMenu(true); 232 | } 233 | break; 234 | case SDL_QUIT: 235 | quit = true; 236 | break; 237 | } 238 | } 239 | } 240 | 241 | delete crusoe; 242 | cleanup(); 243 | 244 | return 0; 245 | } 246 | 247 | -------------------------------------------------------------------------------- /types.h: -------------------------------------------------------------------------------- 1 | #ifndef TYPES_H 2 | #define TYPES_H 3 | 4 | #include 5 | #include 6 | 7 | typedef std::int8_t i8; 8 | typedef std::int16_t i16; 9 | typedef std::int32_t i32; 10 | typedef std::uint8_t u8; 11 | typedef std::uint16_t u16; 12 | typedef std::uint32_t u32; 13 | 14 | typedef std::vector v8; 15 | typedef std::vector v16; 16 | typedef std::vector v32; 17 | 18 | #endif // TYPES_H 19 | --------------------------------------------------------------------------------