From 8f64c95dbd4b69cbb85313e2e514cf068f376b20 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sat, 24 May 2025 21:25:48 +0200 Subject: [PATCH] Draw background as sprite --- main.cpp | 52 ++++++++++++++++++++++++++++------------------------ sprite.hpp | 2 ++ util.hpp | 6 ++++++ 3 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 util.hpp diff --git a/main.cpp b/main.cpp index dd6edb9..c54724e 100644 --- a/main.cpp +++ b/main.cpp @@ -46,6 +46,13 @@ int main() { init_assets(world); auto *texture_assets = world.get(); + world.entity("Background") + .set(Position{.x = 0, .y = 0}) + .set( + Sprite{.texture = &texture_assets->background, + .texture_atlas_index = 0, + .custom_size = Vec2{.x = WINDOW_WIDTH, .y = WINDOW_HEIGHT}}); + world.entity("Sprite1") .set(Position{.x = 0, .y = 0}) .set( @@ -56,30 +63,29 @@ int main() { .set( Sprite{.texture = &texture_assets->fruits, .texture_atlas_index = 5}); - flecs::system sys = - world - .system( - "RenderSprites") - .term_at(0) - .singleton() - .each([](flecs::entity e, SdlHandles const &sdl_handles, - Position const &pos, 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)}; + world.system("RenderSprites") + .term_at(0) + .singleton() + .each([](flecs::entity e, SdlHandles const &sdl_handles, + Position const &pos, 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(layout.width), - static_cast(layout.height)}; + Vec2 size = sprite.custom_size.value_or( + {.x = layout.width, .y = layout.height}); - SDL_RenderTexture(sdl_handles.renderer, sprite.texture->sdl_texture, - &srcrect, &dstrect); - }); + SDL_FRect dstrect{static_cast(pos.x), static_cast(pos.y), + static_cast(size.x), + static_cast(size.y)}; + + SDL_RenderTexture(sdl_handles.renderer, sprite.texture->sdl_texture, + &srcrect, &dstrect); + }); bool exit_gameloop = false; while (!exit_gameloop) { @@ -121,8 +127,6 @@ int main() { // Render SDL_RenderClear(renderer); - SDL_RenderTexture(renderer, texture_assets->background.sdl_texture, nullptr, - nullptr); world.progress(); SDL_RenderPresent(renderer); } diff --git a/sprite.hpp b/sprite.hpp index 1ef7dfd..46ec70d 100644 --- a/sprite.hpp +++ b/sprite.hpp @@ -1,6 +1,7 @@ #pragma once #include "assets.hpp" +#include "util.hpp" #include #include @@ -9,4 +10,5 @@ struct Sprite { Texture const *texture; uint16_t texture_atlas_index; + std::optional custom_size; }; diff --git a/util.hpp b/util.hpp new file mode 100644 index 0000000..1bff1bd --- /dev/null +++ b/util.hpp @@ -0,0 +1,6 @@ +#pragma once + +struct Vec2 { + int x; + int y; +};