Game.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "Game.h"
  9. #include <algorithm>
  10. #include "Renderer.h"
  11. #include "AudioSystem.h"
  12. #include "PhysWorld.h"
  13. #include "Actor.h"
  14. #include "SpriteComponent.h"
  15. #include "MeshComponent.h"
  16. #include "FPSActor.h"
  17. #include "PlaneActor.h"
  18. #include "TargetActor.h"
  19. #include "BallActor.h"
  20. Game::Game()
  21. :mRenderer(nullptr)
  22. ,mAudioSystem(nullptr)
  23. ,mPhysWorld(nullptr)
  24. ,mIsRunning(true)
  25. ,mUpdatingActors(false)
  26. {
  27. }
  28. bool Game::Initialize()
  29. {
  30. if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0)
  31. {
  32. SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
  33. return false;
  34. }
  35. // Create the renderer
  36. mRenderer = new Renderer(this);
  37. if (!mRenderer->Initialize(1024.0f, 768.0f))
  38. {
  39. SDL_Log("Failed to initialize renderer");
  40. delete mRenderer;
  41. mRenderer = nullptr;
  42. return false;
  43. }
  44. // Create the audio system
  45. mAudioSystem = new AudioSystem(this);
  46. if (!mAudioSystem->Initialize())
  47. {
  48. SDL_Log("Failed to initialize audio system");
  49. mAudioSystem->Shutdown();
  50. delete mAudioSystem;
  51. mAudioSystem = nullptr;
  52. return false;
  53. }
  54. // Create the physics world
  55. mPhysWorld = new PhysWorld(this);
  56. LoadData();
  57. mTicksCount = SDL_GetTicks();
  58. return true;
  59. }
  60. void Game::RunLoop()
  61. {
  62. while (mIsRunning)
  63. {
  64. ProcessInput();
  65. UpdateGame();
  66. GenerateOutput();
  67. }
  68. }
  69. void Game::AddPlane(PlaneActor* plane)
  70. {
  71. mPlanes.emplace_back(plane);
  72. }
  73. void Game::RemovePlane(PlaneActor* plane)
  74. {
  75. auto iter = std::find(mPlanes.begin(), mPlanes.end(), plane);
  76. mPlanes.erase(iter);
  77. }
  78. void Game::ProcessInput()
  79. {
  80. SDL_Event event;
  81. while (SDL_PollEvent(&event))
  82. {
  83. switch (event.type)
  84. {
  85. case SDL_QUIT:
  86. mIsRunning = false;
  87. break;
  88. // This fires when a key's initially pressed
  89. case SDL_KEYDOWN:
  90. if (!event.key.repeat)
  91. {
  92. HandleKeyPress(event.key.keysym.sym);
  93. }
  94. break;
  95. case SDL_MOUSEBUTTONDOWN:
  96. HandleKeyPress(event.button.button);
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. const Uint8* state = SDL_GetKeyboardState(NULL);
  103. if (state[SDL_SCANCODE_ESCAPE])
  104. {
  105. mIsRunning = false;
  106. }
  107. for (auto actor : mActors)
  108. {
  109. actor->ProcessInput(state);
  110. }
  111. }
  112. void Game::HandleKeyPress(int key)
  113. {
  114. switch (key)
  115. {
  116. case '-':
  117. {
  118. // Reduce master volume
  119. float volume = mAudioSystem->GetBusVolume("bus:/");
  120. volume = Math::Max(0.0f, volume - 0.1f);
  121. mAudioSystem->SetBusVolume("bus:/", volume);
  122. break;
  123. }
  124. case '=':
  125. {
  126. // Increase master volume
  127. float volume = mAudioSystem->GetBusVolume("bus:/");
  128. volume = Math::Min(1.0f, volume + 0.1f);
  129. mAudioSystem->SetBusVolume("bus:/", volume);
  130. break;
  131. }
  132. case SDL_BUTTON_LEFT:
  133. {
  134. // Fire weapon
  135. mFPSActor->Shoot();
  136. break;
  137. }
  138. default:
  139. break;
  140. }
  141. }
  142. void Game::UpdateGame()
  143. {
  144. // Compute delta time
  145. // Wait until 16ms has elapsed since last frame
  146. while (!SDL_TICKS_PASSED(SDL_GetTicks(), mTicksCount + 16))
  147. ;
  148. float deltaTime = (SDL_GetTicks() - mTicksCount) / 1000.0f;
  149. if (deltaTime > 0.05f)
  150. {
  151. deltaTime = 0.05f;
  152. }
  153. mTicksCount = SDL_GetTicks();
  154. // Update all actors
  155. mUpdatingActors = true;
  156. for (auto actor : mActors)
  157. {
  158. actor->Update(deltaTime);
  159. }
  160. mUpdatingActors = false;
  161. // Move any pending actors to mActors
  162. for (auto pending : mPendingActors)
  163. {
  164. pending->ComputeWorldTransform();
  165. mActors.emplace_back(pending);
  166. }
  167. mPendingActors.clear();
  168. // Add any dead actors to a temp vector
  169. std::vector<Actor*> deadActors;
  170. for (auto actor : mActors)
  171. {
  172. if (actor->GetState() == Actor::EDead)
  173. {
  174. deadActors.emplace_back(actor);
  175. }
  176. }
  177. // Delete dead actors (which removes them from mActors)
  178. for (auto actor : deadActors)
  179. {
  180. delete actor;
  181. }
  182. // Update audio system
  183. mAudioSystem->Update(deltaTime);
  184. }
  185. void Game::GenerateOutput()
  186. {
  187. mRenderer->Draw();
  188. }
  189. void Game::LoadData()
  190. {
  191. // Create actors
  192. Actor* a = nullptr;
  193. Quaternion q;
  194. //MeshComponent* mc = nullptr;
  195. // Setup floor
  196. const float start = -1250.0f;
  197. const float size = 250.0f;
  198. for (int i = 0; i < 10; i++)
  199. {
  200. for (int j = 0; j < 10; j++)
  201. {
  202. a = new PlaneActor(this);
  203. a->SetPosition(Vector3(start + i * size, start + j * size, -100.0f));
  204. }
  205. }
  206. // Left/right walls
  207. q = Quaternion(Vector3::UnitX, Math::PiOver2);
  208. for (int i = 0; i < 10; i++)
  209. {
  210. a = new PlaneActor(this);
  211. a->SetPosition(Vector3(start + i * size, start - size, 0.0f));
  212. a->SetRotation(q);
  213. a = new PlaneActor(this);
  214. a->SetPosition(Vector3(start + i * size, -start + size, 0.0f));
  215. a->SetRotation(q);
  216. }
  217. q = Quaternion::Concatenate(q, Quaternion(Vector3::UnitZ, Math::PiOver2));
  218. // Forward/back walls
  219. for (int i = 0; i < 10; i++)
  220. {
  221. a = new PlaneActor(this);
  222. a->SetPosition(Vector3(start - size, start + i * size, 0.0f));
  223. a->SetRotation(q);
  224. a = new PlaneActor(this);
  225. a->SetPosition(Vector3(-start + size, start + i * size, 0.0f));
  226. a->SetRotation(q);
  227. }
  228. // Setup lights
  229. mRenderer->SetAmbientLight(Vector3(0.2f, 0.2f, 0.2f));
  230. DirectionalLight& dir = mRenderer->GetDirectionalLight();
  231. dir.mDirection = Vector3(0.0f, -0.707f, -0.707f);
  232. dir.mDiffuseColor = Vector3(0.78f, 0.88f, 1.0f);
  233. dir.mSpecColor = Vector3(0.8f, 0.8f, 0.8f);
  234. // UI elements
  235. a = new Actor(this);
  236. a->SetPosition(Vector3(-350.0f, -350.0f, 0.0f));
  237. SpriteComponent* sc = new SpriteComponent(a);
  238. sc->SetTexture(mRenderer->GetTexture("Assets/HealthBar.png"));
  239. a = new Actor(this);
  240. a->SetPosition(Vector3(-390.0f, 275.0f, 0.0f));
  241. a->SetScale(0.75f);
  242. sc = new SpriteComponent(a);
  243. sc->SetTexture(mRenderer->GetTexture("Assets/Radar.png"));
  244. a = new Actor(this);
  245. a->SetScale(2.0f);
  246. mCrosshair = new SpriteComponent(a);
  247. mCrosshair->SetTexture(mRenderer->GetTexture("Assets/Crosshair.png"));
  248. // Start music
  249. mMusicEvent = mAudioSystem->PlayEvent("event:/Music");
  250. // Enable relative mouse mode for camera look
  251. SDL_SetRelativeMouseMode(SDL_TRUE);
  252. // Make an initial call to get relative to clear out
  253. SDL_GetRelativeMouseState(nullptr, nullptr);
  254. // Different camera actors
  255. mFPSActor = new FPSActor(this);
  256. // Create target actors
  257. a = new TargetActor(this);
  258. a->SetPosition(Vector3(1450.0f, 0.0f, 100.0f));
  259. a = new TargetActor(this);
  260. a->SetPosition(Vector3(1450.0f, 0.0f, 400.0f));
  261. a = new TargetActor(this);
  262. a->SetPosition(Vector3(1450.0f, -500.0f, 200.0f));
  263. a = new TargetActor(this);
  264. a->SetPosition(Vector3(1450.0f, 500.0f, 200.0f));
  265. }
  266. void Game::UnloadData()
  267. {
  268. // Delete actors
  269. // Because ~Actor calls RemoveActor, have to use a different style loop
  270. while (!mActors.empty())
  271. {
  272. delete mActors.back();
  273. }
  274. if (mRenderer)
  275. {
  276. mRenderer->UnloadData();
  277. }
  278. }
  279. void Game::Shutdown()
  280. {
  281. UnloadData();
  282. delete mPhysWorld;
  283. if (mRenderer)
  284. {
  285. mRenderer->Shutdown();
  286. }
  287. if (mAudioSystem)
  288. {
  289. mAudioSystem->Shutdown();
  290. }
  291. SDL_Quit();
  292. }
  293. void Game::AddActor(Actor* actor)
  294. {
  295. // If we're updating actors, need to add to pending
  296. if (mUpdatingActors)
  297. {
  298. mPendingActors.emplace_back(actor);
  299. }
  300. else
  301. {
  302. mActors.emplace_back(actor);
  303. }
  304. }
  305. void Game::RemoveActor(Actor* actor)
  306. {
  307. // Is it in pending actors?
  308. auto iter = std::find(mPendingActors.begin(), mPendingActors.end(), actor);
  309. if (iter != mPendingActors.end())
  310. {
  311. // Swap to end of vector and pop off (avoid erase copies)
  312. std::iter_swap(iter, mPendingActors.end() - 1);
  313. mPendingActors.pop_back();
  314. }
  315. // Is it in actors?
  316. iter = std::find(mActors.begin(), mActors.end(), actor);
  317. if (iter != mActors.end())
  318. {
  319. // Swap to end of vector and pop off (avoid erase copies)
  320. std::iter_swap(iter, mActors.end() - 1);
  321. mActors.pop_back();
  322. }
  323. }