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 11KB

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