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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/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. //imguiIO.DisplaySize.x = (float)evt.window.data1;
  110. //imguiIO.DisplaySize.y = (float)evt.window.data2;
  111. window.onResize(evt.window.data1, 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. /*
  141. imguiIO.MouseWheel += (float)evt.wheel.y;
  142. if (!imguiIO.WantCaptureMouse) */
  143. game.onScrollWheel(evt.wheel.y);
  144. break;
  145. }
  146. }
  147. game.cam_.size = window.size();
  148. auto now = std::chrono::steady_clock::now();
  149. std::chrono::duration<float> dur(now - prevTime);
  150. prevTime = now;
  151. float dt = dur.count();
  152. // Display FPS
  153. fpsAcc += dt;
  154. fCount += 1;
  155. if (fpsAcc >= 4) {
  156. info << "FPS: " << fCount / 4.0;
  157. fpsAcc -= 4;
  158. fCount = 0;
  159. }
  160. // We want to warn if one frame takes over 0.1 seconds...
  161. if (dt > 0.1) {
  162. if (slowFrames == 0)
  163. warn << "Delta time too high! (" << dt << "s)";
  164. slowFrames += 1;
  165. // And we never want to do physics as if our one frame is greater than
  166. // 0.5 seconds.
  167. if (dt > 0.5)
  168. dt = 0.5;
  169. } else if (slowFrames > 0) {
  170. if (slowFrames > 1)
  171. warn << slowFrames << " consecutive slow frames.";
  172. slowFrames = 0;
  173. }
  174. // Simple case: we can keep up, only need one physics update
  175. if (dt <= 1 / 25.0) {
  176. ZoneScopedN("game update");
  177. game.update(dt);
  178. // Complex case: run multiple steps this iteration
  179. } else {
  180. int count = (int)ceil(dt / (1/30.0));
  181. float delta = dt / (float)count;
  182. // Don't be too noisy with the occasional double update
  183. if (count > 2) {
  184. info << "Delta time " << dt << "s. Running " << count
  185. << " updates in one frame, with a delta as if we had "
  186. << 1.0 / delta << " FPS.";
  187. }
  188. for (int i = 0; i < count; ++i) {
  189. ZoneScopedN("game update");
  190. game.update(delta);
  191. }
  192. }
  193. // Tick at a consistent TICK_RATE
  194. tickAcc += dt;
  195. while (tickAcc >= 1.0 / TICK_RATE) {
  196. ZoneScopedN("game tick");
  197. tickAcc -= 1.0 / TICK_RATE;
  198. game.tick(1.0 / TICK_RATE);
  199. }
  200. {
  201. //auto [r, g, b, a] = game.backgroundColor();
  202. // TODO: Set clear color
  203. window.clear();
  204. }
  205. // ImGUI
  206. //imguiIO.DeltaTime = dt;
  207. //ImGui::NewFrame();
  208. {
  209. ZoneScopedN("game draw");
  210. game.draw();
  211. }
  212. // Render ImGUI
  213. {
  214. ZoneScopedN("imgui render");
  215. //ImGui::Render();
  216. //ImGuiSDL::Render(ImGui::GetDrawData());
  217. }
  218. {
  219. ZoneScopedN("render present");
  220. window.flip();
  221. }
  222. FrameMark
  223. }
  224. exit:
  225. return EXIT_SUCCESS;
  226. }