diff --git a/assets.cpp b/assets.cpp index 7989858..af5b72e 100644 --- a/assets.cpp +++ b/assets.cpp @@ -13,16 +13,32 @@ static constexpr uint8_t FRUITS_DATA[] = { #embed "fruits.jpg" }; -void init_assets(flecs::world &world) { - auto *renderer = world.get()->renderer; - - auto *background_iostream = - SDL_IOFromConstMem(BACKGROUND_DATA, sizeof(BACKGROUND_DATA)); - SDL_Texture *background = - IMG_LoadTexture_IO(renderer, background_iostream, false); - if (background == nullptr) { +SDL_Texture *load_texture(uint8_t const *data, size_t size, + SDL_Renderer *renderer) { + auto *iostream = SDL_IOFromConstMem(data, size); + SDL_Texture *texture = IMG_LoadTexture_IO(renderer, iostream, false); + if (texture == nullptr) { spdlog::error("Failed to load SDL texture!\nCause: {}", SDL_GetError()); } - world.set(SpriteAssets{.background = background}); + return texture; +} + +void init_assets(flecs::world &world) { + auto *renderer = world.get()->renderer; + + auto *background = + load_texture(BACKGROUND_DATA, sizeof(BACKGROUND_DATA), renderer); + TextureAtlasLayout background_layout = { + .width = 866, .height = 510, .rows = 1, .columns = 1}; + + auto *fruits = load_texture(FRUITS_DATA, sizeof(FRUITS_DATA), renderer); + TextureAtlasLayout fruits_layout = { + .width = 16, .height = 16, .rows = 6, .columns = 38}; + + world.set(TextureAssets{ + .background = Texture{.sdl_texture = background, + .texture_atlas_layout = background_layout}, + .fruits = Texture{.sdl_texture = fruits, + .texture_atlas_layout = fruits_layout}}); } diff --git a/assets.hpp b/assets.hpp index 89086be..418b891 100644 --- a/assets.hpp +++ b/assets.hpp @@ -3,9 +3,21 @@ #include #include -struct SpriteAssets { - SDL_Texture *background; - SDL_Texture *fruits; +struct TextureAtlasLayout { + uint16_t width; + uint16_t height; + uint8_t rows; + uint8_t columns; +}; + +struct Texture { + SDL_Texture *sdl_texture; + TextureAtlasLayout texture_atlas_layout; +}; + +struct TextureAssets { + Texture background; + Texture fruits; }; void init_assets(flecs::world &world); diff --git a/main.cpp b/main.cpp index 192cf6d..dd6edb9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include "assets.hpp" -#include "sdl_types.hpp" #include "input.hpp" +#include "sdl_types.hpp" +#include "sprite.hpp" #include #include @@ -43,6 +44,42 @@ int main() { world.set(ButtonInput{}); world.set(SdlHandles{.window = window, .renderer = renderer}); init_assets(world); + auto *texture_assets = world.get(); + + world.entity("Sprite1") + .set(Position{.x = 0, .y = 0}) + .set( + Sprite{.texture = &texture_assets->fruits, .texture_atlas_index = 0}); + + world.entity("Sprite2") + .set(Position{.x = 32, .y = 32}) + .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)}; + + SDL_FRect dstrect{static_cast(pos.x), + static_cast(pos.y), + static_cast(layout.width), + static_cast(layout.height)}; + + SDL_RenderTexture(sdl_handles.renderer, sprite.texture->sdl_texture, + &srcrect, &dstrect); + }); bool exit_gameloop = false; while (!exit_gameloop) { @@ -84,8 +121,9 @@ int main() { // Render SDL_RenderClear(renderer); - SDL_RenderTexture(renderer, world.get()->background, nullptr, + SDL_RenderTexture(renderer, texture_assets->background.sdl_texture, nullptr, nullptr); + world.progress(); SDL_RenderPresent(renderer); } diff --git a/sprite.hpp b/sprite.hpp new file mode 100644 index 0000000..1ef7dfd --- /dev/null +++ b/sprite.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "assets.hpp" + +#include +#include +#include + +struct Sprite { + Texture const *texture; + uint16_t texture_atlas_index; +};