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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 <imgui.h>
  11. #include <imgui_sdl.h>
  12. #include <cygnet/Renderer.h>
  13. #include <cygnet/Window.h>
  14. #include <swan/swan.h>
  15. using namespace Swan;
  16. #define errassert(expr, str, errfn) do { \
  17. if (!(expr)) { \
  18. panic << (str) << ": " << errfn(); \
  19. return EXIT_FAILURE; \
  20. } \
  21. } while (0)
  22. #define sdlassert(expr, str) errassert(expr, str, SDL_GetError);
  23. #define imgassert(expr, str) errassert(expr, str, IMG_GetError);
  24. // ImGUI and SDL have different numbers for mouse buttons
  25. int sdlButtonToImGuiButton(uint8_t button) {
  26. switch (button) {
  27. case SDL_BUTTON_LEFT:
  28. return 0;
  29. case SDL_BUTTON_RIGHT:
  30. return 1;
  31. case SDL_BUTTON_MIDDLE:
  32. return 2;
  33. case SDL_BUTTON_X1:
  34. return 3;
  35. case SDL_BUTTON_X2:
  36. return 4;
  37. default:
  38. warn << "Unknown mouse button: " << button;
  39. return 4; // Let's call that X2?
  40. }
  41. }
  42. int main(int argc, char **argv) {
  43. uint32_t winFlags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
  44. uint32_t renderFlags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
  45. float guiScale = 1;
  46. for (int i = 1; i < argc; ++i) {
  47. if (strcmp(argv[i], "--lodpi") == 0) {
  48. winFlags &= ~SDL_WINDOW_ALLOW_HIGHDPI;
  49. } else if (strcmp(argv[i], "--fullscreen") == 0) {
  50. winFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
  51. } else if (strcmp(argv[i], "--no-vsync") == 0) {
  52. renderFlags &= ~SDL_RENDERER_PRESENTVSYNC;
  53. } else if (strcmp(argv[i], "--vulkan") == 0) {
  54. winFlags |= SDL_WINDOW_VULKAN;
  55. } else if (strcmp(argv[i], "--sw-render") == 0) {
  56. renderFlags &= ~SDL_RENDERER_ACCELERATED;
  57. renderFlags |= SDL_RENDERER_SOFTWARE;
  58. } else if (strcmp(argv[i], "--2x") == 0) {
  59. guiScale = 2;
  60. } else if (strcmp(argv[i], "--gles") == 0) {
  61. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");
  62. } else {
  63. warn << "Unknown argument: " << argv[i];
  64. }
  65. }
  66. sdlassert(SDL_Init(SDL_INIT_VIDEO) >= 0, "Could not initialize SDL");
  67. Deferred<SDL_Quit> sdl;
  68. int imgFlags = IMG_INIT_PNG;
  69. imgassert(IMG_Init(imgFlags) == imgFlags, "Could not initialize SDL_Image");
  70. Deferred<IMG_Quit> sdlImage;
  71. Cygnet::Window window("Project: SWAN", 640 * guiScale, 480 * guiScale);
  72. // Load and display application icon
  73. CPtr<SDL_Surface, SDL_FreeSurface> icon(
  74. IMG_Load("assets/icon.png"));
  75. sdlassert(icon, "Could not load icon");
  76. SDL_SetWindowIcon(window.sdlWindow(), icon.get());
  77. // Init ImGUI and ImGUI_SDL
  78. /*
  79. IMGUI_CHECKVERSION();
  80. CPtr<ImGuiContext, ImGui::DestroyContext> context(
  81. ImGui::CreateContext());
  82. ImGuiSDL::Initialize(renderer.get(), (int)win.getPixSize().x, (int)win.getPixSize().y);
  83. Deferred<ImGuiSDL::Deinitialize> imguiSDL;
  84. info << "Initialized with window size " << win.getPixSize();
  85. // ImGuiIO is to glue SDL and ImGUI together
  86. ImGuiIO& imguiIO = ImGui::GetIO();
  87. imguiIO.BackendPlatformName = "imgui_sdl + Project: SWAN";
  88. TODO */
  89. // Create a world
  90. Game game;
  91. game.cam_.size = window.size();
  92. std::vector<std::string> mods{ "core.mod" };
  93. game.createWorld("core::default", mods);
  94. auto prevTime = std::chrono::steady_clock::now();
  95. float fpsAcc = 0;
  96. float tickAcc = 0;
  97. int fCount = 0;
  98. int slowFrames = 0;
  99. while (1) {
  100. ZoneScopedN("game loop");
  101. SDL_Event evt;
  102. while (SDL_PollEvent(&evt)) {
  103. switch (evt.type) {
  104. case SDL_QUIT:
  105. goto exit;
  106. break;
  107. case SDL_WINDOWEVENT:
  108. if (evt.window.event == SDL_WINDOWEVENT_RESIZED) {
  109. window.onResize(evt.window.data1, evt.window.data2);
  110. //imguiIO.DisplaySize.x = (float)evt.window.data1;
  111. //imguiIO.DisplaySize.y = (float)evt.window.data2;
  112. }
  113. break;
  114. case SDL_KEYDOWN:
  115. game.onKeyDown(evt.key.keysym);
  116. break;
  117. case SDL_KEYUP:
  118. game.onKeyUp(evt.key.keysym);
  119. break;
  120. case SDL_MOUSEMOTION:
  121. /*
  122. imguiIO.MousePos.x = (float)evt.motion.x;
  123. imguiIO.MousePos.y = (float)evt.motion.y;
  124. if (!imguiIO.WantCaptureMouse) */
  125. game.onMouseMove(evt.motion.x, evt.motion.y);
  126. break;
  127. case SDL_MOUSEBUTTONDOWN:
  128. /*
  129. imguiIO.MouseDown[sdlButtonToImGuiButton(evt.button.button)] = true;
  130. if (!imguiIO.WantCaptureMouse) */
  131. game.onMouseDown(evt.button.x, evt.button.y, evt.button.button);
  132. break;
  133. case SDL_MOUSEBUTTONUP:
  134. /*
  135. imguiIO.MouseDown[sdlButtonToImGuiButton(evt.button.button)] = false;
  136. if (!imguiIO.WantCaptureMouse) */
  137. game.onMouseUp(evt.button.x, evt.button.y, evt.button.button);
  138. break;
  139. case SDL_MOUSEWHEEL:
  140. if (evt.wheel.y == 0) {
  141. break;
  142. }
  143. /*
  144. imguiIO.MouseWheel += (float)evt.wheel.y;
  145. if (!imguiIO.WantCaptureMouse) */
  146. game.onScrollWheel(evt.wheel.y);
  147. break;
  148. }
  149. }
  150. game.cam_.size = window.size();
  151. auto now = std::chrono::steady_clock::now();
  152. std::chrono::duration<float> dur(now - prevTime);
  153. prevTime = now;
  154. float dt = dur.count();
  155. // Display FPS
  156. fpsAcc += dt;
  157. fCount += 1;
  158. if (fpsAcc >= 4) {
  159. info << "FPS: " << fCount / 4.0;
  160. fpsAcc -= 4;
  161. fCount = 0;
  162. }
  163. // We want to warn if one frame takes over 0.1 seconds...
  164. if (dt > 0.1) {
  165. if (slowFrames == 0)
  166. warn << "Delta time too high! (" << dt << "s)";
  167. slowFrames += 1;
  168. // And we never want to do physics as if our one frame is greater than
  169. // 0.5 seconds.
  170. if (dt > 0.5)
  171. dt = 0.5;
  172. } else if (slowFrames > 0) {
  173. if (slowFrames > 1)
  174. warn << slowFrames << " consecutive slow frames.";
  175. slowFrames = 0;
  176. }
  177. // Simple case: we can keep up, only need one physics update
  178. if (dt <= 1 / 25.0) {
  179. ZoneScopedN("game update");
  180. game.update(dt);
  181. // Complex case: run multiple steps this iteration
  182. } else {
  183. int count = (int)ceil(dt / (1/30.0));
  184. float delta = dt / (float)count;
  185. // Don't be too noisy with the occasional double update
  186. if (count > 2) {
  187. info << "Delta time " << dt << "s. Running " << count
  188. << " updates in one frame, with a delta as if we had "
  189. << 1.0 / delta << " FPS.";
  190. }
  191. for (int i = 0; i < count; ++i) {
  192. ZoneScopedN("game update");
  193. game.update(delta);
  194. }
  195. }
  196. // Tick at a consistent TICK_RATE
  197. tickAcc += dt;
  198. while (tickAcc >= 1.0 / TICK_RATE) {
  199. ZoneScopedN("game tick");
  200. tickAcc -= 1.0 / TICK_RATE;
  201. game.tick(1.0 / TICK_RATE);
  202. }
  203. {
  204. window.clear(game.backgroundColor());
  205. }
  206. // ImGUI
  207. //imguiIO.DeltaTime = dt;
  208. //ImGui::NewFrame();
  209. {
  210. ZoneScopedN("game draw");
  211. game.draw();
  212. }
  213. // Render ImGUI
  214. {
  215. ZoneScopedN("imgui render");
  216. //ImGui::Render();
  217. //ImGuiSDL::Render(ImGui::GetDrawData());
  218. }
  219. {
  220. ZoneScopedN("render present");
  221. window.flip();
  222. }
  223. FrameMark
  224. }
  225. exit:
  226. return EXIT_SUCCESS;
  227. }