Game.cpp 12 KB

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