Draw background as sprite

This commit is contained in:
2025-05-24 21:25:48 +02:00
parent dd287dc667
commit 8f64c95dbd
3 changed files with 36 additions and 24 deletions

View File

@@ -46,6 +46,13 @@ int main() {
init_assets(world); init_assets(world);
auto *texture_assets = world.get<TextureAssets>(); auto *texture_assets = world.get<TextureAssets>();
world.entity("Background")
.set<Position>(Position{.x = 0, .y = 0})
.set<Sprite>(
Sprite{.texture = &texture_assets->background,
.texture_atlas_index = 0,
.custom_size = Vec2{.x = WINDOW_WIDTH, .y = WINDOW_HEIGHT}});
world.entity("Sprite1") world.entity("Sprite1")
.set<Position>(Position{.x = 0, .y = 0}) .set<Position>(Position{.x = 0, .y = 0})
.set<Sprite>( .set<Sprite>(
@@ -56,30 +63,29 @@ int main() {
.set<Sprite>( .set<Sprite>(
Sprite{.texture = &texture_assets->fruits, .texture_atlas_index = 5}); Sprite{.texture = &texture_assets->fruits, .texture_atlas_index = 5});
flecs::system sys = world.system<SdlHandles const, Position const, Sprite const>("RenderSprites")
world .term_at(0)
.system<SdlHandles const, Position const, Sprite const>( .singleton()
"RenderSprites") .each([](flecs::entity e, SdlHandles const &sdl_handles,
.term_at(0) Position const &pos, Sprite const &sprite) {
.singleton() TextureAtlasLayout layout = sprite.texture->texture_atlas_layout;
.each([](flecs::entity e, SdlHandles const &sdl_handles, uint8_t row = sprite.texture_atlas_index / layout.columns;
Position const &pos, Sprite const &sprite) { uint8_t column = sprite.texture_atlas_index % layout.columns;
TextureAtlasLayout layout = sprite.texture->texture_atlas_layout; SDL_FRect srcrect{static_cast<float>(column * layout.width),
uint8_t row = sprite.texture_atlas_index / layout.columns; static_cast<float>(row * layout.height),
uint8_t column = sprite.texture_atlas_index % layout.columns; static_cast<float>(layout.width),
SDL_FRect srcrect{static_cast<float>(column * layout.width), static_cast<float>(layout.height)};
static_cast<float>(row * layout.height),
static_cast<float>(layout.width),
static_cast<float>(layout.height)};
SDL_FRect dstrect{static_cast<float>(pos.x), Vec2 size = sprite.custom_size.value_or(
static_cast<float>(pos.y), {.x = layout.width, .y = layout.height});
static_cast<float>(layout.width),
static_cast<float>(layout.height)};
SDL_RenderTexture(sdl_handles.renderer, sprite.texture->sdl_texture, SDL_FRect dstrect{static_cast<float>(pos.x), static_cast<float>(pos.y),
&srcrect, &dstrect); static_cast<float>(size.x),
}); static_cast<float>(size.y)};
SDL_RenderTexture(sdl_handles.renderer, sprite.texture->sdl_texture,
&srcrect, &dstrect);
});
bool exit_gameloop = false; bool exit_gameloop = false;
while (!exit_gameloop) { while (!exit_gameloop) {
@@ -121,8 +127,6 @@ int main() {
// Render // Render
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_RenderTexture(renderer, texture_assets->background.sdl_texture, nullptr,
nullptr);
world.progress(); world.progress();
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "assets.hpp" #include "assets.hpp"
#include "util.hpp"
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <cstdint> #include <cstdint>
@@ -9,4 +10,5 @@
struct Sprite { struct Sprite {
Texture const *texture; Texture const *texture;
uint16_t texture_atlas_index; uint16_t texture_atlas_index;
std::optional<Vec2> custom_size;
}; };

6
util.hpp Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
struct Vec2 {
int x;
int y;
};