Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Game.cpp 12KB

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