Implement Texture Atlases
This commit is contained in:
34
assets.cpp
34
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<SdlHandles>()->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>(SpriteAssets{.background = background});
|
||||
return texture;
|
||||
}
|
||||
|
||||
void init_assets(flecs::world &world) {
|
||||
auto *renderer = world.get<SdlHandles>()->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>(TextureAssets{
|
||||
.background = Texture{.sdl_texture = background,
|
||||
.texture_atlas_layout = background_layout},
|
||||
.fruits = Texture{.sdl_texture = fruits,
|
||||
.texture_atlas_layout = fruits_layout}});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user