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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <vector>
  2. #include <time.h>
  3. #include "common.h"
  4. #include "Player.h"
  5. double getTime() {
  6. struct timespec ts;
  7. clock_gettime(CLOCK_MONOTONIC, &ts);
  8. return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
  9. }
  10. template<typename T>
  11. void draw_ents(std::vector<T> ents) {
  12. for (auto &ent: ents)
  13. ent.draw();
  14. }
  15. int main() {
  16. sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
  17. sf::Transform transform;
  18. transform.scale(UNIT_SIZE, UNIT_SIZE);
  19. Win win = { window, transform };
  20. Player player(Vec2(1, 1));
  21. double prevtime = getTime();
  22. double acc = 0;
  23. int fcount = 0;
  24. while (window.isOpen()) {
  25. sf::Event event;
  26. while (window.pollEvent(event)) {
  27. if (event.type == sf::Event::Closed)
  28. window.close();
  29. }
  30. double now = getTime();
  31. double dt = now - prevtime;
  32. prevtime = now;
  33. acc += dt;
  34. fcount += 1;
  35. if (acc >= 1) {
  36. fprintf(stderr, "fps %i\n", fcount);
  37. acc = 0;
  38. fcount = 0;
  39. }
  40. window.clear(sf::Color(135, 206, 250));
  41. player.draw(win);
  42. player.update(dt);
  43. window.display();
  44. }
  45. return 0;
  46. }