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

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