From a216cfc457cdde754c5673e9423d00147a2f1574 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Mon, 6 Jan 2025 13:34:27 +0100 Subject: fps in hud --- game/hud/HudConfig.h | 7 +++++++ game/hud/HudScript.cpp | 13 +++++++++++-- game/hud/HudSubScene.cpp | 12 ++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) (limited to 'game/hud') diff --git a/game/hud/HudConfig.h b/game/hud/HudConfig.h index 5972f0a..e3497fb 100644 --- a/game/hud/HudConfig.h +++ b/game/hud/HudConfig.h @@ -5,6 +5,7 @@ static constexpr crepe::vec2 TOP_LEFT = {-530,-230}; static constexpr const char* HUD_DISTANCE = "hud_distance"; static constexpr const char* HUD_BEST = "hud_best"; static constexpr const char* HUD_COINS = "hud_coins"; +static constexpr const char* HUD_FPS = "hud_fps"; // Distance static constexpr const char* DISTANCE_PLACEHOLDER = "0000m"; @@ -24,4 +25,10 @@ static constexpr const char* COINS = "0000"; static constexpr int COINS_LENGTH = 4; static constexpr float COINS_CHAR_WIDTH = 10; static constexpr crepe::vec2 COINS_OFFSET = {0,50}; + +// FPS +static constexpr const char* FPS = "00"; +static constexpr int FPS_LENGTH = 2; +static constexpr float FPS_CHAR_WIDTH = 10; +static constexpr crepe::vec2 FPS_OFFSET = {1030,0}; \ No newline at end of file diff --git a/game/hud/HudScript.cpp b/game/hud/HudScript.cpp index deeea14..71c84a1 100644 --- a/game/hud/HudScript.cpp +++ b/game/hud/HudScript.cpp @@ -21,8 +21,6 @@ void HudScript::init() { void HudScript::frame_update(crepe::duration_t dt) { - // string number = std::to_string(savemgr->get(DISTANCE_RUN,0).get()); - // Distance Text & txt_dt = this->get_components_by_name(HUD_DISTANCE).front(); Transform & tf = this->get_components_by_name(PLAYER_NAME).front(); @@ -37,4 +35,15 @@ void HudScript::frame_update(crepe::duration_t dt) { txt_co.text = amount_of_coins; txt_co.dimensions = {COINS_CHAR_WIDTH*amount_of_coins.size(),(COINS_CHAR_WIDTH)*2}; txt_co.offset = TOP_LEFT+FONTOFFSET+COINS_OFFSET + vec2{amount_of_coins.size() * COINS_CHAR_WIDTH/2,0}; + + // FPS + Text & txt_fps = this->get_components_by_name(HUD_FPS).front(); + float fps = this->get_loop_timer().get_fps(); + string fps_amount = to_string(this->get_loop_timer().get_fps()); + txt_fps.text = fps_amount; + txt_fps.dimensions = {FPS_CHAR_WIDTH*fps_amount.size(),(FPS_CHAR_WIDTH)*2}; + txt_fps.offset = TOP_LEFT+FONTOFFSET+FPS_OFFSET + vec2{fps_amount.size() * FPS_CHAR_WIDTH/2,0}; + if(fps >= 30) txt_fps.data.text_color = Color::YELLOW; + if(fps >= 50) txt_fps.data.text_color = Color::GREEN; + if(fps < 30) txt_fps.data.text_color = Color::RED; } diff --git a/game/hud/HudSubScene.cpp b/game/hud/HudSubScene.cpp index 126f933..dd36d1c 100644 --- a/game/hud/HudSubScene.cpp +++ b/game/hud/HudSubScene.cpp @@ -28,9 +28,17 @@ void HudSubScene::create(Scene & scn){ // Coins GameObject hud_coin = scn.new_object(HUD_COINS); - crepe::vec2 size = {COINS_CHAR_WIDTH*COINS_LENGTH,(COINS_CHAR_WIDTH)*2}; - hud_coin.add_component(size, FONT,Text::Data{ + crepe::vec2 size_coin = {COINS_CHAR_WIDTH*COINS_LENGTH,(COINS_CHAR_WIDTH)*2}; + hud_coin.add_component(size_coin, FONT,Text::Data{ .world_space = false, .text_color = Color::YELLOW, }, TOP_LEFT+FONTOFFSET+COINS_OFFSET + vec2{COINS_LENGTH * COINS_CHAR_WIDTH/2,0}, COINS); + + // Fps + GameObject hud_fps = scn.new_object(HUD_FPS); + crepe::vec2 size_fps = {FPS_CHAR_WIDTH*FPS_LENGTH,(FPS_CHAR_WIDTH)*2}; + hud_fps.add_component(size_fps, FONT,Text::Data{ + .world_space = false, + .text_color = Color::GREEN, + }, TOP_LEFT+FONTOFFSET+FPS_OFFSET + vec2{FPS_LENGTH * FPS_CHAR_WIDTH/2,0}, FPS); } -- cgit v1.2.3