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.

Game.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 <cstring>
  11. #include "global.h"
  12. #include "Camera.h"
  13. #include "Game.h"
  14. #include "loader/Loader.h"
  15. #include "Log.h"
  16. #include "Render.h"
  17. #include "Sound.h"
  18. #include "StaticMesh.h"
  19. #include "TextureManager.h"
  20. #include "World.h"
  21. #include "utils/strings.h"
  22. #include "games/TombRaider1.h"
  23. #ifdef MULTITEXTURE
  24. std::map<int, int> gMapTex2Bump;
  25. #endif
  26. Game::Game() {
  27. mLoaded = false;
  28. mLara = -1;
  29. mTextureStart = 0;
  30. mTextureOffset = 0;
  31. }
  32. Game::~Game() {
  33. }
  34. unsigned int Game::getTextureStart() {
  35. return mTextureStart;
  36. }
  37. unsigned int Game::getTextureOffset() {
  38. return mTextureOffset;
  39. }
  40. int Game::initialize() {
  41. // Enable Renderer
  42. getRender().setMode(Render::modeLoadScreen);
  43. mTextureStart = getTextureManager().getTextureCount();
  44. return 0;
  45. }
  46. void Game::display() {
  47. getRender().display();
  48. }
  49. void Game::destroy() {
  50. mLoaded = false;
  51. mLara = -1;
  52. getRender().setMode(Render::modeDisabled);
  53. getWorld().destroy();
  54. getRender().ClearWorld();
  55. getSound().clear(); // Remove all previously loaded sounds
  56. }
  57. bool Game::isLoaded() {
  58. return mLoaded;
  59. }
  60. int Game::loadLevel(const char *level) {
  61. if (mLoaded)
  62. destroy();
  63. levelName = level;
  64. getLog() << "Loading " << levelName << Log::endl;
  65. int error = 0;
  66. auto loader = Loader::createLoader(level);
  67. if (loader) {
  68. // First Loader test
  69. error = loader->load(level);
  70. if (error != 0) {
  71. return error;
  72. }
  73. // And now...?
  74. getLog() << "Tried Loader..." << Log::endl;
  75. }
  76. if ((!loader) || (error == 0)) {
  77. // Old TombRaider level loader
  78. error = mTombRaider.Load(levelName.c_str());
  79. if (error != 0)
  80. return error;
  81. // If required, load the external sound effect file MAIN.SFX into TombRaider
  82. if ((mTombRaider.getEngine() == TR_VERSION_2) || (mTombRaider.getEngine() == TR_VERSION_3)) {
  83. std::string tmp(levelName);
  84. size_t pos = tmp.rfind('/');
  85. tmp.erase(pos + 1);
  86. tmp += "MAIN.SFX";
  87. error = mTombRaider.loadSFX(tmp.c_str());
  88. if (error != 0)
  89. getLog() << "Could not load " << tmp << Log::endl;
  90. }
  91. // Process data
  92. processTextures();
  93. processRooms();
  94. processModels();
  95. processSprites();
  96. processMoveables();
  97. processPakSounds();
  98. mTombRaider.reset();
  99. if (mLara == -1) {
  100. //! \todo Cutscene support
  101. getLog() << "Can't find Lara entity in level pak!" << Log::endl;
  102. destroy();
  103. return -1;
  104. } else {
  105. mLoaded = true;
  106. getRender().setMode(Render::modeVertexLight);
  107. return 0;
  108. }
  109. }
  110. }
  111. void Game::handleAction(ActionEvents action, bool isFinished) {
  112. if (mLoaded && (!isFinished)) {
  113. if (action == forwardAction) {
  114. getLara().move('f');
  115. } else if (action == backwardAction) {
  116. getLara().move('b');
  117. } else if (action == leftAction) {
  118. getLara().move('l');
  119. } else if (action == rightAction) {
  120. getLara().move('r');
  121. } else if (action == jumpAction) {
  122. } else if (action == crouchAction) {
  123. } else if (action == useAction) {
  124. } else if (action == holsterAction) {
  125. } else if (action == walkAction) {
  126. }
  127. }
  128. }
  129. void Game::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  130. if (mLoaded) {
  131. // Move Camera on X Axis
  132. if (xrel > 0)
  133. while (xrel-- > 0)
  134. getCamera().command(CAMERA_ROTATE_RIGHT);
  135. else if (xrel < 0)
  136. while (xrel++ < 0)
  137. getCamera().command(CAMERA_ROTATE_LEFT);
  138. // Move Camera on Y Axis
  139. if (yrel > 0)
  140. while (yrel-- > 0)
  141. getCamera().command(CAMERA_ROTATE_UP);
  142. else if (yrel < 0)
  143. while (yrel++ < 0)
  144. getCamera().command(CAMERA_ROTATE_DOWN);
  145. // Fix Laras rotation
  146. float angles[3] = { 0.0f, getCamera().getRadianYaw(), getCamera().getRadianPitch() };
  147. getLara().setAngles(angles);
  148. }
  149. }
  150. Entity &Game::getLara() {
  151. assert(mLara >= 0);
  152. assert(mLara < (int)getWorld().sizeEntity());
  153. return getWorld().getEntity(mLara);
  154. }
  155. void Game::processSprites() {
  156. for (int i = 0; i < (mTombRaider.NumItems() - 1); i++) {
  157. if ((mTombRaider.Engine() == TR_VERSION_1) && (mTombRaider.Item()[i].intensity1 == -1))
  158. continue;
  159. for (int j = 0; j < mTombRaider.NumSpriteSequences(); j++) {
  160. if (mTombRaider.SpriteSequence()[j].object_id == mTombRaider.Item()[i].object_id)
  161. getWorld().addSprite(*new SpriteSequence(mTombRaider, i, j));
  162. }
  163. }
  164. getLog() << "Found " << mTombRaider.NumSpriteSequences() << " sprites." << Log::endl;
  165. }
  166. void Game::processRooms() {
  167. for (int index = 0; index < mTombRaider.NumRooms(); index++)
  168. getWorld().addRoom(*new Room(mTombRaider, index));
  169. getLog() << "Found " << mTombRaider.NumRooms() << " rooms." << Log::endl;
  170. }
  171. void Game::processModels() {
  172. for (int index = 0; index < mTombRaider.getMeshCount(); index++)
  173. getWorld().addStaticMesh(*new StaticMesh(mTombRaider, index));
  174. getLog() << "Found " << mTombRaider.getMeshCount() << " meshes." << Log::endl;
  175. }
  176. void Game::processPakSounds()
  177. {
  178. unsigned char *riff;
  179. unsigned int riffSz;
  180. //tr2_sound_source_t *sound;
  181. //tr2_sound_details_t *detail;
  182. //float pos[3];
  183. unsigned int i;
  184. unsigned long id;
  185. /* detail
  186. short sample;
  187. short volume;
  188. short sound_range;
  189. short flags; // bits 8-15: priority?, 2-7: number of sound samples
  190. // in this group, bits 0-1: channel number
  191. */
  192. for (i = 0; i < mTombRaider.getSoundSamplesCount(); ++i)
  193. {
  194. mTombRaider.getSoundSample(i, &riffSz, &riff);
  195. getSound().addWave(riff, riffSz, &id, Sound::SoundFlagsNone);
  196. //if (((i + 1) == TR_SOUND_F_PISTOL) && (id > 0))
  197. //{
  198. //m_testSFX = id;
  199. //}
  200. delete [] riff;
  201. // sound[i].sound_id; // internal sound index
  202. // sound[i].flags; // 0x40, 0x80, or 0xc0
  203. //pos[0] = sound[i].x;
  204. //pos[1] = sound[i].y;
  205. //pos[2] = sound[i].z;
  206. //getSound().SourceAt(id, pos);
  207. }
  208. getLog() << "Found " << mTombRaider.getSoundSamplesCount() << " sound samples." << Log::endl;
  209. }
  210. void Game::processTextures()
  211. {
  212. unsigned char *image;
  213. unsigned char *bumpmap;
  214. int i;
  215. //if ( mTombRaider.getNumBumpMaps())
  216. // gBumpMapStart = mTombRaider.NumTextures();
  217. for (i = 0; i < mTombRaider.NumTextures(); ++i)
  218. {
  219. mTombRaider.Texture(i, &image, &bumpmap);
  220. // Overwrite any previous level textures on load
  221. getTextureManager().loadBufferSlot(image, 256, 256,
  222. RGBA, 32, (mTextureStart - 1) + i);
  223. #ifdef MULTITEXTURE
  224. gMapTex2Bump[(mTextureStart - 1) + i] = -1;
  225. #endif
  226. if (bumpmap)
  227. {
  228. #ifdef MULTITEXTURE
  229. gMapTex2Bump[(mTextureStart - 1) + i] = (mTextureStart - 1) + i +
  230. mTombRaider.NumTextures();
  231. #endif
  232. getTextureManager().loadBufferSlot(bumpmap, 256, 256,
  233. RGBA, 32,
  234. (mTextureStart - 1) + i + mTombRaider.NumTextures());
  235. }
  236. if (image)
  237. delete [] image;
  238. if (bumpmap)
  239. delete [] bumpmap;
  240. }
  241. mTextureOffset = (mTextureStart - 1) + mTombRaider.NumTextures();
  242. getLog() << "Found " << mTombRaider.NumTextures() << " textures." << Log::endl;
  243. }
  244. void Game::processMoveables()
  245. {
  246. unsigned int statCount = 0;
  247. tr2_moveable_t *moveable = mTombRaider.Moveable();
  248. tr2_item_t *item = mTombRaider.Item();
  249. tr2_sprite_sequence_t *sprite_sequence = mTombRaider.SpriteSequence();
  250. for (int i = 0; i < mTombRaider.NumItems(); ++i)
  251. {
  252. int j;
  253. int object_id = item[i].object_id;
  254. // It may not be a moveable, test for sprite
  255. if (!(mTombRaider.Engine() == TR_VERSION_1 && item[i].intensity1 == -1)) {
  256. for (j = 0; j < (int)mTombRaider.NumSpriteSequences(); ++j) {
  257. if (sprite_sequence[j].object_id == object_id)
  258. break;
  259. }
  260. // It's not a moveable, skip sprite
  261. if (j < (int)mTombRaider.NumSpriteSequences())
  262. continue;
  263. }
  264. for (j = 0; j < (int)mTombRaider.NumMoveables(); ++j) {
  265. if ((int)moveable[j].object_id == object_id)
  266. break;
  267. }
  268. // It's not a moveable or even a sprite? Skip unknown
  269. if (j == (int)mTombRaider.NumMoveables())
  270. continue;
  271. processMoveable(j, i, object_id);
  272. statCount++;
  273. }
  274. /*
  275. // Get models that aren't items
  276. for (int i = 0; i < mTombRaider.NumMoveables(); ++i)
  277. {
  278. switch ((int)moveable[i].object_id)
  279. {
  280. case 30:
  281. case 2: // Which tr needs this as model again?
  282. processMoveable(i, i, (int)moveable[i].object_id);
  283. break;
  284. default:
  285. switch (mTombRaider.Engine())
  286. {
  287. case TR_VERSION_1:
  288. switch ((int)moveable[i].object_id)
  289. {
  290. case TombRaider1::LaraMutant:
  291. processMoveable(i, i, (int)moveable[i].object_id);
  292. break;
  293. }
  294. break;
  295. case TR_VERSION_4:
  296. switch ((int)moveable[i].object_id)
  297. {
  298. case TR4_PISTOLS_ANIM:
  299. case TR4_UZI_ANIM:
  300. case TR4_SHOTGUN_ANIM:
  301. case TR4_CROSSBOW_ANIM:
  302. case TR4_GRENADE_GUN_ANIM:
  303. case TR4_SIXSHOOTER_ANIM:
  304. processMoveable(i, i, (int)moveable[i].object_id);
  305. break;
  306. }
  307. break;
  308. case TR_VERSION_2:
  309. case TR_VERSION_3:
  310. case TR_VERSION_5:
  311. case TR_VERSION_UNKNOWN:
  312. break;
  313. }
  314. }
  315. }
  316. */
  317. getLog() << "Found " << mTombRaider.NumMoveables() + statCount << " moveables." << Log::endl;
  318. }
  319. // index moveable, i item, sometimes both moveable
  320. // object_id of item or moveable
  321. void Game::processMoveable(int index, int i, int object_id) {
  322. // Check if the SkeletalModel is already cached
  323. bool cached = false;
  324. unsigned int model;
  325. for (model = 0; model < getWorld().sizeSkeletalModel(); model++) {
  326. if (getWorld().getSkeletalModel(model).getId() == object_id) {
  327. cached = true;
  328. break;
  329. }
  330. }
  331. // Create a new SkeletalModel, if needed
  332. if (!cached)
  333. getWorld().addSkeletalModel(*new SkeletalModel(mTombRaider, index, object_id));
  334. // Create a new Entity, using the cached or the new SkeletalModel
  335. Entity &entity = *new Entity(mTombRaider, index, i, model);
  336. getWorld().addEntity(entity);
  337. // Store reference to Lara
  338. if (entity.getObjectId() == 0)
  339. mLara = getWorld().sizeEntity() - 1;
  340. // Store reference to the SkyMesh
  341. if (i == mTombRaider.getSkyModelId())
  342. getRender().setSkyMesh(i, //moveable[i].starting_mesh,
  343. (mTombRaider.Engine() == TR_VERSION_2));
  344. }