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

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