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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. mName = NULL;
  29. mLara = -1;
  30. mTextureStart = 0;
  31. mTextureOffset = 0;
  32. }
  33. Game::~Game() {
  34. destroy();
  35. }
  36. unsigned int Game::getTextureStart() {
  37. return mTextureStart;
  38. }
  39. unsigned int Game::getTextureOffset() {
  40. return mTextureOffset;
  41. }
  42. int Game::initialize() {
  43. // Enable Renderer
  44. getRender().setMode(Render::modeLoadScreen);
  45. mTextureStart = getTextureManager().getTextureCount();
  46. return 0;
  47. }
  48. void Game::destroy() {
  49. delete [] mName;
  50. mName = NULL;
  51. mLoaded = false;
  52. mLara = -1;
  53. getRender().setMode(Render::modeDisabled);
  54. getWorld().destroy();
  55. getRender().ClearWorld();
  56. getSound().clear(); // Remove all previously loaded sounds
  57. }
  58. bool Game::isLoaded() {
  59. return mLoaded;
  60. }
  61. int Game::loadLevel(const char *level) {
  62. if (mLoaded)
  63. destroy();
  64. mName = bufferString("%s", level);
  65. getConsole().print("Loading %s", mName);
  66. int error = mTombRaider.Load(mName);
  67. if (error != 0)
  68. return error;
  69. // If required, load the external sound effect file MAIN.SFX into TombRaider
  70. if ((mTombRaider.getEngine() == TR_VERSION_2) || (mTombRaider.getEngine() == TR_VERSION_3)) {
  71. char *tmp = bufferString("%sMAIN.SFX", level); // Ensure theres enough space
  72. size_t length = strlen(tmp);
  73. size_t dir = 0;
  74. for (long i = length - 1; i >= 0; i--) {
  75. if ((tmp[i] == '/') || (tmp[i] == '\\')) {
  76. dir = i + 1; // Find where the filename (bla.tr2) starts
  77. break;
  78. }
  79. }
  80. strcpy(tmp + dir, "MAIN.SFX"); // overwrite the name itself with MAIN.SFX
  81. tmp[dir + 8] = '\0';
  82. error = mTombRaider.loadSFX(tmp);
  83. if (error != 0)
  84. getConsole().print("Could not load %s", tmp);
  85. delete [] tmp;
  86. }
  87. // Process data
  88. processTextures();
  89. processRooms();
  90. processModels();
  91. processSprites();
  92. processMoveables();
  93. processPakSounds();
  94. mTombRaider.reset();
  95. if (mLara == -1) {
  96. getConsole().print("Can't find Lara entity in level pak!");
  97. destroy();
  98. return -1;
  99. } else {
  100. mLoaded = true;
  101. getRender().setMode(Render::modeVertexLight);
  102. return 0;
  103. }
  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) {
  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. getConsole().print("Found %d sprites.", mTombRaider.NumSpriteSequences());
  159. }
  160. void Game::processRooms() {
  161. for (int index = 0; index < mTombRaider.NumRooms(); index++)
  162. getWorld().addRoom(*new Room(mTombRaider, index));
  163. getConsole().print("Found %d rooms.", mTombRaider.NumRooms());
  164. }
  165. void Game::processModels() {
  166. for (int index = 0; index < mTombRaider.getMeshCount(); index++)
  167. getWorld().addStaticMesh(*new StaticMesh(mTombRaider, index));
  168. getConsole().print("Found %d meshes.", mTombRaider.getMeshCount());
  169. }
  170. void Game::processPakSounds()
  171. {
  172. unsigned char *riff;
  173. unsigned int riffSz;
  174. //tr2_sound_source_t *sound;
  175. //tr2_sound_details_t *detail;
  176. //float pos[3];
  177. unsigned int i;
  178. unsigned long id;
  179. /* detail
  180. short sample;
  181. short volume;
  182. short sound_range;
  183. short flags; // bits 8-15: priority?, 2-7: number of sound samples
  184. // in this group, bits 0-1: channel number
  185. */
  186. for (i = 0; i < mTombRaider.getSoundSamplesCount(); ++i)
  187. {
  188. mTombRaider.getSoundSample(i, &riffSz, &riff);
  189. getSound().addWave(riff, riffSz, &id, Sound::SoundFlagsNone);
  190. //if (((i + 1) == TR_SOUND_F_PISTOL) && (id > 0))
  191. //{
  192. //m_testSFX = id;
  193. //}
  194. delete [] riff;
  195. // sound[i].sound_id; // internal sound index
  196. // sound[i].flags; // 0x40, 0x80, or 0xc0
  197. //pos[0] = sound[i].x;
  198. //pos[1] = sound[i].y;
  199. //pos[2] = sound[i].z;
  200. //getSound().SourceAt(id, pos);
  201. }
  202. getConsole().print("Found %u sound samples.", mTombRaider.getSoundSamplesCount());
  203. }
  204. void Game::processTextures()
  205. {
  206. unsigned char *image;
  207. unsigned char *bumpmap;
  208. int i;
  209. //if ( mTombRaider.getNumBumpMaps())
  210. // gBumpMapStart = mTombRaider.NumTextures();
  211. for (i = 0; i < mTombRaider.NumTextures(); ++i)
  212. {
  213. mTombRaider.Texture(i, &image, &bumpmap);
  214. // Overwrite any previous level textures on load
  215. getTextureManager().loadBufferSlot(image, 256, 256,
  216. RGBA, 32, (mTextureStart - 1) + i);
  217. #ifdef MULTITEXTURE
  218. gMapTex2Bump[(mTextureStart - 1) + i] = -1;
  219. #endif
  220. if (bumpmap)
  221. {
  222. #ifdef MULTITEXTURE
  223. gMapTex2Bump[(mTextureStart - 1) + i] = (mTextureStart - 1) + i +
  224. mTombRaider.NumTextures();
  225. #endif
  226. getTextureManager().loadBufferSlot(bumpmap, 256, 256,
  227. RGBA, 32,
  228. (mTextureStart - 1) + i + mTombRaider.NumTextures());
  229. }
  230. if (image)
  231. delete [] image;
  232. if (bumpmap)
  233. delete [] bumpmap;
  234. }
  235. mTextureOffset = (mTextureStart - 1) + mTombRaider.NumTextures();
  236. getConsole().print("Found %d textures.", mTombRaider.NumTextures());
  237. }
  238. void Game::processMoveables()
  239. {
  240. unsigned int statCount = 0;
  241. tr2_moveable_t *moveable = mTombRaider.Moveable();
  242. tr2_item_t *item = mTombRaider.Item();
  243. tr2_sprite_sequence_t *sprite_sequence = mTombRaider.SpriteSequence();
  244. for (int i = 0; i < mTombRaider.NumItems(); ++i)
  245. {
  246. int j;
  247. int object_id = item[i].object_id;
  248. // It may not be a moveable, test for sprite
  249. if (!(mTombRaider.Engine() == TR_VERSION_1 && item[i].intensity1 == -1)) {
  250. for (j = 0; j < (int)mTombRaider.NumSpriteSequences(); ++j) {
  251. if (sprite_sequence[j].object_id == object_id)
  252. break;
  253. }
  254. // It's not a moveable, skip sprite
  255. if (j < (int)mTombRaider.NumSpriteSequences())
  256. continue;
  257. }
  258. for (j = 0; j < (int)mTombRaider.NumMoveables(); ++j) {
  259. if ((int)moveable[j].object_id == object_id)
  260. break;
  261. }
  262. // It's not a moveable or even a sprite? Skip unknown
  263. if (j == (int)mTombRaider.NumMoveables())
  264. continue;
  265. processMoveable(j, i, object_id);
  266. statCount++;
  267. }
  268. /*
  269. // Get models that aren't items
  270. for (int i = 0; i < mTombRaider.NumMoveables(); ++i)
  271. {
  272. switch ((int)moveable[i].object_id)
  273. {
  274. case 30:
  275. case 2: // Which tr needs this as model again?
  276. processMoveable(i, i, (int)moveable[i].object_id);
  277. break;
  278. default:
  279. switch (mTombRaider.Engine())
  280. {
  281. case TR_VERSION_1:
  282. switch ((int)moveable[i].object_id)
  283. {
  284. case TombRaider1::LaraMutant:
  285. processMoveable(i, i, (int)moveable[i].object_id);
  286. break;
  287. }
  288. break;
  289. case TR_VERSION_4:
  290. switch ((int)moveable[i].object_id)
  291. {
  292. case TR4_PISTOLS_ANIM:
  293. case TR4_UZI_ANIM:
  294. case TR4_SHOTGUN_ANIM:
  295. case TR4_CROSSBOW_ANIM:
  296. case TR4_GRENADE_GUN_ANIM:
  297. case TR4_SIXSHOOTER_ANIM:
  298. processMoveable(i, i, (int)moveable[i].object_id);
  299. break;
  300. }
  301. break;
  302. case TR_VERSION_2:
  303. case TR_VERSION_3:
  304. case TR_VERSION_5:
  305. case TR_VERSION_UNKNOWN:
  306. break;
  307. }
  308. }
  309. }
  310. */
  311. getConsole().print("Found %d moveables.", mTombRaider.NumMoveables() + statCount);
  312. }
  313. // index moveable, i item, sometimes both moveable
  314. // object_id of item or moveable
  315. void Game::processMoveable(int index, int i, int object_id) {
  316. // Check if the SkeletalModel is already cached
  317. bool cached = false;
  318. unsigned int model;
  319. for (model = 0; model < getWorld().sizeSkeletalModel(); model++) {
  320. if (getWorld().getSkeletalModel(model).getId() == object_id) {
  321. cached = true;
  322. break;
  323. }
  324. }
  325. // Create a new SkeletalModel, if needed
  326. if (!cached)
  327. getWorld().addSkeletalModel(*new SkeletalModel(mTombRaider, index, object_id));
  328. // Create a new Entity, using the cached or the new SkeletalModel
  329. Entity &entity = *new Entity(mTombRaider, index, i, model);
  330. getWorld().addEntity(entity);
  331. // Store reference to Lara
  332. if (entity.getObjectId() == 0)
  333. mLara = getWorld().sizeEntity() - 1;
  334. // Store reference to the SkyMesh
  335. if (i == mTombRaider.getSkyModelId())
  336. getRender().setSkyMesh(i, //moveable[i].starting_mesh,
  337. (mTombRaider.Engine() == TR_VERSION_2));
  338. }