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

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