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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include <time.h>
  2. #include <unistd.h>
  3. #include <vector>
  4. #include <memory>
  5. #include <chrono>
  6. #include <ratio>
  7. #define GLFW_INCLUDE_NONE
  8. #include <GLFW/glfw3.h>
  9. #include <SDL_image.h>
  10. #include <string.h>
  11. #include <imgui.h>
  12. #include <cygnet/gl.h>
  13. #include <cygnet/Renderer.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. static Game *gameptr;
  25. static double pixelRatio = 1;
  26. static void keyCallback(GLFWwindow *, int key, int scancode, int action, int) {
  27. if (action == GLFW_PRESS) {
  28. gameptr->onKeyDown(scancode);
  29. } else if (action == GLFW_RELEASE) {
  30. gameptr->onKeyUp(scancode);
  31. }
  32. }
  33. static void mouseButtonCallback(GLFWwindow *, int button, int action, int) {
  34. if (action == GLFW_PRESS) {
  35. gameptr->onMouseDown(button);
  36. } else if (action == GLFW_RELEASE) {
  37. gameptr->onMouseUp(button);
  38. }
  39. }
  40. static void cursorPositionCallback(GLFWwindow *, double xpos, double ypos) {
  41. gameptr->onMouseMove(xpos * pixelRatio, ypos * pixelRatio);
  42. }
  43. static void scrollCallback(GLFWwindow *, double dx, double dy) {
  44. gameptr->onScrollWheel(dy);
  45. }
  46. static void windowSizeCallback(GLFWwindow *window, int width, int height) {
  47. int dw, dh;
  48. glfwGetFramebufferSize(window, &dw, &dh);
  49. glViewport(0, 0, dw, dh);
  50. Cygnet::glCheck();
  51. gameptr->cam_.size = {dw, dh};
  52. pixelRatio = (double)dw / (double)width;
  53. }
  54. int main(int argc, char **argv) {
  55. glfwInit();
  56. Deferred<glfwTerminate> glfw;
  57. glfwSetErrorCallback(+[](int error, const char* description) {
  58. warn << "GLFW Error: " << error << ": " << description;
  59. });
  60. int imgFlags = IMG_INIT_PNG;
  61. imgassert(IMG_Init(imgFlags) == imgFlags, "Could not initialize SDL_Image");
  62. Deferred<IMG_Quit> sdlImage;
  63. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  64. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  65. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  66. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  67. GLFWwindow *window = glfwCreateWindow(640, 480, "Project: SWAN", nullptr, nullptr);
  68. if (!window) {
  69. panic << "Failed to create window";
  70. return 1;
  71. }
  72. glfwMakeContextCurrent(window);
  73. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  74. glEnable(GL_BLEND);
  75. // Create one global VAO, so we can pretend VAOs don't exist
  76. GLuint globalVao;
  77. glGenVertexArrays(1, &globalVao);
  78. glBindVertexArray(globalVao);
  79. // Load and display application icon
  80. /*
  81. CPtr<SDL_Surface, SDL_FreeSurface> icon(
  82. IMG_Load("assets/icon.png"));
  83. sdlassert(icon, "Could not load icon");
  84. SDL_SetWindowIcon(window.sdlWindow(), icon.get());*/
  85. // Init ImGUI and ImGUI_SDL
  86. /*
  87. IMGUI_CHECKVERSION();
  88. CPtr<ImGuiContext, ImGui::DestroyContext> context(
  89. ImGui::CreateContext());
  90. ImGuiSDL::Initialize(renderer.get(), (int)win.getPixSize().x, (int)win.getPixSize().y);
  91. Deferred<ImGuiSDL::Deinitialize> imguiSDL;
  92. info << "Initialized with window size " << win.getPixSize();
  93. // ImGuiIO is to glue SDL and ImGUI together
  94. ImGuiIO& imguiIO = ImGui::GetIO();
  95. imguiIO.BackendPlatformName = "imgui_sdl + Project: SWAN";
  96. TODO */
  97. // Create a world
  98. Game game;
  99. std::vector<std::string> mods{ "core.mod" };
  100. game.createWorld("core::default", mods);
  101. gameptr = &game;
  102. glfwSetKeyCallback(window, keyCallback);
  103. glfwSetMouseButtonCallback(window, mouseButtonCallback);
  104. glfwSetCursorPosCallback(window, cursorPositionCallback);
  105. glfwSetScrollCallback(window, scrollCallback);
  106. glfwSetWindowSizeCallback(window, windowSizeCallback);
  107. // Initialize window size stuff
  108. {
  109. int width, height;
  110. glfwGetWindowSize(window, &width, &height);
  111. windowSizeCallback(window, width, height);
  112. }
  113. auto prevTime = std::chrono::steady_clock::now();
  114. float fpsAcc = 0;
  115. float tickAcc = 0;
  116. int fCount = 0;
  117. int slowFrames = 0;
  118. while (!glfwWindowShouldClose(window)) {
  119. ZoneScopedN("game loop");
  120. auto now = std::chrono::steady_clock::now();
  121. std::chrono::duration<float> dur(now - prevTime);
  122. prevTime = now;
  123. float dt = dur.count();
  124. // Display FPS
  125. fpsAcc += dt;
  126. fCount += 1;
  127. if (fpsAcc >= 4) {
  128. info << "FPS: " << fCount / 4.0;
  129. fpsAcc -= 4;
  130. fCount = 0;
  131. }
  132. // We want to warn if one frame takes over 0.1 seconds...
  133. if (dt > 0.1) {
  134. if (slowFrames == 0)
  135. warn << "Delta time too high! (" << dt << "s)";
  136. slowFrames += 1;
  137. // And we never want to do physics as if our one frame is greater than
  138. // 0.5 seconds.
  139. if (dt > 0.5)
  140. dt = 0.5;
  141. } else if (slowFrames > 0) {
  142. if (slowFrames > 1)
  143. warn << slowFrames << " consecutive slow frames.";
  144. slowFrames = 0;
  145. }
  146. // Simple case: we can keep up, only need one physics update
  147. if (dt <= 1 / 25.0) {
  148. ZoneScopedN("game update");
  149. game.update(dt);
  150. // Complex case: run multiple steps this iteration
  151. } else {
  152. int count = (int)ceil(dt / (1/30.0));
  153. float delta = dt / (float)count;
  154. // Don't be too noisy with the occasional double update
  155. if (count > 2) {
  156. info << "Delta time " << dt << "s. Running " << count
  157. << " updates in one frame, with a delta as if we had "
  158. << 1.0 / delta << " FPS.";
  159. }
  160. for (int i = 0; i < count; ++i) {
  161. ZoneScopedN("game update");
  162. game.update(delta);
  163. }
  164. }
  165. // Tick at a consistent TICK_RATE
  166. tickAcc += dt;
  167. while (tickAcc >= 1.0 / TICK_RATE) {
  168. ZoneScopedN("game tick");
  169. tickAcc -= 1.0 / TICK_RATE;
  170. game.tick(1.0 / TICK_RATE);
  171. }
  172. {
  173. Cygnet::Color color = game.backgroundColor();
  174. glClearColor(color.r, color.g, color.b, color.a);
  175. glClear(GL_COLOR_BUFFER_BIT);
  176. }
  177. // ImGUI
  178. //imguiIO.DeltaTime = dt;
  179. //ImGui::NewFrame();
  180. {
  181. ZoneScopedN("game draw");
  182. game.draw();
  183. }
  184. // Render ImGUI
  185. {
  186. ZoneScopedN("imgui render");
  187. //ImGui::Render();
  188. //ImGuiSDL::Render(ImGui::GetDrawData());
  189. }
  190. {
  191. ZoneScopedN("render present");
  192. glfwSwapBuffers(window);
  193. }
  194. glfwPollEvents();
  195. FrameMark
  196. }
  197. }