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 43KB

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