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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. int main() {
  15. sf::RenderWindow window(sf::VideoMode(800, 600), "good gaem");
  16. window.setVerticalSyncEnabled(true);
  17. Win win(&window);
  18. Game game;
  19. game.loadMod("core.mod");
  20. game.createWorld();
  21. game.world_->setCurrentPlane(game.world_->addPlane());
  22. game.world_->player_ = new Player(Vec2(1, 1));
  23. Tile::TileID tStone = game.world_->getTileID("core::stone");
  24. WorldPlane &plane = game.world_->getPlane(game.world_->current_plane_);
  25. for (int x = 1; x < 10; ++x) {
  26. for (int y = 1; y < 10; ++y) {
  27. plane.setTile(x, y, tStone);
  28. }
  29. }
  30. double prevtime = getTime();
  31. double fpsAcc = 0;
  32. double tickAcc = 0;
  33. int fcount = 0;
  34. while (window.isOpen()) {
  35. sf::Event event;
  36. while (window.pollEvent(event)) {
  37. if (event.type == sf::Event::Closed) {
  38. window.close();
  39. } else if (event.type == sf::Event::Resized) {
  40. sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
  41. window.setView(sf::View(visibleArea));
  42. }
  43. }
  44. // Display FPS
  45. double now = getTime();
  46. double dt = now - prevtime;
  47. prevtime = now;
  48. fpsAcc += dt;
  49. fcount += 1;
  50. if (fpsAcc >= 1) {
  51. fprintf(stderr, "FPS: %i\n", fcount);
  52. fpsAcc -= 1;
  53. fcount = 0;
  54. }
  55. game.update(dt);
  56. // Call tick TICK_RATE times per second
  57. tickAcc += dt;
  58. while (tickAcc >= 1.0 / TICK_RATE) {
  59. tickAcc -= 1.0 / TICK_RATE;
  60. game.tick();
  61. }
  62. window.clear(sf::Color(135, 206, 250));
  63. game.draw(win);
  64. window.display();
  65. }
  66. return 0;
  67. }