Compare commits
3 Commits
hans
...
1d6eb124de
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d6eb124de | |||
| a0cbf16a64 | |||
| a91d70daa4 |
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Launch",
|
||||
"program": "${workspaceFolder}/build/HansTheGatherer",
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
@@ -7,40 +7,24 @@ 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
|
||||
entt
|
||||
URL https://github.com/skypjack/entt/archive/refs/tags/v3.15.0.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)
|
||||
FetchContent_MakeAvailable(entt)
|
||||
find_package(entt CONFIG REQUIRED)
|
||||
|
||||
add_executable(HansTheGatherer
|
||||
src/main.cpp
|
||||
src/audio.cpp
|
||||
src/assets.cpp
|
||||
src/level.cpp
|
||||
src/physics.cpp
|
||||
src/render.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(HansTheGatherer SDL3::SDL3 SDL3_ttf::SDL3_ttf flecs::flecs spdlog::spdlog)
|
||||
target_link_libraries(HansTheGatherer EnTT)
|
||||
|
||||
set_property(TARGET HansTheGatherer PROPERTY CXX_STANDARD 20)
|
||||
|
||||
BIN
perf.data.old
Normal file
BIN
perf.data.old
Normal file
Binary file not shown.
139
src/assets.cpp
139
src/assets.cpp
@@ -1,9 +1,7 @@
|
||||
#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"
|
||||
@@ -37,79 +35,90 @@ 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)
|
||||
// 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(entt::registry& registry)
|
||||
{
|
||||
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());
|
||||
}
|
||||
// auto renderer = registry.ctx().get<SdlHandles>().renderer;
|
||||
|
||||
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);
|
||||
// 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);
|
||||
// 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);
|
||||
// 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);
|
||||
// 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}});
|
||||
// registry.ctx().emplace<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}});
|
||||
registry.ctx().emplace<TextureAssets>(
|
||||
TextureAssets{.background = Texture{.texture_atlas_layout = background_layout},
|
||||
.fruits = Texture{.texture_atlas_layout = fruits_layout},
|
||||
.spiders = Texture{.texture_atlas_layout = spiders_layout},
|
||||
.basket = Texture{.texture_atlas_layout = basket_layout}});
|
||||
|
||||
auto background_music = load_audio(BACKGROUND_MUSIC_DATA, sizeof(BACKGROUND_MUSIC_DATA));
|
||||
auto pickup_sound = load_audio(PICKUP_SOUND_DATA, sizeof(PICKUP_SOUND_DATA));
|
||||
auto hit_sound = load_audio(HIT_SOUND_DATA, sizeof(HIT_SOUND_DATA));
|
||||
world.set<AudioAssets>(AudioAssets{.background_music = background_music,
|
||||
.pickup_sound = pickup_sound,
|
||||
.hit_sound = hit_sound});
|
||||
// auto background_music = load_audio(BACKGROUND_MUSIC_DATA, sizeof(BACKGROUND_MUSIC_DATA));
|
||||
// auto pickup_sound = load_audio(PICKUP_SOUND_DATA, sizeof(PICKUP_SOUND_DATA));
|
||||
// auto hit_sound = load_audio(HIT_SOUND_DATA, sizeof(HIT_SOUND_DATA));
|
||||
registry.ctx().emplace<AudioAssets>(AudioAssets{
|
||||
.background_music = {.buffer = BACKGROUND_MUSIC_DATA,
|
||||
.buffer_length = sizeof(BACKGROUND_MUSIC_DATA)},
|
||||
.pickup_sound = {.buffer = PICKUP_SOUND_DATA, .buffer_length = sizeof(PICKUP_SOUND_DATA)},
|
||||
.hit_sound = {.buffer = HIT_SOUND_DATA, .buffer_length = sizeof(HIT_SOUND_DATA)}});
|
||||
// registry.ctx().emplace<AudioAssets>(AudioAssets{.background_music = background_music,
|
||||
// .pickup_sound = pickup_sound,
|
||||
// .hit_sound = hit_sound});
|
||||
|
||||
auto font = load_font(DEFAULT_FONT_DATA, sizeof(DEFAULT_FONT_DATA));
|
||||
world.set<FontAssets>(FontAssets{.default_font = font});
|
||||
// auto font = load_font(DEFAULT_FONT_DATA, sizeof(DEFAULT_FONT_DATA));
|
||||
// registry.ctx().emplace<FontAssets>(FontAssets{.default_font = font});
|
||||
}
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
|
||||
#include "audio.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <flecs.h>
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
struct TextureAtlasLayout
|
||||
{
|
||||
@@ -16,7 +14,7 @@ struct TextureAtlasLayout
|
||||
|
||||
struct Texture
|
||||
{
|
||||
SDL_Texture* sdl_texture;
|
||||
// SDL_Texture* sdl_texture;
|
||||
TextureAtlasLayout texture_atlas_layout;
|
||||
};
|
||||
|
||||
@@ -35,17 +33,17 @@ struct AudioAssets
|
||||
AudioAsset hit_sound;
|
||||
};
|
||||
|
||||
struct FontAsset
|
||||
{
|
||||
TTF_Font* font;
|
||||
};
|
||||
// struct FontAsset
|
||||
// {
|
||||
// TTF_Font* font;
|
||||
// };
|
||||
|
||||
struct FontAssets
|
||||
{
|
||||
FontAsset default_font;
|
||||
};
|
||||
// struct FontAssets
|
||||
// {
|
||||
// FontAsset default_font;
|
||||
// };
|
||||
|
||||
struct AssetModule
|
||||
{
|
||||
AssetModule(flecs::world& world);
|
||||
AssetModule(entt::registry& registry);
|
||||
};
|
||||
|
||||
@@ -1,34 +1,31 @@
|
||||
#include "audio.hpp"
|
||||
#include "assets.hpp"
|
||||
|
||||
AudioModule::AudioModule(flecs::world& world)
|
||||
AudioModule::AudioModule(entt::registry& registry)
|
||||
{
|
||||
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->pickup_sound.spec, NULL, NULL);
|
||||
// auto const& audio_assets = registry.ctx().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.pickup_sound.spec, NULL, NULL);
|
||||
|
||||
SDL_ResumeAudioStreamDevice(music_stream);
|
||||
SDL_ResumeAudioStreamDevice(sound_stream);
|
||||
// SDL_ResumeAudioStreamDevice(music_stream);
|
||||
// SDL_ResumeAudioStreamDevice(sound_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);
|
||||
}
|
||||
});
|
||||
// registry.ctx().emplace<AudioStreams>(
|
||||
// AudioStreams{.music_stream = music_stream, .sound_stream = sound_stream});
|
||||
}
|
||||
|
||||
// void AudioModule::FeedAudioStreams(entt::registry& registry)
|
||||
// {
|
||||
// auto audio_streams = registry.ctx().get<AudioStreams>();
|
||||
// auto audio_assets = registry.ctx().get<AudioAssets>();
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <cstdint>
|
||||
#include <flecs.h>
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
struct AudioAsset
|
||||
{
|
||||
SDL_AudioSpec spec;
|
||||
uint8_t* buffer;
|
||||
// SDL_AudioSpec spec;
|
||||
uint8_t const* buffer;
|
||||
uint32_t buffer_length;
|
||||
};
|
||||
|
||||
struct AudioStreams
|
||||
{
|
||||
SDL_AudioStream* music_stream;
|
||||
SDL_AudioStream* sound_stream;
|
||||
};
|
||||
// struct AudioStreams
|
||||
// {
|
||||
// SDL_AudioStream* music_stream;
|
||||
// SDL_AudioStream* sound_stream;
|
||||
// };
|
||||
|
||||
struct AudioModule
|
||||
{
|
||||
AudioModule(flecs::world& world);
|
||||
AudioModule(entt::registry& registry);
|
||||
// static void FeedAudioStreams(entt::registry& registry);
|
||||
};
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
#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;
|
||||
|
||||
struct SdlHandles
|
||||
{
|
||||
SDL_Window* window;
|
||||
SDL_Renderer* renderer;
|
||||
TTF_TextEngine* text_engine;
|
||||
};
|
||||
// struct SdlHandles
|
||||
// {
|
||||
// SDL_Window* window;
|
||||
// SDL_Renderer* renderer;
|
||||
// TTF_TextEngine* text_engine;
|
||||
// };
|
||||
|
||||
struct Game
|
||||
{
|
||||
|
||||
123
src/level.cpp
Normal file
123
src/level.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#include "level.hpp"
|
||||
#include "input.hpp"
|
||||
|
||||
void LevelModule::MoveBasket(entt::registry& registry)
|
||||
{
|
||||
auto const& input = registry.ctx().get<ButtonInput>();
|
||||
auto basket_view = registry.view<Position, Size const, Sprite const, Basket>();
|
||||
|
||||
for (auto [entity, pos, size, sprite] : basket_view.each())
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void LevelModule::SpawnFruits(entt::registry& registry)
|
||||
{
|
||||
auto& game = registry.ctx().get<Game>();
|
||||
auto const& texture_assets = registry.ctx().get<TextureAssets>();
|
||||
|
||||
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)
|
||||
{
|
||||
auto item = registry.create();
|
||||
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);
|
||||
|
||||
registry.emplace<Fruit>(item);
|
||||
registry.emplace<WorldPosition>(item);
|
||||
registry.emplace<Position>(item, Position{.x = xpos, .y = -16});
|
||||
registry.emplace<Velocity>(item, Velocity{.x = 0, .y = vel});
|
||||
registry.emplace<Sprite>(
|
||||
item, Sprite{.texture = &texture_assets.fruits, .texture_atlas_index = index});
|
||||
registry.emplace<Size>(item, Size{.w = 32, .h = 32});
|
||||
}
|
||||
else
|
||||
{
|
||||
std::uniform_int_distribution<> index_dist(0, 8 - 1);
|
||||
uint16_t index = index_dist(game.random_engine);
|
||||
|
||||
registry.emplace<Spider>(item);
|
||||
registry.emplace<WorldPosition>(item);
|
||||
registry.emplace<Position>(item, Position{.x = xpos, .y = -16});
|
||||
registry.emplace<Velocity>(item, Velocity{.x = 0, .y = vel});
|
||||
registry.emplace<Sprite>(
|
||||
item, Sprite{.texture = &texture_assets.spiders, .texture_atlas_index = index});
|
||||
registry.emplace<Size>(item, Size{.w = 32, .h = 32});
|
||||
}
|
||||
|
||||
auto collision_box = registry.create();
|
||||
registry.emplace<Parent>(collision_box, item);
|
||||
registry.emplace<WorldPosition>(collision_box);
|
||||
registry.emplace<Position>(collision_box, Position{.x = 0, .y = 0});
|
||||
registry.emplace<Size>(collision_box, Size{.w = 32, .h = 32});
|
||||
registry.emplace<CollisionBox>(collision_box, item);
|
||||
}
|
||||
}
|
||||
|
||||
void LevelModule::CollectFruit(entt::registry& registry)
|
||||
{
|
||||
auto& game = registry.ctx().get<Game>();
|
||||
// auto const& audio_streams = registry.ctx().get<AudioStreams>();
|
||||
auto const& audio_assets = registry.ctx().get<AudioAssets>();
|
||||
|
||||
auto view = registry.view<Position, Fruit, Collided>();
|
||||
for (auto [entity, pos] : view.each())
|
||||
{
|
||||
game.score += 10;
|
||||
pos.x += 1000;
|
||||
// registry.destroy(entity);
|
||||
|
||||
// SDL_PutAudioStreamData(audio_streams.sound_stream,
|
||||
// audio_assets.pickup_sound.buffer,
|
||||
// audio_assets.pickup_sound.buffer_length);
|
||||
}
|
||||
}
|
||||
|
||||
void LevelModule::CollectSpider(entt::registry& registry)
|
||||
{
|
||||
auto& game = registry.ctx().get<Game>();
|
||||
// auto const& audio_streams = registry.ctx().get<AudioStreams>();
|
||||
auto const& audio_assets = registry.ctx().get<AudioAssets>();
|
||||
|
||||
auto view = registry.view<Position, Spider, Collided>();
|
||||
for (auto [entity, pos] : view.each())
|
||||
{
|
||||
game.score -= 50;
|
||||
pos.x += 1000;
|
||||
// registry.destroy(entity);
|
||||
|
||||
// SDL_PutAudioStreamData(audio_streams.sound_stream,
|
||||
// audio_assets.hit_sound.buffer,
|
||||
// audio_assets.hit_sound.buffer_length);
|
||||
};
|
||||
}
|
||||
|
||||
void LevelModule::DespawnItems(entt::registry& registry)
|
||||
{
|
||||
auto view = registry.view<WorldPosition const, Item>();
|
||||
for (auto [entity, pos] : view.each())
|
||||
{
|
||||
if (pos.y >= WINDOW_HEIGHT)
|
||||
registry.destroy(entity);
|
||||
}
|
||||
}
|
||||
205
src/level.hpp
205
src/level.hpp
@@ -1,12 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "definitions.hpp"
|
||||
#include "input.hpp"
|
||||
#include "physics.hpp"
|
||||
#include "sprite.hpp"
|
||||
|
||||
#include <flecs.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
struct Background
|
||||
{
|
||||
};
|
||||
|
||||
struct Fruit
|
||||
{
|
||||
@@ -24,175 +26,44 @@ struct Basket
|
||||
{
|
||||
};
|
||||
|
||||
struct BasketCollisionBox
|
||||
{
|
||||
};
|
||||
|
||||
struct LevelModule
|
||||
{
|
||||
LevelModule(flecs::world& world)
|
||||
LevelModule(entt::registry& registry)
|
||||
{
|
||||
auto const* texture_assets = world.get<TextureAssets>();
|
||||
auto const* texture_assets = ®istry.ctx().get<TextureAssets>();
|
||||
|
||||
world.component<Item>();
|
||||
world.component<Fruit>().is_a<Item>();
|
||||
world.component<Spider>().is_a<Item>();
|
||||
auto background = registry.create();
|
||||
registry.emplace<Position>(background, Position{.x = 0, .y = 0});
|
||||
registry.emplace<Sprite>(
|
||||
background, Sprite{.texture = &texture_assets->background, .texture_atlas_index = 0});
|
||||
registry.emplace<Size>(background, Size{.w = WINDOW_WIDTH, .h = WINDOW_HEIGHT});
|
||||
registry.emplace<Background>(background);
|
||||
|
||||
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 = registry.create();
|
||||
registry.emplace<WorldPosition>(basket);
|
||||
registry.emplace<Position>(basket,
|
||||
Position{.x = WINDOW_WIDTH / 2 - 32, .y = WINDOW_HEIGHT - 32});
|
||||
registry.emplace<Sprite>(
|
||||
basket, Sprite{.texture = &texture_assets->basket, .texture_atlas_index = 0});
|
||||
registry.emplace<Size>(basket, Size{.w = 64, .h = 32});
|
||||
registry.emplace<Basket>(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<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, TextureAssets const>("SpawnFruits")
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.term_at(1)
|
||||
.singleton()
|
||||
.each(
|
||||
[](flecs::iter& it, size_t index, Game& game, TextureAssets const& texture_assets)
|
||||
{
|
||||
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(e)
|
||||
.add<WorldPosition>()
|
||||
.set<Position>(Position{.x = 0, .y = 0})
|
||||
.set<Size>(Size{.w = 32, .h = 32})
|
||||
.add<CollisionBox>();
|
||||
}
|
||||
});
|
||||
|
||||
// world.system<WorldPosition const, Item>("DespawnItems")
|
||||
// .kind(flecs::OnValidate)
|
||||
// .each(
|
||||
// [](flecs::entity e, WorldPosition const& pos, Item)
|
||||
// {
|
||||
// if (pos.y >= WINDOW_HEIGHT)
|
||||
// e.destruct();
|
||||
// });
|
||||
|
||||
world.system<Game, AudioStreams, AudioAssets, Position, Fruit, Collided>("CollectFruit")
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.term_at(1)
|
||||
.singleton()
|
||||
.term_at(2)
|
||||
.singleton()
|
||||
.each(
|
||||
[](flecs::entity e,
|
||||
Game& game,
|
||||
AudioStreams& audio_streams,
|
||||
AudioAssets& audio_assets,
|
||||
Position& pos,
|
||||
Fruit,
|
||||
Collided)
|
||||
{
|
||||
game.score += 10;
|
||||
pos.x += 1000;
|
||||
// e.destruct();
|
||||
|
||||
SDL_PutAudioStreamData(audio_streams.sound_stream,
|
||||
audio_assets.pickup_sound.buffer,
|
||||
audio_assets.pickup_sound.buffer_length);
|
||||
});
|
||||
|
||||
world.system<Game, AudioStreams, AudioAssets, Position, Spider, Collided>("CollectSpider")
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.term_at(1)
|
||||
.singleton()
|
||||
.term_at(2)
|
||||
.singleton()
|
||||
.each(
|
||||
[](flecs::entity e,
|
||||
Game& game,
|
||||
AudioStreams& audio_streams,
|
||||
AudioAssets& audio_assets,
|
||||
Position& pos,
|
||||
Spider,
|
||||
Collided)
|
||||
{
|
||||
game.score -= 50;
|
||||
pos.x += 1000;
|
||||
// e.destruct();
|
||||
|
||||
SDL_PutAudioStreamData(audio_streams.sound_stream,
|
||||
audio_assets.hit_sound.buffer,
|
||||
audio_assets.hit_sound.buffer_length);
|
||||
});
|
||||
|
||||
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;
|
||||
});
|
||||
auto basket_cb = registry.create();
|
||||
registry.emplace<WorldPosition>(basket_cb);
|
||||
registry.emplace<Position>(basket_cb, Position{.x = 0, .y = 16});
|
||||
registry.emplace<Size>(basket_cb, Size{.w = 64, .h = 16});
|
||||
registry.emplace<CollisionBox>(basket_cb);
|
||||
registry.emplace<Parent>(basket_cb, basket);
|
||||
registry.emplace<BasketCollisionBox>(basket_cb);
|
||||
}
|
||||
|
||||
static void MoveBasket(entt::registry& registry);
|
||||
static void SpawnFruits(entt::registry& registry);
|
||||
static void CollectFruit(entt::registry ®istry);
|
||||
static void CollectSpider(entt::registry ®istry);
|
||||
static void DespawnItems(entt::registry ®istry);
|
||||
};
|
||||
|
||||
155
src/main.cpp
155
src/main.cpp
@@ -5,114 +5,91 @@
|
||||
#include "physics.hpp"
|
||||
#include "render.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <flecs.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
void increment_ticks(entt::registry& registry)
|
||||
{
|
||||
auto& game = registry.ctx().get<Game>();
|
||||
game.ticks += 1;
|
||||
|
||||
if (game.ticks % 60 == 0)
|
||||
{
|
||||
game.time = std::max(0, game.time - 1);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
spdlog::info("Initialize SDL...");
|
||||
entt::registry registry;
|
||||
|
||||
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
|
||||
registry.ctx().emplace<Game>(Game{.ticks = 0, .time = 60, .score = 0, .random_engine = {}});
|
||||
registry.ctx().emplace<ButtonInput>(ButtonInput{});
|
||||
|
||||
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.set<Game>(Game{.ticks = 0, .time = 60, .score = 0, .random_engine = {}});
|
||||
world.set<ButtonInput>(ButtonInput{});
|
||||
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, TranslateSystem>("IncrementTicks")
|
||||
.term_at(0)
|
||||
.singleton()
|
||||
.term_at(1)
|
||||
.singleton()
|
||||
.each(
|
||||
[](Game& game, TranslateSystem& translate_system)
|
||||
{
|
||||
game.ticks += 1;
|
||||
|
||||
if (game.ticks % 60 == 0)
|
||||
{
|
||||
game.time = std::max(0, game.time - 1);
|
||||
}
|
||||
|
||||
if (game.time == 0)
|
||||
translate_system.translate_system.disable();
|
||||
});
|
||||
AssetModule asset_module(registry);
|
||||
AudioModule audio_module(registry);
|
||||
RenderModule render_module(registry);
|
||||
PhysicsModule physics_module(registry);
|
||||
LevelModule level_module(registry);
|
||||
|
||||
bool exit_gameloop = false;
|
||||
while (!exit_gameloop)
|
||||
{
|
||||
auto* input = world.get_mut<ButtonInput>();
|
||||
auto* input = ®istry.ctx().get<ButtonInput>();
|
||||
|
||||
// Clear just pressed/released
|
||||
input->just_pressed.clear();
|
||||
input->just_released.clear();
|
||||
|
||||
// Input
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
// 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;
|
||||
// }
|
||||
// }
|
||||
|
||||
// AudioModule::FeedAudioStreams(registry);
|
||||
|
||||
if (registry.ctx().get<Game>().time != 0)
|
||||
{
|
||||
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;
|
||||
}
|
||||
increment_ticks(registry);
|
||||
LevelModule::MoveBasket(registry);
|
||||
LevelModule::SpawnFruits(registry);
|
||||
LevelModule::CollectFruit(registry);
|
||||
LevelModule::CollectSpider(registry);
|
||||
// LevelModule::DespawnItems(registry);
|
||||
}
|
||||
|
||||
world.progress();
|
||||
}
|
||||
PhysicsModule::TranslatePhysicsObject(registry);
|
||||
PhysicsModule::PropagatePosition(registry);
|
||||
PhysicsModule::RemoveCollisionMarker(registry);
|
||||
PhysicsModule::CollisionCheck(registry);
|
||||
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
// SDL_RenderClear(sdl_handles.renderer);
|
||||
RenderModule::RenderSprites(registry);
|
||||
RenderModule::RenderScore(registry);
|
||||
// SDL_RenderPresent(sdl_handles.renderer);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
134
src/physics.cpp
134
src/physics.cpp
@@ -1,86 +1,68 @@
|
||||
#include "physics.hpp"
|
||||
#include "level.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
PhysicsModule::PhysicsModule(flecs::world& world)
|
||||
void PhysicsModule::TranslatePhysicsObject(entt::registry& registry)
|
||||
{
|
||||
auto translate_system = world.system<Position, Velocity const>("TranslatePhysicsObject")
|
||||
.each(
|
||||
[](Position& pos, Velocity const& vel)
|
||||
{
|
||||
pos.x += vel.x;
|
||||
pos.y += vel.y;
|
||||
});
|
||||
auto view = registry.view<Position, Velocity const>();
|
||||
|
||||
world.set<TranslateSystem>(TranslateSystem{translate_system});
|
||||
for (auto [entity, pos, vel] : view.each())
|
||||
{
|
||||
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);
|
||||
void PhysicsModule::PropagatePosition(entt::registry& registry)
|
||||
{
|
||||
auto root_transform_view = registry.view<Position const, WorldPosition>(entt::exclude<Parent>);
|
||||
auto transform_view = registry.view<Position const, WorldPosition, Parent const>();
|
||||
|
||||
world.system<WorldPosition, Position const>("PropagatePosition")
|
||||
.kind(propagate_phase)
|
||||
.each(
|
||||
[](flecs::entity e, WorldPosition& world_pos, Position const& local_pos)
|
||||
for (auto [entity, pos, world_pos] : root_transform_view.each())
|
||||
{
|
||||
world_pos.x = pos.x;
|
||||
world_pos.y = pos.y;
|
||||
}
|
||||
|
||||
for (auto [entity, pos, world_pos, parent] : transform_view.each())
|
||||
{
|
||||
auto parent_pos = registry.get<WorldPosition const>(parent.parent);
|
||||
|
||||
world_pos.x = parent_pos.x + pos.x;
|
||||
world_pos.y = parent_pos.y + pos.y;
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsModule::RemoveCollisionMarker(entt::registry& registry)
|
||||
{
|
||||
auto view = registry.view<Collided const>();
|
||||
for (auto [entity] : view.each())
|
||||
{
|
||||
registry.remove<Collided>(entity);
|
||||
}
|
||||
}
|
||||
void PhysicsModule::CollisionCheck(entt::registry& registry)
|
||||
{
|
||||
auto view = registry.view<WorldPosition const, Size const, Parent const, CollisionBox>(
|
||||
entt::exclude<BasketCollisionBox>);
|
||||
auto basket_cb_view = registry.view<WorldPosition const, Size const, BasketCollisionBox>();
|
||||
|
||||
for (auto [e, world_pos, size, parent] : view.each())
|
||||
{
|
||||
auto fruit = parent.parent;
|
||||
|
||||
for (auto [basket, basket_world_pos, basket_size] : basket_cb_view.each())
|
||||
{
|
||||
if (basket_world_pos.x + basket_size.w >= world_pos.x &&
|
||||
basket_world_pos.x <= world_pos.x + size.w &&
|
||||
basket_world_pos.y + basket_size.h >= world_pos.y &&
|
||||
basket_world_pos.y <= world_pos.y + size.h)
|
||||
{
|
||||
if (e.parent() != flecs::entity::null() && e.parent().has<WorldPosition>())
|
||||
return;
|
||||
registry.emplace<Collided>(fruit);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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>();
|
||||
}
|
||||
});
|
||||
});
|
||||
PhysicsModule::PhysicsModule(entt::registry& registry)
|
||||
{
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <flecs.h>
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
struct WorldPosition
|
||||
{
|
||||
@@ -26,6 +26,16 @@ struct Size
|
||||
int h;
|
||||
};
|
||||
|
||||
struct Parent
|
||||
{
|
||||
entt::entity parent;
|
||||
};
|
||||
|
||||
struct Children
|
||||
{
|
||||
std::vector<entt::entity> children;
|
||||
};
|
||||
|
||||
struct CollisionBox
|
||||
{
|
||||
};
|
||||
@@ -34,12 +44,12 @@ struct Collided
|
||||
{
|
||||
};
|
||||
|
||||
struct TranslateSystem
|
||||
{
|
||||
flecs::system translate_system;
|
||||
};
|
||||
|
||||
struct PhysicsModule
|
||||
{
|
||||
PhysicsModule(flecs::world& world);
|
||||
PhysicsModule(entt::registry& registry);
|
||||
|
||||
static void TranslatePhysicsObject(entt::registry& registry);
|
||||
static void PropagatePosition(entt::registry& registry);
|
||||
static void RemoveCollisionMarker(entt::registry& registry);
|
||||
static void CollisionCheck(entt::registry& registry);
|
||||
};
|
||||
|
||||
125
src/render.cpp
125
src/render.cpp
@@ -1,73 +1,72 @@
|
||||
#include "render.hpp"
|
||||
|
||||
#include "definitions.hpp"
|
||||
#include "level.hpp"
|
||||
#include "physics.hpp"
|
||||
#include "sprite.hpp"
|
||||
|
||||
#include <format>
|
||||
|
||||
RenderModule::RenderModule(flecs::world& world)
|
||||
RenderModule::RenderModule(entt::registry& registry)
|
||||
{
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
void RenderModule::RenderSprites(entt::registry& registry)
|
||||
{
|
||||
// auto const& sdl_handles = registry.ctx().get<SdlHandles>();
|
||||
auto const& game = registry.ctx().get<Game>();
|
||||
auto sprites_view =
|
||||
registry.view<Position const, Size const, Sprite const>(entt::exclude<Background>);
|
||||
auto background_view = registry.view<Position const, Size const, Sprite const, Background>();
|
||||
|
||||
for (auto [entity, pos, size, sprite] : background_view.each())
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
for (auto [entity, pos, size, sprite] : sprites_view.each())
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void RenderModule::RenderScore(entt::registry& registry)
|
||||
{
|
||||
// auto const& sdl_handles = registry.ctx().get<SdlHandles>();
|
||||
auto const& game = registry.ctx().get<Game>();
|
||||
// auto const& font_assets = registry.ctx().get<FontAssets>();
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <flecs.h>
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
struct RenderModule
|
||||
{
|
||||
RenderModule(flecs::world& world);
|
||||
RenderModule(entt::registry& registry);
|
||||
|
||||
static void RenderSprites(entt::registry& registry);
|
||||
static void RenderScore(entt::registry& registry);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user