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

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