From 47bfead825aeda147a65dd01cc8b3b95b630c4c2 Mon Sep 17 00:00:00 2001 From: brxxh <11dac2t@huhn-online.de> Date: Wed, 2 Jul 2025 11:46:43 +0200 Subject: [PATCH] Cleanup --- examples/tappyplane/tappyplane.cpp | 33 +++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/examples/tappyplane/tappyplane.cpp b/examples/tappyplane/tappyplane.cpp index 95bab15..0bc2399 100644 --- a/examples/tappyplane/tappyplane.cpp +++ b/examples/tappyplane/tappyplane.cpp @@ -22,6 +22,11 @@ #include #include +// @ToDo: Introduce a struct that automates a sprite's scale. Right now we are +// doing this all by hand by doing IMAGE_SIZE / WINDOW_SIZE. + +// @ToDo: Introduce Delta-Time for each update function? + namespace TappyPlane { using SpriteName = std::tuple; @@ -66,10 +71,12 @@ struct Background { sprites = {paradiso::Sprite{ .bitmap = sprite_map["background"], .pivot = {paradiso::Vector2::make(0.0f, 0.0f)}, + // The background sprite is as big as the window. .scale = {paradiso::Vector2::make(1.0f, 1.0f)}}, paradiso::Sprite{ .bitmap = sprite_map["background"], .pivot = {paradiso::Vector2::make(2.0f, 0.0f)}, + // The background sprite is as big as the window. .scale = {paradiso::Vector2::make(1.0f, 1.0f)}}}; } @@ -308,6 +315,7 @@ struct Plane { static constexpr paradiso::Vector2 INIT_PIVOT{ paradiso::Vector2::make(0.0f, INIT_POSITION_Y)}; + // Every 10th frame. static const unsigned int ANIM_INTERVAL = 10; // The image's size is 88x73 px. @@ -636,7 +644,7 @@ struct App { }; for (const auto& [name, filename] : assets) { - std::print("{} : {} -> ", name, filename); + std::print("{:<20} : {:<30} -> ", name, filename); auto bitmap = paradiso::BitmapIO::get().load(filename); app.sprites[name] = bitmap; @@ -708,7 +716,8 @@ struct App { return false; } - if (game_state == GameState::Start) { + switch (game_state) { + case GameState::Start: if (window.keyboard_input().size() && window.keyboard_input().top().key == ACTION_KEY && window.keyboard_input().top().action == 1) { @@ -724,7 +733,8 @@ struct App { plane.update_animation(); // Only the animation. ground.update(); start_ui.update(); - } else if (game_state == GameState::Playing) { + break; + case GameState::Playing: background.update(); ground.update(); rocks1.update(); @@ -740,7 +750,8 @@ struct App { plane.aabb.is_colliding_with(ground.aabb)) { game_state = GameState::GameOver; } - } else if (game_state == GameState::GameOver) { + break; + case GameState::GameOver: if (window.keyboard_input().size() && window.keyboard_input().top().key == ACTION_KEY) { @@ -752,6 +763,7 @@ struct App { plane.reset(); game_state = GameState::Start; } + break; } context.set_viewport( @@ -773,7 +785,18 @@ struct App { game_over_ui.draw(shader); } - // Einen kurzen Moment warten, um auf 60 FPS zu kommen. + // + // Einen kurzen Moment warten, um FRAME_RATE (siehe oben) zu + // erreichen. + // + // Wichtig: Das beeinflusst auch die allgemeine + // Geschwindigkeit des Spiels, da das Aktualisieren des Game-States + // nicht unabhängig von der Frame-Rate läuft. + // + // Man könnte Delta-Time einführen oder das Aktualisieren des + // Game-States auf eine bestimmte Geschwindigkeit (Tick-Speed) + // bringen und zwischen den Frames interpolieren. + // auto t2 = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast(t2 - t1);