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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <vector>
  2. #include <time.h>
  3. #include <unistd.h>
  4. #include <swan/common.h>
  5. #include <swan/Player.h>
  6. #include <swan/World.h>
  7. #include <swan/Game.h>
  8. using namespace Swan;
  9. static double getTime() {
  10. struct timespec ts;
  11. clock_gettime(CLOCK_MONOTONIC, &ts);
  12. return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
  13. }
  14. template<typename T>
  15. static void draw_ents(std::vector<T> ents) {
  16. for (auto &ent: ents)
  17. ent.draw();
  18. }
  19. int main() {
  20. sf::RenderWindow window(sf::VideoMode(800, 600), "good gaem");
  21. window.setVerticalSyncEnabled(true);
  22. sf::Transform transform;
  23. transform.scale(UNIT_SIZE, UNIT_SIZE);
  24. Win win = { window, transform };
  25. Game game;
  26. game.world_ = new World();
  27. game.world_->player_ = new Player(Vec2(1, 1));
  28. game.world_->current_plane_ = new WorldPlane();
  29. game.world_->planes_.push_back(game.world_->current_plane_);
  30. game.loadMod("core.mod/build/release/core.mod.so");
  31. double prevtime = getTime();
  32. double fpsAcc = 0;
  33. double tickAcc = 0;
  34. int fcount = 0;
  35. while (window.isOpen()) {
  36. sf::Event event;
  37. while (window.pollEvent(event)) {
  38. if (event.type == sf::Event::Closed) {
  39. window.close();
  40. } else if (event.type == sf::Event::Resized) {
  41. sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
  42. window.setView(sf::View(visibleArea));
  43. }
  44. }
  45. // Display FPS
  46. double now = getTime();
  47. double dt = now - prevtime;
  48. prevtime = now;
  49. fpsAcc += dt;
  50. fcount += 1;
  51. if (fpsAcc >= 1) {
  52. fprintf(stderr, "FPS: %i\n", fcount);
  53. fpsAcc -= 1;
  54. fcount = 0;
  55. }
  56. game.update(dt);
  57. // Call tick TICK_RATE times per second
  58. tickAcc += dt;
  59. while (tickAcc >= 1.0 / TICK_RATE) {
  60. tickAcc -= 1.0 / TICK_RATE;
  61. game.tick();
  62. }
  63. window.clear(sf::Color(135, 206, 250));
  64. game.draw(win);
  65. window.display();
  66. }
  67. return 0;
  68. }