Add render module
This commit is contained in:
@@ -38,6 +38,7 @@ add_executable(HansTheGatherer
|
||||
src/audio.cpp
|
||||
src/assets.cpp
|
||||
src/physics.cpp
|
||||
src/render.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(HansTheGatherer SDL3::SDL3 SDL3_ttf::SDL3_ttf flecs::flecs spdlog::spdlog)
|
||||
|
||||
59
src/main.cpp
59
src/main.cpp
@@ -3,14 +3,9 @@
|
||||
#include "input.hpp"
|
||||
#include "level.hpp"
|
||||
#include "physics.hpp"
|
||||
#include "sprite.hpp"
|
||||
#include "render.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_error.h>
|
||||
#include <SDL3/SDL_events.h>
|
||||
#include <SDL3/SDL_iostream.h>
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include <SDL3/SDL_video.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <flecs.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
@@ -52,6 +47,7 @@ int main()
|
||||
|
||||
world.import <AssetModule>();
|
||||
world.import <AudioModule>();
|
||||
world.import <RenderModule>();
|
||||
world.import <PhysicsModule>();
|
||||
world.import <LevelModule>();
|
||||
|
||||
@@ -69,54 +65,6 @@ int main()
|
||||
}
|
||||
});
|
||||
|
||||
world.system<SdlHandles const, Position const, Size const, Sprite const>("RenderSprites")
|
||||
.kind(flecs::OnStore)
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.each(
|
||||
[](SdlHandles const& sdl_handles,
|
||||
Position const& pos,
|
||||
Size const& size,
|
||||
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<float>(column * layout.width),
|
||||
static_cast<float>(row * layout.height),
|
||||
static_cast<float>(layout.width),
|
||||
static_cast<float>(layout.height)};
|
||||
|
||||
SDL_FRect dstrect{static_cast<float>(pos.x),
|
||||
static_cast<float>(pos.y),
|
||||
static_cast<float>(size.w),
|
||||
static_cast<float>(size.h)};
|
||||
|
||||
SDL_RenderTexture(
|
||||
sdl_handles.renderer, sprite.texture->sdl_texture, &srcrect, &dstrect);
|
||||
});
|
||||
|
||||
world.system<SdlHandles const, Game const, FontAssets const>("RenderScore")
|
||||
.kind(flecs::OnStore)
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.term_at(1)
|
||||
.singleton()
|
||||
.term_at(2)
|
||||
.singleton()
|
||||
.each(
|
||||
[](SdlHandles const& sdl_handles, Game const& game, FontAssets const& font_assets)
|
||||
{
|
||||
auto score_string = std::format("Score: {}\nTime: {}", game.score, game.time);
|
||||
auto text = TTF_CreateText(sdl_handles.text_engine,
|
||||
font_assets.default_font.font,
|
||||
score_string.c_str(),
|
||||
score_string.length());
|
||||
TTF_DrawRendererText(text, 0.0, 0.0);
|
||||
|
||||
TTF_DestroyText(text);
|
||||
});
|
||||
|
||||
bool exit_gameloop = false;
|
||||
while (!exit_gameloop)
|
||||
{
|
||||
@@ -154,10 +102,7 @@ int main()
|
||||
}
|
||||
}
|
||||
|
||||
// Render
|
||||
SDL_RenderClear(renderer);
|
||||
world.progress();
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
SDL_DestroyRenderer(renderer);
|
||||
|
||||
73
src/render.cpp
Normal file
73
src/render.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "render.hpp"
|
||||
|
||||
#include "definitions.hpp"
|
||||
#include "physics.hpp"
|
||||
#include "sprite.hpp"
|
||||
|
||||
#include <format>
|
||||
|
||||
RenderModule::RenderModule(flecs::world& world)
|
||||
{
|
||||
world.system<SdlHandles>("RenderClear")
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.kind(flecs::PreStore)
|
||||
.each([](SdlHandles& sdl_handles) { SDL_RenderClear(sdl_handles.renderer); });
|
||||
|
||||
flecs::entity render_present_phase =
|
||||
world.entity().add(flecs::Phase).depends_on(flecs::OnStore);
|
||||
|
||||
world.system<SdlHandles>("RenderPresent")
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.kind(render_present_phase)
|
||||
.each([](SdlHandles& sdl_handles) { SDL_RenderPresent(sdl_handles.renderer); });
|
||||
|
||||
world.system<SdlHandles const, Position const, Size const, Sprite const>("RenderSprites")
|
||||
.kind(flecs::OnStore)
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.each(
|
||||
[](SdlHandles const& sdl_handles,
|
||||
Position const& pos,
|
||||
Size const& size,
|
||||
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<float>(column * layout.width),
|
||||
static_cast<float>(row * layout.height),
|
||||
static_cast<float>(layout.width),
|
||||
static_cast<float>(layout.height)};
|
||||
|
||||
SDL_FRect dstrect{static_cast<float>(pos.x),
|
||||
static_cast<float>(pos.y),
|
||||
static_cast<float>(size.w),
|
||||
static_cast<float>(size.h)};
|
||||
|
||||
SDL_RenderTexture(
|
||||
sdl_handles.renderer, sprite.texture->sdl_texture, &srcrect, &dstrect);
|
||||
});
|
||||
|
||||
world.system<SdlHandles const, Game const, FontAssets const>("RenderScore")
|
||||
.kind(flecs::OnStore)
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.term_at(1)
|
||||
.singleton()
|
||||
.term_at(2)
|
||||
.singleton()
|
||||
.each(
|
||||
[](SdlHandles const& sdl_handles, Game const& game, FontAssets const& font_assets)
|
||||
{
|
||||
auto score_string = std::format("Score: {}\nTime: {}", game.score, game.time);
|
||||
auto text = TTF_CreateText(sdl_handles.text_engine,
|
||||
font_assets.default_font.font,
|
||||
score_string.c_str(),
|
||||
score_string.length());
|
||||
TTF_DrawRendererText(text, 0.0, 0.0);
|
||||
|
||||
TTF_DestroyText(text);
|
||||
});
|
||||
}
|
||||
8
src/render.hpp
Normal file
8
src/render.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <flecs.h>
|
||||
|
||||
struct RenderModule
|
||||
{
|
||||
RenderModule(flecs::world& world);
|
||||
};
|
||||
Reference in New Issue
Block a user