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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*!
  2. * \file src/Game.cpp
  3. * \brief Game abstraction
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include <map>
  9. #include <cstdlib>
  10. #include "global.h"
  11. #include "Console.h"
  12. #include "Game.h"
  13. #include "OpenRaider.h"
  14. #include "Sound.h"
  15. #include "utils/strings.h"
  16. #include "games/TombRaider1.h"
  17. #ifdef EXPERIMENTAL
  18. std::vector<unsigned int> gColorTextureHACK;
  19. #endif
  20. #ifdef MULTITEXTURE
  21. std::map<int, int> gMapTex2Bump;
  22. #endif
  23. Game::Game() {
  24. mLoaded = false;
  25. mName = NULL;
  26. mLara = -1;
  27. mTextureStart = 0;
  28. mTextureOffset = 0;
  29. }
  30. Game::~Game() {
  31. destroy();
  32. }
  33. unsigned int Game::getTextureStart() {
  34. return mTextureStart;
  35. }
  36. unsigned int Game::getTextureOffset() {
  37. return mTextureOffset;
  38. }
  39. int Game::initialize() {
  40. // Enable Renderer
  41. mTextureStart = getRender().initTextures(getOpenRaider().mDataDir);
  42. getRender().setMode(Render::modeLoadScreen);
  43. return 0;
  44. }
  45. void Game::destroy() {
  46. if (mName)
  47. delete [] mName;
  48. mName = NULL;
  49. mLoaded = false;
  50. mLara = -1;
  51. getRender().setMode(Render::modeDisabled);
  52. getWorld().destroy();
  53. getRender().ClearWorld();
  54. getSound().clear(); // Remove all previously loaded sounds
  55. }
  56. bool Game::isLoaded() {
  57. return mLoaded;
  58. }
  59. int Game::loadLevel(const char *level) {
  60. if (mLoaded)
  61. destroy();
  62. mName = bufferString("%s", level);
  63. getConsole().print("Loading %s", mName);
  64. int error = mTombRaider.Load(mName);
  65. if (error != 0)
  66. return error;
  67. // If required, load the external sound effect file MAIN.SFX into TombRaider
  68. if ((mTombRaider.getEngine() == TR_VERSION_2) || (mTombRaider.getEngine() == TR_VERSION_3)) {
  69. char *tmp = bufferString("%sMAIN.SFX", level); // Ensure theres enough space
  70. size_t length = strlen(tmp);
  71. size_t dir = 0;
  72. for (int i = length - 1; i >= 0; i--) {
  73. if ((tmp[i] == '/') || (tmp[i] == '\\')) {
  74. dir = i + 1; // Find where the filename (bla.tr2) starts
  75. break;
  76. }
  77. }
  78. strcpy(tmp + dir, "MAIN.SFX"); // overwrite the name itself with MAIN.SFX
  79. tmp[dir + 8] = '\0';
  80. error = mTombRaider.loadSFX(tmp);
  81. if (error != 0)
  82. getConsole().print("Could not load %s", tmp);
  83. delete [] tmp;
  84. }
  85. // Process data
  86. processTextures();
  87. processRooms();
  88. processModels();
  89. processSprites();
  90. processMoveables();
  91. processPakSounds();
  92. mTombRaider.reset();
  93. if (mLara == -1) {
  94. getConsole().print("Can't find Lara entity in level pak!");
  95. destroy();
  96. return -1;
  97. } else {
  98. mLoaded = true;
  99. getRender().setMode(Render::modeVertexLight);
  100. return 0;
  101. }
  102. }
  103. void Game::handleAction(ActionEvents action, bool isFinished) {
  104. if (mLoaded && (!isFinished)) {
  105. if (action == forwardAction) {
  106. getLara().move('f');
  107. } else if (action == backwardAction) {
  108. getLara().move('b');
  109. } else if (action == leftAction) {
  110. getLara().move('l');
  111. } else if (action == rightAction) {
  112. getLara().move('r');
  113. }
  114. }
  115. }
  116. void Game::handleMouseMotion(int xrel, int yrel) {
  117. if (mLoaded) {
  118. // Move Camera on X Axis
  119. if (xrel > 0)
  120. while (xrel-- > 0)
  121. getCamera().command(CAMERA_ROTATE_RIGHT);
  122. else if (xrel < 0)
  123. while (xrel++ < 0)
  124. getCamera().command(CAMERA_ROTATE_LEFT);
  125. // Move Camera on Y Axis
  126. if (yrel > 0)
  127. while (yrel-- > 0)
  128. getCamera().command(CAMERA_ROTATE_UP);
  129. else if (yrel < 0)
  130. while (yrel++ < 0)
  131. getCamera().command(CAMERA_ROTATE_DOWN);
  132. // Fix Laras rotation
  133. getLara().setAngle(getCamera().getRadianYaw());
  134. }
  135. }
  136. Entity &Game::getLara() {
  137. assert(mLara >= 0);
  138. assert(mLara < (int)getWorld().sizeEntity());
  139. return getWorld().getEntity(mLara);
  140. }
  141. void Game::processSprites() {
  142. for (int i = 0; i < (mTombRaider.NumItems() - 1); i++) {
  143. if ((mTombRaider.Engine() == TR_VERSION_1) && (mTombRaider.Item()[i].intensity1 == -1))
  144. continue;
  145. for (int j = 0; j < mTombRaider.NumSpriteSequences(); j++) {
  146. if (mTombRaider.SpriteSequence()[j].object_id == mTombRaider.Item()[i].object_id)
  147. getWorld().addSprite(*new SpriteSequence(mTombRaider, i, j));
  148. }
  149. }
  150. getConsole().print("Found %d sprites.", mTombRaider.NumSpriteSequences());
  151. }
  152. void Game::processRooms() {
  153. for (int index = 0; index < mTombRaider.NumRooms(); index++)
  154. getWorld().addRoom(*new Room(mTombRaider, index));
  155. getConsole().print("Found %d rooms.", mTombRaider.NumRooms());
  156. }
  157. void Game::processPakSounds()
  158. {
  159. unsigned char *riff;
  160. unsigned int riffSz;
  161. //tr2_sound_source_t *sound;
  162. //tr2_sound_details_t *detail;
  163. //float pos[3];
  164. unsigned int i;
  165. int id;
  166. /* detail
  167. short sample;
  168. short volume;
  169. short sound_range;
  170. short flags; // bits 8-15: priority?, 2-7: number of sound samples
  171. // in this group, bits 0-1: channel number
  172. */
  173. for (i = 0; i < mTombRaider.getSoundSamplesCount(); ++i)
  174. {
  175. mTombRaider.getSoundSample(i, &riffSz, &riff);
  176. getSound().addWave(riff, riffSz, &id, Sound::SoundFlagsNone);
  177. //if (((i + 1) == TR_SOUND_F_PISTOL) && (id > 0))
  178. //{
  179. //m_testSFX = id;
  180. //}
  181. delete [] riff;
  182. // sound[i].sound_id; // internal sound index
  183. // sound[i].flags; // 0x40, 0x80, or 0xc0
  184. //pos[0] = sound[i].x;
  185. //pos[1] = sound[i].y;
  186. //pos[2] = sound[i].z;
  187. //getSound().SourceAt(id, pos);
  188. }
  189. getConsole().print("Found %u sound samples.", mTombRaider.getSoundSamplesCount());
  190. }
  191. void Game::processTextures()
  192. {
  193. unsigned char *image;
  194. unsigned char *bumpmap;
  195. int i;
  196. //if ( mTombRaider.getNumBumpMaps())
  197. // gBumpMapStart = mTombRaider.NumTextures();
  198. for (i = 0; i < mTombRaider.NumTextures(); ++i)
  199. {
  200. mTombRaider.Texture(i, &image, &bumpmap);
  201. // Overwrite any previous level textures on load
  202. getRender().loadTexture(image, 256, 256, (mTextureStart - 1) + i);
  203. #ifdef MULTITEXTURE
  204. gMapTex2Bump[(mTextureStart - 1) + i] = -1;
  205. #endif
  206. if (bumpmap)
  207. {
  208. #ifdef MULTITEXTURE
  209. gMapTex2Bump[(mTextureStart - 1) + i] = (mTextureStart - 1) + i +
  210. mTombRaider.NumTextures();
  211. #endif
  212. getRender().loadTexture(bumpmap, 256, 256, (mTextureStart - 1) + i +
  213. mTombRaider.NumTextures());
  214. }
  215. if (image)
  216. delete [] image;
  217. if (bumpmap)
  218. delete [] bumpmap;
  219. }
  220. mTextureOffset = (mTextureStart - 1) + mTombRaider.NumTextures();
  221. getConsole().print("Found %d textures.", mTombRaider.NumTextures());
  222. }
  223. void Game::processMoveables()
  224. {
  225. unsigned int statCount = 0;
  226. tr2_moveable_t *moveable = mTombRaider.Moveable();
  227. tr2_item_t *item = mTombRaider.Item();
  228. tr2_sprite_sequence_t *sprite_sequence = mTombRaider.SpriteSequence();
  229. for (int i = 0; i < mTombRaider.NumItems(); ++i)
  230. {
  231. int j;
  232. int object_id = item[i].object_id;
  233. // It may not be a moveable, test for sprite
  234. if (!(mTombRaider.Engine() == TR_VERSION_1 && item[i].intensity1 == -1)) {
  235. for (j = 0; j < (int)mTombRaider.NumSpriteSequences(); ++j) {
  236. if (sprite_sequence[j].object_id == object_id)
  237. break;
  238. }
  239. // It's not a moveable, skip sprite
  240. if (j < (int)mTombRaider.NumSpriteSequences())
  241. continue;
  242. }
  243. for (j = 0; j < (int)mTombRaider.NumMoveables(); ++j) {
  244. if ((int)moveable[j].object_id == object_id)
  245. break;
  246. }
  247. // It's not a moveable or even a sprite? Skip unknown
  248. if (j == (int)mTombRaider.NumMoveables())
  249. continue;
  250. processMoveable(j, i, object_id);
  251. statCount++;
  252. }
  253. /*
  254. // Get models that aren't items
  255. for (int i = 0; i < mTombRaider.NumMoveables(); ++i)
  256. {
  257. switch ((int)moveable[i].object_id)
  258. {
  259. case 30:
  260. case 2: // Which tr needs this as model again?
  261. processMoveable(i, i, (int)moveable[i].object_id);
  262. break;
  263. default:
  264. switch (mTombRaider.Engine())
  265. {
  266. case TR_VERSION_1:
  267. switch ((int)moveable[i].object_id)
  268. {
  269. case TombRaider1::LaraMutant:
  270. processMoveable(i, i, (int)moveable[i].object_id);
  271. break;
  272. }
  273. break;
  274. case TR_VERSION_4:
  275. switch ((int)moveable[i].object_id)
  276. {
  277. case TR4_PISTOLS_ANIM:
  278. case TR4_UZI_ANIM:
  279. case TR4_SHOTGUN_ANIM:
  280. case TR4_CROSSBOW_ANIM:
  281. case TR4_GRENADE_GUN_ANIM:
  282. case TR4_SIXSHOOTER_ANIM:
  283. processMoveable(i, i, (int)moveable[i].object_id);
  284. break;
  285. }
  286. break;
  287. case TR_VERSION_2:
  288. case TR_VERSION_3:
  289. case TR_VERSION_5:
  290. case TR_VERSION_UNKNOWN:
  291. break;
  292. }
  293. }
  294. }
  295. */
  296. getConsole().print("Found %d moveables.", mTombRaider.NumMoveables() + statCount);
  297. }
  298. // index moveable, i item, sometimes both moveable
  299. // object_id of item or moveable
  300. void Game::processMoveable(int index, int i, int object_id) {
  301. // Check if the SkeletalModel is already cached
  302. bool cached = false;
  303. unsigned int model;
  304. for (model = 0; model < getWorld().sizeSkeletalModel(); model++) {
  305. if (getWorld().getSkeletalModel(model).getId() == object_id) {
  306. cached = true;
  307. break;
  308. }
  309. }
  310. // Create a new SkeletalModel, if needed
  311. if (!cached)
  312. getWorld().addSkeletalModel(*new SkeletalModel(mTombRaider, index, object_id));
  313. // Create a new Entity, using the cached or the new SkeletalModel
  314. Entity &entity = *new Entity(mTombRaider, index, i, model);
  315. getWorld().addEntity(entity);
  316. // Store reference to Lara
  317. if (entity.getObjectId() == 0)
  318. mLara = getWorld().sizeEntity() - 1;
  319. // Store reference to the SkyMesh
  320. if (i == mTombRaider.getSkyModelId())
  321. getRender().setSkyMesh(i, //moveable[i].starting_mesh,
  322. (mTombRaider.Engine() == TR_VERSION_2));
  323. }
  324. bool compareFaceTextureId(const void *voidA, const void *voidB)
  325. {
  326. texture_tri_t *a = (texture_tri_t *)voidA, *b = (texture_tri_t *)voidB;
  327. if (!a || !b)
  328. return false; // error really
  329. return (a->texture < b->texture);
  330. }
  331. #ifdef EXPERIMENTAL
  332. void Game::setupTextureColor(texture_tri_t *r_tri, float *colorf)
  333. {
  334. unsigned char color[4];
  335. unsigned int colorI;
  336. color[0] = (unsigned char)(colorf[0]*255.0f);
  337. color[1] = (unsigned char)(colorf[1]*255.0f);
  338. color[2] = (unsigned char)(colorf[2]*255.0f);
  339. color[3] = (unsigned char)(colorf[3]*255.0f);
  340. ((unsigned char *)(&colorI))[3] = color[0];
  341. ((unsigned char *)(&colorI))[2] = color[1];
  342. ((unsigned char *)(&colorI))[1] = color[2];
  343. ((unsigned char *)(&colorI))[0] = color[3];
  344. bool found = false;
  345. unsigned int foundIndex = 0;
  346. for (foundIndex = 0; foundIndex < gColorTextureHACK.size(); foundIndex++) {
  347. if (gColorTextureHACK[foundIndex] == colorI) {
  348. found = true;
  349. break;
  350. }
  351. }
  352. if (!found)
  353. {
  354. gColorTextureHACK.push_back(colorI);
  355. r_tri->texture = mTextureOffset + gColorTextureHACK.size();
  356. getRender().loadTexture(Texture::generateColorTexture(color, 32, 32),
  357. 32, 32, r_tri->texture);
  358. }
  359. else
  360. {
  361. //getConsole().print("Color already loaded %i -> 0x%08x",
  362. // gColorTextureHACK.getCurrentIndex(),
  363. // gColorTextureHACK.current());
  364. r_tri->texture = mTextureOffset + foundIndex;
  365. }
  366. //r_tri->texture = white; // White texture
  367. }
  368. #endif
  369. void Game::processModels()
  370. {
  371. for (int index = 0; index < mTombRaider.getMeshCount(); index++) {
  372. int i, j, count, texture;
  373. int vertexIndices[6];
  374. float st[12];
  375. float color[4];
  376. unsigned short transparency;
  377. texture_tri_t *r_tri;
  378. // Assert common sense
  379. if (index < 0 || !mTombRaider.isMeshValid(index))
  380. {
  381. //! \fixme allow sparse lists with matching ids instead?
  382. getWorld().addMesh(NULL); // Filler, to make meshes array ids align
  383. return;
  384. }
  385. #ifndef EXPERIMENTAL
  386. // WHITE texture id
  387. int white = 0;
  388. #endif
  389. model_mesh_t *mesh = new model_mesh_t;
  390. // Mongoose 2002.08.30, Testing support for 'shootable' models ( traceable )
  391. mTombRaider.getMeshCollisionInfo(index, mesh->center, &mesh->radius);
  392. //! \fixme Arrays don't work either =)
  393. // Mesh geometery, colors, etc
  394. mTombRaider.getMeshVertexArrays(index,
  395. &mesh->vertexCount, &mesh->vertices,
  396. &mesh->normalCount, &mesh->normals,
  397. &mesh->colorCount, &mesh->colors);
  398. // Textured Triangles
  399. count = mTombRaider.getMeshTexturedTriangleCount(index);
  400. mesh->texturedTriangles.reserve(count); // little faster
  401. for (i = 0; i < count; ++i)
  402. {
  403. r_tri = new texture_tri_t;
  404. mTombRaider.getMeshTexturedTriangle(index, i,
  405. r_tri->index,
  406. r_tri->st,
  407. &r_tri->texture,
  408. &r_tri->transparency);
  409. r_tri->texture += mTextureStart;
  410. // Add to face vector
  411. mesh->texturedTriangles.push_back(r_tri);
  412. }
  413. // Coloured Triangles
  414. count = mTombRaider.getMeshColoredTriangleCount(index);
  415. mesh->coloredTriangles.reserve(count); // little faster
  416. for (i = 0; i < count; i++)
  417. {
  418. r_tri = new texture_tri_t;
  419. mTombRaider.getMeshColoredTriangle(index, i,
  420. r_tri->index,
  421. color);
  422. r_tri->st[0] = color[0];
  423. r_tri->st[1] = color[1];
  424. r_tri->st[2] = color[2];
  425. r_tri->st[3] = color[3];
  426. r_tri->st[4] = 1.0;
  427. r_tri->st[5] = 1.0;
  428. #ifdef EXPERIMENTAL
  429. setupTextureColor(r_tri, color);
  430. #else
  431. r_tri->texture = white; // White texture
  432. #endif
  433. r_tri->transparency = 0;
  434. // Add to face vector
  435. mesh->coloredTriangles.push_back(r_tri);
  436. }
  437. // Textured Rectangles
  438. count = mTombRaider.getMeshTexturedRectangleCount(index);
  439. mesh->texturedRectangles.reserve(count*2); // little faster
  440. for (i = 0; i < count; ++i)
  441. {
  442. mTombRaider.getMeshTexturedRectangle(index, i,
  443. vertexIndices,
  444. st,
  445. &texture,
  446. &transparency);
  447. r_tri = new texture_tri_t;
  448. for (j = 0; j < 3; ++j)
  449. r_tri->index[j] = vertexIndices[j];
  450. for (j = 0; j < 6; ++j)
  451. r_tri->st[j] = st[j];
  452. r_tri->texture = texture + mTextureStart;
  453. r_tri->transparency = transparency;
  454. // Add to face vector
  455. mesh->texturedRectangles.push_back(r_tri);
  456. r_tri = new texture_tri_t;
  457. for (j = 3; j < 6; ++j)
  458. r_tri->index[j-3] = vertexIndices[j];
  459. for (j = 6; j < 12; ++j)
  460. r_tri->st[j-6] = st[j];
  461. r_tri->texture = texture + mTextureStart;
  462. r_tri->transparency = transparency;
  463. // Add to face vector
  464. mesh->texturedRectangles.push_back(r_tri);
  465. }
  466. // Coloured Rectangles
  467. count = mTombRaider.getMeshColoredRectangleCount(index);
  468. mesh->coloredRectangles.reserve(count*2); // little faster
  469. for (i = 0; i < count; ++i)
  470. {
  471. mTombRaider.getMeshColoredRectangle(index, i,
  472. vertexIndices,
  473. color);
  474. r_tri = new texture_tri_t;
  475. for (j = 0; j < 3; ++j)
  476. r_tri->index[j] = vertexIndices[j];
  477. //for (j = 0; j < 6; ++j)
  478. // r_tri->st[j] = st[j];
  479. r_tri->st[0] = color[0];
  480. r_tri->st[1] = color[1];
  481. r_tri->st[2] = color[2];
  482. r_tri->st[3] = color[3];
  483. r_tri->st[4] = 1.0;
  484. r_tri->st[5] = 1.0;
  485. #ifdef EXPERIMENTAL
  486. //for (j = 6; j < 12; ++j)
  487. // r_tri->st[j-6] = st[j];
  488. setupTextureColor(r_tri, color);
  489. #else
  490. r_tri->texture = white; // White texture
  491. #endif
  492. r_tri->transparency = 0;
  493. // Add to face vector
  494. mesh->coloredRectangles.push_back(r_tri);
  495. r_tri = new texture_tri_t;
  496. for (j = 3; j < 6; ++j)
  497. r_tri->index[j-3] = vertexIndices[j];
  498. //for (j = 6; j < 12; ++j)
  499. // r_tri->st[j-6] = st[j];
  500. r_tri->st[0] = color[0];
  501. r_tri->st[1] = color[1];
  502. r_tri->st[2] = color[2];
  503. r_tri->st[3] = color[3];
  504. r_tri->st[4] = 1.0;
  505. r_tri->st[5] = 1.0;
  506. #ifdef EXPERIMENTAL
  507. setupTextureColor(r_tri, color);
  508. #else
  509. r_tri->texture = white; // White texture
  510. #endif
  511. r_tri->transparency = 0;
  512. // Add to face vector
  513. mesh->coloredRectangles.push_back(r_tri);
  514. }
  515. // Sort faces by texture
  516. std::sort(mesh->texturedTriangles.begin(), mesh->texturedTriangles.end(), compareFaceTextureId);
  517. std::sort(mesh->coloredTriangles.begin(), mesh->coloredTriangles.end(), compareFaceTextureId);
  518. std::sort(mesh->texturedRectangles.begin(), mesh->texturedRectangles.end(), compareFaceTextureId);
  519. std::sort(mesh->coloredRectangles.begin(), mesh->coloredRectangles.end(), compareFaceTextureId);
  520. getWorld().addMesh(mesh);
  521. }
  522. getConsole().print("Found %d meshes.", mTombRaider.getMeshCount());
  523. }