Format project

This commit is contained in:
2025-05-31 12:36:18 +02:00
parent 3a1ebd4dfa
commit b828bf6bd2
8 changed files with 374 additions and 329 deletions

View File

@@ -21,60 +21,56 @@ 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) {
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());
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 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) {
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;
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* 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* 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};
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}});
.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));
auto background_music = load_audio(BACKGROUND_MUSIC_DATA, sizeof(BACKGROUND_MUSIC_DATA));
world.set<AudioAssets>(AudioAssets{.background_music = background_music});
}

View File

@@ -5,26 +5,30 @@
#include <SDL3/SDL.h>
#include <flecs.h>
struct TextureAtlasLayout {
struct TextureAtlasLayout
{
uint16_t width;
uint16_t height;
uint8_t rows;
uint8_t columns;
};
struct Texture {
SDL_Texture *sdl_texture;
struct Texture
{
SDL_Texture* sdl_texture;
TextureAtlasLayout texture_atlas_layout;
};
struct TextureAssets {
struct TextureAssets
{
Texture background;
Texture fruits;
Texture basket;
};
struct AudioAssets {
struct AudioAssets
{
AudioAsset background_music;
};
void init_assets(flecs::world &world);
void init_assets(flecs::world& world);

View File

@@ -3,8 +3,9 @@
#include <SDL3/SDL.h>
#include <cstdint>
struct AudioAsset {
struct AudioAsset
{
SDL_AudioSpec spec;
uint8_t *buffer;
uint8_t* buffer;
uint32_t buffer_length;
};

View File

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

View File

@@ -15,50 +15,57 @@
static constexpr int WINDOW_WIDTH = 400;
static constexpr int WINDOW_HEIGHT = 240;
struct WorldPosition {
struct WorldPosition
{
int x;
int y;
};
struct Position {
struct Position
{
int x;
int y;
};
struct Velocity {
struct Velocity
{
int x;
int y;
};
struct Size {
struct Size
{
int w;
int h;
};
struct Game {
struct Game
{
uint32_t ticks;
};
int main() {
int main()
{
spdlog::info("Initialize SDL...");
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
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) {
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* renderer = SDL_CreateRenderer(window, nullptr);
if (renderer == nullptr)
{
spdlog::critical("Failed to create SDL renderer!\nCause: {}", SDL_GetError());
}
flecs::world world;
@@ -66,29 +73,33 @@ int main() {
world.set<ButtonInput>(ButtonInput{});
world.set<SdlHandles>(SdlHandles{.window = window, .renderer = renderer});
init_assets(world);
auto *texture_assets = world.get<TextureAssets>();
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<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 {};
struct Fruit
{
};
struct Basket
{
};
struct CollisionBox
{
};
world.system<Game>("IncrementTicks")
.term_at(0)
.singleton()
.each([](Game &game_ticks) { game_ticks.ticks += 1; });
.each([](Game& game_ticks) { game_ticks.ticks += 1; });
auto basket = world.entity("Basket")
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<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>();
@@ -99,15 +110,19 @@ int main() {
.set<Size>(Size{.w = 64, .h = 32})
.add<CollisionBox>();
world
.system<SdlHandles const, WorldPosition const, Size const, CollisionBox>(
"DrawBoxes")
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)};
.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);
});
@@ -117,19 +132,19 @@ int main() {
.singleton()
.term_at(1)
.singleton()
.each([](flecs::iter &it, size_t index, Game const &game,
TextureAssets const &texture_assets) {
if ((game.ticks % 100) == 0) {
.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<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,
.set<Sprite>(Sprite{.texture = &texture_assets.fruits,
.texture_atlas_index =
static_cast<uint16_t>(game.ticks % 228)})
.set<Size>(Size{.w = 32, .h = 32});
@@ -146,12 +161,16 @@ int main() {
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()) {
.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 {
}
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;
@@ -159,8 +178,13 @@ int main() {
});
world.system<WorldPosition const, Size const, CollisionBox>("CollisionCheck")
.each([](flecs::iter &it, size_t index, WorldPosition const &world_pos,
Size const &size, CollisionBox) {
.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;
@@ -171,18 +195,21 @@ int main() {
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) {
basket_box_pos->y <= world_pos.y + size.h)
{
spdlog::info("collision");
}
});
world
.system<SdlHandles const, Position const, Size const, Sprite const>(
"RenderSprites")
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) {
.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;
@@ -191,25 +218,31 @@ int main() {
static_cast<float>(layout.width),
static_cast<float>(layout.height)};
SDL_FRect dstrect{static_cast<float>(pos.x), static_cast<float>(pos.y),
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);
SDL_RenderTexture(
sdl_handles.renderer, sprite.texture->sdl_texture, &srcrect, &dstrect);
});
world
.system<ButtonInput const, Position, Size const, Sprite const, Basket>(
"MoveBasket")
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)) {
.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)) {
if (input.pressed.contains(SDLK_RIGHT))
{
pos.x += 5;
}
@@ -218,16 +251,18 @@ int main() {
});
world.system<Position, Velocity const>("MoveSprites")
.each([](Position &pos, Velocity const &vel) {
.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) {
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;
}
@@ -235,8 +270,9 @@ int main() {
SDL_ResumeAudioStreamDevice(stream);
bool exit_gameloop = false;
while (!exit_gameloop) {
auto *input = world.get_mut<ButtonInput>();
while (!exit_gameloop)
{
auto* input = world.get_mut<ButtonInput>();
// Clear just pressed/released
input->just_pressed.clear();
@@ -244,21 +280,26 @@ int main() {
// Input
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
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) {
if (event.key.key == SDLK_ESCAPE)
{
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);
}
break;
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);
}
break;
@@ -266,9 +307,10 @@ int main() {
}
// Game Logic
if (SDL_GetAudioStreamQueued(stream) <
(int)audio_assets->background_music.buffer_length) {
SDL_PutAudioStreamData(stream, audio_assets->background_music.buffer,
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);
}

View File

@@ -2,8 +2,8 @@
#include <SDL3/SDL.h>
struct SdlHandles {
SDL_Window *window;
SDL_Renderer *renderer;
struct SdlHandles
{
SDL_Window* window;
SDL_Renderer* renderer;
};

View File

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

View File

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