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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include <time.h>
  2. #include <unistd.h>
  3. #include <vector>
  4. #include <memory>
  5. #include <chrono>
  6. #include <ratio>
  7. #include <SDL.h>
  8. #include <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(
  126. evt.motion.x * window.pixelRatio(),
  127. evt.motion.y * window.pixelRatio());
  128. break;
  129. case SDL_MOUSEBUTTONDOWN:
  130. /*
  131. imguiIO.MouseDown[sdlButtonToImGuiButton(evt.button.button)] = true;
  132. if (!imguiIO.WantCaptureMouse) */
  133. game.onMouseDown(
  134. evt.button.x * window.pixelRatio(),
  135. evt.button.y * window.pixelRatio(),
  136. evt.button.button);
  137. break;
  138. case SDL_MOUSEBUTTONUP:
  139. /*
  140. imguiIO.MouseDown[sdlButtonToImGuiButton(evt.button.button)] = false;
  141. if (!imguiIO.WantCaptureMouse) */
  142. game.onMouseUp(
  143. evt.button.x * window.pixelRatio(),
  144. evt.button.y * window.pixelRatio(),
  145. evt.button.button);
  146. break;
  147. case SDL_MOUSEWHEEL:
  148. if (evt.wheel.y == 0) {
  149. break;
  150. }
  151. /*
  152. imguiIO.MouseWheel += (float)evt.wheel.y;
  153. if (!imguiIO.WantCaptureMouse) */
  154. game.onScrollWheel(evt.wheel.y);
  155. break;
  156. }
  157. }
  158. game.cam_.size = window.size();
  159. auto now = std::chrono::steady_clock::now();
  160. std::chrono::duration<float> dur(now - prevTime);
  161. prevTime = now;
  162. float dt = dur.count();
  163. // Display FPS
  164. fpsAcc += dt;
  165. fCount += 1;
  166. if (fpsAcc >= 4) {
  167. info << "FPS: " << fCount / 4.0;
  168. fpsAcc -= 4;
  169. fCount = 0;
  170. }
  171. // We want to warn if one frame takes over 0.1 seconds...
  172. if (dt > 0.1) {
  173. if (slowFrames == 0)
  174. warn << "Delta time too high! (" << dt << "s)";
  175. slowFrames += 1;
  176. // And we never want to do physics as if our one frame is greater than
  177. // 0.5 seconds.
  178. if (dt > 0.5)
  179. dt = 0.5;
  180. } else if (slowFrames > 0) {
  181. if (slowFrames > 1)
  182. warn << slowFrames << " consecutive slow frames.";
  183. slowFrames = 0;
  184. }
  185. // Simple case: we can keep up, only need one physics update
  186. if (dt <= 1 / 25.0) {
  187. ZoneScopedN("game update");
  188. game.update(dt);
  189. // Complex case: run multiple steps this iteration
  190. } else {
  191. int count = (int)ceil(dt / (1/30.0));
  192. float delta = dt / (float)count;
  193. // Don't be too noisy with the occasional double update
  194. if (count > 2) {
  195. info << "Delta time " << dt << "s. Running " << count
  196. << " updates in one frame, with a delta as if we had "
  197. << 1.0 / delta << " FPS.";
  198. }
  199. for (int i = 0; i < count; ++i) {
  200. ZoneScopedN("game update");
  201. game.update(delta);
  202. }
  203. }
  204. // Tick at a consistent TICK_RATE
  205. tickAcc += dt;
  206. while (tickAcc >= 1.0 / TICK_RATE) {
  207. ZoneScopedN("game tick");
  208. tickAcc -= 1.0 / TICK_RATE;
  209. game.tick(1.0 / TICK_RATE);
  210. }
  211. {
  212. window.clear(game.backgroundColor());
  213. }
  214. // ImGUI
  215. //imguiIO.DeltaTime = dt;
  216. //ImGui::NewFrame();
  217. {
  218. ZoneScopedN("game draw");
  219. game.draw();
  220. }
  221. // Render ImGUI
  222. {
  223. ZoneScopedN("imgui render");
  224. //ImGui::Render();
  225. //ImGuiSDL::Render(ImGui::GetDrawData());
  226. }
  227. {
  228. ZoneScopedN("render present");
  229. window.flip();
  230. }
  231. FrameMark
  232. }
  233. exit:
  234. return EXIT_SUCCESS;
  235. }