From d019c761bfab273a9f3c1698b40e376e936f708f Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sat, 31 May 2025 13:26:21 +0200 Subject: [PATCH] Add render module --- CMakeLists.txt | 1 + src/main.cpp | 59 ++-------------------------------------- src/render.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/render.hpp | 8 ++++++ 4 files changed, 84 insertions(+), 57 deletions(-) create mode 100644 src/render.cpp create mode 100644 src/render.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d8e273..3a570aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ add_executable(HansTheGatherer src/audio.cpp src/assets.cpp src/physics.cpp + src/render.cpp ) target_link_libraries(HansTheGatherer SDL3::SDL3 SDL3_ttf::SDL3_ttf flecs::flecs spdlog::spdlog) diff --git a/src/main.cpp b/src/main.cpp index 8e2bf5d..274c30c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,14 +3,9 @@ #include "input.hpp" #include "level.hpp" #include "physics.hpp" -#include "sprite.hpp" +#include "render.hpp" #include -#include -#include -#include -#include -#include #include #include #include @@ -52,6 +47,7 @@ int main() world.import (); world.import (); + world.import (); world.import (); world.import (); @@ -69,54 +65,6 @@ int main() } }); - world.system("RenderSprites") - .kind(flecs::OnStore) - .term_at(0) - .singleton() - .each( - [](SdlHandles const& sdl_handles, - Position const& pos, - Size const& size, - Sprite const& sprite) - { - TextureAtlasLayout layout = sprite.texture->texture_atlas_layout; - uint8_t row = sprite.texture_atlas_index / layout.columns; - uint8_t column = sprite.texture_atlas_index % layout.columns; - SDL_FRect srcrect{static_cast(column * layout.width), - static_cast(row * layout.height), - static_cast(layout.width), - static_cast(layout.height)}; - - SDL_FRect dstrect{static_cast(pos.x), - static_cast(pos.y), - static_cast(size.w), - static_cast(size.h)}; - - SDL_RenderTexture( - sdl_handles.renderer, sprite.texture->sdl_texture, &srcrect, &dstrect); - }); - - world.system("RenderScore") - .kind(flecs::OnStore) - .term_at(0) - .singleton() - .term_at(1) - .singleton() - .term_at(2) - .singleton() - .each( - [](SdlHandles const& sdl_handles, Game const& game, FontAssets const& font_assets) - { - auto score_string = std::format("Score: {}\nTime: {}", game.score, game.time); - auto text = TTF_CreateText(sdl_handles.text_engine, - font_assets.default_font.font, - score_string.c_str(), - score_string.length()); - TTF_DrawRendererText(text, 0.0, 0.0); - - TTF_DestroyText(text); - }); - bool exit_gameloop = false; while (!exit_gameloop) { @@ -154,10 +102,7 @@ int main() } } - // Render - SDL_RenderClear(renderer); world.progress(); - SDL_RenderPresent(renderer); } SDL_DestroyRenderer(renderer); diff --git a/src/render.cpp b/src/render.cpp new file mode 100644 index 0000000..caec6ee --- /dev/null +++ b/src/render.cpp @@ -0,0 +1,73 @@ +#include "render.hpp" + +#include "definitions.hpp" +#include "physics.hpp" +#include "sprite.hpp" + +#include + +RenderModule::RenderModule(flecs::world& world) +{ + world.system("RenderClear") + .term_at(0) + .singleton() + .kind(flecs::PreStore) + .each([](SdlHandles& sdl_handles) { SDL_RenderClear(sdl_handles.renderer); }); + + flecs::entity render_present_phase = + world.entity().add(flecs::Phase).depends_on(flecs::OnStore); + + world.system("RenderPresent") + .term_at(0) + .singleton() + .kind(render_present_phase) + .each([](SdlHandles& sdl_handles) { SDL_RenderPresent(sdl_handles.renderer); }); + + world.system("RenderSprites") + .kind(flecs::OnStore) + .term_at(0) + .singleton() + .each( + [](SdlHandles const& sdl_handles, + Position const& pos, + Size const& size, + Sprite const& sprite) + { + TextureAtlasLayout layout = sprite.texture->texture_atlas_layout; + uint8_t row = sprite.texture_atlas_index / layout.columns; + uint8_t column = sprite.texture_atlas_index % layout.columns; + SDL_FRect srcrect{static_cast(column * layout.width), + static_cast(row * layout.height), + static_cast(layout.width), + static_cast(layout.height)}; + + SDL_FRect dstrect{static_cast(pos.x), + static_cast(pos.y), + static_cast(size.w), + static_cast(size.h)}; + + SDL_RenderTexture( + sdl_handles.renderer, sprite.texture->sdl_texture, &srcrect, &dstrect); + }); + + world.system("RenderScore") + .kind(flecs::OnStore) + .term_at(0) + .singleton() + .term_at(1) + .singleton() + .term_at(2) + .singleton() + .each( + [](SdlHandles const& sdl_handles, Game const& game, FontAssets const& font_assets) + { + auto score_string = std::format("Score: {}\nTime: {}", game.score, game.time); + auto text = TTF_CreateText(sdl_handles.text_engine, + font_assets.default_font.font, + score_string.c_str(), + score_string.length()); + TTF_DrawRendererText(text, 0.0, 0.0); + + TTF_DestroyText(text); + }); +} diff --git a/src/render.hpp b/src/render.hpp new file mode 100644 index 0000000..48b944b --- /dev/null +++ b/src/render.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include + +struct RenderModule +{ + RenderModule(flecs::world& world); +};