main.cxx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "SDL.hxx"
  2. #include "SDLWindow.hxx"
  3. #include "SDLRenderer.hxx"
  4. #include "game/AssetManager.hxx"
  5. #include "game/GameStateManager.hxx"
  6. #include <cstdlib>
  7. void main_loop(SDLRenderer& renderer)
  8. {
  9. using namespace std::chrono;
  10. AssetManager am{renderer};
  11. GameStateManager gsm{};
  12. auto start = high_resolution_clock::now();
  13. for (;;) {
  14. SDL_Event evt;
  15. while (SDL_PollEvent(&evt)!=0) {
  16. if (evt.type==SDL_QUIT)
  17. return;
  18. if (auto const state = gsm.current(); state!=nullptr)
  19. state->on_event(gsm, evt);
  20. else
  21. return;
  22. }
  23. auto const end = high_resolution_clock::now();
  24. auto const delta_time = duration_cast<milliseconds>(end-start);
  25. start = end;
  26. if (auto const state = gsm.current(); state != nullptr)
  27. state->update(gsm, delta_time);
  28. else
  29. return;
  30. if (auto const state = gsm.current(); state != nullptr)
  31. state->render(renderer);
  32. else
  33. return;
  34. }
  35. }
  36. int main(int argc, char** argv) try
  37. {
  38. SDL sdl{};
  39. SDLWindow window{
  40. "Snake",
  41. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  42. 0, 0,
  43. SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_BORDERLESS
  44. };
  45. SDLRenderer renderer{window};
  46. main_loop(renderer);
  47. return EXIT_SUCCESS;
  48. } catch (std::exception const& ex) {
  49. SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", ex.what());
  50. return EXIT_FAILURE;
  51. }