Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LoaderTR2.cpp 43KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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. glm::vec3 pos;
  687. pos.x = frame.read16();
  688. pos.y = frame.read16();
  689. pos.z = frame.read16();
  690. BoneFrame* bf = new BoneFrame(pos);
  691. for (int i = 0; i < numMeshes; i++) {
  692. int mesh = startingMesh + i;
  693. glm::vec3 offset;
  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.x = tree.read32();
  703. offset.y = tree.read32();
  704. offset.z = 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. glm::vec3 rot(rotation[0], rotation[1], rotation[2]);
  725. BoneTag* bt = new BoneTag(mesh, offset, rot, flag);
  726. bf->add(bt);
  727. }
  728. AnimationFrame* af = new AnimationFrame(0);
  729. af->add(bf);
  730. SkeletalModel* sm = new SkeletalModel(objectID);
  731. sm->add(af);
  732. getWorld().addSkeletalModel(sm);
  733. //} else {
  734. // Add the whole animation hierarchy
  735. //}
  736. }
  737. if (numMoveables > 0)
  738. Log::get(LOG_INFO) << "LoaderTR2: Found " << numMoveables << " Moveables!" << Log::endl;
  739. else
  740. Log::get(LOG_INFO) << "LoaderTR2: No Moveables in this level?!" << Log::endl;
  741. }
  742. void LoaderTR2::loadItems() {
  743. uint32_t numItems = file.readU32();
  744. for (unsigned int i = 0; i < numItems; i++) {
  745. int16_t objectID = file.read16();
  746. int16_t room = file.read16();
  747. // Item position in world coordinates
  748. int32_t x = file.read32();
  749. int32_t y = file.read32();
  750. int32_t z = file.read32();
  751. uint16_t angle = file.readU16(); // (0xC000 >> 14) * 90deg
  752. int16_t intensity1 = file.read16(); // Constant lighting; -1 means mesh lighting
  753. int16_t intensity2 = file.read16(); // Almost always like intensity1
  754. // 0x0100 - Initially visible
  755. // 0x3E00 - Activation mask, open, can be XORed with related FloorData list fields.
  756. uint16_t flags = file.readU16();
  757. glm::vec3 pos(
  758. static_cast<float>(x),
  759. static_cast<float>(y),
  760. static_cast<float>(z)
  761. );
  762. glm::vec3 rot(
  763. 0.0f,
  764. glm::radians(((angle >> 14) & 0x03) * 90.0f),
  765. 0.0f
  766. );
  767. Entity* e = new Entity(objectID, room, pos, rot);
  768. getWorld().addEntity(e);
  769. if (objectID == 0) {
  770. Game::setLara(getWorld().sizeEntity() - 1);
  771. }
  772. }
  773. if (numItems > 0)
  774. Log::get(LOG_INFO) << "LoaderTR2: Found " << numItems << " Items!" << Log::endl;
  775. else
  776. Log::get(LOG_INFO) << "LoaderTR2: No Items in this level?!" << Log::endl;
  777. }
  778. void LoaderTR2::loadBoxesOverlapsZones() {
  779. uint32_t numBoxes = file.readU32();
  780. for (unsigned int b = 0; b < numBoxes; b++) {
  781. // Sectors (* 1024 units)
  782. uint8_t zMin = file.readU8();
  783. uint8_t zMax = file.readU8();
  784. uint8_t xMin = file.readU8();
  785. uint8_t xMax = file.readU8();
  786. int16_t trueFloor = file.read16(); // Y value (no scaling)
  787. // Index into overlaps[]. The high bit is sometimes set
  788. // this occurs in front of swinging doors and the like
  789. uint16_t overlapIndex = file.readU16();
  790. // TODO store boxes somewhere
  791. }
  792. uint32_t numOverlaps = file.readU32();
  793. std::vector<std::vector<uint16_t>> overlaps;
  794. overlaps.emplace_back();
  795. unsigned int list = 0;
  796. for (unsigned int o = 0; o < numOverlaps; o++) {
  797. // Apparently used by NPCs to decide where to go next.
  798. // List of neighboring boxes for each box.
  799. // Each entry is a uint16, 0x8000 set marks end of list.
  800. uint16_t e = file.readU16();
  801. overlaps.at(list).push_back(e);
  802. if (e & 0x8000) {
  803. overlaps.emplace_back();
  804. list++;
  805. }
  806. }
  807. // TODO store overlaps somewhere
  808. for (unsigned int z = 0; z < numBoxes; z++) {
  809. // Normal room state
  810. int16_t ground1 = file.read16();
  811. int16_t ground2 = file.read16();
  812. int16_t ground3 = file.read16();
  813. int16_t ground4 = file.read16();
  814. int16_t fly = file.read16();
  815. // Alternate room state
  816. int16_t ground1alt = file.read16();
  817. int16_t ground2alt = file.read16();
  818. int16_t ground3alt = file.read16();
  819. int16_t ground4alt = file.read16();
  820. int16_t flyAlt = file.read16();
  821. // TODO store zones somewhere
  822. }
  823. if ((numBoxes > 0) || (numOverlaps > 0))
  824. Log::get(LOG_INFO) << "LoaderTR2: Found NPC NavigationHints (" << numBoxes
  825. << ", " << numOverlaps << ", " << list << "), unimplemented!" << Log::endl;
  826. else
  827. Log::get(LOG_INFO) << "LoaderTR2: No NPC NavigationHints in this level?!" << Log::endl;
  828. }
  829. // ---- Sound ----
  830. void LoaderTR2::loadSoundSources() {
  831. uint32_t numSoundSources = file.readU32();
  832. for (unsigned int s = 0; s < numSoundSources; s++) {
  833. // Absolute world coordinate positions of sound source
  834. int32_t x = file.read32();
  835. int32_t y = file.read32();
  836. int32_t z = file.read32();
  837. // Internal sound index
  838. uint16_t soundID = file.readU16();
  839. // Unknown, 0x40, 0x80 or 0xC0
  840. uint16_t flags = file.readU16();
  841. SoundManager::addSoundSource(x, y, z, soundID, flags);
  842. }
  843. if (numSoundSources > 0)
  844. Log::get(LOG_INFO) << "LoaderTR2: Found " << numSoundSources << " SoundSources" << Log::endl;
  845. else
  846. Log::get(LOG_INFO) << "LoaderTR2: No SoundSources in this level?!" << Log::endl;
  847. }
  848. void LoaderTR2::loadSoundMap() {
  849. for (int i = 0; i < 370; i++) {
  850. SoundManager::addSoundMapEntry(file.read16());
  851. }
  852. }
  853. void LoaderTR2::loadSoundDetails() {
  854. uint32_t numSoundDetails = file.readU32();
  855. for (unsigned int s = 0; s < numSoundDetails; s++) {
  856. uint16_t sample = file.readU16(); // Index into SampleIndices[]
  857. uint16_t volume = file.readU16();
  858. // sound range? distance at which this sound can be heard?
  859. uint16_t unknown1 = file.readU16();
  860. // Bits 8-15: priority?
  861. // Bits 2-7: number of samples in this group
  862. // Bits 0-1: channel number?
  863. uint16_t unknown2 = file.readU16();
  864. SoundManager::addSoundDetail(sample, ((float)volume) / 32767.0f);
  865. }
  866. if (numSoundDetails > 0)
  867. Log::get(LOG_INFO) << "LoaderTR2: Found " << numSoundDetails << " SoundDetails" << Log::endl;
  868. else
  869. Log::get(LOG_INFO) << "LoaderTR2: No SoundDetails in this level?!" << Log::endl;
  870. }
  871. void LoaderTR2::loadSampleIndices() {
  872. uint32_t numSampleIndices = file.readU32();
  873. for (unsigned int i = 0; i < numSampleIndices; i++) {
  874. SoundManager::addSampleIndex(file.readU32());
  875. }
  876. if (numSampleIndices > 0)
  877. Log::get(LOG_INFO) << "LoaderTR2: Found " << numSampleIndices << " SampleIndices" << Log::endl;
  878. else
  879. Log::get(LOG_INFO) << "LoaderTR2: No SampleIndices in this level?!" << Log::endl;
  880. }
  881. void LoaderTR2::loadExternalSoundFile(std::string f) {
  882. size_t dir = f.find_last_of("/\\");
  883. if (dir != std::string::npos) {
  884. f.replace(dir + 1, std::string::npos, "MAIN.SFX");
  885. } else {
  886. f = "MAIN.SFX";
  887. }
  888. BinaryFile sfx;
  889. if (sfx.open(f) != 0) {
  890. Log::get(LOG_INFO) << "LoaderTR2: Can't open \"" << f << "\"!" << Log::endl;
  891. return;
  892. }
  893. int riffCount = 0;
  894. while (!sfx.eof()) {
  895. char test[5];
  896. test[4] = '\0';
  897. for (int i = 0; i < 4; i++)
  898. test[i] = sfx.read8();
  899. if (std::string("RIFF") != std::string(test)) {
  900. Log::get(LOG_DEBUG) << "LoaderTR2: External SFX invalid! (" << riffCount
  901. << ", \"" << test << "\" != \"RIFF\")" << Log::endl;
  902. return;
  903. }
  904. // riffSize is (fileLength - 8)
  905. uint32_t riffSize = sfx.readU32();
  906. unsigned char buff[riffSize + 8];
  907. sfx.seek(sfx.tell() - 8);
  908. for (int i = 0; i < (riffSize + 8); i++)
  909. buff[i] = sfx.readU8();
  910. int ret = Sound::loadBuffer(buff, riffSize + 8);
  911. assert(ret >= 0);
  912. riffCount++;
  913. }
  914. if (riffCount > 0)
  915. Log::get(LOG_INFO) << "LoaderTR2: Found " << riffCount << " SoundSamples in SFX" << Log::endl;
  916. else
  917. Log::get(LOG_INFO) << "LoaderTR2: No SoundSamples in SFX?!" << Log::endl;
  918. }
  919. // ---- Stuff ----
  920. void LoaderTR2::loadCameras() {
  921. uint32_t numCameras = file.readU32();
  922. for (unsigned int c = 0; c < numCameras; c++) {
  923. int32_t x = file.read32();
  924. int32_t y = file.read32();
  925. int32_t z = file.read32();
  926. int16_t room = file.read16();
  927. file.seek(file.tell() + 2); // Unknown, correlates to Boxes? Zones?
  928. // TODO store cameras somewhere
  929. }
  930. if (numCameras > 0)
  931. Log::get(LOG_INFO) << "LoaderTR2: Found " << numCameras << " Cameras, unimplemented!" << Log::endl;
  932. }
  933. void LoaderTR2::loadCinematicFrames() {
  934. uint16_t numCinematicFrames = file.readU16();
  935. for (unsigned int c = 0; c < numCinematicFrames; c++) {
  936. int16_t rotY = file.read16(); // Y rotation, +-32767 = +-180deg
  937. int16_t rotZ = file.read16(); // Z rotation, like rotY
  938. int16_t rotZ2 = file.read16(); // Like rotZ?
  939. int16_t posZ = file.read16(); // Camera pos relative to what?
  940. int16_t posY = file.read16();
  941. int16_t posX = file.read16();
  942. int16_t unknown = file.read16(); // Changing this can cause runtime error
  943. int16_t rotX = file.read16(); // X rotation, like rotY
  944. // TODO store cinematic frames somewhere
  945. }
  946. if (numCinematicFrames > 0)
  947. Log::get(LOG_INFO) << "LoaderTR2: Found " << numCinematicFrames
  948. << " CinematicFrames, unimplemented!" << Log::endl;
  949. }
  950. void LoaderTR2::loadDemoData() {
  951. uint16_t numDemoData = file.readU16();
  952. for (unsigned int d = 0; d < numDemoData; d++)
  953. file.readU8();
  954. // TODO store demo data somewhere, find out meaning
  955. if (numDemoData > 0)
  956. Log::get(LOG_INFO) << "LoaderTR2: Found " << numDemoData << " bytes DemoData, unimplemented!" <<
  957. Log::endl;
  958. }