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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <time.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <vector>
  5. #include <memory>
  6. #include <chrono>
  7. #include <ratio>
  8. #include <SDL2/SDL.h>
  9. #include <SDL2/SDL_image.h>
  10. #include <swan/swan.h>
  11. #include <swan/util.h>
  12. using namespace Swan;
  13. #define errassert(expr, str, errfn) do { \
  14. if (!(expr)) { \
  15. fprintf(stderr, "%s: %s\n", str, errfn()); \
  16. return EXIT_FAILURE; \
  17. } \
  18. } while (0)
  19. #define sdlassert(expr, str) errassert(expr, str, SDL_GetError);
  20. #define imgassert(expr, str) errassert(expr, str, IMG_GetError);
  21. template<typename T>
  22. using DeleteFunc = void (*)(T *);
  23. int main() {
  24. sdlassert(SDL_Init(SDL_INIT_VIDEO) >= 0, "Could not initialize SDL");
  25. auto sdl = makeDeferred([] { SDL_Quit(); });
  26. int imgflags = IMG_INIT_PNG;
  27. imgassert(IMG_Init(imgflags) == imgflags, "Could not initialize SDL_Image");
  28. auto sdl_image = makeDeferred([] { IMG_Quit(); });
  29. auto window = makeRaiiPtr(
  30. SDL_CreateWindow(
  31. "Project: SWAN",
  32. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  33. 640, 480,
  34. SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
  35. SDL_DestroyWindow);
  36. auto renderer = makeRaiiPtr(
  37. SDL_CreateRenderer(
  38. window.get(), -1, SDL_RENDERER_ACCELERATED),
  39. SDL_DestroyRenderer);
  40. sdlassert(renderer, "Could not create renderer\n");
  41. Win win(renderer.get());
  42. Game game(win);
  43. std::vector<std::unique_ptr<Mod>> mods;
  44. mods.push_back(game.loadMod("core.mod"));
  45. game.createWorld("core::default", std::move(mods));
  46. auto prevTime = std::chrono::steady_clock::now();
  47. float fpsAcc = 0;
  48. float tickAcc = 0;
  49. int fcount = 0;
  50. int slowFrames = 0;
  51. while (1) {
  52. SDL_Event evt;
  53. while (SDL_PollEvent(&evt)) {
  54. switch (evt.type) {
  55. case SDL_QUIT:
  56. goto exit;
  57. break;
  58. case SDL_KEYDOWN:
  59. game.onKeyDown(evt.key.keysym);
  60. break;
  61. case SDL_KEYUP:
  62. game.onKeyUp(evt.key.keysym);
  63. break;
  64. case SDL_MOUSEMOTION:
  65. game.onMouseMove(evt.motion.x, evt.motion.y);
  66. break;
  67. case SDL_MOUSEBUTTONDOWN:
  68. game.onMouseDown(evt.button.x, evt.button.y, evt.button.button);
  69. break;
  70. case SDL_MOUSEBUTTONUP:
  71. game.onMouseUp(evt.button.x, evt.button.y, evt.button.button);
  72. break;
  73. }
  74. }
  75. auto now = std::chrono::steady_clock::now();
  76. std::chrono::duration<float> dur(now - prevTime);
  77. prevTime = now;
  78. float dt = dur.count();
  79. // Display FPS
  80. fpsAcc += dt;
  81. fcount += 1;
  82. if (fpsAcc >= 4) {
  83. fprintf(stderr, "FPS: %.3f\n", fcount / 4.0);
  84. fpsAcc -= 4;
  85. fcount = 0;
  86. }
  87. game.update(dt);
  88. if (dt > 0.1) {
  89. if (slowFrames == 0)
  90. fprintf(stderr, "Warning: delta time is too high! (%.3fs).\n", dt);
  91. slowFrames += 1;
  92. } else {
  93. if (slowFrames > 0) {
  94. if (slowFrames > 1)
  95. fprintf(stderr, "%i consecutive slow frames.\n", slowFrames);
  96. slowFrames = 0;
  97. }
  98. tickAcc += dt;
  99. while (tickAcc >= 1.0 / TICK_RATE) {
  100. tickAcc -= 1.0 / TICK_RATE;
  101. game.tick(1.0 / TICK_RATE);
  102. }
  103. }
  104. game.draw();
  105. SDL_UpdateWindowSurface(window.get());
  106. }
  107. exit:
  108. return EXIT_SUCCESS;
  109. }