A 2D tile-based sandbox game.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_WINDOWEVENT:
  82. if (evt.window.event == SDL_WINDOWEVENT_RESIZED)
  83. win.onResize(evt.window.data1, evt.window.data2);
  84. break;
  85. case SDL_KEYDOWN:
  86. game.onKeyDown(evt.key.keysym);
  87. break;
  88. case SDL_KEYUP:
  89. game.onKeyUp(evt.key.keysym);
  90. break;
  91. case SDL_MOUSEMOTION:
  92. game.onMouseMove(evt.motion.x, evt.motion.y);
  93. break;
  94. case SDL_MOUSEBUTTONDOWN:
  95. game.onMouseDown(evt.button.x, evt.button.y, evt.button.button);
  96. break;
  97. case SDL_MOUSEBUTTONUP:
  98. game.onMouseUp(evt.button.x, evt.button.y, evt.button.button);
  99. break;
  100. }
  101. }
  102. auto now = std::chrono::steady_clock::now();
  103. std::chrono::duration<float> dur(now - prev_time);
  104. prev_time = now;
  105. float dt = dur.count();
  106. // Display FPS
  107. fps_acc += dt;
  108. fcount += 1;
  109. if (fps_acc >= 4) {
  110. info << "FPS: " << fcount / 4.0;
  111. fps_acc -= 4;
  112. fcount = 0;
  113. }
  114. // We want to warn if one frame takes over 0.1 seconds...
  115. if (dt > 0.1) {
  116. if (slow_frames == 0)
  117. warn << "Delta time too high! (" << dt << "s)";
  118. slow_frames += 1;
  119. // And we never want to do physics as if our one frame is greater than
  120. // 0.5 seconds.
  121. if (dt > 0.5)
  122. dt = 0.5;
  123. } else if (slow_frames > 0) {
  124. if (slow_frames > 1)
  125. warn << slow_frames << " consecutive slow frames.";
  126. slow_frames = 0;
  127. }
  128. // Simple case: we can keep up, only need one physics update
  129. RTClock update_clock;
  130. if (dt <= 1 / 25.0) {
  131. pcounter.countGameUpdatesPerFrame(1);
  132. game.update(dt);
  133. // Complex case: run multiple steps this iteration
  134. } else {
  135. int count = (int)ceil(dt / (1/30.0));
  136. pcounter.countGameUpdatesPerFrame(count);
  137. float delta = dt / (float)count;
  138. info << "Delta time " << dt << "s. Running " << count << " updates in one frame, with a delta as if we had " << 1.0 / delta << " FPS.";
  139. for (int i = 0; i < count; ++i)
  140. game.update(delta);
  141. }
  142. pcounter.countGameUpdate(update_clock.duration());
  143. // Tick at a consistent TICK_RATE
  144. tick_acc += dt;
  145. while (tick_acc >= 1.0 / TICK_RATE) {
  146. tick_acc -= 1.0 / TICK_RATE;
  147. RTClock tick_clock;
  148. game.tick(1.0 / TICK_RATE);
  149. pcounter.countGameTick(tick_clock.duration());
  150. }
  151. SDL_RenderClear(renderer.get());
  152. RTClock draw_clock;
  153. game.draw();
  154. pcounter.countGameDraw(draw_clock.duration());
  155. pcounter.countFrameTime(total_frame_clock.duration());
  156. RTClock present_clock;
  157. SDL_RenderPresent(renderer.get());
  158. pcounter.countRenderPresent(present_clock.duration());
  159. }
  160. exit:
  161. return EXIT_SUCCESS;
  162. }