Added constant FPS, background and working player
authorNils Forssén <nilfo359@student.liu.se>
Fri, 10 Nov 2023 00:29:03 +0000 (01:29 +0100)
committerNils Forssén <nilfo359@student.liu.se>
Fri, 10 Nov 2023 00:29:03 +0000 (01:29 +0100)
src/_main.cc
src/constants.h

index c248dd55bb39de0b50a283f43be52a654c6849c0..d1123518e94e1a022e6d05735d9159cc04499c56 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "constants.h"
 #include "Player.h"
+#include "Map.h"
 
 int main ()
 {
@@ -16,33 +17,45 @@ int main ()
         "DespYrat"
     };
 
-    Player test_player{};
-
-    sf::Texture t;
-    t.loadFromFile("assets/hel_bakgrund.png");
-    sf::Sprite s;
-    s.setTexture(t);
-
-    s.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF);
+    Map game_map{};
+    Player player{};
     
+    sf::Clock game_clock;
     while (window.isOpen())
     {
-        /* hantera events */
+        // Handle input
         sf::Event event;
-        // hämta ett event i taget
         while (window.pollEvent(event))
         {
-            if (event.type == sf::Event::Closed)
+            switch (event.type)
             {
+            case sf::Event::Closed:
                 window.close();
+                break;
+            default:
+                break;
             }
+
+            player.handle_input(event);
         }
 
-        /* rita ut */
-        window.clear(); // rensa skärmen
-        window.draw(s);
-        test_player.render(window);
-        test_player.update();
-        window.display(); // visa ändringarna
+
+        // Update
+        game_map.update();
+        player.update();
+
+        //Draw
+        window.clear();
+
+        game_map.render(window);
+        player.render(window);
+
+
+        window.display();
+
+        // Make game run at constant fps
+        sf::sleep(sf::milliseconds(1000.0 / FPS) - game_clock.getElapsedTime());
+        game_clock.restart();
+
     }
 }
index e1581fc5ccbb71b5d7c51c866fe1227e97a80baa..b76a1b5cd4b6891551a18f1556b51db9b0e48ec8 100644 (file)
@@ -6,4 +6,6 @@ const unsigned int S_HEIGHT {720};
 
 const double S_SCALE_KOEFF {S_WIDTH / 1920.0};
 
+const unsigned int FPS {60};
+
 #endif
\ No newline at end of file