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

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