Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoaderTR2.cpp 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. /*!
  2. * \file src/loader/LoaderTR2.cpp
  3. * \brief TR2 level file loader
  4. *
  5. * \author xythobuz
  6. */
  7. #include <vector>
  8. #include "glm/glm.hpp"
  9. #include "glm/gtc/matrix_transform.hpp"
  10. #include "global.h"
  11. #include "Game.h"
  12. #include "Log.h"
  13. #include "Mesh.h"
  14. #include "Room.h"
  15. #include "SoundManager.h"
  16. #include "TextureManager.h"
  17. #include "World.h"
  18. #include "system/Sound.h"
  19. #include "utils/pixel.h"
  20. #include "loader/LoaderTR2.h"
  21. LoaderTR2::LoaderTR2() {
  22. }
  23. LoaderTR2::~LoaderTR2() {
  24. }
  25. int LoaderTR2::load(std::string f) {
  26. if (file.open(f) != 0) {
  27. return 1; // Could not open file
  28. }
  29. if (file.readU32() != 0x2D) {
  30. return 2; // Not a TR2 level?!
  31. }
  32. loadPaletteTextiles();
  33. file.seek(file.tell() + 4); // Unused value?
  34. loadRooms();
  35. loadFloorData();
  36. loadMeshes();
  37. loadMoveables();
  38. loadStaticMeshes();
  39. loadTextures();
  40. loadSprites();
  41. loadCameras();
  42. loadSoundSources();
  43. loadBoxesOverlapsZones();
  44. loadAnimatedTextures();
  45. loadItems();
  46. file.seek(file.tell() + 8192); // Skip Light map, only for 8bit coloring
  47. loadCinematicFrames();
  48. loadDemoData();
  49. loadSoundMap();
  50. loadSoundDetails();
  51. loadSampleIndices();
  52. loadExternalSoundFile(f);
  53. return 0; // TODO Not finished with implementation!
  54. }
  55. // ---- Textures ----
  56. void LoaderTR2::loadPaletteTextiles() {
  57. file.seek(file.tell() + 768); // Skip 8bit palette, 256 * 3 bytes
  58. // Read the 16bit palette, 256 * 4 bytes, RGBA, A unused
  59. for (auto& x : palette)
  60. x = file.readU32();
  61. uint32_t numTextiles = file.readU32();
  62. file.seek(file.tell() + (numTextiles * 256 * 256)); // Skip 8bit textiles
  63. // Read the 16bit textiles, numTextiles * 256 * 256 * 2 bytes
  64. for (unsigned int i = 0; i < numTextiles; i++) {
  65. std::array<uint8_t, 256 * 256 * 2> arr;
  66. for (auto& x : arr) {
  67. x = file.readU8();
  68. }
  69. // Convert 16bit textile to 32bit textile
  70. unsigned char* img = argb16to32(&arr[0], 256, 256);
  71. int r = getTextureManager().loadBufferSlot(img, 256, 256,
  72. TextureManager::ColorMode::ARGB, 32,
  73. TextureManager::TextureStorage::GAME, i);
  74. assert(r >= 0); //! \fixme properly handle error when texture could not be loaded!
  75. delete [] img;
  76. }
  77. if (numTextiles > 0)
  78. getLog() << "LoaderTR2: Found " << numTextiles << " Textures!" << Log::endl;
  79. else
  80. getLog() << "LoaderTR2: No Textures in this level?!" << Log::endl;
  81. }
  82. void LoaderTR2::loadTextures() {
  83. uint32_t numObjectTextures = file.readU32();
  84. for (unsigned int o = 0; o < numObjectTextures; o++) {
  85. // 0 means that a texture is all-opaque, and that transparency
  86. // information is ignored.
  87. // 1 means that transparency information is used. In 8-bit color,
  88. // index 0 is the transparent color, while in 16-bit color, the
  89. // top bit (0x8000) is the alpha channel (1 = opaque, 0 = transparent)
  90. uint16_t attribute = file.readU16();
  91. // Index into the textile list
  92. uint16_t tile = file.readU16();
  93. TextureTile* t = new TextureTile(attribute, tile);
  94. // The four corner vertices of the texture
  95. // The Pixel values are the actual coordinates of the vertexs pixel
  96. // The Coordinate values depend on where the other vertices are in
  97. // the object texture. And if the object texture is used to specify
  98. // a triangle, then the fourth vertexs values will all be zero
  99. // Coordinate is 1 if Pixel is the low val, 255 if high val in object texture
  100. for (int i = 0; i < 4; i++) {
  101. uint8_t xCoordinate = file.readU8();
  102. uint8_t xPixel = file.readU8();
  103. uint8_t yCoordinate = file.readU8();
  104. uint8_t yPixel = file.readU8();
  105. assert((xCoordinate == 1) || (xCoordinate == 255) || (xCoordinate == 0));
  106. assert((yCoordinate == 1) || (yCoordinate == 255) || (yCoordinate == 0));
  107. t->add(new TextureTileVertex(xCoordinate, xPixel, yCoordinate, yPixel));
  108. }
  109. getTextureManager().addTile(t);
  110. }
  111. if (numObjectTextures > 0)
  112. getLog() << "LoaderTR2: Found " << numObjectTextures << " Textiles!" << Log::endl;
  113. else
  114. getLog() << "LoaderTR2: No Textiles in this level?!" << Log::endl;
  115. }
  116. void LoaderTR2::loadAnimatedTextures() {
  117. uint32_t numWords = file.readU32() - 1;
  118. uint16_t numAnimatedTextures = file.readU16();
  119. std::vector<uint16_t> animatedTextures;
  120. for (unsigned int a = 0; a < numWords; a++) {
  121. animatedTextures.push_back(file.readU16());
  122. }
  123. int pos = 0;
  124. for (unsigned int a = 0; a < numAnimatedTextures; a++) {
  125. int count = animatedTextures.at(pos) + 1;
  126. if ((pos + count) >= numWords) {
  127. getLog() << "LoaderTR2: Invalid AnimatedTextures ("
  128. << pos + count << " >= " << numWords << ")!" << Log::endl;
  129. return;
  130. }
  131. for (int i = 0; i < count; i++) {
  132. getTextureManager().addAnimatedTile(a, animatedTextures.at(pos + i + 1));
  133. }
  134. pos += count + 1;
  135. }
  136. if ((numAnimatedTextures > 0) || (numWords > 0))
  137. getLog() << "LoaderTR2: Found " << numAnimatedTextures << " Animated Textures!" << Log::endl;
  138. else
  139. getLog() << "LoaderTR2: No Animated Textures in this level?!" << Log::endl;
  140. if (pos != numWords)
  141. getLog() << "LoaderTR2: Extra bytes at end of AnimatedTextures?!" << Log::endl;
  142. }
  143. // ---- Rooms ----
  144. void LoaderTR2::loadRooms() {
  145. uint16_t numRooms = file.readU16();
  146. for (unsigned int i = 0; i < numRooms; i++) {
  147. // Room Header
  148. int32_t xOffset = file.read32();
  149. int32_t zOffset = file.read32();
  150. int32_t yBottom = file.read32(); // lowest point == largest y value
  151. int32_t yTop = file.read32(); // highest point == smallest y value
  152. // Number of data words (2 bytes) to follow
  153. uint32_t dataToFollow = file.readU32();
  154. uint16_t numVertices = file.readU16();
  155. std::vector<glm::vec3> vertices;
  156. float bbox[2][3] = {
  157. { 0.0f, 0.0f, 0.0f },
  158. { 0.0f, 0.0f, 0.0f }
  159. };
  160. for (unsigned int v = 0; v < numVertices; v++) {
  161. // Vertex coordinates, relative to x/zOffset
  162. int32_t x = file.read16() + xOffset;
  163. int16_t y = file.read16();
  164. int32_t z = file.read16() + zOffset;
  165. int16_t light1 = file.read16();
  166. // Set of flags for special rendering effects
  167. // 0x8000 - Something to do with water surface?
  168. // 0x4000 - Underwater lighting modulation/movement if seen from above
  169. // 0x2000 - Water/Quicksand surface movement
  170. // 0x0010 - Normal?
  171. uint16_t attributes = file.readU16();
  172. int16_t light2 = file.read16(); // Almost always equal to light1
  173. vertices.emplace_back(x, y, z);
  174. if (v == 0) {
  175. for (int i = 0; i < 2; i++) {
  176. bbox[i][0] = x;
  177. bbox[i][1] = y;
  178. bbox[i][2] = z;
  179. }
  180. } else {
  181. if (x < bbox[0][0])
  182. bbox[0][0] = x;
  183. if (x > bbox[1][0])
  184. bbox[1][0] = x;
  185. if (y < bbox[0][1])
  186. bbox[0][1] = y;
  187. if (y > bbox[1][1])
  188. bbox[1][1] = y;
  189. if (z < bbox[0][2])
  190. bbox[0][2] = z;
  191. if (z > bbox[1][2])
  192. bbox[1][2] = z;
  193. }
  194. }
  195. float pos[3] {
  196. static_cast<float>(xOffset),
  197. 0.0f,
  198. static_cast<float>(zOffset)
  199. };
  200. Room* room = new Room(pos);
  201. bbox[0][0] += pos[0];
  202. bbox[1][0] += pos[0];
  203. bbox[0][2] += pos[2];
  204. bbox[1][2] += pos[2];
  205. room->getBoundingBox().setBoundingBox(bbox[0], bbox[1]);
  206. uint16_t numRectangles = file.readU16();
  207. for (unsigned int r = 0; r < numRectangles; r++) {
  208. // Indices into the vertex list read just before
  209. uint16_t vertex1 = file.readU16();
  210. uint16_t vertex2 = file.readU16();
  211. uint16_t vertex3 = file.readU16();
  212. uint16_t vertex4 = file.readU16();
  213. // Index into the object-texture list
  214. uint16_t texture = file.readU16();
  215. room->getMesh().addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
  216. vertices.at(vertex3), vertices.at(vertex4),
  217. texture);
  218. }
  219. uint16_t numTriangles = file.readU16();
  220. for (unsigned int t = 0; t < numTriangles; t++) {
  221. // Indices into the room vertex list
  222. uint16_t vertex1 = file.readU16();
  223. uint16_t vertex2 = file.readU16();
  224. uint16_t vertex3 = file.readU16();
  225. // Index into the object-texture list
  226. uint16_t texture = file.readU16();
  227. room->getMesh().addTexturedTriangle(vertices.at(vertex1), vertices.at(vertex2),
  228. vertices.at(vertex3), texture);
  229. }
  230. uint16_t numSprites = file.readU16();
  231. for (unsigned int s = 0; s < numSprites; s++) {
  232. uint16_t vertex = file.readU16(); // Index into vertex list
  233. uint16_t sprite = file.readU16(); // Index into sprite list
  234. // TODO store sprites somewhere
  235. }
  236. glm::mat4 transform = glm::translate(glm::mat4(1.0f), glm::vec3(pos[0], pos[1], pos[2]));
  237. room->addAdjacentRoom(i); // Always set room itself as first
  238. uint16_t numPortals = file.readU16();
  239. for (unsigned int p = 0; p < numPortals; p++) {
  240. // Which room this portal leads to
  241. uint16_t adjoiningRoom = file.readU16();
  242. // Which way the portal faces
  243. // The normal points away from the adjacent room
  244. // To be seen through, it must point toward the viewpoint
  245. int16_t xNormal = file.read16();
  246. int16_t yNormal = file.read16();
  247. int16_t zNormal = file.read16();
  248. // The corners of this portal
  249. // The right-hand rule applies with respect to the normal
  250. int16_t xCorner1 = file.read16();
  251. int16_t yCorner1 = file.read16();
  252. int16_t zCorner1 = file.read16();
  253. int16_t xCorner2 = file.read16();
  254. int16_t yCorner2 = file.read16();
  255. int16_t zCorner2 = file.read16();
  256. int16_t xCorner3 = file.read16();
  257. int16_t yCorner3 = file.read16();
  258. int16_t zCorner3 = file.read16();
  259. int16_t xCorner4 = file.read16();
  260. int16_t yCorner4 = file.read16();
  261. int16_t zCorner4 = file.read16();
  262. glm::vec4 vertices[4] = {
  263. glm::vec4(
  264. static_cast<float>(xCorner1),
  265. static_cast<float>(yCorner1),
  266. static_cast<float>(zCorner1),
  267. 1.0f
  268. ), glm::vec4(
  269. static_cast<float>(xCorner2),
  270. static_cast<float>(yCorner2),
  271. static_cast<float>(zCorner2),
  272. 1.0f
  273. ), glm::vec4(
  274. static_cast<float>(xCorner3),
  275. static_cast<float>(yCorner3),
  276. static_cast<float>(zCorner3),
  277. 1.0f
  278. ), glm::vec4(
  279. static_cast<float>(xCorner4),
  280. static_cast<float>(yCorner4),
  281. static_cast<float>(zCorner4),
  282. 0.0f
  283. )
  284. };
  285. // Portals have relative coordinates
  286. vertices[0] = transform * vertices[0];
  287. vertices[1] = transform * vertices[1];
  288. vertices[2] = transform * vertices[2];
  289. vertices[3] = transform * vertices[3];
  290. float normals[3] = {
  291. static_cast<float>(xNormal),
  292. static_cast<float>(yNormal),
  293. static_cast<float>(zNormal)
  294. };
  295. glm::vec3 verts[4] = {
  296. glm::vec3(vertices[0]),
  297. glm::vec3(vertices[1]),
  298. glm::vec3(vertices[2]),
  299. glm::vec3(vertices[3])
  300. };
  301. room->addPortal(new Portal(verts, normals, adjoiningRoom));
  302. room->addAdjacentRoom(adjoiningRoom);
  303. }
  304. uint16_t numZSectors = file.readU16();
  305. uint16_t numXSectors = file.readU16();
  306. room->setNumXSectors(numXSectors);
  307. room->setNumZSectors(numZSectors);
  308. for (unsigned int s = 0; s < (numZSectors * numXSectors); s++) {
  309. // Sectors are 1024*1024 world coordinates. Floor and Ceiling are
  310. // signed numbers of 256 units of height.
  311. // Floor/Ceiling value of 0x81 is used to indicate impenetrable
  312. // walls around the sector.
  313. // Floor values are used by the original engine to determine
  314. // what objects can be traversed and how. Relative steps of 1 (256)
  315. // can be walked up, 2..7 must be jumped up, larger than 7 is too high
  316. // If RoomAbove/Below is not none, the Ceiling/Floor is a collisional
  317. // portal to that room
  318. uint16_t indexFloorData = file.readU16();
  319. uint16_t indexBox = file.readU16(); // 0xFFFF if none
  320. uint8_t roomBelow = file.readU8(); // 0xFF if none
  321. int8_t floor = file.read8(); // Absolute height of floor (divided by 256)
  322. uint8_t roomAbove = file.readU8(); // 0xFF if none
  323. int8_t ceiling = file.read8(); // Absolute height of ceiling (/ 256)
  324. bool wall = false;
  325. if ((((uint8_t)floor) == 0x81) || (((uint8_t)ceiling) == 0x81)) {
  326. wall = true;
  327. }
  328. room->addSector(new Sector(floor * 256.0f, ceiling * 256.0f, wall));
  329. }
  330. int16_t intensity1 = file.read16();
  331. int16_t intensity2 = file.read16();
  332. int16_t lightMode = file.read16();
  333. uint16_t numLights = file.readU16();
  334. for (unsigned int l = 0; l < numLights; l++) {
  335. // Position of light, in world coordinates
  336. int32_t x = file.read32();
  337. int32_t y = file.read32();
  338. int32_t z = file.read32();
  339. uint16_t intensity1 = file.readU16();
  340. uint16_t intensity2 = file.readU16(); // Almost always equal to intensity1
  341. uint32_t fade1 = file.readU32(); // Falloff value?
  342. uint32_t fade2 = file.readU32(); // Falloff value?
  343. // TODO store light somewhere
  344. }
  345. uint16_t numStaticMeshes = file.readU16();
  346. for (unsigned int s = 0; s < numStaticMeshes; s++) {
  347. // Absolute position in world coordinates
  348. int32_t x = file.read32();
  349. int32_t y = file.read32();
  350. int32_t z = file.read32();
  351. // High two bits (0xC000) indicate steps of
  352. // 90 degrees (eg. (rotation >> 14) * 90)
  353. uint16_t rotation = file.readU16();
  354. // Constant lighting, 0xFFFF means use mesh lighting
  355. uint16_t intensity1 = file.readU16();
  356. uint16_t intensity2 = file.readU16();
  357. // Which StaticMesh item to draw
  358. uint16_t objectID = file.readU16();
  359. // TODO store static meshes somewhere
  360. }
  361. int16_t alternateRoom = file.read16(); // TODO
  362. uint16_t flags = file.readU16();
  363. int roomFlags = 0;
  364. if (flags & 0x0001) {
  365. roomFlags |= RoomFlagUnderWater;
  366. }
  367. room->setFlags(room->getFlags() | roomFlags);
  368. getWorld().addRoom(room);
  369. if ((numPortals == 0) && (numVertices == 0)
  370. && (numRectangles == 0) && (numTriangles == 0))
  371. getLog() << "LoaderTR2: Room " << i << " seems invalid: " << numPortals << "p "
  372. << numRectangles << "r " << numTriangles << "t " << numVertices
  373. << "v" << Log::endl;
  374. }
  375. if (numRooms > 0)
  376. getLog() << "LoaderTR2: Found " << numRooms << " Rooms!" << Log::endl;
  377. else
  378. getLog() << "LoaderTR2: No Rooms in this Level?!" << Log::endl;
  379. }
  380. void LoaderTR2::loadFloorData() {
  381. uint32_t numFloorData = file.readU32();
  382. for (unsigned int f = 0; f < numFloorData; f++) {
  383. uint16_t unused = file.readU16();
  384. // TODO store floor data somewhere
  385. }
  386. if (numFloorData > 0)
  387. getLog() << "LoaderTR2: Found " << numFloorData << " words FloorData, unimplemented!" << Log::endl;
  388. else
  389. getLog() << "LoaderTR2: No FloorData in this level?!" << Log::endl;
  390. }
  391. void LoaderTR2::loadSprites() {
  392. uint32_t numSpriteTextures = file.readU32();
  393. std::vector<Sprite> sprites;
  394. for (unsigned int s = 0; s < numSpriteTextures; s++) {
  395. uint16_t tile = file.readU16();
  396. uint8_t x = file.readU8();
  397. uint8_t y = file.readU8();
  398. uint16_t width = file.readU16(); // Actually (width * 256) + 255
  399. uint16_t height = file.readU16(); // Actually (height * 256) + 255
  400. // Required for what?
  401. int16_t leftSide = file.read16();
  402. int16_t topSide = file.read16();
  403. int16_t rightSide = file.read16();
  404. int16_t bottomSide = file.read16();
  405. sprites.emplace_back(tile, x, y, width, height);
  406. }
  407. uint32_t numSpriteSequences = file.readU32();
  408. for (unsigned int s = 0; s < numSpriteSequences; s++) {
  409. int32_t objectID = file.read32(); // Item identifier, matched in Items[]
  410. int16_t negativeLength = file.read16(); // Negative sprite count
  411. int16_t offset = file.read16(); // Where sequence starts in sprite texture list
  412. assert(negativeLength < 0);
  413. assert(offset >= 0);
  414. assert((offset + (negativeLength * -1)) <= numSpriteTextures);
  415. SpriteSequence* ss = new SpriteSequence(objectID);
  416. for (int i = 0; i < (negativeLength * -1); i++) {
  417. ss->add(sprites.at(offset + i));
  418. }
  419. getWorld().addSprite(ss);
  420. }
  421. if ((numSpriteTextures > 0) || (numSpriteSequences > 0))
  422. getLog() << "LoaderTR2: Found " << numSpriteTextures << " Sprites in " << numSpriteSequences <<
  423. " Sequences!" << Log::endl;
  424. else
  425. getLog() << "LoaderTR2: No Sprites in this level?!" << Log::endl;
  426. }
  427. // ---- Meshes ----
  428. void LoaderTR2::loadMeshes() {
  429. // Number of bitu16s of mesh data to follow
  430. // Read all the mesh data into a buffer, because
  431. // only afterward we can read the number of meshes
  432. // in this data block
  433. uint32_t numMeshData = file.readU32();
  434. std::vector<uint16_t> buffer;
  435. for (unsigned int i = 0; i < numMeshData; i++) {
  436. buffer.push_back(file.readU16());
  437. }
  438. uint32_t numMeshPointers = file.readU32();
  439. for (unsigned int i = 0; i < numMeshPointers; i++) {
  440. uint32_t meshPointer = file.readU32();
  441. if (numMeshData < (meshPointer / 2)) {
  442. getLog() << "LoaderTR2: Invalid Mesh: "
  443. << (meshPointer / 2) << " > " << numMeshData << Log::endl;
  444. continue;
  445. }
  446. char* tmpPtr = reinterpret_cast<char*>(&buffer[meshPointer / 2]);
  447. BinaryMemory mem(tmpPtr, (numMeshData * 2) - meshPointer);
  448. int16_t mx = mem.read16();
  449. int16_t my = mem.read16();
  450. int16_t mz = mem.read16();
  451. int32_t collisionSize = mem.read32();
  452. // TODO store mesh collision info somewhere
  453. uint16_t numVertices = mem.readU16();
  454. std::vector<glm::vec3> vertices;
  455. for (int v = 0; v < numVertices; v++) {
  456. int16_t x = mem.read16();
  457. int16_t y = mem.read16();
  458. int16_t z = mem.read16();
  459. vertices.emplace_back(x, y, z);
  460. }
  461. Mesh* mesh = new Mesh();
  462. int16_t numNormals = mem.read16();
  463. if (numNormals > 0) {
  464. // External vertex lighting is used, with the lighting calculated
  465. // from the rooms ambient and point-source lighting values. The
  466. // latter appears to use a simple Lambert law for directionality:
  467. // intensity is proportional to
  468. // max((normal direction).(direction to source), 0)
  469. for (int n = 0; n < numNormals; n++) {
  470. int16_t x = mem.read16();
  471. int16_t y = mem.read16();
  472. int16_t z = mem.read16();
  473. mesh->addNormal(glm::vec3(x, y, z));
  474. }
  475. } else if (numNormals < 0) {
  476. // Internal vertex lighting is used,
  477. // using the data included with the mesh
  478. for (int l = 0; l < (numNormals * -1); l++) {
  479. int16_t light = mem.read16();
  480. // TODO store lights somewhere
  481. }
  482. }
  483. int16_t numTexturedRectangles = mem.read16();
  484. for (int r = 0; r < numTexturedRectangles; r++) {
  485. uint16_t vertex1 = mem.readU16();
  486. uint16_t vertex2 = mem.readU16();
  487. uint16_t vertex3 = mem.readU16();
  488. uint16_t vertex4 = mem.readU16();
  489. uint16_t texture = mem.readU16();
  490. mesh->addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
  491. vertices.at(vertex3), vertices.at(vertex4),
  492. texture);
  493. }
  494. int16_t numTexturedTriangles = mem.read16();
  495. for (int t = 0; t < numTexturedTriangles; t++) {
  496. uint16_t vertex1 = mem.readU16();
  497. uint16_t vertex2 = mem.readU16();
  498. uint16_t vertex3 = mem.readU16();
  499. uint16_t texture = mem.readU16();
  500. mesh->addTexturedTriangle(vertices.at(vertex1), vertices.at(vertex2),
  501. vertices.at(vertex3), texture);
  502. }
  503. int16_t numColoredRectangles = mem.read16();
  504. for (int r = 0; r < numColoredRectangles; r++) {
  505. uint16_t vertex1 = mem.readU16();
  506. uint16_t vertex2 = mem.readU16();
  507. uint16_t vertex3 = mem.readU16();
  508. uint16_t vertex4 = mem.readU16();
  509. uint16_t texture = mem.readU16();
  510. int index = (texture & 0xFF00) >> 8;
  511. float red = (palette.at(index) & 0xFF000000) >> 24,
  512. green = (palette.at(index) & 0x00FF0000) >> 16,
  513. blue = (palette.at(index) & 0x0000FF00) >> 8;
  514. mesh->addColoredRectangle(vertices.at(vertex1), vertices.at(vertex2),
  515. vertices.at(vertex3), vertices.at(vertex4),
  516. red, green, blue);
  517. }
  518. int16_t numColoredTriangles = mem.read16();
  519. for (int t = 0; t < numColoredTriangles; t++) {
  520. uint16_t vertex1 = mem.readU16();
  521. uint16_t vertex2 = mem.readU16();
  522. uint16_t vertex3 = mem.readU16();
  523. uint16_t texture = mem.readU16();
  524. int index = (texture & 0xFF00) >> 8;
  525. float red = (palette.at(index) & 0xFF000000) >> 24,
  526. green = (palette.at(index) & 0x00FF0000) >> 16,
  527. blue = (palette.at(index) & 0x0000FF00) >> 8;
  528. mesh->addColoredTriangle(vertices.at(vertex1), vertices.at(vertex2),
  529. vertices.at(vertex3), red, green, blue);
  530. }
  531. getWorld().addMesh(mesh);
  532. }
  533. if (numMeshPointers > 0)
  534. getLog() << "LoaderTR2: Found " << numMeshPointers << " Meshes!" << Log::endl;
  535. else
  536. getLog() << "LoaderTR2: No Meshes in this level?!" << Log::endl;
  537. }
  538. void LoaderTR2::loadStaticMeshes() {
  539. uint32_t numStaticMeshes = file.readU32();
  540. for (unsigned int s = 0; s < numStaticMeshes; s++) {
  541. uint32_t objectID = file.readU32(); // Matched in Items[]
  542. uint16_t mesh = file.readU16(); // Offset into MeshPointers[]
  543. // tr2_vertex BoundingBox[2][2];
  544. // First index is which one, second index is opposite corners
  545. int16_t x11 = file.read16();
  546. int16_t y11 = file.read16();
  547. int16_t z11 = file.read16();
  548. int16_t x12 = file.read16();
  549. int16_t y12 = file.read16();
  550. int16_t z12 = file.read16();
  551. int16_t x21 = file.read16();
  552. int16_t y21 = file.read16();
  553. int16_t z21 = file.read16();
  554. int16_t x22 = file.read16();
  555. int16_t y22 = file.read16();
  556. int16_t z22 = file.read16();
  557. // Meaning uncertain. Usually 2, and 3 for objects Lara can
  558. // travel through, like TR2s skeletons and underwater plants
  559. uint16_t flags = file.readU16();
  560. getWorld().addStaticMesh(new StaticMesh(objectID, mesh));
  561. }
  562. if (numStaticMeshes > 0)
  563. getLog() << "LoaderTR2: Found " << numStaticMeshes << " StaticMeshes!" << Log::endl;
  564. else
  565. getLog() << "LoaderTR2: No StaticMeshes in this level?!" << Log::endl;
  566. }
  567. // ---- Moveables ----
  568. struct Animation_t {
  569. uint32_t frameOffset;
  570. uint8_t frameRate, frameSize;
  571. uint16_t stateID, frameStart, frameEnd, nextAnimation;
  572. uint16_t nextFrame, numStateChanges, stateChangeOffset;
  573. uint16_t numAnimCommands, animCommandOffset;
  574. Animation_t(uint32_t fo, uint8_t fr, uint8_t fs, uint16_t si,
  575. uint16_t fst, uint16_t fe, uint16_t na, uint16_t nf,
  576. uint16_t ns, uint16_t so, uint16_t nac, uint16_t ao)
  577. : frameOffset(fo), frameRate(fr), frameSize(fs),
  578. stateID(si), frameStart(fst), frameEnd(fe), nextAnimation(na),
  579. nextFrame(nf), numStateChanges(ns), stateChangeOffset(so),
  580. numAnimCommands(nac), animCommandOffset(ao) { }
  581. };
  582. struct StateChange_t {
  583. uint16_t stateID, numAnimDispatches, animDispatchOffset;
  584. StateChange_t(uint16_t s, uint16_t n, uint16_t a)
  585. : stateID(s), numAnimDispatches(n), animDispatchOffset(a) { }
  586. };
  587. struct AnimDispatch_t {
  588. int16_t low, high, nextAnimation, nextFrame;
  589. AnimDispatch_t(int16_t l, int16_t h, int16_t na, int16_t nf)
  590. : low(l), high(h), nextAnimation(na), nextFrame(nf) { }
  591. };
  592. void LoaderTR2::loadMoveables() {
  593. uint32_t numAnimations = file.readU32();
  594. std::vector<Animation_t> animations;
  595. for (unsigned int a = 0; a < numAnimations; a++) {
  596. // *Byte* Offset into Frames[] (so divide by 2!)
  597. uint32_t frameOffset = file.readU32();
  598. uint8_t frameRate = file.readU8(); // Engine ticks per frame
  599. // Number of bit16s in Frames[] used by this animation
  600. // Be careful when parsing frames using the FrameSize value
  601. // as the size of each frame, since an animations frame range
  602. // may extend into the next animations frame range, and that
  603. // may have a different FrameSize value.
  604. uint8_t frameSize = file.readU8();
  605. uint16_t stateID = file.readU16();
  606. file.seek(file.tell() + 8); // Skip 8 unknown bytes
  607. uint16_t frameStart = file.readU16(); // First frame in this animation
  608. uint16_t frameEnd = file.readU16(); // Last frame in this animation
  609. uint16_t nextAnimation = file.readU16();
  610. uint16_t nextFrame = file.readU16();
  611. uint16_t numStateChanges = file.readU16();
  612. uint16_t stateChangeOffset = file.readU16(); // Index into StateChanges[]
  613. uint16_t numAnimCommands = file.readU16(); // How many animation commands to use
  614. uint16_t animCommandOffset = file.readU16(); // Index into AnimCommand[]
  615. animations.emplace_back(frameOffset, frameRate, frameSize,
  616. stateID, frameStart, frameEnd, nextAnimation, nextFrame, numStateChanges,
  617. stateChangeOffset, numAnimCommands, animCommandOffset);
  618. }
  619. if (numAnimations > 0)
  620. getLog() << "LoaderTR2: Found " << numAnimations << " Animations!" << Log::endl;
  621. else
  622. getLog() << "LoaderTR2: No Animations in this level?!" << Log::endl;
  623. uint32_t numStateChanges = file.readU32();
  624. std::vector<StateChange_t> stateChanges;
  625. for (unsigned int s = 0; s < numStateChanges; s++) {
  626. uint16_t stateID = file.readU16();
  627. uint16_t numAnimDispatches = file.readU16(); // Number of ranges (always 1..5?)
  628. uint16_t animDispatchOffset = file.readU16(); // Index into AnimDispatches[]
  629. stateChanges.emplace_back(stateID, numAnimDispatches, animDispatchOffset);
  630. }
  631. if (numStateChanges > 0)
  632. getLog() << "LoaderTR2: Found " << numStateChanges << " StateChanges!" << Log::endl;
  633. else
  634. getLog() << "LoaderTR2: No StateChanges in this level?!" << Log::endl;
  635. uint32_t numAnimDispatches = file.readU32();
  636. std::vector<AnimDispatch_t> animDispatches;
  637. for (unsigned int a = 0; a < numAnimDispatches; a++) {
  638. int16_t low = file.read16(); // Lowest frame that uses this range
  639. int16_t high = file.read16(); // Highest frame (+1?) that uses this range
  640. int16_t nextAnimation = file.read16(); // Animation to go to
  641. int16_t nextFrame = file.read16(); // Frame offset to go to
  642. animDispatches.emplace_back(low, high, nextAnimation, nextFrame);
  643. }
  644. if (numAnimDispatches > 0)
  645. getLog() << "LoaderTR2: Found " << numAnimDispatches << " AnimationDispatches!" << Log::endl;
  646. else
  647. getLog() << "LoaderTR2: No AnimationDispatches in this level?!" << Log::endl;
  648. uint32_t numAnimCommands = file.readU32();
  649. std::vector<int16_t> animCommands;
  650. for (unsigned int a = 0; a < numAnimCommands; a++) {
  651. // A list of Opcodes with zero or more operands each,
  652. // some referring to the whole animation (jump/grab points),
  653. // some to specific frames (sound, bubbles, ...).
  654. animCommands.push_back(file.read16());
  655. }
  656. if (numAnimCommands > 0)
  657. getLog() << "LoaderTR2: Found " << numAnimCommands << " AnimationCommands!" << Log::endl;
  658. else
  659. getLog() << "LoaderTR2: No AnimationCommands in this level?!" << Log::endl;
  660. // This is really one uint32_t flags, followed by
  661. // three int32_t x, y, z. However, we're given the number
  662. // of 32bits, as well as byte indices later, so we store
  663. // it as a single list of int32_t.
  664. uint32_t numMeshTrees = file.readU32();
  665. std::vector<int32_t> meshTrees;
  666. for (unsigned int m = 0; m < numMeshTrees; m++) {
  667. // 0x0002 - Put parent mesh on the mesh stack
  668. // 0x0001 - Pop mesh from stack, use as parent mesh
  669. // When both are not set, use previous mesh as parent mesh
  670. // When both are set, do 0x0001 first, then 0x0002, thereby
  671. // reading the stack but not changing it
  672. //uint32_t flags = file.readU32();
  673. // Offset of mesh origin from the parent mesh origin
  674. //int32_t x = file.read32();
  675. //int32_t y = file.read32();
  676. //int32_t z = file.read32();
  677. meshTrees.push_back(file.read32());
  678. }
  679. if (numMeshTrees > 0)
  680. getLog() << "LoaderTR2: Found " << numMeshTrees << " MeshTrees!" << Log::endl;
  681. else
  682. getLog() << "LoaderTR2: No MeshTrees in this level?!" << Log::endl;
  683. uint32_t numFrames = file.readU32();
  684. std::vector<uint16_t> frames;
  685. for (unsigned int f = 0; f < numFrames; f++) {
  686. // int16 bb1x, bb1y, bb1z
  687. // int16 bb2x, bb2y, bb2z
  688. // int16 offsetX, offsetY, offsetZ
  689. // What follows next is a list of angles with numMeshes (from Moveable) entries.
  690. // If the top bit (0x8000) of the first uint16 is set, a single X angle follows,
  691. // if the second bit (0x4000) is set, a Y angle follows, both are a Z angle.
  692. // If none is set, it's a three-axis rotation. The next 10 bits (0x3FF0) are
  693. // the X rotation, the next 10 (0x000F 0xFC00) are Y, the next (0x03FF) are
  694. // the Z rotation. The scaling is always 0x100->90deg.
  695. // Rotation order: Y, X, Z!
  696. frames.push_back(file.readU16());
  697. }
  698. if (numFrames > 0)
  699. getLog() << "LoaderTR2: Found " << numFrames << " Frames!" << Log::endl;
  700. else
  701. getLog() << "LoaderTR2: No Frames in this level?!" << Log::endl;
  702. uint32_t numMoveables = file.readU32();
  703. for (unsigned int m = 0; m < numMoveables; m++) {
  704. // Item identifier, matched in Items[]
  705. uint32_t objectID = file.readU32();
  706. uint16_t numMeshes = file.readU16();
  707. uint16_t startingMesh = file.readU16(); // Offset into MeshPointers[]
  708. uint32_t meshTree = file.readU32(); // Offset into MeshTree[]
  709. // *Byte* offset into Frames[] (divide by 2 for Frames[i])
  710. uint32_t frameOffset = file.readU32(); // Only needed if no animation
  711. // If animation index is 0xFFFF, the object is stationary or
  712. // animated by the engine (ponytail)
  713. uint16_t animation = file.readU16();
  714. // TODO load all animations, not only the first frame!
  715. //if (animation == 0xFFFF) {
  716. // Just add the frame indicated in frameOffset, nothing else
  717. char* tmp = reinterpret_cast<char*>(&frames[0]) + frameOffset;
  718. BinaryMemory frame(tmp + 12, (numFrames * 2) - frameOffset - 12); // skip two BBs
  719. float pos[3];
  720. pos[0] = frame.read16();
  721. pos[1] = frame.read16();
  722. pos[2] = frame.read16();
  723. BoneFrame* bf = new BoneFrame(pos);
  724. for (int i = 0; i < numMeshes; i++) {
  725. int mesh = startingMesh + i;
  726. float offset[3] = { 0.0f, 0.0f, 0.0f };
  727. float rotation[3] = { 0.0f, 0.0f, 0.0f };
  728. char flag = (i == 0) ? 2 : 0;
  729. // Nonprimary tag - positioned relative to first tag
  730. if (i != 0) {
  731. tmp = reinterpret_cast<char*>(&meshTrees[0]) + meshTree; // TODO (meshTree * 4)?
  732. tmp += (i - 1) * 16; // TODO ?
  733. BinaryMemory tree(tmp, (numMeshTrees * 4) - meshTree - ((i - 1) * 16));
  734. flag = (char)tree.readU32();
  735. offset[0] = tree.read32();
  736. offset[1] = tree.read32();
  737. offset[2] = tree.read32();
  738. uint16_t a = frame.readU16();
  739. if (a & 0xC000) {
  740. // Single angle
  741. int index = 0;
  742. if ((a & 0x8000) && (a & 0x4000))
  743. index = 2;
  744. else if (a & 0x4000)
  745. index = 1;
  746. rotation[index] = ((float)(a & 0x03FF)) * 360.0f / 1024.0f;
  747. } else {
  748. // Three angles
  749. uint16_t b = frame.readU16();
  750. rotation[0] = (a & 0x3FF0) >> 4;
  751. rotation[1] = ((a & 0x000F) << 6) | ((b & 0xFC00) >> 10);
  752. rotation[2] = b & 0x03FF;
  753. for (int i = 0; i < 3; i++)
  754. rotation[i] = rotation[i] * 360.0f / 1024.0f;
  755. }
  756. }
  757. BoneTag* bt = new BoneTag(mesh, offset, rotation, flag);
  758. bf->add(bt);
  759. }
  760. AnimationFrame* af = new AnimationFrame(0);
  761. af->add(bf);
  762. SkeletalModel* sm = new SkeletalModel(objectID);
  763. sm->add(af);
  764. getWorld().addSkeletalModel(sm);
  765. //} else {
  766. // Add the whole animation hierarchy
  767. //}
  768. }
  769. if (numMoveables > 0)
  770. getLog() << "LoaderTR2: Found " << numMoveables << " Moveables!" << Log::endl;
  771. else
  772. getLog() << "LoaderTR2: No Moveables in this level?!" << Log::endl;
  773. }
  774. void LoaderTR2::loadItems() {
  775. uint32_t numItems = file.readU32();
  776. for (unsigned int i = 0; i < numItems; i++) {
  777. int16_t objectID = file.read16();
  778. int16_t room = file.read16();
  779. // Item position in world coordinates
  780. int32_t x = file.read32();
  781. int32_t y = file.read32();
  782. int32_t z = file.read32();
  783. uint16_t angle = file.readU16(); // (0xC000 >> 14) * 90deg
  784. int16_t intensity1 = file.read16(); // Constant lighting; -1 means mesh lighting
  785. int16_t intensity2 = file.read16(); // Almost always like intensity1
  786. // 0x0100 - Initially visible
  787. // 0x3E00 - Activation mask, open, can be XORed with related FloorData list fields.
  788. uint16_t flags = file.readU16();
  789. // TODO for now we're only creating Entities for each Moveable Item
  790. for (int m = 0; m < getWorld().sizeSkeletalModel(); m++) {
  791. if (getWorld().getSkeletalModel(m).getId() == objectID) {
  792. float pos[3] = {
  793. static_cast<float>(x),
  794. static_cast<float>(y),
  795. static_cast<float>(z)
  796. };
  797. float rot[3] = {
  798. 0.0f,
  799. glm::radians(((angle >> 14) & 0x03) * 90.0f),
  800. 0.0f
  801. };
  802. Entity* e = new Entity(pos, rot, objectID, room, m);
  803. getWorld().addEntity(e);
  804. if (objectID == 0) {
  805. getGame().setLara(getWorld().sizeEntity() - 1);
  806. }
  807. }
  808. }
  809. }
  810. if (numItems > 0)
  811. getLog() << "LoaderTR2: Found " << numItems << " Items!" << Log::endl;
  812. else
  813. getLog() << "LoaderTR2: No Items in this level?!" << Log::endl;
  814. }
  815. void LoaderTR2::loadBoxesOverlapsZones() {
  816. uint32_t numBoxes = file.readU32();
  817. for (unsigned int b = 0; b < numBoxes; b++) {
  818. // Sectors (* 1024 units)
  819. uint8_t zMin = file.readU8();
  820. uint8_t zMax = file.readU8();
  821. uint8_t xMin = file.readU8();
  822. uint8_t xMax = file.readU8();
  823. int16_t trueFloor = file.read16(); // Y value (no scaling)
  824. // Index into overlaps[]. The high bit is sometimes set
  825. // this occurs in front of swinging doors and the like
  826. uint16_t overlapIndex = file.readU16();
  827. // TODO store boxes somewhere
  828. }
  829. uint32_t numOverlaps = file.readU32();
  830. std::vector<std::vector<uint16_t>> overlaps;
  831. overlaps.emplace_back();
  832. unsigned int list = 0;
  833. for (unsigned int o = 0; o < numOverlaps; o++) {
  834. // Apparently used by NPCs to decide where to go next.
  835. // List of neighboring boxes for each box.
  836. // Each entry is a uint16, 0x8000 set marks end of list.
  837. uint16_t e = file.readU16();
  838. overlaps.at(list).push_back(e);
  839. if (e & 0x8000) {
  840. overlaps.emplace_back();
  841. list++;
  842. }
  843. }
  844. // TODO store overlaps somewhere
  845. for (unsigned int z = 0; z < numBoxes; z++) {
  846. // Normal room state
  847. int16_t ground1 = file.read16();
  848. int16_t ground2 = file.read16();
  849. int16_t ground3 = file.read16();
  850. int16_t ground4 = file.read16();
  851. int16_t fly = file.read16();
  852. // Alternate room state
  853. int16_t ground1alt = file.read16();
  854. int16_t ground2alt = file.read16();
  855. int16_t ground3alt = file.read16();
  856. int16_t ground4alt = file.read16();
  857. int16_t flyAlt = file.read16();
  858. // TODO store zones somewhere
  859. }
  860. if ((numBoxes > 0) || (numOverlaps > 0))
  861. getLog() << "LoaderTR2: Found NPC NavigationHints (" << numBoxes
  862. << ", " << numOverlaps << ", " << list << "), unimplemented!" << Log::endl;
  863. else
  864. getLog() << "LoaderTR2: No NPC NavigationHints in this level?!" << Log::endl;
  865. }
  866. // ---- Sound ----
  867. void LoaderTR2::loadSoundSources() {
  868. uint32_t numSoundSources = file.readU32();
  869. for (unsigned int s = 0; s < numSoundSources; s++) {
  870. // Absolute world coordinate positions of sound source
  871. int32_t x = file.read32();
  872. int32_t y = file.read32();
  873. int32_t z = file.read32();
  874. // Internal sound index
  875. uint16_t soundID = file.readU16();
  876. // Unknown, 0x40, 0x80 or 0xC0
  877. uint16_t flags = file.readU16();
  878. SoundManager::addSoundSource(x, y, z, soundID, flags);
  879. }
  880. if (numSoundSources > 0)
  881. getLog() << "LoaderTR2: Found " << numSoundSources << " SoundSources" << Log::endl;
  882. else
  883. getLog() << "LoaderTR2: No SoundSources in this level?!" << Log::endl;
  884. }
  885. void LoaderTR2::loadSoundMap() {
  886. for (int i = 0; i < 370; i++) {
  887. SoundManager::addSoundMapEntry(file.read16());
  888. }
  889. }
  890. void LoaderTR2::loadSoundDetails() {
  891. uint32_t numSoundDetails = file.readU32();
  892. for (unsigned int s = 0; s < numSoundDetails; s++) {
  893. uint16_t sample = file.readU16(); // Index into SampleIndices[]
  894. uint16_t volume = file.readU16();
  895. // sound range? distance at which this sound can be heard?
  896. uint16_t unknown1 = file.readU16();
  897. // Bits 8-15: priority?
  898. // Bits 2-7: number of samples in this group
  899. // Bits 0-1: channel number?
  900. uint16_t unknown2 = file.readU16();
  901. SoundManager::addSoundDetail(sample, ((float)volume) / 32767.0f);
  902. }
  903. if (numSoundDetails > 0)
  904. getLog() << "LoaderTR2: Found " << numSoundDetails << " SoundDetails" << Log::endl;
  905. else
  906. getLog() << "LoaderTR2: No SoundDetails in this level?!" << Log::endl;
  907. }
  908. void LoaderTR2::loadSampleIndices() {
  909. uint32_t numSampleIndices = file.readU32();
  910. for (unsigned int i = 0; i < numSampleIndices; i++) {
  911. SoundManager::addSampleIndex(file.readU32());
  912. }
  913. if (numSampleIndices > 0)
  914. getLog() << "LoaderTR2: Found " << numSampleIndices << " SampleIndices" << Log::endl;
  915. else
  916. getLog() << "LoaderTR2: No SampleIndices in this level?!" << Log::endl;
  917. }
  918. void LoaderTR2::loadExternalSoundFile(std::string f) {
  919. size_t dir = f.find_last_of("/\\");
  920. if (dir != std::string::npos) {
  921. f.replace(dir + 1, std::string::npos, "MAIN.SFX");
  922. } else {
  923. f = "MAIN.SFX";
  924. }
  925. BinaryFile sfx;
  926. if (sfx.open(f) != 0) {
  927. getLog() << "LoaderTR2: Can't open \"" << f << "\"!" << Log::endl;
  928. return;
  929. }
  930. int riffCount = 0;
  931. while (!sfx.eof()) {
  932. char test[5];
  933. test[4] = '\0';
  934. for (int i = 0; i < 4; i++)
  935. test[i] = sfx.read8();
  936. if (std::string("RIFF") != std::string(test)) {
  937. getLog() << "LoaderTR2: External SFX invalid! (" << riffCount
  938. << ", \"" << test << "\" != \"RIFF\")" << Log::endl;
  939. return;
  940. }
  941. // riffSize is (fileLength - 8)
  942. uint32_t riffSize = sfx.readU32();
  943. unsigned char buff[riffSize + 8];
  944. sfx.seek(sfx.tell() - 8);
  945. for (int i = 0; i < (riffSize + 8); i++)
  946. buff[i] = sfx.readU8();
  947. unsigned long src;
  948. int ret = Sound::loadBuffer(buff, riffSize + 8);
  949. assert(ret >= 0);
  950. riffCount++;
  951. }
  952. if (riffCount > 0)
  953. getLog() << "LoaderTR2: Found " << riffCount << " SoundSamples in SFX" << Log::endl;
  954. else
  955. getLog() << "LoaderTR2: No SoundSamples in SFX?!" << Log::endl;
  956. }
  957. // ---- Stuff ----
  958. void LoaderTR2::loadCameras() {
  959. uint32_t numCameras = file.readU32();
  960. for (unsigned int c = 0; c < numCameras; c++) {
  961. int32_t x = file.read32();
  962. int32_t y = file.read32();
  963. int32_t z = file.read32();
  964. int16_t room = file.read16();
  965. file.seek(file.tell() + 2); // Unknown, correlates to Boxes? Zones?
  966. // TODO store cameras somewhere
  967. }
  968. if (numCameras > 0)
  969. getLog() << "LoaderTR2: Found " << numCameras << " Cameras, unimplemented!" << Log::endl;
  970. }
  971. void LoaderTR2::loadCinematicFrames() {
  972. uint16_t numCinematicFrames = file.readU16();
  973. for (unsigned int c = 0; c < numCinematicFrames; c++) {
  974. int16_t rotY = file.read16(); // Y rotation, +-32767 = +-180deg
  975. int16_t rotZ = file.read16(); // Z rotation, like rotY
  976. int16_t rotZ2 = file.read16(); // Like rotZ?
  977. int16_t posZ = file.read16(); // Camera pos relative to what?
  978. int16_t posY = file.read16();
  979. int16_t posX = file.read16();
  980. int16_t unknown = file.read16(); // Changing this can cause runtime error
  981. int16_t rotX = file.read16(); // X rotation, like rotY
  982. // TODO store cinematic frames somewhere
  983. }
  984. if (numCinematicFrames > 0)
  985. getLog() << "LoaderTR2: Found " << numCinematicFrames
  986. << " CinematicFrames, unimplemented!" << Log::endl;
  987. }
  988. void LoaderTR2::loadDemoData() {
  989. uint16_t numDemoData = file.readU16();
  990. for (unsigned int d = 0; d < numDemoData; d++)
  991. file.readU8();
  992. // TODO store demo data somewhere, find out meaning
  993. if (numDemoData > 0)
  994. getLog() << "LoaderTR2: Found " << numDemoData << " bytes DemoData, unimplemented!" <<
  995. Log::endl;
  996. }