Open Source Tomb Raider Engine
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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;
  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!" <<
  137. Log::endl;
  138. else
  139. Log::get(LOG_INFO) << "LoaderTR2: No Animated Textures in this level?!" << Log::endl;
  140. if (pos != numWords)
  141. Log::get(LOG_DEBUG) << "LoaderTR2: Extra bytes at end of AnimatedTextures?!" << Log::endl;
  142. }
  143. // ---- Rooms ----
  144. void LoaderTR2::loadRooms() {
  145. uint16_t numRooms = file.readU16();
  146. for (unsigned int i = 0; i < numRooms; i++) {
  147. // Room Header
  148. int32_t xOffset = file.read32();
  149. int32_t zOffset = file.read32();
  150. int32_t yBottom = file.read32(); // lowest point == largest y value
  151. int32_t yTop = file.read32(); // highest point == smallest y value
  152. glm::vec3 pos(xOffset, 0.0f, zOffset);
  153. // Number of data words (2 bytes) to follow
  154. uint32_t dataToFollow = file.readU32();
  155. glm::vec3 bbox[2] = {
  156. glm::vec3(0.0f, 0.0f, 0.0f),
  157. glm::vec3(0.0f, 0.0f, 0.0f)
  158. };
  159. uint16_t numVertices = file.readU16();
  160. std::vector<RoomVertexTR2> vertices;
  161. for (unsigned int v = 0; v < numVertices; v++) {
  162. RoomVertexTR2 vert;
  163. vert.x = file.read16();
  164. vert.y = file.read16();
  165. vert.z = file.read16();
  166. vert.light1 = file.read16();
  167. vert.attributes = file.readU16();
  168. vert.light2 = file.read16();
  169. vertices.push_back(vert);
  170. // Fill bounding box
  171. if (v == 0) {
  172. for (int i = 0; i < 2; i++) {
  173. bbox[i].x = vert.x;
  174. bbox[i].y = vert.y;
  175. bbox[i].z = vert.z;
  176. }
  177. } else {
  178. if (vert.x < bbox[0].x)
  179. bbox[0].x = vert.x;
  180. if (vert.x > bbox[1].x)
  181. bbox[1].x = vert.x;
  182. if (vert.y < bbox[0].y)
  183. bbox[0].y = vert.y;
  184. if (vert.y > bbox[1].y)
  185. bbox[1].y = vert.y;
  186. if (vert.z < bbox[0].z)
  187. bbox[0].z = vert.z;
  188. if (vert.z > bbox[1].z)
  189. bbox[1].z = vert.z;
  190. }
  191. }
  192. bbox[0] += pos;
  193. bbox[1] += pos;
  194. uint16_t numRectangles = file.readU16();
  195. std::vector<IndexedRectangle> rectangles;
  196. for (unsigned int r = 0; r < numRectangles; r++) {
  197. // Indices into the vertex list read just before
  198. uint16_t vertex1 = file.readU16();
  199. uint16_t vertex2 = file.readU16();
  200. uint16_t vertex3 = file.readU16();
  201. uint16_t vertex4 = file.readU16();
  202. // Index into the object-texture list
  203. uint16_t texture = file.readU16();
  204. rectangles.emplace_back(texture, vertex1, vertex2, vertex3, vertex4);
  205. }
  206. uint16_t numTriangles = file.readU16();
  207. std::vector<IndexedRectangle> triangles;
  208. for (unsigned int t = 0; t < numTriangles; t++) {
  209. // Indices into the room vertex list
  210. uint16_t vertex1 = file.readU16();
  211. uint16_t vertex2 = file.readU16();
  212. uint16_t vertex3 = file.readU16();
  213. // Index into the object-texture list
  214. uint16_t texture = file.readU16();
  215. triangles.emplace_back(texture, vertex1, vertex2, vertex3);
  216. }
  217. uint16_t numSprites = file.readU16();
  218. std::vector<RoomSprite*> roomSprites;
  219. for (unsigned int s = 0; s < numSprites; s++) {
  220. uint16_t vertex = file.readU16(); // Index into vertex list
  221. uint16_t sprite = file.readU16(); // Index into sprite list
  222. auto& v = vertices.at(vertex);
  223. roomSprites.push_back(new RoomSprite(glm::vec3(v.x, v.y, v.z) + pos, sprite));
  224. }
  225. uint16_t numPortals = file.readU16();
  226. std::vector<Portal*> portals;
  227. for (unsigned int p = 0; p < numPortals; p++) {
  228. // Which room this portal leads to
  229. uint16_t adjoiningRoom = file.readU16();
  230. // Which way the portal faces
  231. // The normal points away from the adjacent room
  232. // To be seen through, it must point toward the viewpoint
  233. int16_t xNormal = file.read16();
  234. int16_t yNormal = file.read16();
  235. int16_t zNormal = file.read16();
  236. // The corners of this portal
  237. // The right-hand rule applies with respect to the normal
  238. int16_t xCorner1 = file.read16();
  239. int16_t yCorner1 = file.read16();
  240. int16_t zCorner1 = file.read16();
  241. int16_t xCorner2 = file.read16();
  242. int16_t yCorner2 = file.read16();
  243. int16_t zCorner2 = file.read16();
  244. int16_t xCorner3 = file.read16();
  245. int16_t yCorner3 = file.read16();
  246. int16_t zCorner3 = file.read16();
  247. int16_t xCorner4 = file.read16();
  248. int16_t yCorner4 = file.read16();
  249. int16_t zCorner4 = file.read16();
  250. //! \fixme TODO translate vertices by room offset!
  251. portals.push_back(new Portal(adjoiningRoom,
  252. glm::vec3(xNormal, yNormal, zNormal),
  253. glm::vec3(xCorner1, yCorner1, zCorner1),
  254. glm::vec3(xCorner2, yCorner2, zCorner2),
  255. glm::vec3(xCorner3, yCorner3, zCorner3),
  256. glm::vec3(xCorner4, yCorner4, zCorner4)));
  257. }
  258. uint16_t numZSectors = file.readU16();
  259. uint16_t numXSectors = file.readU16();
  260. for (unsigned int s = 0; s < (numZSectors * numXSectors); s++) {
  261. // Sectors are 1024*1024 world coordinates. Floor and Ceiling are
  262. // signed numbers of 256 units of height.
  263. // Floor/Ceiling value of 0x81 is used to indicate impenetrable
  264. // walls around the sector.
  265. // Floor values are used by the original engine to determine
  266. // what objects can be traversed and how. Relative steps of 1 (256)
  267. // can be walked up, 2..7 must be jumped up, larger than 7 is too high
  268. // If RoomAbove/Below is not none, the Ceiling/Floor is a collisional
  269. // portal to that room
  270. uint16_t indexFloorData = file.readU16();
  271. uint16_t indexBox = file.readU16(); // 0xFFFF if none
  272. uint8_t roomBelow = file.readU8(); // 0xFF if none
  273. int8_t floor = file.read8(); // Absolute height of floor (divided by 256)
  274. uint8_t roomAbove = file.readU8(); // 0xFF if none
  275. int8_t ceiling = file.read8(); // Absolute height of ceiling (/ 256)
  276. bool wall = false;
  277. if ((((uint8_t)floor) == 0x81) || (((uint8_t)ceiling) == 0x81)) {
  278. wall = true;
  279. }
  280. //room->addSector(new Sector(floor * 256.0f, ceiling * 256.0f, wall));
  281. // TODO store sectors
  282. }
  283. int16_t intensity1 = file.read16();
  284. int16_t intensity2 = file.read16();
  285. int16_t lightMode = file.read16();
  286. uint16_t numLights = file.readU16();
  287. for (unsigned int l = 0; l < numLights; l++) {
  288. // Position of light, in world coordinates
  289. int32_t x = file.read32();
  290. int32_t y = file.read32();
  291. int32_t z = file.read32();
  292. uint16_t intensity1 = file.readU16();
  293. uint16_t intensity2 = file.readU16(); // Almost always equal to intensity1
  294. uint32_t fade1 = file.readU32(); // Falloff value?
  295. uint32_t fade2 = file.readU32(); // Falloff value?
  296. // TODO store light somewhere
  297. }
  298. uint16_t numStaticMeshes = file.readU16();
  299. std::vector<StaticModel*> staticModels;
  300. for (unsigned int s = 0; s < numStaticMeshes; s++) {
  301. // Absolute position in world coordinates
  302. int32_t x = file.read32();
  303. int32_t y = file.read32();
  304. int32_t z = file.read32();
  305. // High two bits (0xC000) indicate steps of
  306. // 90 degrees (eg. (rotation >> 14) * 90)
  307. uint16_t rotation = file.readU16();
  308. // Constant lighting, 0xFFFF means use mesh lighting
  309. uint16_t intensity1 = file.readU16();
  310. uint16_t intensity2 = file.readU16();
  311. // Which StaticMesh item to draw
  312. uint16_t objectID = file.readU16();
  313. staticModels.push_back(new StaticModel(glm::vec3(x, y, z),
  314. glm::radians((rotation >> 14) * 90.0f),
  315. objectID));
  316. }
  317. int16_t alternateRoom = file.read16();
  318. uint16_t flags = file.readU16();
  319. unsigned int roomFlags = 0;
  320. if (flags & 0x0001) {
  321. roomFlags |= RoomFlagUnderWater;
  322. }
  323. BoundingBox* boundingbox = new BoundingBox(bbox[0], bbox[1]);
  324. RoomMesh* mesh = new RoomMesh(vertices, rectangles, triangles);
  325. Room* room = new Room(pos, boundingbox, mesh, roomFlags, alternateRoom,
  326. numXSectors, numZSectors, i);
  327. for (auto p : portals)
  328. room->addPortal(p);
  329. for (auto m : staticModels)
  330. room->addModel(m);
  331. for (auto s : roomSprites)
  332. room->addSprite(s);
  333. getWorld().addRoom(room);
  334. // Sanity check
  335. if ((numPortals == 0) && (numVertices == 0)
  336. && (numRectangles == 0) && (numTriangles == 0))
  337. Log::get(LOG_DEBUG) << "LoaderTR2: Room " << i << " seems invalid: " << numPortals << "p "
  338. << numRectangles << "r " << numTriangles << "t " << numVertices
  339. << "v" << Log::endl;
  340. }
  341. if (numRooms > 0)
  342. Log::get(LOG_INFO) << "LoaderTR2: Found " << numRooms << " Rooms!" << Log::endl;
  343. else
  344. Log::get(LOG_INFO) << "LoaderTR2: No Rooms in this Level?!" << Log::endl;
  345. }
  346. void LoaderTR2::loadFloorData() {
  347. uint32_t numFloorData = file.readU32();
  348. for (unsigned int f = 0; f < numFloorData; f++) {
  349. uint16_t unused = file.readU16();
  350. // TODO store floor data somewhere
  351. }
  352. if (numFloorData > 0)
  353. Log::get(LOG_INFO) << "LoaderTR2: Found " << numFloorData << " words FloorData, unimplemented!" <<
  354. Log::endl;
  355. else
  356. Log::get(LOG_INFO) << "LoaderTR2: No FloorData in this level?!" << Log::endl;
  357. }
  358. void LoaderTR2::loadSprites() {
  359. uint32_t numSpriteTextures = file.readU32();
  360. for (unsigned int s = 0; s < numSpriteTextures; s++) {
  361. uint16_t tile = file.readU16();
  362. uint8_t x = file.readU8();
  363. uint8_t y = file.readU8();
  364. uint16_t width = file.readU16(); // Actually (width * 256) + 255
  365. uint16_t height = file.readU16(); // Actually (height * 256) + 255
  366. // Required for what?
  367. int16_t leftSide = file.read16();
  368. int16_t topSide = file.read16();
  369. int16_t rightSide = file.read16();
  370. int16_t bottomSide = file.read16();
  371. Sprite* sp = new Sprite(tile, x, y, width, height);
  372. getWorld().addSprite(sp);
  373. }
  374. uint32_t numSpriteSequences = file.readU32();
  375. for (unsigned int s = 0; s < numSpriteSequences; s++) {
  376. int32_t objectID = file.read32(); // Item identifier, matched in Items[]
  377. int16_t negativeLength = file.read16(); // Negative sprite count
  378. int16_t offset = file.read16(); // Where sequence starts in sprite texture list
  379. assert(negativeLength < 0);
  380. assert(offset >= 0);
  381. assert((offset + (negativeLength * -1)) <= numSpriteTextures);
  382. SpriteSequence* ss = new SpriteSequence(objectID, offset, (negativeLength * -1));
  383. getWorld().addSpriteSequence(ss);
  384. }
  385. if ((numSpriteTextures > 0) || (numSpriteSequences > 0))
  386. Log::get(LOG_INFO) << "LoaderTR2: Found " << numSpriteTextures << " Sprites in " <<
  387. numSpriteSequences <<
  388. " Sequences!" << Log::endl;
  389. else
  390. Log::get(LOG_INFO) << "LoaderTR2: No Sprites in this level?!" << Log::endl;
  391. }
  392. // ---- Meshes ----
  393. void LoaderTR2::loadMeshes() {
  394. // Number of bitu16s of mesh data to follow
  395. // Read all the mesh data into a buffer, because
  396. // only afterward we can read the number of meshes
  397. // in this data block
  398. uint32_t numMeshData = file.readU32();
  399. std::vector<uint16_t> buffer;
  400. for (unsigned int i = 0; i < numMeshData; i++) {
  401. buffer.push_back(file.readU16());
  402. }
  403. uint32_t numMeshPointers = file.readU32();
  404. for (unsigned int i = 0; i < numMeshPointers; i++) {
  405. uint32_t meshPointer = file.readU32();
  406. if (numMeshData < (meshPointer / 2)) {
  407. Log::get(LOG_DEBUG) << "LoaderTR2: Invalid Mesh: "
  408. << (meshPointer / 2) << " > " << numMeshData << Log::endl;
  409. continue;
  410. }
  411. char* tmpPtr = reinterpret_cast<char*>(&buffer[meshPointer / 2]);
  412. BinaryMemory mem(tmpPtr, (numMeshData * 2) - meshPointer);
  413. int16_t mx = mem.read16();
  414. int16_t my = mem.read16();
  415. int16_t mz = mem.read16();
  416. int32_t collisionSize = mem.read32();
  417. // TODO store mesh collision info somewhere
  418. uint16_t numVertices = mem.readU16();
  419. std::vector<glm::vec3> vertices;
  420. for (int v = 0; v < numVertices; v++) {
  421. int16_t x = mem.read16();
  422. int16_t y = mem.read16();
  423. int16_t z = mem.read16();
  424. vertices.emplace_back(x, y, z);
  425. }
  426. int16_t numNormals = mem.read16();
  427. if (numNormals > 0) {
  428. // External vertex lighting is used, with the lighting calculated
  429. // from the rooms ambient and point-source lighting values. The
  430. // latter appears to use a simple Lambert law for directionality:
  431. // intensity is proportional to
  432. // max((normal direction).(direction to source), 0)
  433. for (int n = 0; n < numNormals; n++) {
  434. int16_t x = mem.read16();
  435. int16_t y = mem.read16();
  436. int16_t z = mem.read16();
  437. //mesh->addNormal(glm::vec3(x, y, z));
  438. }
  439. } else if (numNormals < 0) {
  440. // Internal vertex lighting is used,
  441. // using the data included with the mesh
  442. for (int l = 0; l < (numNormals * -1); l++) {
  443. int16_t light = mem.read16();
  444. // TODO store lights somewhere
  445. }
  446. }
  447. int16_t numTexturedRectangles = mem.read16();
  448. std::vector<IndexedRectangle> texturedRectangles;
  449. for (int r = 0; r < numTexturedRectangles; r++) {
  450. uint16_t vertex1 = mem.readU16();
  451. uint16_t vertex2 = mem.readU16();
  452. uint16_t vertex3 = mem.readU16();
  453. uint16_t vertex4 = mem.readU16();
  454. uint16_t texture = mem.readU16();
  455. texturedRectangles.emplace_back(texture, vertex1, vertex2, vertex3, vertex4);
  456. }
  457. int16_t numTexturedTriangles = mem.read16();
  458. std::vector<IndexedRectangle> texturedTriangles;
  459. for (int t = 0; t < numTexturedTriangles; t++) {
  460. uint16_t vertex1 = mem.readU16();
  461. uint16_t vertex2 = mem.readU16();
  462. uint16_t vertex3 = mem.readU16();
  463. uint16_t texture = mem.readU16();
  464. texturedTriangles.emplace_back(texture, vertex1, vertex2, vertex3);
  465. }
  466. int16_t numColoredRectangles = mem.read16();
  467. std::vector<IndexedColoredRectangle> coloredRectangles;
  468. for (int r = 0; r < numColoredRectangles; r++) {
  469. uint16_t vertex1 = mem.readU16();
  470. uint16_t vertex2 = mem.readU16();
  471. uint16_t vertex3 = mem.readU16();
  472. uint16_t vertex4 = mem.readU16();
  473. uint16_t texture = mem.readU16();
  474. int index = (texture & 0xFF00) >> 8;
  475. uint8_t red = (palette.at(index) & 0xFF000000) >> 24,
  476. green = (palette.at(index) & 0x00FF0000) >> 16,
  477. blue = (palette.at(index) & 0x0000FF00) >> 8;
  478. coloredRectangles.emplace_back(red, green, blue, vertex1, vertex2, vertex3, vertex4);
  479. }
  480. int16_t numColoredTriangles = mem.read16();
  481. std::vector<IndexedColoredRectangle> coloredTriangles;
  482. for (int t = 0; t < numColoredTriangles; t++) {
  483. uint16_t vertex1 = mem.readU16();
  484. uint16_t vertex2 = mem.readU16();
  485. uint16_t vertex3 = mem.readU16();
  486. uint16_t texture = mem.readU16();
  487. int index = (texture & 0xFF00) >> 8;
  488. uint8_t red = (palette.at(index) & 0xFF000000) >> 24,
  489. green = (palette.at(index) & 0x00FF0000) >> 16,
  490. blue = (palette.at(index) & 0x0000FF00) >> 8;
  491. coloredTriangles.emplace_back(red, green, blue, vertex1, vertex2, vertex3);
  492. }
  493. Mesh* mesh = new Mesh(vertices, texturedRectangles, texturedTriangles,
  494. coloredRectangles, coloredTriangles);
  495. getWorld().addMesh(mesh);
  496. }
  497. if (numMeshPointers > 0)
  498. Log::get(LOG_INFO) << "LoaderTR2: Found " << numMeshPointers << " Meshes!" << Log::endl;
  499. else
  500. Log::get(LOG_INFO) << "LoaderTR2: No Meshes in this level?!" << Log::endl;
  501. }
  502. void LoaderTR2::loadStaticMeshes() {
  503. uint32_t numStaticMeshes = file.readU32();
  504. for (unsigned int s = 0; s < numStaticMeshes; s++) {
  505. uint32_t objectID = file.readU32(); // Matched in Items[]
  506. uint16_t mesh = file.readU16(); // Offset into MeshPointers[]
  507. // tr2_vertex BoundingBox[2][2];
  508. // First index is which one, second index is opposite corners
  509. int16_t x11 = file.read16();
  510. int16_t y11 = file.read16();
  511. int16_t z11 = file.read16();
  512. int16_t x12 = file.read16();
  513. int16_t y12 = file.read16();
  514. int16_t z12 = file.read16();
  515. int16_t x21 = file.read16();
  516. int16_t y21 = file.read16();
  517. int16_t z21 = file.read16();
  518. int16_t x22 = file.read16();
  519. int16_t y22 = file.read16();
  520. int16_t z22 = file.read16();
  521. // Meaning uncertain. Usually 2, and 3 for objects Lara can
  522. // travel through, like TR2s skeletons and underwater plants
  523. uint16_t flags = file.readU16();
  524. BoundingBox* bbox1 = new BoundingBox(glm::vec3(x11, y11, z11), glm::vec3(x12, y12, z12));
  525. BoundingBox* bbox2 = new BoundingBox(glm::vec3(x21, y21, z21), glm::vec3(x22, y22, z22));
  526. getWorld().addStaticMesh(new StaticMesh(objectID, mesh, bbox1, bbox2));
  527. }
  528. if (numStaticMeshes > 0)
  529. Log::get(LOG_INFO) << "LoaderTR2: Found " << numStaticMeshes << " StaticMeshes!" << Log::endl;
  530. else
  531. Log::get(LOG_INFO) << "LoaderTR2: No StaticMeshes in this level?!" << Log::endl;
  532. }
  533. // ---- Moveables ----
  534. struct Animation_t {
  535. uint32_t frameOffset;
  536. uint8_t frameRate, frameSize;
  537. uint16_t stateID, frameStart, frameEnd, nextAnimation;
  538. uint16_t nextFrame, numStateChanges, stateChangeOffset;
  539. uint16_t numAnimCommands, animCommandOffset;
  540. Animation_t(uint32_t fo, uint8_t fr, uint8_t fs, uint16_t si,
  541. uint16_t fst, uint16_t fe, uint16_t na, uint16_t nf,
  542. uint16_t ns, uint16_t so, uint16_t nac, uint16_t ao)
  543. : frameOffset(fo), frameRate(fr), frameSize(fs),
  544. stateID(si), frameStart(fst), frameEnd(fe), nextAnimation(na),
  545. nextFrame(nf), numStateChanges(ns), stateChangeOffset(so),
  546. numAnimCommands(nac), animCommandOffset(ao) { }
  547. };
  548. struct StateChange_t {
  549. uint16_t stateID, numAnimDispatches, animDispatchOffset;
  550. StateChange_t(uint16_t s, uint16_t n, uint16_t a)
  551. : stateID(s), numAnimDispatches(n), animDispatchOffset(a) { }
  552. };
  553. struct AnimDispatch_t {
  554. int16_t low, high, nextAnimation, nextFrame;
  555. AnimDispatch_t(int16_t l, int16_t h, int16_t na, int16_t nf)
  556. : low(l), high(h), nextAnimation(na), nextFrame(nf) { }
  557. };
  558. void LoaderTR2::loadMoveables() {
  559. uint32_t numAnimations = file.readU32();
  560. std::vector<Animation_t> animations;
  561. for (unsigned int a = 0; a < numAnimations; a++) {
  562. // *Byte* Offset into Frames[] (so divide by 2!)
  563. uint32_t frameOffset = file.readU32();
  564. uint8_t frameRate = file.readU8(); // Engine ticks per frame
  565. // Number of bit16s in Frames[] used by this animation
  566. // Be careful when parsing frames using the FrameSize value
  567. // as the size of each frame, since an animations frame range
  568. // may extend into the next animations frame range, and that
  569. // may have a different FrameSize value.
  570. uint8_t frameSize = file.readU8();
  571. uint16_t stateID = file.readU16();
  572. file.seek(file.tell() + 8); // Skip 8 unknown bytes
  573. uint16_t frameStart = file.readU16(); // First frame in this animation
  574. uint16_t frameEnd = file.readU16(); // Last frame in this animation
  575. uint16_t nextAnimation = file.readU16();
  576. uint16_t nextFrame = file.readU16();
  577. uint16_t numStateChanges = file.readU16();
  578. uint16_t stateChangeOffset = file.readU16(); // Index into StateChanges[]
  579. uint16_t numAnimCommands = file.readU16(); // How many animation commands to use
  580. uint16_t animCommandOffset = file.readU16(); // Index into AnimCommand[]
  581. animations.emplace_back(frameOffset, frameRate, frameSize,
  582. stateID, frameStart, frameEnd, nextAnimation, nextFrame, numStateChanges,
  583. stateChangeOffset, numAnimCommands, animCommandOffset);
  584. }
  585. if (numAnimations > 0)
  586. Log::get(LOG_INFO) << "LoaderTR2: Found " << numAnimations << " Animations!" << Log::endl;
  587. else
  588. Log::get(LOG_INFO) << "LoaderTR2: No Animations in this level?!" << Log::endl;
  589. uint32_t numStateChanges = file.readU32();
  590. std::vector<StateChange_t> stateChanges;
  591. for (unsigned int s = 0; s < numStateChanges; s++) {
  592. uint16_t stateID = file.readU16();
  593. uint16_t numAnimDispatches = file.readU16(); // Number of ranges (always 1..5?)
  594. uint16_t animDispatchOffset = file.readU16(); // Index into AnimDispatches[]
  595. stateChanges.emplace_back(stateID, numAnimDispatches, animDispatchOffset);
  596. }
  597. if (numStateChanges > 0)
  598. Log::get(LOG_INFO) << "LoaderTR2: Found " << numStateChanges << " StateChanges!" << Log::endl;
  599. else
  600. Log::get(LOG_INFO) << "LoaderTR2: No StateChanges in this level?!" << Log::endl;
  601. uint32_t numAnimDispatches = file.readU32();
  602. std::vector<AnimDispatch_t> animDispatches;
  603. for (unsigned int a = 0; a < numAnimDispatches; a++) {
  604. int16_t low = file.read16(); // Lowest frame that uses this range
  605. int16_t high = file.read16(); // Highest frame (+1?) that uses this range
  606. int16_t nextAnimation = file.read16(); // Animation to go to
  607. int16_t nextFrame = file.read16(); // Frame offset to go to
  608. animDispatches.emplace_back(low, high, nextAnimation, nextFrame);
  609. }
  610. if (numAnimDispatches > 0)
  611. Log::get(LOG_INFO) << "LoaderTR2: Found " << numAnimDispatches << " AnimationDispatches!" <<
  612. Log::endl;
  613. else
  614. Log::get(LOG_INFO) << "LoaderTR2: No AnimationDispatches in this level?!" << Log::endl;
  615. uint32_t numAnimCommands = file.readU32();
  616. std::vector<int16_t> animCommands;
  617. for (unsigned int a = 0; a < numAnimCommands; a++) {
  618. // A list of Opcodes with zero or more operands each,
  619. // some referring to the whole animation (jump/grab points),
  620. // some to specific frames (sound, bubbles, ...).
  621. animCommands.push_back(file.read16());
  622. }
  623. if (numAnimCommands > 0)
  624. Log::get(LOG_INFO) << "LoaderTR2: Found " << numAnimCommands << " AnimationCommands!" << Log::endl;
  625. else
  626. Log::get(LOG_INFO) << "LoaderTR2: No AnimationCommands in this level?!" << Log::endl;
  627. // This is really one uint32_t flags, followed by
  628. // three int32_t x, y, z. However, we're given the number
  629. // of 32bits, as well as byte indices later, so we store
  630. // it as a single list of int32_t.
  631. uint32_t numMeshTrees = file.readU32();
  632. std::vector<int32_t> meshTrees;
  633. for (unsigned int m = 0; m < numMeshTrees; m++) {
  634. // 0x0002 - Put parent mesh on the mesh stack
  635. // 0x0001 - Pop mesh from stack, use as parent mesh
  636. // When both are not set, use previous mesh as parent mesh
  637. // When both are set, do 0x0001 first, then 0x0002, thereby
  638. // reading the stack but not changing it
  639. //uint32_t flags = file.readU32();
  640. // Offset of mesh origin from the parent mesh origin
  641. //int32_t x = file.read32();
  642. //int32_t y = file.read32();
  643. //int32_t z = file.read32();
  644. meshTrees.push_back(file.read32());
  645. }
  646. if (numMeshTrees > 0)
  647. Log::get(LOG_INFO) << "LoaderTR2: Found " << numMeshTrees << " MeshTrees!" << Log::endl;
  648. else
  649. Log::get(LOG_INFO) << "LoaderTR2: No MeshTrees in this level?!" << Log::endl;
  650. uint32_t numFrames = file.readU32();
  651. std::vector<uint16_t> frames;
  652. for (unsigned int f = 0; f < numFrames; f++) {
  653. // int16 bb1x, bb1y, bb1z
  654. // int16 bb2x, bb2y, bb2z
  655. // int16 offsetX, offsetY, offsetZ
  656. // What follows next is a list of angles with numMeshes (from Moveable) entries.
  657. // If the top bit (0x8000) of the first uint16 is set, a single X angle follows,
  658. // if the second bit (0x4000) is set, a Y angle follows, both are a Z angle.
  659. // If none is set, it's a three-axis rotation. The next 10 bits (0x3FF0) are
  660. // the X rotation, the next 10 (0x000F 0xFC00) are Y, the next (0x03FF) are
  661. // the Z rotation. The scaling is always 0x100->90deg.
  662. // Rotation order: Y, X, Z!
  663. frames.push_back(file.readU16());
  664. }
  665. if (numFrames > 0)
  666. Log::get(LOG_INFO) << "LoaderTR2: Found " << numFrames << " Frames!" << Log::endl;
  667. else
  668. Log::get(LOG_INFO) << "LoaderTR2: No Frames in this level?!" << Log::endl;
  669. uint32_t numMoveables = file.readU32();
  670. for (unsigned int m = 0; m < numMoveables; m++) {
  671. // Item identifier, matched in Items[]
  672. uint32_t objectID = file.readU32();
  673. uint16_t numMeshes = file.readU16();
  674. uint16_t startingMesh = file.readU16(); // Offset into MeshPointers[]
  675. uint32_t meshTree = file.readU32(); // Offset into MeshTree[]
  676. // *Byte* offset into Frames[] (divide by 2 for Frames[i])
  677. uint32_t frameOffset = file.readU32(); // Only needed if no animation
  678. // If animation index is 0xFFFF, the object is stationary or
  679. // animated by the engine (ponytail)
  680. uint16_t animation = file.readU16();
  681. // TODO load all animations, not only the first frame!
  682. //if (animation == 0xFFFF) {
  683. // Just add the frame indicated in frameOffset, nothing else
  684. char* tmp = reinterpret_cast<char*>(&frames[0]) + frameOffset;
  685. BinaryMemory frame(tmp + 12, (numFrames * 2) - frameOffset - 12); // skip two BBs
  686. float pos[3];
  687. pos[0] = frame.read16();
  688. pos[1] = frame.read16();
  689. pos[2] = frame.read16();
  690. BoneFrame* bf = new BoneFrame(pos);
  691. for (int i = 0; i < numMeshes; i++) {
  692. int mesh = startingMesh + i;
  693. float offset[3] = { 0.0f, 0.0f, 0.0f };
  694. float rotation[3] = { 0.0f, 0.0f, 0.0f };
  695. char flag = (i == 0) ? 2 : 0;
  696. // Nonprimary tag - positioned relative to first tag
  697. if (i != 0) {
  698. tmp = reinterpret_cast<char*>(&meshTrees[0]) + meshTree; // TODO (meshTree * 4)?
  699. tmp += (i - 1) * 16; // TODO ?
  700. BinaryMemory tree(tmp, (numMeshTrees * 4) - meshTree - ((i - 1) * 16));
  701. flag = (char)tree.readU32();
  702. offset[0] = tree.read32();
  703. offset[1] = tree.read32();
  704. offset[2] = tree.read32();
  705. uint16_t a = frame.readU16();
  706. if (a & 0xC000) {
  707. // Single angle
  708. int index = 0;
  709. if ((a & 0x8000) && (a & 0x4000))
  710. index = 2;
  711. else if (a & 0x4000)
  712. index = 1;
  713. rotation[index] = ((float)(a & 0x03FF)) * 360.0f / 1024.0f;
  714. } else {
  715. // Three angles
  716. uint16_t b = frame.readU16();
  717. rotation[0] = (a & 0x3FF0) >> 4;
  718. rotation[1] = ((a & 0x000F) << 6) | ((b & 0xFC00) >> 10);
  719. rotation[2] = b & 0x03FF;
  720. for (int i = 0; i < 3; i++)
  721. rotation[i] = rotation[i] * 360.0f / 1024.0f;
  722. }
  723. }
  724. BoneTag* bt = new BoneTag(mesh, offset, rotation, flag);
  725. bf->add(bt);
  726. }
  727. AnimationFrame* af = new AnimationFrame(0);
  728. af->add(bf);
  729. SkeletalModel* sm = new SkeletalModel(objectID);
  730. sm->add(af);
  731. getWorld().addSkeletalModel(sm);
  732. //} else {
  733. // Add the whole animation hierarchy
  734. //}
  735. }
  736. if (numMoveables > 0)
  737. Log::get(LOG_INFO) << "LoaderTR2: Found " << numMoveables << " Moveables!" << Log::endl;
  738. else
  739. Log::get(LOG_INFO) << "LoaderTR2: No Moveables in this level?!" << Log::endl;
  740. }
  741. void LoaderTR2::loadItems() {
  742. uint32_t numItems = file.readU32();
  743. for (unsigned int i = 0; i < numItems; i++) {
  744. int16_t objectID = file.read16();
  745. int16_t room = file.read16();
  746. // Item position in world coordinates
  747. int32_t x = file.read32();
  748. int32_t y = file.read32();
  749. int32_t z = file.read32();
  750. uint16_t angle = file.readU16(); // (0xC000 >> 14) * 90deg
  751. int16_t intensity1 = file.read16(); // Constant lighting; -1 means mesh lighting
  752. int16_t intensity2 = file.read16(); // Almost always like intensity1
  753. // 0x0100 - Initially visible
  754. // 0x3E00 - Activation mask, open, can be XORed with related FloorData list fields.
  755. uint16_t flags = file.readU16();
  756. glm::vec3 pos(
  757. static_cast<float>(x),
  758. static_cast<float>(y),
  759. static_cast<float>(z)
  760. );
  761. glm::vec3 rot(
  762. 0.0f,
  763. glm::radians(((angle >> 14) & 0x03) * 90.0f),
  764. 0.0f
  765. );
  766. Entity* e = new Entity(objectID, room, pos, rot);
  767. getWorld().addEntity(e);
  768. if (objectID == 0) {
  769. Game::setLara(getWorld().sizeEntity() - 1);
  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. }