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

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