From 1ec14406f0816dc047dba23961878157acd809a9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Fri, 10 Nov 2023 01:29:03 +0100 Subject: [PATCH] Added constant FPS, background and working player --- src/_main.cc | 47 ++++++++++++++++++++++++++++++----------------- src/constants.h | 2 ++ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/_main.cc b/src/_main.cc index c248dd5..d112351 100644 --- a/src/_main.cc +++ b/src/_main.cc @@ -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(); + } } diff --git a/src/constants.h b/src/constants.h index e1581fc..b76a1b5 100644 --- a/src/constants.h +++ b/src/constants.h @@ -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 -- 2.30.2