Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Game.cpp 11KB

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