Open Source Tomb Raider Engine
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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