Compare commits
8 Commits
2e24f504c5
...
f9336dcbd8
| Author | SHA1 | Date | |
|---|---|---|---|
| f9336dcbd8 | |||
| d019c761bf | |||
| e2a2d6efdf | |||
| 81f4d6157e | |||
| ae5733ae62 | |||
| 6eb5046523 | |||
| b18543e04e | |||
| 0c99e625ba |
@@ -6,16 +6,41 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
# Option to switch real platform vs. SDL implementation...
|
||||
|
||||
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(
|
||||
flecs
|
||||
URL https://github.com/SanderMertens/flecs/archive/refs/tags/v4.0.5.tar.gz
|
||||
OVERRIDE_FIND_PACKAGE
|
||||
)
|
||||
FetchContent_MakeAvailable(flecs)
|
||||
|
||||
find_package(SDL3 CONFIG REQUIRED)
|
||||
find_package(SDL3_ttf CONFIG REQUIRED)
|
||||
find_package(flecs CONFIG REQUIRED)
|
||||
find_package(spdlog CONFIG REQUIRED)
|
||||
|
||||
add_executable(HansTheGatherer
|
||||
src/main.cpp
|
||||
src/audio.cpp
|
||||
src/assets.cpp
|
||||
src/physics.cpp
|
||||
src/render.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(HansTheGatherer SDL3::SDL3 flecs::flecs spdlog::spdlog)
|
||||
target_link_libraries(HansTheGatherer SDL3::SDL3 SDL3_ttf::SDL3_ttf flecs::flecs spdlog::spdlog)
|
||||
|
||||
set_property(TARGET HansTheGatherer PROPERTY CXX_STANDARD 20)
|
||||
|
||||
2
NOTICE
2
NOTICE
@@ -6,3 +6,5 @@ Assets used by this project:
|
||||
URL: https://trashboat93.itch.io/jungle-background-parallax
|
||||
- "Jamaican Sunrise - Reggae" by DavidKBD
|
||||
URL: https://davidkbd.itch.io/tropical-dreams-spring-and-summer-music-pack
|
||||
- "SPIDER ENEMY PIXEL ART" by camacebra
|
||||
URL: https://camacebra.itch.io/spider-pixel-art-pack-16x16
|
||||
BIN
assets/fonts/OpenTTD-Sans.ttf
Normal file
BIN
assets/fonts/OpenTTD-Sans.ttf
Normal file
Binary file not shown.
BIN
assets/images/spiders.bmp
Normal file
BIN
assets/images/spiders.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.1 KiB |
@@ -13,6 +13,10 @@ static constexpr uint8_t FRUITS_DATA[] = {
|
||||
#embed "../assets/images/fruits.bmp"
|
||||
};
|
||||
|
||||
static constexpr uint8_t SPIDERS_DATA[] = {
|
||||
#embed "../assets/images/spiders.bmp"
|
||||
};
|
||||
|
||||
static constexpr uint8_t BASKET_DATA[] = {
|
||||
#embed "../assets/images/basket.bmp"
|
||||
};
|
||||
@@ -21,6 +25,10 @@ static constexpr uint8_t BACKGROUND_MUSIC_DATA[] = {
|
||||
#embed "../assets/sounds/JamaicanSunrise.wav"
|
||||
};
|
||||
|
||||
static constexpr uint8_t DEFAULT_FONT_DATA[] = {
|
||||
#embed "../assets/fonts/OpenTTD-Sans.ttf"
|
||||
};
|
||||
|
||||
SDL_Texture* load_texture(uint8_t const* data, size_t size, SDL_Renderer* renderer)
|
||||
{
|
||||
auto* iostream = SDL_IOFromConstMem(data, size);
|
||||
@@ -53,6 +61,18 @@ AudioAsset load_audio(uint8_t const* data, size_t size)
|
||||
return audio_asset;
|
||||
}
|
||||
|
||||
FontAsset load_font(uint8_t const* data, size_t size)
|
||||
{
|
||||
FontAsset font_asset;
|
||||
|
||||
auto* iostream = SDL_IOFromConstMem(data, size);
|
||||
auto* ttf = TTF_OpenFontIO(iostream, false, 20);
|
||||
|
||||
font_asset.font = ttf;
|
||||
|
||||
return font_asset;
|
||||
}
|
||||
|
||||
AssetModule::AssetModule(flecs::world& world)
|
||||
{
|
||||
auto* renderer = world.get<SdlHandles>()->renderer;
|
||||
@@ -63,14 +83,21 @@ AssetModule::AssetModule(flecs::world& world)
|
||||
auto* fruits = load_texture(FRUITS_DATA, sizeof(FRUITS_DATA), renderer);
|
||||
TextureAtlasLayout fruits_layout = {.width = 16, .height = 16, .rows = 6, .columns = 38};
|
||||
|
||||
auto* spiders = load_texture(SPIDERS_DATA, sizeof(SPIDERS_DATA), renderer);
|
||||
TextureAtlasLayout spiders_layout = {.width = 16, .height = 16, .rows = 2, .columns = 4};
|
||||
|
||||
auto* basket = load_texture(BASKET_DATA, sizeof(BASKET_DATA), renderer);
|
||||
TextureAtlasLayout basket_layout = {.width = 16, .height = 16, .rows = 1, .columns = 1};
|
||||
|
||||
world.set<TextureAssets>(TextureAssets{
|
||||
.background = Texture{.sdl_texture = background, .texture_atlas_layout = background_layout},
|
||||
.fruits = Texture{.sdl_texture = fruits, .texture_atlas_layout = fruits_layout},
|
||||
.spiders = Texture{.sdl_texture = spiders, .texture_atlas_layout = spiders_layout},
|
||||
.basket = Texture{.sdl_texture = basket, .texture_atlas_layout = basket_layout}});
|
||||
|
||||
auto background_music = load_audio(BACKGROUND_MUSIC_DATA, sizeof(BACKGROUND_MUSIC_DATA));
|
||||
world.set<AudioAssets>(AudioAssets{.background_music = background_music});
|
||||
|
||||
auto font = load_font(DEFAULT_FONT_DATA, sizeof(DEFAULT_FONT_DATA));
|
||||
world.set<FontAssets>(FontAssets{.default_font = font});
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "audio.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <flecs.h>
|
||||
|
||||
struct TextureAtlasLayout
|
||||
@@ -23,6 +24,7 @@ struct TextureAssets
|
||||
{
|
||||
Texture background;
|
||||
Texture fruits;
|
||||
Texture spiders;
|
||||
Texture basket;
|
||||
};
|
||||
|
||||
@@ -31,6 +33,16 @@ struct AudioAssets
|
||||
AudioAsset background_music;
|
||||
};
|
||||
|
||||
struct FontAsset
|
||||
{
|
||||
TTF_Font* font;
|
||||
};
|
||||
|
||||
struct FontAssets
|
||||
{
|
||||
FontAsset default_font;
|
||||
};
|
||||
|
||||
struct AssetModule
|
||||
{
|
||||
AssetModule(flecs::world& world);
|
||||
|
||||
33
src/audio.cpp
Normal file
33
src/audio.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "audio.hpp"
|
||||
#include "assets.hpp"
|
||||
|
||||
AudioModule::AudioModule(flecs::world& world)
|
||||
{
|
||||
auto* audio_assets = world.get<AudioAssets>();
|
||||
auto* music_stream = SDL_OpenAudioDeviceStream(
|
||||
SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_assets->background_music.spec, NULL, NULL);
|
||||
auto* sound_stream = SDL_OpenAudioDeviceStream(
|
||||
SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_assets->background_music.spec, NULL, NULL);
|
||||
|
||||
SDL_ResumeAudioStreamDevice(music_stream);
|
||||
|
||||
world.set<AudioStreams>(
|
||||
AudioStreams{.music_stream = music_stream, .sound_stream = sound_stream});
|
||||
|
||||
world.system<AudioStreams, AudioAssets>("FeedAudioStreams")
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.term_at(1)
|
||||
.singleton()
|
||||
.each(
|
||||
[](AudioStreams& audio_streams, AudioAssets& audio_assets)
|
||||
{
|
||||
if (SDL_GetAudioStreamQueued(audio_streams.music_stream) <
|
||||
static_cast<int>(audio_assets.background_music.buffer_length))
|
||||
{
|
||||
SDL_PutAudioStreamData(audio_streams.music_stream,
|
||||
audio_assets.background_music.buffer,
|
||||
audio_assets.background_music.buffer_length);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <cstdint>
|
||||
#include <flecs.h>
|
||||
|
||||
struct AudioAsset
|
||||
{
|
||||
@@ -9,3 +10,14 @@ struct AudioAsset
|
||||
uint8_t* buffer;
|
||||
uint32_t buffer_length;
|
||||
};
|
||||
|
||||
struct AudioStreams
|
||||
{
|
||||
SDL_AudioStream* music_stream;
|
||||
SDL_AudioStream* sound_stream;
|
||||
};
|
||||
|
||||
struct AudioModule
|
||||
{
|
||||
AudioModule(flecs::world& world);
|
||||
};
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <random>
|
||||
|
||||
static constexpr int WINDOW_WIDTH = 400;
|
||||
static constexpr int WINDOW_HEIGHT = 240;
|
||||
@@ -9,10 +11,13 @@ struct SdlHandles
|
||||
{
|
||||
SDL_Window* window;
|
||||
SDL_Renderer* renderer;
|
||||
TTF_TextEngine* text_engine;
|
||||
};
|
||||
|
||||
struct Game
|
||||
{
|
||||
uint32_t ticks;
|
||||
uint32_t score;
|
||||
int32_t time;
|
||||
int32_t score;
|
||||
std::default_random_engine random_engine;
|
||||
};
|
||||
|
||||
106
src/level.hpp
106
src/level.hpp
@@ -12,6 +12,14 @@ struct Fruit
|
||||
{
|
||||
};
|
||||
|
||||
struct Spider
|
||||
{
|
||||
};
|
||||
|
||||
struct Item
|
||||
{
|
||||
};
|
||||
|
||||
struct Basket
|
||||
{
|
||||
};
|
||||
@@ -22,6 +30,10 @@ struct LevelModule
|
||||
{
|
||||
auto const* texture_assets = world.get<TextureAssets>();
|
||||
|
||||
world.component<Item>();
|
||||
world.component<Fruit>().is_a<Item>();
|
||||
world.component<Spider>().is_a<Item>();
|
||||
|
||||
world.entity("Background")
|
||||
.set<Position>(Position{.x = 0, .y = 0})
|
||||
.set<Sprite>(Sprite{.texture = &texture_assets->background, .texture_atlas_index = 0})
|
||||
@@ -38,39 +50,62 @@ struct LevelModule
|
||||
world.entity()
|
||||
.child_of(basket)
|
||||
.add<WorldPosition>()
|
||||
.set<Position>(Position{.x = 0, .y = 0})
|
||||
.set<Size>(Size{.w = 64, .h = 32})
|
||||
.set<Position>(Position{.x = 0, .y = 16})
|
||||
.set<Size>(Size{.w = 64, .h = 16})
|
||||
.add<CollisionBox>();
|
||||
|
||||
world.system<Game const, TextureAssets const>("SpawnFruits")
|
||||
world.system<Game, TextureAssets const>("SpawnFruits")
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.term_at(1)
|
||||
.singleton()
|
||||
.each(
|
||||
[](flecs::iter& it,
|
||||
size_t index,
|
||||
Game const& game,
|
||||
TextureAssets const& texture_assets)
|
||||
[](flecs::iter& it, size_t index, Game& game, TextureAssets const& texture_assets)
|
||||
{
|
||||
if ((game.ticks % 100) == 0)
|
||||
{
|
||||
auto fruit =
|
||||
it.world()
|
||||
.entity()
|
||||
.add<Fruit>()
|
||||
.add<WorldPosition>()
|
||||
.set<Position>(Position{
|
||||
.x = static_cast<int>(game.ticks % WINDOW_WIDTH), .y = -16})
|
||||
.set<Velocity>(Velocity{.x = 0, .y = 1})
|
||||
.set<Sprite>(Sprite{.texture = &texture_assets.fruits,
|
||||
.texture_atlas_index =
|
||||
static_cast<uint16_t>(game.ticks % 228)})
|
||||
.set<Size>(Size{.w = 32, .h = 32});
|
||||
std::bernoulli_distribution spider_dist(0.25);
|
||||
bool spider = spider_dist(game.random_engine);
|
||||
|
||||
std::binomial_distribution<> velocity_dist(3, 0.5);
|
||||
int vel = velocity_dist(game.random_engine) + 1;
|
||||
|
||||
if ((game.ticks % 50) == 0)
|
||||
{
|
||||
flecs::entity e;
|
||||
std::uniform_int_distribution<> xpos_dist(32, WINDOW_WIDTH - 32);
|
||||
int xpos = xpos_dist(game.random_engine);
|
||||
if (!spider)
|
||||
{
|
||||
std::uniform_int_distribution<> index_dist(0, 228 - 1);
|
||||
uint16_t index = index_dist(game.random_engine);
|
||||
|
||||
e = it.world()
|
||||
.entity()
|
||||
.add<Fruit>()
|
||||
.add<WorldPosition>()
|
||||
.set<Position>(Position{.x = xpos, .y = -16})
|
||||
.set<Velocity>(Velocity{.x = 0, .y = vel})
|
||||
.set<Sprite>(Sprite{.texture = &texture_assets.fruits,
|
||||
.texture_atlas_index = index})
|
||||
.set<Size>(Size{.w = 32, .h = 32});
|
||||
}
|
||||
else
|
||||
{
|
||||
std::uniform_int_distribution<> index_dist(0, 8 - 1);
|
||||
uint16_t index = index_dist(game.random_engine);
|
||||
|
||||
e = it.world()
|
||||
.entity()
|
||||
.add<Spider>()
|
||||
.add<WorldPosition>()
|
||||
.set<Position>(Position{.x = xpos, .y = -16})
|
||||
.set<Velocity>(Velocity{.x = 0, .y = vel})
|
||||
.set<Sprite>(Sprite{.texture = &texture_assets.spiders,
|
||||
.texture_atlas_index = index})
|
||||
.set<Size>(Size{.w = 32, .h = 32});
|
||||
}
|
||||
it.world()
|
||||
.entity("CollisionBox")
|
||||
.child_of(fruit)
|
||||
.child_of(e)
|
||||
.add<WorldPosition>()
|
||||
.set<Position>(Position{.x = 0, .y = 0})
|
||||
.set<Size>(Size{.w = 32, .h = 32})
|
||||
@@ -78,19 +113,36 @@ struct LevelModule
|
||||
}
|
||||
});
|
||||
|
||||
// world.system<WorldPosition const, Fruit>("DespawnFruits")
|
||||
// world.system<WorldPosition const, Item>("DespawnItems")
|
||||
// .kind(flecs::OnValidate)
|
||||
// .each(
|
||||
// [](flecs::entity e, WorldPosition const& pos, Fruit)
|
||||
// [](flecs::entity e, WorldPosition const& pos, Item)
|
||||
// {
|
||||
// if (pos.y >= WINDOW_HEIGHT)
|
||||
// e.destruct();
|
||||
// });
|
||||
|
||||
world.system<Game, Fruit, Collided>("CollectItem")
|
||||
world.system<Game, Position, Fruit, Collided>("CollectFruit")
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.each([](flecs::entity e, Game& game, Fruit, Collided) { game.score += 10; });
|
||||
.each(
|
||||
[](flecs::entity e, Game& game, Position& pos, Fruit, Collided)
|
||||
{
|
||||
game.score += 10;
|
||||
pos.x += 1000;
|
||||
// e.destruct();
|
||||
});
|
||||
|
||||
world.system<Game, Position, Spider, Collided>("CollectSpider")
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.each(
|
||||
[](flecs::entity e, Game& game, Position& pos, Spider, Collided)
|
||||
{
|
||||
game.score -= 50;
|
||||
pos.x += 1000;
|
||||
// e.destruct();
|
||||
});
|
||||
|
||||
world.system<ButtonInput const, Position, Size const, Sprite const, Basket>("MoveBasket")
|
||||
.term_at(0)
|
||||
@@ -115,4 +167,4 @@ struct LevelModule
|
||||
pos.x = pos.x > WINDOW_WIDTH - size.w ? WINDOW_WIDTH - size.w : pos.x;
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
74
src/main.cpp
74
src/main.cpp
@@ -3,14 +3,10 @@
|
||||
#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>
|
||||
|
||||
@@ -26,6 +22,8 @@ int main()
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
TTF_Init();
|
||||
|
||||
auto* window = SDL_CreateWindow("HansTheGatherer", WINDOW_WIDTH, WINDOW_HEIGHT, 0);
|
||||
if (window == nullptr)
|
||||
{
|
||||
@@ -38,59 +36,40 @@ int main()
|
||||
spdlog::critical("Failed to create SDL renderer!\nCause: {}", SDL_GetError());
|
||||
}
|
||||
|
||||
auto* text_engine = TTF_CreateRendererTextEngine(renderer);
|
||||
|
||||
flecs::world world;
|
||||
|
||||
world.set<Game>(Game{.ticks = 0});
|
||||
world.set<Game>(Game{.ticks = 0, .time = 60, .score = 0, .random_engine = {}});
|
||||
world.set<ButtonInput>(ButtonInput{});
|
||||
world.set<SdlHandles>(SdlHandles{.window = window, .renderer = renderer});
|
||||
world.set<SdlHandles>(
|
||||
SdlHandles{.window = window, .renderer = renderer, .text_engine = text_engine});
|
||||
|
||||
world.import <AssetModule>();
|
||||
world.import <AudioModule>();
|
||||
world.import <RenderModule>();
|
||||
world.import <PhysicsModule>();
|
||||
world.import <LevelModule>();
|
||||
|
||||
world.system<Game>("IncrementTicks")
|
||||
world.system<Game, TranslateSystem>("IncrementTicks")
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.each([](Game& game_ticks) { game_ticks.ticks += 1; });
|
||||
|
||||
world.system<SdlHandles const, Position const, Size const, Sprite const>("RenderSprites")
|
||||
.kind(flecs::PreUpdate)
|
||||
.term_at(0)
|
||||
.term_at(1)
|
||||
.singleton()
|
||||
.each(
|
||||
[](SdlHandles const& sdl_handles,
|
||||
Position const& pos,
|
||||
Size const& size,
|
||||
Sprite const& sprite)
|
||||
[](Game& game, TranslateSystem& translate_system)
|
||||
{
|
||||
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)};
|
||||
game.ticks += 1;
|
||||
|
||||
SDL_FRect dstrect{static_cast<float>(pos.x),
|
||||
static_cast<float>(pos.y),
|
||||
static_cast<float>(size.w),
|
||||
static_cast<float>(size.h)};
|
||||
if (game.ticks % 60 == 0)
|
||||
{
|
||||
game.time = std::max(0, game.time - 1);
|
||||
}
|
||||
|
||||
SDL_RenderTexture(
|
||||
sdl_handles.renderer, sprite.texture->sdl_texture, &srcrect, &dstrect);
|
||||
if (game.time == 0)
|
||||
translate_system.translate_system.disable();
|
||||
});
|
||||
|
||||
auto* audio_assets = world.get<AudioAssets>();
|
||||
auto* stream = SDL_OpenAudioDeviceStream(
|
||||
SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_assets->background_music.spec, NULL, NULL);
|
||||
if (!stream)
|
||||
{
|
||||
SDL_Log("Couldn't create audio stream: %s", SDL_GetError());
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_ResumeAudioStreamDevice(stream);
|
||||
|
||||
bool exit_gameloop = false;
|
||||
while (!exit_gameloop)
|
||||
{
|
||||
@@ -128,18 +107,7 @@ int main()
|
||||
}
|
||||
}
|
||||
|
||||
// Game Logic
|
||||
if (SDL_GetAudioStreamQueued(stream) < (int)audio_assets->background_music.buffer_length)
|
||||
{
|
||||
SDL_PutAudioStreamData(stream,
|
||||
audio_assets->background_music.buffer,
|
||||
audio_assets->background_music.buffer_length);
|
||||
}
|
||||
|
||||
// Render
|
||||
SDL_RenderClear(renderer);
|
||||
world.progress();
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
SDL_DestroyRenderer(renderer);
|
||||
|
||||
@@ -5,13 +5,15 @@
|
||||
|
||||
PhysicsModule::PhysicsModule(flecs::world& world)
|
||||
{
|
||||
world.system<Position, Velocity const>("TranslatePhysicsObject")
|
||||
.each(
|
||||
[](Position& pos, Velocity const& vel)
|
||||
{
|
||||
pos.x += vel.x;
|
||||
pos.y += vel.y;
|
||||
});
|
||||
auto translate_system = world.system<Position, Velocity const>("TranslatePhysicsObject")
|
||||
.each(
|
||||
[](Position& pos, Velocity const& vel)
|
||||
{
|
||||
pos.x += vel.x;
|
||||
pos.y += vel.y;
|
||||
});
|
||||
|
||||
world.set<TranslateSystem>(TranslateSystem{translate_system});
|
||||
|
||||
// Introduce phase that runs after OnUpdate but before OnValidate
|
||||
flecs::entity propagate_phase = world.entity().add(flecs::Phase).depends_on(flecs::OnUpdate);
|
||||
@@ -48,6 +50,10 @@ PhysicsModule::PhysicsModule(flecs::world& world)
|
||||
|
||||
auto basket_query = world.query<Basket>();
|
||||
|
||||
world.system<Collided>("RemoveCollisionMarker")
|
||||
.kind(flecs::PreUpdate)
|
||||
.each([](flecs::entity e, Collided) { e.remove<Collided>(); });
|
||||
|
||||
world.system<WorldPosition const, Size const, CollisionBox>("CollisionCheck")
|
||||
.kind(flecs::OnValidate)
|
||||
.each(
|
||||
|
||||
@@ -34,6 +34,11 @@ struct Collided
|
||||
{
|
||||
};
|
||||
|
||||
struct TranslateSystem
|
||||
{
|
||||
flecs::system translate_system;
|
||||
};
|
||||
|
||||
struct PhysicsModule
|
||||
{
|
||||
PhysicsModule(flecs::world& world);
|
||||
|
||||
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);
|
||||
};
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"dependencies": [
|
||||
"spdlog",
|
||||
"sdl3",
|
||||
"spdlog",
|
||||
"flecs"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user