Remove SDL dependency

This commit is contained in:
2025-06-09 01:01:31 +02:00
parent a0cbf16a64
commit 27c747ac31
10 changed files with 176 additions and 227 deletions

View File

@@ -7,19 +7,6 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Option to switch real platform vs. SDL implementation... # Option to switch real platform vs. SDL implementation...
include(FetchContent) include(FetchContent)
FetchContent_Declare(
SDL3
URL https://github.com/libsdl-org/SDL/releases/download/release-3.2.14/SDL3-3.2.14.tar.gz
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(SDL3)
FetchContent_Declare(
SDL3_ttf
URL https://github.com/libsdl-org/SDL_ttf/releases/download/release-3.2.2/SDL3_ttf-3.2.2.tar.gz
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(SDL3_ttf)
FetchContent_Declare( FetchContent_Declare(
entt entt
@@ -29,9 +16,6 @@ FetchContent_Declare(
FetchContent_MakeAvailable(entt) FetchContent_MakeAvailable(entt)
find_package(entt CONFIG REQUIRED) find_package(entt CONFIG REQUIRED)
find_package(SDL3 CONFIG REQUIRED)
find_package(SDL3_ttf CONFIG REQUIRED)
add_executable(HansTheGatherer add_executable(HansTheGatherer
src/main.cpp src/main.cpp
src/audio.cpp src/audio.cpp
@@ -41,6 +25,6 @@ add_executable(HansTheGatherer
src/render.cpp src/render.cpp
) )
target_link_libraries(HansTheGatherer SDL3::SDL3 SDL3_ttf::SDL3_ttf EnTT) target_link_libraries(HansTheGatherer EnTT)
set_property(TARGET HansTheGatherer PROPERTY CXX_STANDARD 20) set_property(TARGET HansTheGatherer PROPERTY CXX_STANDARD 20)

View File

@@ -1,6 +1,5 @@
#include "assets.hpp" #include "assets.hpp"
#include "audio.hpp" #include "audio.hpp"
#include "definitions.hpp"
#include <cstdint> #include <cstdint>
@@ -36,79 +35,90 @@ static constexpr uint8_t DEFAULT_FONT_DATA[] = {
#embed "../assets/fonts/OpenTTD-Sans.ttf" #embed "../assets/fonts/OpenTTD-Sans.ttf"
}; };
SDL_Texture* load_texture(uint8_t const* data, size_t size, SDL_Renderer* renderer) // SDL_Texture* load_texture(uint8_t const* data, size_t size, SDL_Renderer* renderer)
{ // {
auto* iostream = SDL_IOFromConstMem(data, size); // auto* iostream = SDL_IOFromConstMem(data, size);
SDL_Surface* surface = SDL_LoadBMP_IO(iostream, false); // SDL_Surface* surface = SDL_LoadBMP_IO(iostream, false);
if (surface == nullptr) // if (surface == nullptr)
{ // {
// spdlog::error("Failed to load SDL surface!\nCause: {}", SDL_GetError()); // // spdlog::error("Failed to load SDL surface!\nCause: {}", SDL_GetError());
} // }
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); // SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture == nullptr) // if (texture == nullptr)
{ // {
// spdlog::error("Failed to create texture from surface!\nCause: {}", SDL_GetError()); // // spdlog::error("Failed to create texture from surface!\nCause: {}", SDL_GetError());
} // }
return texture; // return texture;
} // }
AudioAsset load_audio(uint8_t const* data, size_t size) // AudioAsset load_audio(uint8_t const* data, size_t size)
{ // {
AudioAsset audio_asset; // AudioAsset audio_asset;
auto* iostream = SDL_IOFromConstMem(data, size); // auto* iostream = SDL_IOFromConstMem(data, size);
bool res = SDL_LoadWAV_IO( // bool res = SDL_LoadWAV_IO(
iostream, false, &audio_asset.spec, &audio_asset.buffer, &audio_asset.buffer_length); // iostream, false, &audio_asset.spec, &audio_asset.buffer, &audio_asset.buffer_length);
if (!res) // if (!res)
{ // {
// spdlog::error("Failed to load audio file!\nCause: {}", SDL_GetError()); // // spdlog::error("Failed to load audio file!\nCause: {}", SDL_GetError());
} // }
return audio_asset; // return audio_asset;
} // }
FontAsset load_font(uint8_t const* data, size_t size) // FontAsset load_font(uint8_t const* data, size_t size)
{ // {
FontAsset font_asset; // FontAsset font_asset;
auto* iostream = SDL_IOFromConstMem(data, size); // auto* iostream = SDL_IOFromConstMem(data, size);
auto* ttf = TTF_OpenFontIO(iostream, false, 20); // auto* ttf = TTF_OpenFontIO(iostream, false, 20);
font_asset.font = ttf; // font_asset.font = ttf;
return font_asset; // return font_asset;
} // }
AssetModule::AssetModule(entt::registry& registry) AssetModule::AssetModule(entt::registry& registry)
{ {
auto renderer = registry.ctx().get<SdlHandles>().renderer; // auto renderer = registry.ctx().get<SdlHandles>().renderer;
auto* background = load_texture(BACKGROUND_DATA, sizeof(BACKGROUND_DATA), renderer); // auto* background = load_texture(BACKGROUND_DATA, sizeof(BACKGROUND_DATA), renderer);
TextureAtlasLayout background_layout = {.width = 866, .height = 510, .rows = 1, .columns = 1}; TextureAtlasLayout background_layout = {.width = 866, .height = 510, .rows = 1, .columns = 1};
auto* fruits = load_texture(FRUITS_DATA, sizeof(FRUITS_DATA), renderer); // auto* fruits = load_texture(FRUITS_DATA, sizeof(FRUITS_DATA), renderer);
TextureAtlasLayout fruits_layout = {.width = 16, .height = 16, .rows = 6, .columns = 38}; TextureAtlasLayout fruits_layout = {.width = 16, .height = 16, .rows = 6, .columns = 38};
auto* spiders = load_texture(SPIDERS_DATA, sizeof(SPIDERS_DATA), renderer); // auto* spiders = load_texture(SPIDERS_DATA, sizeof(SPIDERS_DATA), renderer);
TextureAtlasLayout spiders_layout = {.width = 16, .height = 16, .rows = 2, .columns = 4}; TextureAtlasLayout spiders_layout = {.width = 16, .height = 16, .rows = 2, .columns = 4};
auto* basket = load_texture(BASKET_DATA, sizeof(BASKET_DATA), renderer); // auto* basket = load_texture(BASKET_DATA, sizeof(BASKET_DATA), renderer);
TextureAtlasLayout basket_layout = {.width = 16, .height = 16, .rows = 1, .columns = 1}; TextureAtlasLayout basket_layout = {.width = 16, .height = 16, .rows = 1, .columns = 1};
registry.ctx().emplace<TextureAssets>(TextureAssets{ // registry.ctx().emplace<TextureAssets>(TextureAssets{
.background = Texture{.sdl_texture = background, .texture_atlas_layout = background_layout}, // .background = Texture{.sdl_texture = background, .texture_atlas_layout =
.fruits = Texture{.sdl_texture = fruits, .texture_atlas_layout = fruits_layout}, // background_layout}, .fruits = Texture{.sdl_texture = fruits, .texture_atlas_layout =
.spiders = Texture{.sdl_texture = spiders, .texture_atlas_layout = spiders_layout}, // fruits_layout}, .spiders = Texture{.sdl_texture = spiders, .texture_atlas_layout =
.basket = Texture{.sdl_texture = basket, .texture_atlas_layout = basket_layout}}); // spiders_layout}, .basket = Texture{.sdl_texture = basket, .texture_atlas_layout =
// basket_layout}});
registry.ctx().emplace<TextureAssets>(
TextureAssets{.background = Texture{.texture_atlas_layout = background_layout},
.fruits = Texture{.texture_atlas_layout = fruits_layout},
.spiders = Texture{.texture_atlas_layout = spiders_layout},
.basket = Texture{.texture_atlas_layout = basket_layout}});
auto background_music = load_audio(BACKGROUND_MUSIC_DATA, sizeof(BACKGROUND_MUSIC_DATA)); // auto background_music = load_audio(BACKGROUND_MUSIC_DATA, sizeof(BACKGROUND_MUSIC_DATA));
auto pickup_sound = load_audio(PICKUP_SOUND_DATA, sizeof(PICKUP_SOUND_DATA)); // auto pickup_sound = load_audio(PICKUP_SOUND_DATA, sizeof(PICKUP_SOUND_DATA));
auto hit_sound = load_audio(HIT_SOUND_DATA, sizeof(HIT_SOUND_DATA)); // auto hit_sound = load_audio(HIT_SOUND_DATA, sizeof(HIT_SOUND_DATA));
registry.ctx().emplace<AudioAssets>(AudioAssets{.background_music = background_music, registry.ctx().emplace<AudioAssets>(AudioAssets{
.pickup_sound = pickup_sound, .background_music = {.buffer = BACKGROUND_MUSIC_DATA,
.hit_sound = hit_sound}); .buffer_length = sizeof(BACKGROUND_MUSIC_DATA)},
.pickup_sound = {.buffer = PICKUP_SOUND_DATA, .buffer_length = sizeof(PICKUP_SOUND_DATA)},
.hit_sound = {.buffer = HIT_SOUND_DATA, .buffer_length = sizeof(HIT_SOUND_DATA)}});
// registry.ctx().emplace<AudioAssets>(AudioAssets{.background_music = background_music,
// .pickup_sound = pickup_sound,
// .hit_sound = hit_sound});
auto font = load_font(DEFAULT_FONT_DATA, sizeof(DEFAULT_FONT_DATA)); // auto font = load_font(DEFAULT_FONT_DATA, sizeof(DEFAULT_FONT_DATA));
registry.ctx().emplace<FontAssets>(FontAssets{.default_font = font}); // registry.ctx().emplace<FontAssets>(FontAssets{.default_font = font});
} }

View File

@@ -2,8 +2,6 @@
#include "audio.hpp" #include "audio.hpp"
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <entt/entt.hpp> #include <entt/entt.hpp>
struct TextureAtlasLayout struct TextureAtlasLayout
@@ -16,7 +14,7 @@ struct TextureAtlasLayout
struct Texture struct Texture
{ {
SDL_Texture* sdl_texture; // SDL_Texture* sdl_texture;
TextureAtlasLayout texture_atlas_layout; TextureAtlasLayout texture_atlas_layout;
}; };
@@ -35,15 +33,15 @@ struct AudioAssets
AudioAsset hit_sound; AudioAsset hit_sound;
}; };
struct FontAsset // struct FontAsset
{ // {
TTF_Font* font; // TTF_Font* font;
}; // };
struct FontAssets // struct FontAssets
{ // {
FontAsset default_font; // FontAsset default_font;
}; // };
struct AssetModule struct AssetModule
{ {

View File

@@ -3,29 +3,29 @@
AudioModule::AudioModule(entt::registry& registry) AudioModule::AudioModule(entt::registry& registry)
{ {
auto const& audio_assets = registry.ctx().get<AudioAssets>(); // auto const& audio_assets = registry.ctx().get<AudioAssets>();
auto* music_stream = SDL_OpenAudioDeviceStream( // auto* music_stream = SDL_OpenAudioDeviceStream(
SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_assets.background_music.spec, NULL, NULL); // SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_assets.background_music.spec, NULL, NULL);
auto* sound_stream = SDL_OpenAudioDeviceStream( // auto* sound_stream = SDL_OpenAudioDeviceStream(
SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_assets.pickup_sound.spec, NULL, NULL); // SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_assets.pickup_sound.spec, NULL, NULL);
SDL_ResumeAudioStreamDevice(music_stream); // SDL_ResumeAudioStreamDevice(music_stream);
SDL_ResumeAudioStreamDevice(sound_stream); // SDL_ResumeAudioStreamDevice(sound_stream);
registry.ctx().emplace<AudioStreams>( // registry.ctx().emplace<AudioStreams>(
AudioStreams{.music_stream = music_stream, .sound_stream = sound_stream}); // AudioStreams{.music_stream = music_stream, .sound_stream = sound_stream});
} }
void AudioModule::FeedAudioStreams(entt::registry& registry) // void AudioModule::FeedAudioStreams(entt::registry& registry)
{ // {
auto audio_streams = registry.ctx().get<AudioStreams>(); // auto audio_streams = registry.ctx().get<AudioStreams>();
auto audio_assets = registry.ctx().get<AudioAssets>(); // auto audio_assets = registry.ctx().get<AudioAssets>();
if (SDL_GetAudioStreamQueued(audio_streams.music_stream) < // if (SDL_GetAudioStreamQueued(audio_streams.music_stream) <
static_cast<int>(audio_assets.background_music.buffer_length)) // static_cast<int>(audio_assets.background_music.buffer_length))
{ // {
SDL_PutAudioStreamData(audio_streams.music_stream, // SDL_PutAudioStreamData(audio_streams.music_stream,
audio_assets.background_music.buffer, // audio_assets.background_music.buffer,
audio_assets.background_music.buffer_length); // audio_assets.background_music.buffer_length);
} // }
} // }

View File

@@ -1,24 +1,23 @@
#pragma once #pragma once
#include <SDL3/SDL.h>
#include <cstdint> #include <cstdint>
#include <entt/entt.hpp> #include <entt/entt.hpp>
struct AudioAsset struct AudioAsset
{ {
SDL_AudioSpec spec; // SDL_AudioSpec spec;
uint8_t* buffer; uint8_t const* buffer;
uint32_t buffer_length; uint32_t buffer_length;
}; };
struct AudioStreams // struct AudioStreams
{ // {
SDL_AudioStream* music_stream; // SDL_AudioStream* music_stream;
SDL_AudioStream* sound_stream; // SDL_AudioStream* sound_stream;
}; // };
struct AudioModule struct AudioModule
{ {
AudioModule(entt::registry& registry); AudioModule(entt::registry& registry);
static void FeedAudioStreams(entt::registry& registry); // static void FeedAudioStreams(entt::registry& registry);
}; };

View File

@@ -1,18 +1,16 @@
#pragma once #pragma once
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <random> #include <random>
static constexpr int WINDOW_WIDTH = 400; static constexpr int WINDOW_WIDTH = 400;
static constexpr int WINDOW_HEIGHT = 240; static constexpr int WINDOW_HEIGHT = 240;
struct SdlHandles // struct SdlHandles
{ // {
SDL_Window* window; // SDL_Window* window;
SDL_Renderer* renderer; // SDL_Renderer* renderer;
TTF_TextEngine* text_engine; // TTF_TextEngine* text_engine;
}; // };
struct Game struct Game
{ {

View File

@@ -1,11 +1,10 @@
#pragma once #pragma once
#include <SDL3/SDL.h>
#include <set> #include <set>
struct ButtonInput struct ButtonInput
{ {
std::set<SDL_Keycode> pressed; std::set<int> pressed;
std::set<SDL_Keycode> just_pressed; std::set<int> just_pressed;
std::set<SDL_Keycode> just_released; std::set<int> just_released;
}; };

View File

@@ -77,7 +77,7 @@ void LevelModule::SpawnFruits(entt::registry& registry)
void LevelModule::CollectFruit(entt::registry& registry) void LevelModule::CollectFruit(entt::registry& registry)
{ {
auto& game = registry.ctx().get<Game>(); auto& game = registry.ctx().get<Game>();
auto const& audio_streams = registry.ctx().get<AudioStreams>(); // auto const& audio_streams = registry.ctx().get<AudioStreams>();
auto const& audio_assets = registry.ctx().get<AudioAssets>(); auto const& audio_assets = registry.ctx().get<AudioAssets>();
auto view = registry.view<Position, Fruit, Collided>(); auto view = registry.view<Position, Fruit, Collided>();
@@ -87,16 +87,16 @@ void LevelModule::CollectFruit(entt::registry& registry)
pos.x += 1000; pos.x += 1000;
// registry.destroy(entity); // registry.destroy(entity);
SDL_PutAudioStreamData(audio_streams.sound_stream, // SDL_PutAudioStreamData(audio_streams.sound_stream,
audio_assets.pickup_sound.buffer, // audio_assets.pickup_sound.buffer,
audio_assets.pickup_sound.buffer_length); // audio_assets.pickup_sound.buffer_length);
} }
} }
void LevelModule::CollectSpider(entt::registry& registry) void LevelModule::CollectSpider(entt::registry& registry)
{ {
auto& game = registry.ctx().get<Game>(); auto& game = registry.ctx().get<Game>();
auto const& audio_streams = registry.ctx().get<AudioStreams>(); // auto const& audio_streams = registry.ctx().get<AudioStreams>();
auto const& audio_assets = registry.ctx().get<AudioAssets>(); auto const& audio_assets = registry.ctx().get<AudioAssets>();
auto view = registry.view<Position, Spider, Collided>(); auto view = registry.view<Position, Spider, Collided>();
@@ -106,9 +106,9 @@ void LevelModule::CollectSpider(entt::registry& registry)
pos.x += 1000; pos.x += 1000;
// registry.destroy(entity); // registry.destroy(entity);
SDL_PutAudioStreamData(audio_streams.sound_stream, // SDL_PutAudioStreamData(audio_streams.sound_stream,
audio_assets.hit_sound.buffer, // audio_assets.hit_sound.buffer,
audio_assets.hit_sound.buffer_length); // audio_assets.hit_sound.buffer_length);
}; };
} }

View File

@@ -5,60 +5,25 @@
#include "physics.hpp" #include "physics.hpp"
#include "render.hpp" #include "render.hpp"
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <entt/entt.hpp> #include <entt/entt.hpp>
void increment_ticks(entt::registry& registry) void increment_ticks(entt::registry& registry)
{ {
auto& game = registry.ctx().get<Game>(); auto& game = registry.ctx().get<Game>();
// auto& translate_system = registry.ctx().get<TranslateSystem>();
game.ticks += 1; game.ticks += 1;
if (game.ticks % 60 == 0) if (game.ticks % 60 == 0)
{ {
game.time = std::max(0, game.time - 1); game.time = std::max(0, game.time - 1);
} }
// if (game.time == 0)
// translate_system.translate_system.disable();
} }
int main() int main()
{ {
// spdlog::info("Initialize SDL...");
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO))
{
// spdlog::critical("Failed to initialize SDL!\nCause: {}", SDL_GetError());
std::terminate();
}
TTF_Init();
auto* window = SDL_CreateWindow("HansTheGatherer", WINDOW_WIDTH, WINDOW_HEIGHT, 0);
if (window == nullptr)
{
// spdlog::critical("Failed to create SDL window!\nCause: {}", SDL_GetError());
}
auto* renderer = SDL_CreateRenderer(window, nullptr);
if (renderer == nullptr)
{
// spdlog::critical("Failed to create SDL renderer!\nCause: {}", SDL_GetError());
}
auto* text_engine = TTF_CreateRendererTextEngine(renderer);
entt::registry registry; entt::registry registry;
registry.ctx().emplace<Game>(Game{.ticks = 0, .time = 60, .score = 0, .random_engine = {}}); registry.ctx().emplace<Game>(Game{.ticks = 0, .time = 60, .score = 0, .random_engine = {}});
registry.ctx().emplace<ButtonInput>(ButtonInput{}); registry.ctx().emplace<ButtonInput>(ButtonInput{});
auto sdl_handles = registry.ctx().emplace<SdlHandles>(
SdlHandles{.window = window, .renderer = renderer, .text_engine = text_engine});
AssetModule asset_module(registry); AssetModule asset_module(registry);
AudioModule audio_module(registry); AudioModule audio_module(registry);
@@ -76,34 +41,34 @@ int main()
input->just_released.clear(); input->just_released.clear();
// Input // Input
SDL_Event event; // SDL_Event event;
while (SDL_PollEvent(&event)) // while (SDL_PollEvent(&event))
{ // {
switch (event.type) // switch (event.type)
{ // {
case SDL_EVENT_QUIT: // case SDL_EVENT_QUIT:
exit_gameloop = true; // exit_gameloop = true;
break; // break;
case SDL_EVENT_KEY_DOWN: // case SDL_EVENT_KEY_DOWN:
if (event.key.key == SDLK_ESCAPE) // if (event.key.key == SDLK_ESCAPE)
{ // {
exit_gameloop = true; // exit_gameloop = true;
} // }
if (input->pressed.insert(event.key.key).second) // if (input->pressed.insert(event.key.key).second)
{ // {
input->just_pressed.insert(event.key.key); // input->just_pressed.insert(event.key.key);
} // }
break; // break;
case SDL_EVENT_KEY_UP: // case SDL_EVENT_KEY_UP:
if (input->pressed.erase(event.key.key) != 0) // if (input->pressed.erase(event.key.key) != 0)
{ // {
input->just_released.insert(event.key.key); // input->just_released.insert(event.key.key);
} // }
break; // break;
} // }
} // }
AudioModule::FeedAudioStreams(registry); // AudioModule::FeedAudioStreams(registry);
if (registry.ctx().get<Game>().time != 0) if (registry.ctx().get<Game>().time != 0)
{ {
@@ -120,15 +85,11 @@ int main()
PhysicsModule::RemoveCollisionMarker(registry); PhysicsModule::RemoveCollisionMarker(registry);
PhysicsModule::CollisionCheck(registry); PhysicsModule::CollisionCheck(registry);
SDL_RenderClear(sdl_handles.renderer); // SDL_RenderClear(sdl_handles.renderer);
RenderModule::RenderSprites(registry); RenderModule::RenderSprites(registry);
RenderModule::RenderScore(registry); RenderModule::RenderScore(registry);
SDL_RenderPresent(sdl_handles.renderer); // SDL_RenderPresent(sdl_handles.renderer);
} }
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0; return 0;
} }

View File

@@ -13,7 +13,7 @@ RenderModule::RenderModule(entt::registry& registry)
void RenderModule::RenderSprites(entt::registry& registry) void RenderModule::RenderSprites(entt::registry& registry)
{ {
auto const& sdl_handles = registry.ctx().get<SdlHandles>(); // auto const& sdl_handles = registry.ctx().get<SdlHandles>();
auto const& game = registry.ctx().get<Game>(); auto const& game = registry.ctx().get<Game>();
auto sprites_view = auto sprites_view =
registry.view<Position const, Size const, Sprite const>(entt::exclude<Background>); registry.view<Position const, Size const, Sprite const>(entt::exclude<Background>);
@@ -24,17 +24,17 @@ void RenderModule::RenderSprites(entt::registry& registry)
TextureAtlasLayout layout = sprite.texture->texture_atlas_layout; TextureAtlasLayout layout = sprite.texture->texture_atlas_layout;
uint8_t row = sprite.texture_atlas_index / layout.columns; uint8_t row = sprite.texture_atlas_index / layout.columns;
uint8_t column = sprite.texture_atlas_index % layout.columns; uint8_t column = sprite.texture_atlas_index % layout.columns;
SDL_FRect srcrect{static_cast<float>(column * layout.width), // SDL_FRect srcrect{static_cast<float>(column * layout.width),
static_cast<float>(row * layout.height), // static_cast<float>(row * layout.height),
static_cast<float>(layout.width), // static_cast<float>(layout.width),
static_cast<float>(layout.height)}; // static_cast<float>(layout.height)};
SDL_FRect dstrect{static_cast<float>(pos.x), // SDL_FRect dstrect{static_cast<float>(pos.x),
static_cast<float>(pos.y), // static_cast<float>(pos.y),
static_cast<float>(size.w), // static_cast<float>(size.w),
static_cast<float>(size.h)}; // static_cast<float>(size.h)};
SDL_RenderTexture(sdl_handles.renderer, sprite.texture->sdl_texture, &srcrect, &dstrect); // SDL_RenderTexture(sdl_handles.renderer, sprite.texture->sdl_texture, &srcrect, &dstrect);
} }
for (auto [entity, pos, size, sprite] : sprites_view.each()) for (auto [entity, pos, size, sprite] : sprites_view.each())
@@ -42,31 +42,31 @@ void RenderModule::RenderSprites(entt::registry& registry)
TextureAtlasLayout layout = sprite.texture->texture_atlas_layout; TextureAtlasLayout layout = sprite.texture->texture_atlas_layout;
uint8_t row = sprite.texture_atlas_index / layout.columns; uint8_t row = sprite.texture_atlas_index / layout.columns;
uint8_t column = sprite.texture_atlas_index % layout.columns; uint8_t column = sprite.texture_atlas_index % layout.columns;
SDL_FRect srcrect{static_cast<float>(column * layout.width), // SDL_FRect srcrect{static_cast<float>(column * layout.width),
static_cast<float>(row * layout.height), // static_cast<float>(row * layout.height),
static_cast<float>(layout.width), // static_cast<float>(layout.width),
static_cast<float>(layout.height)}; // static_cast<float>(layout.height)};
SDL_FRect dstrect{static_cast<float>(pos.x), // SDL_FRect dstrect{static_cast<float>(pos.x),
static_cast<float>(pos.y), // static_cast<float>(pos.y),
static_cast<float>(size.w), // static_cast<float>(size.w),
static_cast<float>(size.h)}; // static_cast<float>(size.h)};
SDL_RenderTexture(sdl_handles.renderer, sprite.texture->sdl_texture, &srcrect, &dstrect); // SDL_RenderTexture(sdl_handles.renderer, sprite.texture->sdl_texture, &srcrect, &dstrect);
} }
} }
void RenderModule::RenderScore(entt::registry& registry) void RenderModule::RenderScore(entt::registry& registry)
{ {
auto const& sdl_handles = registry.ctx().get<SdlHandles>(); // auto const& sdl_handles = registry.ctx().get<SdlHandles>();
auto const& game = registry.ctx().get<Game>(); auto const& game = registry.ctx().get<Game>();
auto const& font_assets = registry.ctx().get<FontAssets>(); // auto const& font_assets = registry.ctx().get<FontAssets>();
auto score_string = std::format("Score: {}\nTime: {}", game.score, game.time); auto score_string = std::format("Score: {}\nTime: {}", game.score, game.time);
auto text = TTF_CreateText(sdl_handles.text_engine, // auto text = TTF_CreateText(sdl_handles.text_engine,
font_assets.default_font.font, // font_assets.default_font.font,
score_string.c_str(), // score_string.c_str(),
score_string.length()); // score_string.length());
TTF_DrawRendererText(text, 0.0, 0.0); // TTF_DrawRendererText(text, 0.0, 0.0);
TTF_DestroyText(text); // TTF_DestroyText(text);
} }