Game.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * 2D Game Engine
  3. * Game.cpp: Base Game engine class
  4. * Based on pikuma.com 2D game engine in C++ and Lua course
  5. * Copyright (c) 2021 986-Studio. All Right Reserved
  6. *
  7. * Created by Manoël Trapier on 09/02/2021.
  8. */
  9. #include <SDL.h>
  10. #include <SDL_image.h>
  11. #include <glm/glm.hpp>
  12. #include <stdlib.h>
  13. #include <memory>
  14. #include <fstream>
  15. #include <Game.h>
  16. #include <Logger.h>
  17. #include <ECS.h>
  18. #include <Components/Animation.h>
  19. #include <Components/Transform.h>
  20. #include <Components/RigidBody.h>
  21. #include <Components/Sprite.h>
  22. #include <Components/BoxCollider.h>
  23. #include <Components/KeyboardControl.h>
  24. #include <Components/CameraFollow.h>
  25. #include <Components/ProjectileEmitter.h>
  26. #include <Components/Heakth.h>
  27. #include <Components/TextLabel.h>
  28. #include <Systems/Movement.h>
  29. #include <Systems/Render.h>
  30. #include <Systems/Animation.h>
  31. #include <Systems/Collision.h>
  32. #include <Systems/Damage.h>
  33. #include <Systems/KeyboardMovement.h>
  34. #include <Systems/CameraMovement.h>
  35. #include <Systems/ProjectileEmit.h>
  36. #include <Systems/ProjectileLifeCycle.h>
  37. #include <Systems/RenderText.h>
  38. #include <Systems/RenderHealth.h>
  39. uint32_t Game::windowsWidth = 0;
  40. uint32_t Game::windowsHeight = 0;
  41. uint32_t Game::mapWidth = 0;
  42. uint32_t Game::mapHeight = 0;
  43. Game::Game()
  44. {
  45. this->isRunning = false;
  46. this->isDebug = false;
  47. this->millisecsPerviousFrame = 0;
  48. this->registry = std::make_unique<Registry>();
  49. this->assetStore = std::make_unique<AssetStore>();
  50. this->eventBus = std::make_unique<EventBus>();
  51. }
  52. Game::~Game()
  53. {
  54. }
  55. void Game::Initialize()
  56. {
  57. int ret;
  58. SDL_DisplayMode displayMode;
  59. ret = SDL_Init(SDL_INIT_EVERYTHING);
  60. if (ret)
  61. {
  62. Logger::Critical("SDL Initialisation error.");
  63. return;
  64. }
  65. ret = TTF_Init();
  66. if (ret)
  67. {
  68. Logger::Critical("SDL_ttf Initialisation error.");
  69. return;
  70. }
  71. SDL_GetCurrentDisplayMode(0, &displayMode);
  72. Game::windowsWidth = 800; //displayMode.w;
  73. Game::windowsHeight = 600; //displayMode.h;
  74. this->window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  75. Game::windowsWidth, Game::windowsHeight, SDL_WINDOW_BORDERLESS);
  76. if (!window)
  77. {
  78. Logger::Critical("SDL window creation error.");
  79. return;
  80. }
  81. this->renderer = SDL_CreateRenderer(this->window, -1, 0);
  82. if (!renderer)
  83. {
  84. Logger::Critical("SDL renderer creation error.");
  85. return;
  86. }
  87. //SDL_SetWindowFullscreen(this->window, SDL_WINDOW_FULLSCREEN);
  88. this->camera.x = 0;
  89. this->camera.y = 0;
  90. this->camera.w = Game::windowsWidth;
  91. this->camera.h = Game::windowsHeight;
  92. this->isRunning = true;
  93. }
  94. void Game::Destroy()
  95. {
  96. if (this->renderer)
  97. {
  98. SDL_DestroyRenderer(this->renderer);
  99. }
  100. if (this->window)
  101. {
  102. SDL_DestroyWindow(this->window);
  103. }
  104. SDL_Quit();
  105. }
  106. void Game::Run()
  107. {
  108. this->Setup();
  109. #if 0
  110. for(int i = 0 ; i < 100; i++)
  111. {
  112. this->ProcessInput();
  113. this->Update();
  114. this->Render();
  115. }
  116. #else
  117. while(this->isRunning)
  118. {
  119. this->ProcessInput();
  120. this->Update();
  121. this->Render();
  122. }
  123. #endif
  124. }
  125. void Game::LoadLevel(int level)
  126. {
  127. this->registry->addSystem<MovementSystem>();
  128. this->registry->addSystem<RenderSystem>();
  129. this->registry->addSystem<AnimationSystem>();
  130. this->registry->addSystem<CollisionSystem>();
  131. this->registry->addSystem<DamageSystem>();
  132. this->registry->addSystem<KeyboardMovementSystem>();
  133. this->registry->addSystem<CameraMovementSystem>();
  134. this->registry->addSystem<ProjectileEmitSystem>();
  135. this->registry->addSystem<ProjectileLifeCycleSystem>();
  136. this->registry->addSystem<RenderTextSystem>();
  137. this->registry->addSystem<RenderHealthSystem>();
  138. this->assetStore->addTexture(this->renderer, "tank-image", "assets/images/tank-panther-right.png");
  139. this->assetStore->addTexture(this->renderer, "truck-image", "assets/images/truck-ford-right.png");
  140. this->assetStore->addTexture(this->renderer, "chopper-image", "assets/images/chopper-spritesheet.png");
  141. this->assetStore->addTexture(this->renderer, "main-tileset", "assets/tilemaps/jungle.png");
  142. this->assetStore->addTexture(this->renderer, "radar-image", "assets/images/radar.png");
  143. this->assetStore->addTexture(this->renderer, "bullet-image", "assets/images/bullet.png");
  144. this->assetStore->addFont("charriot-font", "assets/fonts/charriot.ttf", 20);
  145. this->assetStore->addFont("arial-font", "assets/fonts/charriot.ttf", 20);
  146. this->assetStore->addFont("pico8-font", "assets/fonts/charriot.ttf", 10);
  147. Logger::Debug("struct SpriteComponent size is %d", sizeof(struct SpriteComponent));
  148. // jungle.map
  149. int tileSize = 32;
  150. double tileScale = 2.0;
  151. int mapNumCols = 25;
  152. int mapNumRows = 20;
  153. std::fstream mapFile;
  154. mapFile.open("assets/tilemaps/jungle.map");
  155. assert(mapFile.is_open());
  156. for (int y = 0; y < mapNumRows; y++)
  157. {
  158. for (int x = 0; x < mapNumCols; x++)
  159. {
  160. char ch[2] = {0, 0};
  161. mapFile.get(ch[0]);
  162. int srcRectY = atoi(ch) * tileSize;
  163. mapFile.get(ch[0]);
  164. int srcRectX = atoi(ch) * tileSize;
  165. mapFile.ignore();
  166. Entity tile = registry->createEntity();
  167. tile.addComponent<TransformComponent>(glm::vec2(x * (tileScale * tileSize), y * (tileScale * tileSize)), glm::vec2(tileScale, tileScale), 0.0);
  168. tile.addComponent<SpriteComponent>("main-tileset", 0, tileSize, tileSize, false, srcRectX, srcRectY);
  169. tile.group("tiles");
  170. }
  171. }
  172. mapFile.close();
  173. Game::mapWidth = mapNumCols * tileSize * tileScale;
  174. Game::mapHeight = mapNumRows * tileSize * tileScale;
  175. Entity chopper = this->registry->createEntity();
  176. chopper.addComponent<TransformComponent>(glm::vec2(240, 110), glm::vec2(1, 1), 0);
  177. chopper.addComponent<SpriteComponent>("chopper-image", 1, 32, 32);
  178. chopper.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  179. chopper.addComponent<BoxColliderComponent>(32, 32);
  180. chopper.addComponent<AnimationComponent>(2, 15, true);
  181. chopper.addComponent<KeyboardControlComponent>(glm::vec2(0, -80), glm::vec2(80, 0),
  182. glm::vec2(0, 80), glm::vec2(-80, 0));
  183. chopper.addComponent<ProjectileEmitterComponent>(glm::vec2(180, 180), 0, 10000, 10, true);
  184. chopper.addComponent<HealthComponent>(100);
  185. chopper.addComponent<CameraFollowComponent>();
  186. chopper.tag("player");
  187. Entity radar = this->registry->createEntity();
  188. radar.addComponent<TransformComponent>(glm::vec2(this->windowsWidth - 75, 10), glm::vec2(1, 1), 0);
  189. radar.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  190. radar.addComponent<SpriteComponent>("radar-image", 3, 64, 64, true);
  191. radar.addComponent<AnimationComponent>(8, 5, true);
  192. Entity tank = this->registry->createEntity();
  193. tank.addComponent<TransformComponent>(glm::vec2(500, 500), glm::vec2(1, 1), 0);
  194. tank.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  195. tank.addComponent<SpriteComponent>("tank-image", 1, 32, 32);
  196. tank.addComponent<BoxColliderComponent>(32, 32);
  197. tank.addComponent<ProjectileEmitterComponent>(glm::vec2(100, 0), 3000, 5000, 10, false);
  198. tank.addComponent<HealthComponent>(100);
  199. tank.group("enemies");
  200. Entity truck = this->registry->createEntity();
  201. truck.addComponent<TransformComponent>(glm::vec2(120, 500), glm::vec2(1, 1), 0);
  202. truck.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  203. truck.addComponent<SpriteComponent>("truck-image", 2, 32, 32);
  204. truck.addComponent<BoxColliderComponent>(32, 32);
  205. truck.addComponent<ProjectileEmitterComponent>(glm::vec2(0, -100), 2000, 5000, 10, false);
  206. truck.addComponent<HealthComponent>(100);
  207. truck.group("enemies");
  208. SDL_Color green = { 64, 255, 127, 255 };
  209. Entity label = this->registry->createEntity();
  210. label.addComponent<TextLabelComponent>(glm::vec2(this->windowsWidth/2 - 60, 10), "SUPER CHOPPER BROS 1.0", "charriot-font", green);
  211. }
  212. void Game::Setup()
  213. {
  214. LoadLevel(1);
  215. }
  216. void Game::Update()
  217. {
  218. uint32_t timeToWait = MILLISECS_PER_FRAME - (SDL_GetTicks() - this->millisecsPerviousFrame);
  219. if (timeToWait > 0 && timeToWait <= MILLISECS_PER_FRAME)
  220. {
  221. SDL_Delay(timeToWait);
  222. }
  223. double deltaTime = (SDL_GetTicks() - this->millisecsPerviousFrame) / 1000.0;
  224. this->millisecsPerviousFrame = SDL_GetTicks();
  225. eventBus->reset();
  226. registry->getSystem<DamageSystem>().subscriptToEvents(eventBus);
  227. registry->getSystem<KeyboardMovementSystem>().subscriptToEvents(eventBus);
  228. registry->getSystem<ProjectileEmitSystem>().subscriptToEvents(eventBus);
  229. registry->update();
  230. registry->getSystem<MovementSystem>().update(deltaTime);
  231. registry->getSystem<AnimationSystem>().update();
  232. registry->getSystem<CollisionSystem>().update(this->eventBus);
  233. registry->getSystem<ProjectileEmitSystem>().update(this->registry);
  234. registry->getSystem<ProjectileLifeCycleSystem>().update();
  235. registry->getSystem<CameraMovementSystem>().update(this->camera);
  236. }
  237. void Game::ProcessInput()
  238. {
  239. SDL_Event event;
  240. while(SDL_PollEvent(&event))
  241. {
  242. switch(event.type)
  243. {
  244. case SDL_QUIT:
  245. this->isRunning = false;
  246. break;
  247. case SDL_KEYDOWN:
  248. if (event.key.keysym.sym == SDLK_ESCAPE)
  249. {
  250. this->isRunning = false;
  251. }
  252. else if (event.key.keysym.sym == SDLK_d)
  253. {
  254. this->isDebug = !this->isDebug;
  255. }
  256. else
  257. {
  258. this->eventBus->emitEvent<KeyPressedEvent>(event.key.keysym.sym);
  259. }
  260. break;
  261. default:
  262. break;
  263. }
  264. }
  265. }
  266. void Game::Render()
  267. {
  268. SDL_SetRenderDrawColor(this->renderer, 21, 21, 21, 255);
  269. SDL_RenderClear(this->renderer);
  270. registry->getSystem<RenderSystem>().update(this->renderer, this->assetStore, this->camera);
  271. registry->getSystem<RenderTextSystem>().update(this->renderer, this->assetStore, this->camera);
  272. registry->getSystem<RenderHealthSystem>().update(this->renderer, this->assetStore, this->camera);
  273. if (this->isDebug)
  274. {
  275. registry->getSystem<CollisionSystem>().debugRender(this->renderer, this->camera);
  276. }
  277. SDL_RenderPresent(this->renderer);
  278. }