Compare commits

...

3 Commits

Author SHA1 Message Date
2e24f504c5 Add spiders 2025-05-31 18:54:40 +02:00
e7fab3bfc3 Fall faster fruits 2025-05-31 18:29:06 +02:00
4fa7a07844 Add score text 2025-05-31 18:24:24 +02:00
10 changed files with 171 additions and 25 deletions

View File

@@ -6,7 +6,30 @@ 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)
@@ -16,6 +39,6 @@ add_executable(HansTheGatherer
src/physics.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
View File

@@ -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

Binary file not shown.

BIN
assets/images/spiders.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

@@ -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});
}

View File

@@ -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);

View File

@@ -1,6 +1,7 @@
#pragma once
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
static constexpr int WINDOW_WIDTH = 400;
static constexpr int WINDOW_HEIGHT = 240;
@@ -9,10 +10,12 @@ 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;
};

View File

@@ -12,6 +12,10 @@ struct Fruit
{
};
struct Spider
{
};
struct Basket
{
};
@@ -38,8 +42,8 @@ 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")
@@ -53,24 +57,44 @@ struct LevelModule
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});
bool spider = std::rand() % 5 == 0;
int vel = std::rand() % 2 + 1;
if ((game.ticks % 30) == 0)
{
flecs::entity e;
if (!spider)
{
e = it.world()
.entity()
.add<Fruit>()
.add<WorldPosition>()
.set<Position>(
Position{.x = std::rand() % WINDOW_WIDTH, .y = -16})
.set<Velocity>(Velocity{.x = 0, .y = vel})
.set<Sprite>(
Sprite{.texture = &texture_assets.fruits,
.texture_atlas_index =
static_cast<uint16_t>(std::rand() % 228)})
.set<Size>(Size{.w = 32, .h = 32});
}
else
{
e = it.world()
.entity()
.add<Spider>()
.add<WorldPosition>()
.set<Position>(
Position{.x = std::rand() % WINDOW_WIDTH, .y = -16})
.set<Velocity>(Velocity{.x = 0, .y = vel})
.set<Sprite>(Sprite{.texture = &texture_assets.spiders,
.texture_atlas_index =
static_cast<uint16_t>(std::rand() % 8)})
.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})
@@ -87,10 +111,25 @@ struct LevelModule
// 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;
});
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;
});
world.system<ButtonInput const, Position, Size const, Sprite const, Basket>("MoveBasket")
.term_at(0)

View File

@@ -11,6 +11,7 @@
#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 +27,8 @@ int main()
std::terminate();
}
TTF_Init();
auto* window = SDL_CreateWindow("HansTheGatherer", WINDOW_WIDTH, WINDOW_HEIGHT, 0);
if (window == nullptr)
{
@@ -38,11 +41,14 @@ 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});
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 <PhysicsModule>();
@@ -51,10 +57,19 @@ int main()
world.system<Game>("IncrementTicks")
.term_at(0)
.singleton()
.each([](Game& game_ticks) { game_ticks.ticks += 1; });
.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::PreUpdate)
.kind(flecs::OnStore)
.term_at(0)
.singleton()
.each(
@@ -80,6 +95,27 @@ int main()
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);

View File

@@ -48,6 +48,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(