Compare commits

...

8 Commits

Author SHA1 Message Date
2ca005f934 Add score text 2025-05-31 18:20:57 +02:00
c7fd00a1b8 Fix propagate system 2025-05-31 17:16:42 +02:00
a456a87728 Propagate world position system 2025-05-31 16:58:58 +02:00
dbddea69f5 Add asset module 2025-05-31 16:58:58 +02:00
af6fd5fc45 Add physics and level module 2025-05-31 16:58:58 +02:00
b828bf6bd2 Format project 2025-05-31 12:36:20 +02:00
3a1ebd4dfa Restructure project 2025-05-31 12:36:20 +02:00
d8c0876aa4 Force compile commands 2025-05-31 12:36:20 +02:00
20 changed files with 639 additions and 436 deletions

View File

@@ -2,13 +2,19 @@ cmake_minimum_required(VERSION 3.24)
project(HansTheGatherer)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Option to switch real platform vs. SDL implementation...
find_package(SDL3 CONFIG REQUIRED)
find_package(flecs CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
add_executable(HansTheGatherer main.cpp assets.cpp)
add_executable(HansTheGatherer
src/main.cpp
src/assets.cpp
src/physics.cpp
)
target_link_libraries(HansTheGatherer SDL3::SDL3 flecs::flecs spdlog::spdlog)

View File

@@ -1,80 +0,0 @@
#include "assets.hpp"
#include "audio.hpp"
#include "sdl_types.hpp"
#include <cstdint>
#include <spdlog/spdlog.h>
static constexpr uint8_t BACKGROUND_DATA[] = {
#embed "assets/images/jungle.bmp"
};
static constexpr uint8_t FRUITS_DATA[] = {
#embed "assets/images/fruits.bmp"
};
static constexpr uint8_t BASKET_DATA[] = {
#embed "assets/images/basket.bmp"
};
static constexpr uint8_t BACKGROUND_MUSIC_DATA[] = {
#embed "assets/sounds/JamaicanSunrise.wav"
};
SDL_Texture *load_texture(uint8_t const *data, size_t size,
SDL_Renderer *renderer) {
auto *iostream = SDL_IOFromConstMem(data, size);
SDL_Surface *surface = SDL_LoadBMP_IO(iostream, false);
if (surface == nullptr) {
spdlog::error("Failed to load SDL surface!\nCause: {}", SDL_GetError());
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture == nullptr) {
spdlog::error("Failed to create texture from surface!\nCause: {}",
SDL_GetError());
}
return texture;
}
AudioAsset load_audio(uint8_t const *data, size_t size) {
AudioAsset audio_asset;
auto *iostream = SDL_IOFromConstMem(data, size);
bool res = SDL_LoadWAV_IO(iostream, false, &audio_asset.spec,
&audio_asset.buffer, &audio_asset.buffer_length);
if (!res) {
spdlog::error("Failed to load audio file!\nCause: {}", SDL_GetError());
}
return audio_asset;
}
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};
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},
.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});
}

View File

@@ -1,30 +0,0 @@
#pragma once
#include "audio.hpp"
#include <SDL3/SDL.h>
#include <flecs.h>
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;
Texture basket;
};
struct AudioAssets {
AudioAsset background_music;
};
void init_assets(flecs::world &world);

Binary file not shown.

View File

@@ -1,10 +0,0 @@
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
struct AudioAsset {
SDL_AudioSpec spec;
uint8_t *buffer;
uint32_t buffer_length;
};

View File

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

286
main.cpp
View File

@@ -1,286 +0,0 @@
#include "assets.hpp"
#include "input.hpp"
#include "sdl_types.hpp"
#include "sprite.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 <flecs.h>
#include <spdlog/spdlog.h>
static constexpr int WINDOW_WIDTH = 400;
static constexpr int WINDOW_HEIGHT = 240;
struct WorldPosition {
int x;
int y;
};
struct Position {
int x;
int y;
};
struct Velocity {
int x;
int y;
};
struct Size {
int w;
int h;
};
struct Game {
uint32_t ticks;
};
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();
}
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());
}
flecs::world world;
world.set<Game>(Game{.ticks = 0});
world.set<ButtonInput>(ButtonInput{});
world.set<SdlHandles>(SdlHandles{.window = window, .renderer = renderer});
init_assets(world);
auto *texture_assets = world.get<TextureAssets>();
world.entity("Background")
.set<Position>(Position{.x = 0, .y = 0})
.set<Sprite>(Sprite{.texture = &texture_assets->background,
.texture_atlas_index = 0})
.set<Size>(Size{.w = WINDOW_WIDTH, .h = WINDOW_HEIGHT});
struct Fruit {};
struct Basket {};
struct CollisionBox {};
world.system<Game>("IncrementTicks")
.term_at(0)
.singleton()
.each([](Game &game_ticks) { game_ticks.ticks += 1; });
auto basket = world.entity("Basket")
.add<WorldPosition>()
.set<Position>(Position{.x = WINDOW_WIDTH / 2 - 32,
.y = WINDOW_HEIGHT - 32})
.set<Sprite>(Sprite{.texture = &texture_assets->basket,
.texture_atlas_index = 0})
.set<Size>(Size{.w = 64, .h = 32})
.add<Basket>();
world.entity("CollisionBox")
.child_of(basket)
.add<WorldPosition>()
.set<Position>(Position{.x = 0, .y = 0})
.set<Size>(Size{.w = 64, .h = 32})
.add<CollisionBox>();
world
.system<SdlHandles const, WorldPosition const, Size const, CollisionBox>(
"DrawBoxes")
.term_at(0)
.singleton()
.each([](SdlHandles const &sdl_handles, WorldPosition const &pos,
Size const &size, CollisionBox) {
SDL_FRect rect{static_cast<float>(pos.x), static_cast<float>(pos.y),
static_cast<float>(size.w), static_cast<float>(size.h)};
SDL_SetRenderDrawColor(sdl_handles.renderer, 0, 0, 255, 255);
SDL_RenderRect(sdl_handles.renderer, &rect);
});
world.system<Game const, 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) {
if ((game.ticks % 100) == 0) {
auto fruit =
it.world()
.entity()
.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});
it.world()
.entity("CollisionBox")
.child_of(fruit)
.add<WorldPosition>()
.set<Position>(Position{.x = 0, .y = 0})
.set<Size>(Size{.w = 32, .h = 32})
.add<CollisionBox>();
}
});
world.system<WorldPosition, Position const>("PropagatePosition")
.kind(flecs::PostUpdate)
.each([](flecs::entity e, WorldPosition &world_pos,
Position const &local_pos) {
if (e.parent() == flecs::entity::null()) {
world_pos.x = local_pos.x;
world_pos.y = local_pos.y;
} else {
auto parent_world_pos = e.parent().get<WorldPosition>();
world_pos.x = parent_world_pos->x + local_pos.x;
world_pos.y = parent_world_pos->y + local_pos.y;
}
});
world.system<WorldPosition const, Size const, CollisionBox>("CollisionCheck")
.each([](flecs::iter &it, size_t index, WorldPosition const &world_pos,
Size const &size, CollisionBox) {
auto basket_box = it.world().lookup("Basket::CollisionBox");
if (it.entity(index) == basket_box)
return;
auto basket_box_pos = basket_box.get<WorldPosition>();
auto basket_box_size = basket_box.get<Size>();
if (basket_box_pos->x + basket_box_size->w >= world_pos.x &&
basket_box_pos->x <= world_pos.x + size.w &&
basket_box_pos->y + basket_box_size->h >= world_pos.y &&
basket_box_pos->y <= world_pos.y + size.h) {
spdlog::info("collision");
}
});
world
.system<SdlHandles const, Position const, Size const, Sprite const>(
"RenderSprites")
.kind(flecs::PreUpdate)
.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<ButtonInput const, Position, Size const, Sprite const, Basket>(
"MoveBasket")
.term_at(0)
.singleton()
.each([](ButtonInput const &input, Position &pos, Size const &size,
Sprite const &sprite, Basket) {
if (input.pressed.contains(SDLK_LEFT)) {
pos.x -= 5;
}
if (input.pressed.contains(SDLK_RIGHT)) {
pos.x += 5;
}
pos.x = pos.x < 0 ? 0 : pos.x;
pos.x = pos.x > WINDOW_WIDTH - size.w ? WINDOW_WIDTH - size.w : pos.x;
});
world.system<Position, Velocity const>("MoveSprites")
.each([](Position &pos, Velocity const &vel) {
pos.x += vel.x;
pos.y += vel.y;
});
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) {
auto *input = world.get_mut<ButtonInput>();
// Clear just pressed/released
input->just_pressed.clear();
input->just_released.clear();
// Input
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT:
exit_gameloop = true;
break;
case SDL_EVENT_KEY_DOWN:
if (event.key.key == SDLK_ESCAPE) {
exit_gameloop = true;
}
if (input->pressed.insert(event.key.key).second) {
input->just_pressed.insert(event.key.key);
}
break;
case SDL_EVENT_KEY_UP:
if (input->pressed.erase(event.key.key) != 0) {
input->just_released.insert(event.key.key);
}
break;
}
}
// 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);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

View File

@@ -1,9 +0,0 @@
#pragma once
#include <SDL3/SDL.h>
struct SdlHandles {
SDL_Window *window;
SDL_Renderer *renderer;
};

95
src/assets.cpp Normal file
View File

@@ -0,0 +1,95 @@
#include "assets.hpp"
#include "audio.hpp"
#include "definitions.hpp"
#include <cstdint>
#include <spdlog/spdlog.h>
static constexpr uint8_t BACKGROUND_DATA[] = {
#embed "../assets/images/jungle.bmp"
};
static constexpr uint8_t FRUITS_DATA[] = {
#embed "../assets/images/fruits.bmp"
};
static constexpr uint8_t BASKET_DATA[] = {
#embed "../assets/images/basket.bmp"
};
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);
SDL_Surface* surface = SDL_LoadBMP_IO(iostream, false);
if (surface == nullptr)
{
spdlog::error("Failed to load SDL surface!\nCause: {}", SDL_GetError());
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture == nullptr)
{
spdlog::error("Failed to create texture from surface!\nCause: {}", SDL_GetError());
}
return texture;
}
AudioAsset load_audio(uint8_t const* data, size_t size)
{
AudioAsset audio_asset;
auto* iostream = SDL_IOFromConstMem(data, size);
bool res = SDL_LoadWAV_IO(
iostream, false, &audio_asset.spec, &audio_asset.buffer, &audio_asset.buffer_length);
if (!res)
{
spdlog::error("Failed to load audio file!\nCause: {}", SDL_GetError());
}
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;
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};
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},
.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});
}

48
src/assets.hpp Normal file
View File

@@ -0,0 +1,48 @@
#pragma once
#include "audio.hpp"
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <flecs.h>
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;
Texture basket;
};
struct AudioAssets
{
AudioAsset background_music;
};
struct FontAsset
{
TTF_Font* font;
};
struct FontAssets
{
FontAsset default_font;
};
struct AssetModule
{
AssetModule(flecs::world& world);
};

11
src/audio.hpp Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
struct AudioAsset
{
SDL_AudioSpec spec;
uint8_t* buffer;
uint32_t buffer_length;
};

21
src/definitions.hpp Normal file
View File

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

11
src/input.hpp Normal file
View File

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

123
src/level.hpp Normal file
View File

@@ -0,0 +1,123 @@
#pragma once
#include "definitions.hpp"
#include "input.hpp"
#include "physics.hpp"
#include "sprite.hpp"
#include <flecs.h>
#include <spdlog/spdlog.h>
struct Fruit
{
};
struct Basket
{
};
struct LevelModule
{
LevelModule(flecs::world& world)
{
auto const* texture_assets = world.get<TextureAssets>();
world.entity("Background")
.set<Position>(Position{.x = 0, .y = 0})
.set<Sprite>(Sprite{.texture = &texture_assets->background, .texture_atlas_index = 0})
.set<Size>(Size{.w = WINDOW_WIDTH, .h = WINDOW_HEIGHT});
auto basket =
world.entity("Basket")
.add<WorldPosition>()
.set<Position>(Position{.x = WINDOW_WIDTH / 2 - 32, .y = WINDOW_HEIGHT - 32})
.set<Sprite>(Sprite{.texture = &texture_assets->basket, .texture_atlas_index = 0})
.set<Size>(Size{.w = 64, .h = 32})
.add<Basket>();
world.entity()
.child_of(basket)
.add<WorldPosition>()
.set<Position>(Position{.x = 0, .y = 16})
.set<Size>(Size{.w = 64, .h = 16})
.add<CollisionBox>();
world.system<Game const, 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)
{
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});
it.world()
.entity("CollisionBox")
.child_of(fruit)
.add<WorldPosition>()
.set<Position>(Position{.x = 0, .y = 0})
.set<Size>(Size{.w = 32, .h = 32})
.add<CollisionBox>();
}
});
// world.system<WorldPosition const, Fruit>("DespawnFruits")
// .kind(flecs::OnValidate)
// .each(
// [](flecs::entity e, WorldPosition const& pos, Fruit)
// {
// if (pos.y >= WINDOW_HEIGHT)
// e.destruct();
// });
world.system<Game, Position, Fruit, Collided>("CollectItem")
.term_at(0)
.singleton()
.each(
[](flecs::entity e, Game& game, Position& pos, Fruit, Collided)
{
game.score += 10;
pos.x = 1000;
});
world.system<ButtonInput const, Position, Size const, Sprite const, Basket>("MoveBasket")
.term_at(0)
.singleton()
.each(
[](ButtonInput const& input,
Position& pos,
Size const& size,
Sprite const& sprite,
Basket)
{
if (input.pressed.contains(SDLK_LEFT))
{
pos.x -= 5;
}
if (input.pressed.contains(SDLK_RIGHT))
{
pos.x += 5;
}
pos.x = pos.x < 0 ? 0 : pos.x;
pos.x = pos.x > WINDOW_WIDTH - size.w ? WINDOW_WIDTH - size.w : pos.x;
});
}
};

188
src/main.cpp Normal file
View File

@@ -0,0 +1,188 @@
#include "assets.hpp"
#include "definitions.hpp"
#include "input.hpp"
#include "level.hpp"
#include "physics.hpp"
#include "sprite.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>
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);
flecs::world world;
world.import <flecs::stats>();
world.set<flecs::Rest>({});
world.set<Game>(Game{.ticks = 0, .time = 60, .score = 0});
world.set<ButtonInput>(ButtonInput{});
world.set<SdlHandles>(
SdlHandles{.window = window, .renderer = renderer, .text_engine = text_engine});
world.import <AssetModule>();
world.import <PhysicsModule>();
world.import <LevelModule>();
world.system<Game>("IncrementTicks")
.term_at(0)
.singleton()
.each(
[](Game& game)
{
game.ticks += 1;
if (game.ticks % 60 == 0)
{
game.time--;
}
});
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);
});
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)
{
auto* input = world.get_mut<ButtonInput>();
// Clear just pressed/released
input->just_pressed.clear();
input->just_released.clear();
// Input
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_EVENT_QUIT:
exit_gameloop = true;
break;
case SDL_EVENT_KEY_DOWN:
if (event.key.key == SDLK_ESCAPE)
{
exit_gameloop = true;
}
if (input->pressed.insert(event.key.key).second)
{
input->just_pressed.insert(event.key.key);
}
break;
case SDL_EVENT_KEY_UP:
if (input->pressed.erase(event.key.key) != 0)
{
input->just_released.insert(event.key.key);
}
break;
}
}
// 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);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

84
src/physics.cpp Normal file
View File

@@ -0,0 +1,84 @@
#include "physics.hpp"
#include "level.hpp"
#include <spdlog/spdlog.h>
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;
});
// Introduce phase that runs after OnUpdate but before OnValidate
flecs::entity propagate_phase = world.entity().add(flecs::Phase).depends_on(flecs::OnUpdate);
world.system<WorldPosition, Position const>("PropagatePosition")
.kind(propagate_phase)
.each(
[](flecs::entity e, WorldPosition& world_pos, Position const& local_pos)
{
if (e.parent() != flecs::entity::null() && e.parent().has<WorldPosition>())
return;
world_pos.x = local_pos.x;
world_pos.y = local_pos.y;
std::function<void(flecs::entity, WorldPosition const&)> propagate_to_children;
propagate_to_children = [&](flecs::entity parent, WorldPosition const& parent_pos)
{
parent.children(
[=](flecs::entity child)
{
auto local_pos = child.get<Position>();
auto world_pos = child.get_mut<WorldPosition>();
world_pos->x = parent_pos.x + local_pos->x;
world_pos->y = parent_pos.y + local_pos->y;
propagate_to_children(child, *world_pos);
});
};
propagate_to_children(e, world_pos);
});
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(
[basket_query](
flecs::entity e, WorldPosition const& world_pos, Size const& size, CollisionBox)
{
if (e.parent().has<Basket>())
return;
auto fruit = e.parent();
auto basket = basket_query.first();
basket.children(
[fruit, world_pos, size](flecs::entity basket_child)
{
if (!basket_child.has<CollisionBox>())
return;
auto basket_child_pos = basket_child.get<WorldPosition>();
auto basket_child_size = basket_child.get<Size>();
if (basket_child_pos->x + basket_child_size->w >= world_pos.x &&
basket_child_pos->x <= world_pos.x + size.w &&
basket_child_pos->y + basket_child_size->h >= world_pos.y &&
basket_child_pos->y <= world_pos.y + size.h)
{
fruit.add<Collided>();
}
});
});
}

40
src/physics.hpp Normal file
View File

@@ -0,0 +1,40 @@
#pragma once
#include <flecs.h>
struct WorldPosition
{
int x;
int y;
};
struct Position
{
int x;
int y;
};
struct Velocity
{
int x;
int y;
};
struct Size
{
int w;
int h;
};
struct CollisionBox
{
};
struct Collided
{
};
struct PhysicsModule
{
PhysicsModule(flecs::world& world);
};

View File

@@ -5,7 +5,8 @@
#include <SDL3/SDL.h>
#include <cstdint>
struct Sprite {
Texture const *texture;
uint16_t texture_atlas_index;
struct Sprite
{
Texture const* texture;
uint16_t texture_atlas_index;
};

7
src/util.hpp Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
struct Vec2
{
int x;
int y;
};

View File

@@ -1,6 +0,0 @@
#pragma once
struct Vec2 {
int x;
int y;
};