A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cc 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include <time.h>
  2. #include <unistd.h>
  3. #include <vector>
  4. #include <memory>
  5. #include <chrono>
  6. #include <ratio>
  7. #include <SDL2/SDL.h>
  8. #include <SDL2/SDL_image.h>
  9. #include <string.h>
  10. #include <swan/swan.h>
  11. using namespace Swan;
  12. #define errassert(expr, str, errfn) do { \
  13. if (!(expr)) { \
  14. panic << (str) << ": " << errfn(); \
  15. return EXIT_FAILURE; \
  16. } \
  17. } while (0)
  18. #define sdlassert(expr, str) errassert(expr, str, SDL_GetError);
  19. #define imgassert(expr, str) errassert(expr, str, IMG_GetError);
  20. template<typename T>
  21. using DeleteFunc = void (*)(T *);
  22. int main(int argc, char **argv) {
  23. uint32_t winflags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
  24. uint32_t renderflags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
  25. for (int i = 1; i < argc; ++i) {
  26. if (strcmp(argv[i], "--lodpi") == 0) {
  27. winflags &= ~SDL_WINDOW_ALLOW_HIGHDPI;
  28. } else if (strcmp(argv[i], "--fullscreen") == 0) {
  29. winflags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
  30. } else if (strcmp(argv[i], "--no-vsync") == 0) {
  31. renderflags &= ~SDL_RENDERER_PRESENTVSYNC;
  32. } else if (strcmp(argv[i], "--vulkan") == 0) {
  33. winflags |= SDL_WINDOW_VULKAN;
  34. } else if (strcmp(argv[i], "--sw-render") == 0) {
  35. renderflags &= ~SDL_RENDERER_ACCELERATED;
  36. renderflags |= SDL_RENDERER_SOFTWARE;
  37. } else {
  38. warn << "Unknown argument: " << argv[i];
  39. }
  40. }
  41. sdlassert(SDL_Init(SDL_INIT_VIDEO) >= 0, "Could not initialize SDL");
  42. auto sdl = makeDeferred([] { SDL_Quit(); });
  43. int imgflags = IMG_INIT_PNG;
  44. imgassert(IMG_Init(imgflags) == imgflags, "Could not initialize SDL_Image");
  45. auto sdl_image = makeDeferred([] { IMG_Quit(); });
  46. auto window = makeRaiiPtr(
  47. SDL_CreateWindow(
  48. "Project: SWAN",
  49. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  50. 640, 480, winflags),
  51. SDL_DestroyWindow);
  52. // Load and display application icon
  53. auto icon = makeRaiiPtr(
  54. IMG_Load("assets/icon.png"),
  55. SDL_FreeSurface);
  56. sdlassert(icon, "Could not load icon");
  57. SDL_SetWindowIcon(window.get(), icon.get());
  58. auto renderer = makeRaiiPtr(
  59. SDL_CreateRenderer(window.get(), -1, renderflags),
  60. SDL_DestroyRenderer);
  61. sdlassert(renderer, "Could not create renderer");
  62. Win win(window.get(), renderer.get());
  63. Game game(win);
  64. std::vector<std::unique_ptr<Mod>> mods;
  65. mods.push_back(game.loadMod("core.mod"));
  66. game.createWorld("core::default", std::move(mods));
  67. PerfCounter pcounter;
  68. auto prev_time = std::chrono::steady_clock::now();
  69. float fps_acc = 0;
  70. float tick_acc = 0;
  71. int fcount = 0;
  72. int slow_frames = 0;
  73. while (1) {
  74. RTClock total_frame_clock;
  75. SDL_Event evt;
  76. while (SDL_PollEvent(&evt)) {
  77. switch (evt.type) {
  78. case SDL_QUIT:
  79. goto exit;
  80. break;
  81. case SDL_KEYDOWN:
  82. game.onKeyDown(evt.key.keysym);
  83. break;
  84. case SDL_KEYUP:
  85. game.onKeyUp(evt.key.keysym);
  86. break;
  87. case SDL_MOUSEMOTION:
  88. game.onMouseMove(evt.motion.x, evt.motion.y);
  89. break;
  90. case SDL_MOUSEBUTTONDOWN:
  91. game.onMouseDown(evt.button.x, evt.button.y, evt.button.button);
  92. break;
  93. case SDL_MOUSEBUTTONUP:
  94. game.onMouseUp(evt.button.x, evt.button.y, evt.button.button);
  95. break;
  96. }
  97. }
  98. auto now = std::chrono::steady_clock::now();
  99. std::chrono::duration<float> dur(now - prev_time);
  100. prev_time = now;
  101. float dt = dur.count();
  102. // Display FPS
  103. fps_acc += dt;
  104. fcount += 1;
  105. if (fps_acc >= 4) {
  106. info << "FPS: " << fcount / 4.0;
  107. fps_acc -= 4;
  108. fcount = 0;
  109. }
  110. // We want to warn if one frame takes over 0.1 seconds...
  111. if (dt > 0.1) {
  112. if (slow_frames == 0)
  113. warn << "Delta time too high! (" << dt << "s)";
  114. slow_frames += 1;
  115. // And we never want to do physics as if our one frame is greater than
  116. // 0.5 seconds.
  117. if (dt > 0.5)
  118. dt = 0.5;
  119. } else if (slow_frames > 0) {
  120. if (slow_frames > 1)
  121. warn << slow_frames << " consecutive slow frames.";
  122. slow_frames = 0;
  123. }
  124. // Simple case: we can keep up, only need one physics update
  125. RTClock update_clock;
  126. if (dt <= 1 / 25.0) {
  127. pcounter.countGameUpdatesPerFrame(1);
  128. game.update(dt);
  129. // Complex case: run multiple steps this iteration
  130. } else {
  131. int count = (int)ceilf(dt / (1/30.0));
  132. pcounter.countGameUpdatesPerFrame(count);
  133. float delta = dt / count;
  134. info << "Delta time " << dt << "s. Running " << count << " updates in one frame, with a delta as if we had " << 1.0 / delta << " FPS.";
  135. for (int i = 0; i < count; ++i)
  136. game.update(delta);
  137. }
  138. pcounter.countGameUpdate(update_clock.duration());
  139. // Tick at a consistent TICK_RATE
  140. tick_acc += dt;
  141. while (tick_acc >= 1.0 / TICK_RATE) {
  142. tick_acc -= 1.0 / TICK_RATE;
  143. RTClock tick_clock;
  144. game.tick(1.0 / TICK_RATE);
  145. pcounter.countGameTick(tick_clock.duration());
  146. }
  147. SDL_RenderClear(renderer.get());
  148. RTClock draw_clock;
  149. game.draw();
  150. pcounter.countGameDraw(draw_clock.duration());
  151. pcounter.countFrameTime(total_frame_clock.duration());
  152. RTClock present_clock;
  153. SDL_RenderPresent(renderer.get());
  154. pcounter.countRenderPresent(present_clock.duration());
  155. }
  156. exit:
  157. return EXIT_SUCCESS;
  158. }