Open Source Tomb Raider Engine

LoaderTR2.cpp 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*!
  2. * \file src/loader/LoaderTR2.cpp
  3. * \brief TR2 level file loader
  4. *
  5. * \author xythobuz
  6. */
  7. #include <array>
  8. #include <cstdint>
  9. #include <vector>
  10. #include "global.h"
  11. #include "Log.h"
  12. #include "Mesh.h"
  13. #include "Room.h"
  14. #include "TextureManager.h"
  15. #include "utils/pixel.h"
  16. #include "loader/LoaderTR2.h"
  17. LoaderTR2::LoaderTR2() {
  18. }
  19. LoaderTR2::~LoaderTR2() {
  20. }
  21. int LoaderTR2::load(std::string f) {
  22. if (file.open(f) != 0) {
  23. return 1; // Could not open file
  24. }
  25. if (file.readU32() != 0x2D) {
  26. return 2; // Not a TR2 level?!
  27. }
  28. loadPaletteTextiles();
  29. file.seek(file.tell() + 4); // Unused value?
  30. loadRooms();
  31. loadFloorData();
  32. loadMeshes();
  33. loadMoveables();
  34. loadStaticMeshes();
  35. loadTextures();
  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; // TODO Not finished with implementation!
  50. }
  51. void LoaderTR2::loadPaletteTextiles() {
  52. file.seek(file.tell() + 768); // Skip 8bit palette, 256 * 3 bytes
  53. // Read the 16bit palette, 256 * 4 bytes, RGBA, A unused
  54. std::array<uint32_t, 256> palette; //!< RGBA, A unused
  55. for (auto& x : palette)
  56. x = file.readU32();
  57. // TODO store palette somewhere
  58. uint32_t numTextiles = file.readU32();
  59. file.seek(file.tell() + (numTextiles * 256 * 256)); // Skip 8bit textiles
  60. // Read the 16bit textiles, numTextiles * 256 * 256 * 2 bytes
  61. for (unsigned int i = 0; i < numTextiles; i++) {
  62. std::array<uint8_t, 256 * 256 * 2> arr;
  63. for (auto& x : arr) {
  64. x = file.readU8();
  65. }
  66. // Convert 16bit textile to 32bit textile
  67. unsigned char* img = argb16to32(&arr[0], 256, 256);
  68. int r = getTextureManager().loadBufferSlot(img, 256, 256, ARGB, 32,
  69. TextureManager::TextureStorage::GAME, i);
  70. assert(r >= 0); //! \fixme properly handle error when texture could not be loaded!
  71. delete [] img;
  72. }
  73. }
  74. void LoaderTR2::loadRooms() {
  75. uint16_t numRooms = file.readU16();
  76. for (unsigned int i = 0; i < numRooms; i++) {
  77. // Room Header
  78. int32_t xOffset = file.read32();
  79. int32_t zOffset = file.read32();
  80. int32_t yBottom = file.read32(); // lowest point == largest y value
  81. int32_t yTop = file.read32(); // highest point == smallest y value
  82. // Number of data words (2 bytes) to follow
  83. uint32_t dataToFollow = file.readU32();
  84. std::vector<struct vertex_t> vertices;
  85. uint16_t numVertices = file.readU16();
  86. for (unsigned int v = 0; v < numVertices; v++) {
  87. struct vertex_t vert;
  88. // Vertex coordinates, relative to x/zOffset
  89. vert.x = file.read16();
  90. vert.y = file.read16();
  91. vert.z = file.read16();
  92. vert.light1 = file.read16();
  93. // Set of flags for special rendering effects
  94. // 0x8000 - Something to do with water surface?
  95. // 0x4000 - Underwater lighting modulation/movement if seen from above
  96. // 0x2000 - Water/Quicksand surface movement
  97. // 0x0010 - Normal?
  98. vert.attributes = file.readU16();
  99. vert.light2 = file.read16(); // Almost always equal to light1
  100. vertices.push_back(vert);
  101. }
  102. Room* room = new Room();
  103. uint16_t numRectangles = file.readU16();
  104. for (unsigned int r = 0; r < numRectangles; r++) {
  105. // Indices into the vertex list read just before
  106. uint16_t vertex1 = file.readU16();
  107. uint16_t vertex2 = file.readU16();
  108. uint16_t vertex3 = file.readU16();
  109. uint16_t vertex4 = file.readU16();
  110. // Index into the object-texture list
  111. uint16_t texture = file.readU16();
  112. room->getMesh().addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
  113. vertices.at(vertex3), vertices.at(vertex4), texture);
  114. }
  115. uint16_t numTriangles = file.readU16();
  116. for (unsigned int t = 0; t < numTriangles; t++) {
  117. // Indices into the room vertex list
  118. uint16_t vertex1 = file.readU16();
  119. uint16_t vertex2 = file.readU16();
  120. uint16_t vertex3 = file.readU16();
  121. // Index into the object-texture list
  122. uint16_t texture = file.readU16();
  123. room->getMesh().addTexturedTriangle(vertices.at(vertex1), vertices.at(vertex2),
  124. vertices.at(vertex3), texture);
  125. }
  126. uint16_t numSprites = file.readU16();
  127. for (unsigned int s = 0; s < numSprites; s++) {
  128. uint16_t vertex = file.readU16(); // Index into vertex list
  129. uint16_t texture = file.readU16(); // Index into object-texture list
  130. room->addSprite(new Sprite(vertices.at(vertex), texture));
  131. }
  132. uint16_t numPortals = file.readU16();
  133. for (unsigned int p = 0; p < numPortals; p++) {
  134. // Which room this portal leads to
  135. uint16_t adjoiningRoom = file.readU16();
  136. // Which way the portal faces
  137. // The normal points away from the adjacent room
  138. // To be seen through, it must point toward the viewpoint
  139. int16_t xNormal = file.read16();
  140. int16_t yNormal = file.read16();
  141. int16_t zNormal = file.read16();
  142. // The corners of this portal
  143. // The right-hand rule applies with respect to the normal
  144. int16_t xCorner1 = file.read16();
  145. int16_t yCorner1 = file.read16();
  146. int16_t zCorner1 = file.read16();
  147. int16_t xCorner2 = file.read16();
  148. int16_t yCorner2 = file.read16();
  149. int16_t zCorner2 = file.read16();
  150. int16_t xCorner3 = file.read16();
  151. int16_t yCorner3 = file.read16();
  152. int16_t zCorner3 = file.read16();
  153. int16_t xCorner4 = file.read16();
  154. int16_t yCorner4 = file.read16();
  155. int16_t zCorner4 = file.read16();
  156. // TODO store portals somewhere
  157. }
  158. uint16_t numZSectors = file.readU16();
  159. uint16_t numXSectors = file.readU16();
  160. for (unsigned int s = 0; s < (numZSectors * numXSectors); s++) {
  161. // Sectors are 1024*1024 world coordinates. Floor and Ceiling are
  162. // signed numbers of 256 units of height.
  163. // Floor/Ceiling value of 0x81 is used to indicate impenetrable
  164. // walls around the sector.
  165. // Floor values are used by the original engine to determine
  166. // what objects can be traversed and how. Relative steps of 1 (256)
  167. // can be walked up, 2..7 must be jumped up, larger than 7 is too high
  168. // If RoomAbove/Below is not none, the Ceiling/Floor is a collisional
  169. // portal to that room
  170. uint16_t indexFloorData = file.readU16();
  171. uint16_t indexBox = file.readU16(); // 0xFFFF if none
  172. uint8_t roomBelow = file.readU8(); // 0xFF if none
  173. int8_t floor = file.read8(); // Absolute height of floor (divided by 256)
  174. uint8_t roomAbove = file.readU8(); // 0xFF if none
  175. int8_t ceiling = file.read8(); // Absolute height of ceiling (/ 256)
  176. // TODO store sectors somewhere
  177. }
  178. int16_t intensity1 = file.read16();
  179. int16_t intensity2 = file.read16();
  180. int16_t lightMode = file.read16();
  181. uint16_t numLights = file.readU16();
  182. for (unsigned int l = 0; l < numLights; l++) {
  183. // Position of light, in world coordinates
  184. int32_t x = file.read32();
  185. int32_t y = file.read32();
  186. int32_t z = file.read32();
  187. uint16_t intensity1 = file.readU16();
  188. uint16_t intensity2 = file.readU16(); // Almost always equal to intensity1
  189. uint32_t fade1 = file.readU32(); // Falloff value?
  190. uint32_t fade2 = file.readU32(); // Falloff value?
  191. // TODO store light somewhere
  192. }
  193. uint16_t numStaticMeshes = file.readU16();
  194. for (unsigned int s = 0; s < numStaticMeshes; s++) {
  195. // Absolute position in world coordinates
  196. int32_t x = file.read32();
  197. int32_t y = file.read32();
  198. int32_t z = file.read32();
  199. // High two bits (0xC000) indicate steps of
  200. // 90 degrees (eg. (rotation >> 14) * 90)
  201. uint16_t rotation = file.readU16();
  202. // Constant lighting, 0xFFFF means use mesh lighting
  203. uint16_t intensity1 = file.readU16();
  204. uint16_t intensity2 = file.readU16();
  205. // Which StaticMesh item to draw
  206. uint16_t objectID = file.readU16();
  207. // TODO store static meshes somewhere
  208. }
  209. int16_t alternateRoom = file.read16();
  210. uint16_t flags = file.readU16();
  211. }
  212. }
  213. void LoaderTR2::loadFloorData() {
  214. uint32_t numFloorData = file.readU32();
  215. for (unsigned int f = 0; f < numFloorData; f++) {
  216. uint16_t unused = file.readU16();
  217. // TODO store floor data somewhere
  218. }
  219. }
  220. void LoaderTR2::loadMeshes() {
  221. // Number of bitu16s of mesh data to follow
  222. // Read all the mesh data into a buffer, because
  223. // only afterward we can read the number of meshes
  224. // in this data block
  225. uint32_t numMeshData = file.readU32();
  226. std::vector<uint16_t> buffer;
  227. for (unsigned int i = 0; i < numMeshData; i++) {
  228. buffer.push_back(file.readU16());
  229. }
  230. uint32_t numMeshPointers = file.readU32();
  231. if ((numMeshData == 0) || (numMeshPointers == 0)) {
  232. //! \fixme debug level regulating output?
  233. getLog() << "LoaderTR2: No mesh data found!" << Log::endl;
  234. return;
  235. }
  236. for (unsigned int i = 0; i < numMeshPointers; i++) {
  237. uint32_t meshPointer = file.readU32();
  238. char* tmpPtr = reinterpret_cast<char*>(&buffer[meshPointer]);
  239. BinaryMemory mem(tmpPtr, numMeshData - meshPointer);
  240. // TODO interpret the buffered mesh data
  241. }
  242. }
  243. void LoaderTR2::loadMoveables() {
  244. uint32_t numAnimations = file.readU32();
  245. for (unsigned int a = 0; a < numAnimations; a++) {
  246. // *Byte* Offset into Frames[] (so divide by 2!)
  247. uint32_t frameOffset = file.readU32();
  248. uint8_t frameRate = file.readU8(); // Engine ticks per frame
  249. // Number of bit16s in Frames[] used by this animation
  250. // Be careful when parsing frames using the FrameSize value
  251. // as the size of each frame, since an animations frame range
  252. // may extend into the next animations frame range, and that
  253. // may have a different FrameSize value.
  254. uint8_t frameSize = file.readU8();
  255. uint16_t stateID = file.readU16();
  256. file.seek(file.tell() + 8); // Skip 8 unknown bytes
  257. uint16_t frameStart = file.readU16(); // First frame in this animation
  258. uint16_t frameEnd = file.readU16(); // Last frame in this animation
  259. uint16_t nextAnimation = file.readU16();
  260. uint16_t nextFrame = file.readU16();
  261. uint16_t numStateChanges = file.readU16();
  262. uint16_t stateChangeOffset = file.readU16(); // Index into StateChanges[]
  263. uint16_t numAnimCommands = file.readU16(); // How many animation commands to use
  264. uint16_t animCommandOffset = file.readU16(); // Index into AnimCommand[]
  265. // TODO store animations somewhere
  266. }
  267. uint32_t numStateChanges = file.readU32();
  268. for (unsigned int s = 0; s < numStateChanges; s++) {
  269. uint16_t stateID = file.readU16();
  270. uint16_t numAnimDispatches = file.readU16();
  271. uint16_t animDispatchOffset = file.readU16(); // Index into AnimDispatches[]
  272. // TODO store state changes somewhere
  273. }
  274. uint32_t numAnimDispatches = file.readU32();
  275. for (unsigned int a = 0; a < numAnimDispatches; a++) {
  276. int16_t low = file.read16(); // Lowest frame that uses this range
  277. int16_t high = file.read16(); // Highest frame (+1?) that uses this range
  278. int16_t nextAnimation = file.read16(); // Animation to go to
  279. int16_t nextFrame = file.read16(); // Frame offset to go to
  280. // TODO store animation dispatches somewhere
  281. }
  282. uint32_t numAnimCommands = file.readU32();
  283. std::vector<int16_t> animCommands;
  284. for (unsigned int a = 0; a < numAnimCommands; a++) {
  285. animCommands.push_back(file.read16());
  286. }
  287. uint32_t numMeshTrees = file.readU32();
  288. for (unsigned int m = 0; m < numMeshTrees; m++) {
  289. // 0x0002 - Put parent mesh on the mesh stack
  290. // 0x0001 - Pop mesh from stack, use as parent mesh
  291. // When both are not set, use previous mesh as parent mesh
  292. // When both are set, do 0x0001 first, then 0x0002, thereby
  293. // reading the stack but not changing it
  294. uint32_t flags = file.readU32();
  295. // Offset of mesh origin from the parent mesh origin
  296. /* Where the hell does this come from?!
  297. int32_t x = file.read32();
  298. int32_t y = file.read32();
  299. int32_t z = file.read32();
  300. */
  301. // TODO store mesh trees somewhere
  302. }
  303. uint32_t numFrames = file.readU32();
  304. std::vector<uint16_t> frames;
  305. for (unsigned int f = 0; f < numFrames; f++) {
  306. frames.push_back(file.readU16());
  307. }
  308. uint32_t numMoveables = file.readU32();
  309. for (unsigned int m = 0; m < numMoveables; m++) {
  310. // Item identifier, matched in Items[]
  311. uint32_t objectID = file.readU32();
  312. uint16_t numMeshes = file.readU16();
  313. uint16_t startingMesh = file.readU16(); // Offset into MeshPointers[]
  314. uint32_t meshTree = file.readU32(); // Offset into MeshTree[]
  315. // *Byte* offset into Frames[] (divide by 2 for Frames[i])
  316. uint32_t frameOffset = file.readU32();
  317. // If animation index is 0xFFFF, the object is stationary or
  318. // animated by the engine (ponytail)
  319. uint16_t animation = file.readU16();
  320. // TODO store moveables somewhere
  321. }
  322. // TODO combine all this into moveables with their animations
  323. }
  324. void LoaderTR2::loadStaticMeshes() {
  325. uint32_t numStaticMeshes = file.readU32();
  326. for (unsigned int s = 0; s < numStaticMeshes; s++) {
  327. uint32_t objectID = file.readU32(); // Matched in Items[]
  328. uint16_t mesh = file.readU16(); // Offset into MeshPointers[]
  329. // tr2_vertex BoundingBox[2][2];
  330. // First index is which one, second index is opposite corners
  331. int16_t x11 = file.read16();
  332. int16_t y11 = file.read16();
  333. int16_t z11 = file.read16();
  334. int16_t x12 = file.read16();
  335. int16_t y12 = file.read16();
  336. int16_t z12 = file.read16();
  337. int16_t x21 = file.read16();
  338. int16_t y21 = file.read16();
  339. int16_t z21 = file.read16();
  340. int16_t x22 = file.read16();
  341. int16_t y22 = file.read16();
  342. int16_t z22 = file.read16();
  343. // Meaning uncertain. Usually 2, and 3 for objects Lara can
  344. // travel through, like TR2s skeletons and underwater plants
  345. uint16_t flags = file.readU16();
  346. // TODO store static meshes somewhere
  347. }
  348. }
  349. void LoaderTR2::loadTextures() {
  350. uint32_t numObjectTextures = file.readU32();
  351. for (unsigned int o = 0; o < numObjectTextures; o++) {
  352. // 0 means that a texture is all-opaque, and that transparency
  353. // information is ignored.
  354. // 1 means that transparency information is used. In 8-bit color,
  355. // index 0 is the transparent color, while in 16-bit color, the
  356. // top bit (0x8000) is the alpha channel (1 = opaque, 0 = transparent)
  357. uint16_t attribute = file.readU16();
  358. // Index into the textile list
  359. uint16_t tile = file.readU16();
  360. TextureTile* t = new TextureTile(attribute, tile);
  361. // The four corner vertices of the texture
  362. // The Pixel values are the actual coordinates of the vertexs pixel
  363. // The Coordinate values depend on where the other vertices are in
  364. // the object texture. And if the object texture is used to specify
  365. // a triangle, then the fourth vertexs values will all be zero
  366. // Coordinate is 1 if Pixel is the low val, 255 if high val in object texture
  367. for (int i = 0; i < 4; i++) {
  368. uint8_t xCoordinate = file.readU8();
  369. uint8_t xPixel = file.readU8();
  370. uint8_t yCoordinate = file.readU8();
  371. uint8_t yPixel = file.readU8();
  372. assert((xCoordinate != 1) || (xCoordinate != 255));
  373. assert((yCoordinate != 1) || (yCoordinate != 255));
  374. t->add(new TextureTileVertex(xCoordinate, xPixel, yCoordinate, yPixel));
  375. }
  376. getTextureManager().addTile(t);
  377. }
  378. }
  379. void LoaderTR2::loadSprites() {
  380. uint32_t numSpriteTextures = file.readU32();
  381. for (unsigned int s = 0; s < numSpriteTextures; s++) {
  382. uint16_t tile = file.readU16();
  383. uint8_t x = file.readU8();
  384. uint8_t y = file.readU8();
  385. uint16_t width = file.readU16(); // Actually (width * 256) + 255
  386. uint16_t height = file.readU16(); // Actually (height * 256) + 255
  387. int16_t leftSide = file.read16();
  388. int16_t topSide = file.read16();
  389. int16_t rightSide = file.read16();
  390. int16_t bottomSide = file.read16();
  391. // TODO store sprite textures somewhere
  392. }
  393. uint32_t numSpriteSequences = file.readU32();
  394. for (unsigned int s = 0; s < numSpriteSequences; s++) {
  395. int32_t objectID = file.read32(); // Item identifier, matched in Items[]
  396. int16_t negativeLength = file.read16(); // Negative sprite count
  397. int16_t offset = file.read16(); // Where sequence starts in sprite texture list
  398. // TODO store sprite sequences somewhere
  399. }
  400. }
  401. void LoaderTR2::loadCameras() {
  402. uint32_t numCameras = file.readU32();
  403. for (unsigned int c = 0; c < numCameras; c++) {
  404. int32_t x = file.read32();
  405. int32_t y = file.read32();
  406. int32_t z = file.read32();
  407. int16_t room = file.read16();
  408. file.seek(file.tell() + 2); // Unknown, correlates to Boxes? Zones?
  409. // TODO store cameras somewhere
  410. }
  411. }
  412. void LoaderTR2::loadSoundSources() {
  413. uint32_t numSoundSources = file.readU32();
  414. for (unsigned int s = 0; s < numSoundSources; s++) {
  415. // Absolute world coordinate positions of sound source
  416. int32_t x = file.read32();
  417. int32_t y = file.read32();
  418. int32_t z = file.read32();
  419. // Internal sound index
  420. uint16_t soundID = file.readU16();
  421. // Unknown, 0x40, 0x80 or 0xC0
  422. uint16_t flags = file.readU16();
  423. // TODO store sound sources somewhere
  424. }
  425. }
  426. void LoaderTR2::loadBoxesOverlapsZones() {
  427. uint32_t numBoxes = file.readU32();
  428. for (unsigned int b = 0; b < numBoxes; b++) {
  429. // Sectors (* 1024 units)
  430. uint8_t zMin = file.readU8();
  431. uint8_t zMax = file.readU8();
  432. uint8_t xMin = file.readU8();
  433. uint8_t xMax = file.readU8();
  434. int16_t trueFloor = file.read16(); // Y value (no scaling)
  435. // Index into overlaps[]. The high bit is sometimes set
  436. // this occurs in front of swinging doors and the like
  437. int16_t overlapIndex = file.read16();
  438. // TODO store boxes somewhere
  439. }
  440. uint32_t numOverlaps = file.readU32();
  441. std::vector<uint16_t> overlaps;
  442. for (unsigned int o = 0; o < numOverlaps; o++) {
  443. overlaps.push_back(file.readU16());
  444. }
  445. // TODO store overlaps somewhere
  446. std::vector<int16_t> zones;
  447. for (unsigned int z = 0; z < numBoxes; z++) {
  448. for (unsigned int i = 0; i < 10; i++) {
  449. zones.push_back(file.read16());
  450. }
  451. }
  452. // TODO store zones somewhere
  453. if ((numBoxes > 0) || (numOverlaps > 0))
  454. getLog() << "LoaderTR2: Found NPC NavigationHints, not yet implemented!" << Log::endl;
  455. }
  456. void LoaderTR2::loadAnimatedTextures() {
  457. uint32_t numAnimatedTextures = file.readU32();
  458. std::vector<uint16_t> animatedTextures;
  459. for (unsigned int a = 0; a < numAnimatedTextures; a++) {
  460. animatedTextures.push_back(file.readU16());
  461. }
  462. // TODO store animated textures somewhere. Format?
  463. }
  464. void LoaderTR2::loadItems() {
  465. uint32_t numItems = file.readU32();
  466. for (unsigned int i = 0; i < numItems; i++) {
  467. int16_t objectID = file.read16();
  468. int16_t room = file.read16();
  469. // Item position in world coordinates
  470. int32_t x = file.read32();
  471. int32_t y = file.read32();
  472. int32_t z = file.read32();
  473. int16_t angle = file.read16(); // (0xC000 >> 14) * 90deg
  474. int16_t intensity1 = file.read16(); // Constant lighting; -1 means mesh lighting
  475. int16_t intensity2 = file.read16(); // Almost always like intensity1
  476. // 0x0100 - Initially visible
  477. // 0x3E00 - Activation mask, open, can be XORed with related FloorData list fields.
  478. uint16_t flags = file.readU16();
  479. // TODO store items somewhere
  480. }
  481. }
  482. void LoaderTR2::loadCinematicFrames() {
  483. uint16_t numCinematicFrames = file.readU16();
  484. for (unsigned int c = 0; c < numCinematicFrames; c++) {
  485. int16_t rotY = file.read16(); // Y rotation, +-32767 = +-180deg
  486. int16_t rotZ = file.read16(); // Z rotation, like rotY
  487. int16_t rotZ2 = file.read16(); // Like rotZ?
  488. int16_t posZ = file.read16(); // Camera pos relative to what?
  489. int16_t posY = file.read16();
  490. int16_t posX = file.read16();
  491. int16_t unknown = file.read16(); // Changing this can cause runtime error
  492. int16_t rotX = file.read16(); // X rotation, like rotY
  493. // TODO store cinematic frames somewhere
  494. }
  495. if (numCinematicFrames > 0)
  496. getLog() << "LoaderTR2: Found CinematicFrames, not yet implemented!" << Log::endl;
  497. }
  498. void LoaderTR2::loadDemoData() {
  499. uint16_t numDemoData = file.readU16();
  500. for (unsigned int d = 0; d < numDemoData; d++)
  501. file.readU8();
  502. // TODO store demo data somewhere, find out meaning
  503. if (numDemoData > 0)
  504. getLog() << "LoaderTR2: Found DemoData, not yet implemented!" << Log::endl;
  505. }
  506. void LoaderTR2::loadSoundMap() {
  507. std::array<int16_t, 370> soundMap;
  508. for (auto& x : soundMap) {
  509. x = file.read16();
  510. }
  511. // TODO store sound map somewhere
  512. }
  513. void LoaderTR2::loadSoundDetails() {
  514. uint32_t numSoundDetails = file.readU32();
  515. for (unsigned int s = 0; s < numSoundDetails; s++) {
  516. int16_t sample = file.read16(); // Index into SampleIndices[]
  517. int16_t volume = file.read16();
  518. // sound range? distance at which this sound can be heard?
  519. int16_t unknown1 = file.read16();
  520. // Bits 8-15: priority?
  521. // Bits 2-7: number of samples in this group
  522. // Bits 0-1: channel number?
  523. int16_t unknown2 = file.read16();
  524. // TODO store sound details somewhere
  525. }
  526. }
  527. void LoaderTR2::loadSampleIndices() {
  528. uint32_t numSampleIndices = file.readU32();
  529. std::vector<uint32_t> sampleIndices;
  530. for (unsigned int i = 0; i < numSampleIndices; i++) {
  531. sampleIndices.push_back(file.readU32());
  532. }
  533. // TODO store sample indices somewhere
  534. }
  535. void LoaderTR2::loadExternalSoundFile(std::string f) {
  536. size_t dir = f.find_last_of("/\\");
  537. if (dir != std::string::npos) {
  538. f.replace(dir + 1, std::string::npos, "MAIN.SFX");
  539. } else {
  540. f = "MAIN.SFX";
  541. }
  542. BinaryFile sfx;
  543. if (sfx.open(f) != 0) {
  544. getLog() << "LoaderTR2: Can't open \"" << f << "\"!" << Log::endl;
  545. return;
  546. }
  547. int riffCount = 0;
  548. bool done = false;
  549. while (!done) {
  550. char test[5];
  551. test[4] = '\0';
  552. for (int i = 0; i < 4; i++)
  553. test[i] = sfx.read8();
  554. if (std::string("RIFF") != std::string(test)) {
  555. getLog() << "LoaderTR2: External SFX invalid! (" << riffCount
  556. << ", \"" << test << "\" != \"RIFF\")" << Log::endl;
  557. return;
  558. }
  559. uint32_t riffSize = sfx.readU32();
  560. for (int i = 0; i < 4; i++)
  561. test[i] = sfx.read8();
  562. if (std::string("WAVE") != std::string(test)) {
  563. getLog() << "LoaderTR2: External SFX invalid! (" << riffCount
  564. << ", \"" << test << "\" != \"WAVE\")" << Log::endl;
  565. return;
  566. }
  567. // TODO load riff somehow somewhere
  568. // riffSize is (fileLength - 8)
  569. sfx.seek(sfx.tell() + riffSize - 4);
  570. riffCount++;
  571. if (sfx.eof()) {
  572. done = true;
  573. getLog() << "LoaderTR2: Found " << riffCount << " SoundSamples" << Log::endl;
  574. }
  575. }
  576. }