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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <vector>
  2. #include <time.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <swan/common.h>
  6. #include <swan/World.h>
  7. #include <swan/Game.h>
  8. #include <swan/Timer.h>
  9. #include <swan/Win.h>
  10. #include <SFML/System/Clock.hpp>
  11. #include <SFML/Audio.hpp>
  12. #include <imgui/imgui.h>
  13. #include <imgui-SFML.h>
  14. using namespace Swan;
  15. int main() {
  16. sf::Image icon;
  17. if (!icon.loadFromFile("assets/icon.png")) {
  18. fprintf(stderr, "Failed to load image 'icon.png'\n");
  19. abort();
  20. }
  21. // Cretate window
  22. sf::RenderWindow window(sf::VideoMode(800, 600), "Project: SWAN");
  23. window.setVerticalSyncEnabled(true);
  24. window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
  25. Win win(&window);
  26. // Initialize ImGui
  27. ImGui::SFML::Init(window);
  28. // Create music
  29. sf::SoundBuffer musicbuf;
  30. sf::Sound music;
  31. if (musicbuf.loadFromFile("assets/music/happy-1.wav")) {
  32. music.setBuffer(musicbuf);
  33. music.setLoop(true);
  34. music.play();
  35. } else {
  36. fprintf(stderr, "Failed to load music! Am very sad.\n");
  37. }
  38. Game::initGlobal();
  39. Game game(win);
  40. game.loadMod("core.mod");
  41. game.createWorld("core::default");
  42. sf::Clock clock;
  43. float fpsAcc = 0;
  44. float tickAcc = 0;
  45. int fcount = 0;
  46. int slowFrames = 0;
  47. while (window.isOpen()) {
  48. sf::Event event;
  49. while (window.pollEvent(event)) {
  50. ImGui::SFML::ProcessEvent(event);
  51. switch (event.type) {
  52. case sf::Event::Closed:
  53. window.close();
  54. break;
  55. case sf::Event::Resized:
  56. window.setView(sf::View(sf::FloatRect(
  57. 0, 0, event.size.width, event.size.height)));
  58. break;
  59. case sf::Event::KeyPressed:
  60. game.onKeyPressed(event.key.code);
  61. break;
  62. case sf::Event::KeyReleased:
  63. game.onKeyReleased(event.key.code);
  64. break;
  65. case sf::Event::MouseMoved:
  66. game.onMouseMove(event.mouseMove.x, event.mouseMove.y);
  67. break;
  68. case sf::Event::MouseButtonPressed:
  69. game.onMousePressed(event.mouseButton.button);
  70. break;
  71. case sf::Event::MouseButtonReleased:
  72. game.onMouseReleased(event.mouseButton.button);
  73. break;
  74. default: break;
  75. }
  76. }
  77. float dt = clock.restart().asSeconds();
  78. // Display FPS
  79. fpsAcc += dt;
  80. fcount += 1;
  81. if (fpsAcc >= 4) {
  82. fprintf(stderr, "FPS: %.3f\n", fcount / 4.0);
  83. fpsAcc -= 4;
  84. fcount = 0;
  85. }
  86. if (dt > 0.1) {
  87. if (slowFrames == 0)
  88. fprintf(stderr, "Warning: delta time is too high! (%.3fs).\n", dt);
  89. slowFrames += 1;
  90. } else {
  91. if (slowFrames > 0) {
  92. if (slowFrames > 1)
  93. fprintf(stderr, "%i consecutive slow frames.\n", slowFrames);
  94. slowFrames = 0;
  95. }
  96. game.update(dt);
  97. // Call tick TICK_RATE times per second
  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. ImGui::SFML::Update(window, sf::seconds(dt));
  105. ImGui::Begin("Test Window");
  106. ImGui::Button("No.");
  107. ImGui::End();
  108. ImGui::ShowTestWindow();
  109. window.clear();
  110. game.draw();
  111. ImGui::SFML::Render(window);
  112. window.display();
  113. }
  114. return 0;
  115. }