Game.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <memory>
  13. #include <Game.h>
  14. #include <Logger.h>
  15. #include <ECS.h>
  16. #include <Components/Transform.h>
  17. #include <Components/RigidBody.h>
  18. Game::Game()
  19. {
  20. this->isRunning = false;
  21. this->windowsHeight = 0;
  22. this->windowsWidth = 0;
  23. this->registry = std::make_unique<Registry>();
  24. }
  25. Game::~Game()
  26. {
  27. }
  28. void Game::Initialize()
  29. {
  30. int ret;
  31. SDL_DisplayMode displayMode;
  32. ret = SDL_Init(SDL_INIT_EVERYTHING);
  33. if (ret)
  34. {
  35. Logger::Critical("SDL Initialisation error.");
  36. return;
  37. }
  38. SDL_GetCurrentDisplayMode(0, &displayMode);
  39. this->windowsWidth = displayMode.w;
  40. this->windowsHeight = displayMode.h;
  41. this->window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  42. this->windowsWidth, this->windowsHeight, SDL_WINDOW_BORDERLESS);
  43. if (!window)
  44. {
  45. Logger::Critical("SDL window creation error.");
  46. return;
  47. }
  48. this->renderer = SDL_CreateRenderer(window, -1, 0);
  49. if (!renderer)
  50. {
  51. Logger::Critical("SDL renderer creation error.");
  52. return;
  53. }
  54. SDL_SetWindowFullscreen(this->window, SDL_WINDOW_FULLSCREEN);
  55. this->isRunning = true;
  56. }
  57. void Game::Destroy()
  58. {
  59. if (this->renderer)
  60. {
  61. SDL_DestroyRenderer(this->renderer);
  62. }
  63. if (this->window)
  64. {
  65. SDL_DestroyWindow(this->window);
  66. }
  67. SDL_Quit();
  68. }
  69. void Game::Run()
  70. {
  71. this->Setup();
  72. while(this->isRunning)
  73. {
  74. this->ProcessInput();
  75. this->Update();
  76. this->Render();
  77. }
  78. }
  79. void Game::Setup()
  80. {
  81. Entity tank = this->registry->createEntity();
  82. //this->registry->addComponent<TransformComponent>(tank, glm::vec2(10, 30), glm::vec2(1, 1), 0);
  83. //this->registry->addComponent<RigidBodyComponent>(tank, glm::vec2(50, 0));
  84. tank.addComponent<TransformComponent>(glm::vec2(10, 30), glm::vec2(1, 1), 0);
  85. tank.addComponent<RigidBodyComponent>(glm::vec2(50, 0));
  86. tank.removeComponent<TransformComponent>();
  87. }
  88. void Game::Update()
  89. {
  90. int32_t timeToWait = MILLISECS_PER_FRAME - (SDL_GetTicks() - this->millisecsPerviousFrame);
  91. if (timeToWait > 0 && timeToWait <= MILLISECS_PER_FRAME)
  92. {
  93. SDL_Delay(timeToWait);
  94. }
  95. double deltaTime = (SDL_GetTicks() - this->millisecsPerviousFrame) / 1000.0;
  96. this->millisecsPerviousFrame = SDL_GetTicks();
  97. }
  98. void Game::ProcessInput()
  99. {
  100. SDL_Event event;
  101. while(SDL_PollEvent(&event))
  102. {
  103. switch(event.type)
  104. {
  105. case SDL_QUIT:
  106. this->isRunning = false;
  107. break;
  108. case SDL_KEYDOWN:
  109. if (event.key.keysym.sym == SDLK_ESCAPE)
  110. {
  111. this->isRunning = false;
  112. }
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. }
  119. void Game::Render()
  120. {
  121. SDL_SetRenderDrawColor(this->renderer, 21, 21, 21, 255);
  122. SDL_RenderClear(this->renderer);
  123. // Do something here
  124. SDL_RenderPresent(this->renderer);
  125. }