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

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