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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <SFML/System/Clock.hpp>
  10. #include <SFML/Audio.hpp>
  11. using namespace Swan;
  12. int main() {
  13. sf::Image icon;
  14. if (!icon.loadFromFile("assets/icon.png")) {
  15. fprintf(stderr, "Failed to load image 'icon.png'\n");
  16. abort();
  17. }
  18. // Cretate window
  19. sf::RenderWindow window(sf::VideoMode(800, 600), "Project: SWAN");
  20. window.setVerticalSyncEnabled(true);
  21. window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
  22. Win win(&window);
  23. // Create music
  24. sf::SoundBuffer musicbuf;
  25. sf::Sound music;
  26. if (musicbuf.loadFromFile("assets/music/happy-1.wav")) {
  27. music.setBuffer(musicbuf);
  28. music.setLoop(true);
  29. music.play();
  30. } else {
  31. fprintf(stderr, "Failed to load music! Am very sad.\n");
  32. }
  33. Game::initGlobal();
  34. Game game(win);
  35. game.loadMod("core.mod");
  36. game.createWorld("core::default");
  37. sf::Clock clock;
  38. float fpsAcc = 0;
  39. float tickAcc = 0;
  40. int fcount = 0;
  41. int slowFrames = 0;
  42. while (window.isOpen()) {
  43. sf::Event event;
  44. while (window.pollEvent(event)) {
  45. if (event.type == sf::Event::Closed) {
  46. window.close();
  47. } else if (event.type == sf::Event::Resized) {
  48. sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
  49. window.setView(sf::View(visibleArea));
  50. }
  51. }
  52. float dt = clock.restart().asSeconds();
  53. // Display FPS
  54. fpsAcc += dt;
  55. fcount += 1;
  56. if (fpsAcc >= 4) {
  57. fprintf(stderr, "FPS: %.3f\n", fcount / 4.0);
  58. fpsAcc -= 4;
  59. fcount = 0;
  60. }
  61. if (dt > 0.1) {
  62. if (slowFrames == 0)
  63. fprintf(stderr, "Warning: delta time is too high! (%.3fs).\n", dt);
  64. slowFrames += 1;
  65. } else {
  66. if (slowFrames > 0) {
  67. if (slowFrames > 1)
  68. fprintf(stderr, "%i consecutive slow frames.\n", slowFrames);
  69. slowFrames = 0;
  70. }
  71. game.update(dt);
  72. // Call tick TICK_RATE times per second
  73. tickAcc += dt;
  74. while (tickAcc >= 1.0 / TICK_RATE) {
  75. tickAcc -= 1.0 / TICK_RATE;
  76. game.tick();
  77. }
  78. }
  79. window.clear();
  80. game.draw();
  81. window.display();
  82. }
  83. return 0;
  84. }