Compare commits
2 Commits
19890802a6
...
eb5f2f0e3e
| Author | SHA1 | Date | |
|---|---|---|---|
| eb5f2f0e3e | |||
| a0cbf16a64 |
@@ -7,19 +7,6 @@ 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(
|
||||
entt
|
||||
@@ -29,10 +16,6 @@ FetchContent_Declare(
|
||||
FetchContent_MakeAvailable(entt)
|
||||
find_package(entt CONFIG REQUIRED)
|
||||
|
||||
find_package(SDL3 CONFIG REQUIRED)
|
||||
find_package(SDL3_ttf CONFIG REQUIRED)
|
||||
find_package(spdlog CONFIG REQUIRED)
|
||||
|
||||
add_executable(HansTheGatherer
|
||||
src/main.cpp
|
||||
src/audio.cpp
|
||||
@@ -42,6 +25,6 @@ add_executable(HansTheGatherer
|
||||
src/render.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(HansTheGatherer SDL3::SDL3 SDL3_ttf::SDL3_ttf EnTT 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.
119
src/assets.cpp
119
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)
|
||||
{
|
||||
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* 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;
|
||||
}
|
||||
// return texture;
|
||||
// }
|
||||
|
||||
AudioAsset load_audio(uint8_t const* data, size_t size)
|
||||
{
|
||||
AudioAsset audio_asset;
|
||||
// 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;
|
||||
}
|
||||
// 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;
|
||||
// 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);
|
||||
// auto* iostream = SDL_IOFromConstMem(data, size);
|
||||
// auto* ttf = TTF_OpenFontIO(iostream, false, 20);
|
||||
|
||||
font_asset.font = ttf;
|
||||
// font_asset.font = ttf;
|
||||
|
||||
return font_asset;
|
||||
}
|
||||
// return font_asset;
|
||||
// }
|
||||
|
||||
AssetModule::AssetModule(entt::registry& registry)
|
||||
{
|
||||
auto renderer = registry.ctx().get<SdlHandles>().renderer;
|
||||
// auto renderer = registry.ctx().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};
|
||||
|
||||
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{.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));
|
||||
registry.ctx().emplace<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));
|
||||
registry.ctx().emplace<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,8 +2,6 @@
|
||||
|
||||
#include "audio.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.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,15 +33,15 @@ 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
|
||||
{
|
||||
|
||||
@@ -3,29 +3,29 @@
|
||||
|
||||
AudioModule::AudioModule(entt::registry& registry)
|
||||
{
|
||||
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);
|
||||
// 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);
|
||||
|
||||
registry.ctx().emplace<AudioStreams>(
|
||||
AudioStreams{.music_stream = music_stream, .sound_stream = sound_stream});
|
||||
// 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>();
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
// 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,24 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <cstdint>
|
||||
#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(entt::registry& registry);
|
||||
static void FeedAudioStreams(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
|
||||
{
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
#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;
|
||||
std::set<int> pressed;
|
||||
std::set<int> just_pressed;
|
||||
std::set<int> just_released;
|
||||
};
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#include "level.hpp"
|
||||
#include "input.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
void LevelModule::MoveBasket(entt::registry& registry)
|
||||
{
|
||||
auto const& input = registry.ctx().get<ButtonInput>();
|
||||
@@ -10,14 +8,14 @@ void LevelModule::MoveBasket(entt::registry& registry)
|
||||
|
||||
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;
|
||||
}
|
||||
// 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;
|
||||
@@ -79,7 +77,7 @@ void LevelModule::SpawnFruits(entt::registry& registry)
|
||||
void LevelModule::CollectFruit(entt::registry& registry)
|
||||
{
|
||||
auto& game = registry.ctx().get<Game>();
|
||||
auto const& audio_streams = registry.ctx().get<AudioStreams>();
|
||||
// auto const& audio_streams = registry.ctx().get<AudioStreams>();
|
||||
auto const& audio_assets = registry.ctx().get<AudioAssets>();
|
||||
|
||||
auto view = registry.view<Position, Fruit, Collided>();
|
||||
@@ -89,16 +87,16 @@ void LevelModule::CollectFruit(entt::registry& registry)
|
||||
pos.x += 1000;
|
||||
// registry.destroy(entity);
|
||||
|
||||
SDL_PutAudioStreamData(audio_streams.sound_stream,
|
||||
audio_assets.pickup_sound.buffer,
|
||||
audio_assets.pickup_sound.buffer_length);
|
||||
// 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_streams = registry.ctx().get<AudioStreams>();
|
||||
auto const& audio_assets = registry.ctx().get<AudioAssets>();
|
||||
|
||||
auto view = registry.view<Position, Spider, Collided>();
|
||||
@@ -108,9 +106,9 @@ void LevelModule::CollectSpider(entt::registry& registry)
|
||||
pos.x += 1000;
|
||||
// registry.destroy(entity);
|
||||
|
||||
SDL_PutAudioStreamData(audio_streams.sound_stream,
|
||||
audio_assets.hit_sound.buffer,
|
||||
audio_assets.hit_sound.buffer_length);
|
||||
// SDL_PutAudioStreamData(audio_streams.sound_stream,
|
||||
// audio_assets.hit_sound.buffer,
|
||||
// audio_assets.hit_sound.buffer_length);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#include "sprite.hpp"
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
struct Background
|
||||
{
|
||||
|
||||
105
src/main.cpp
105
src/main.cpp
@@ -5,61 +5,25 @@
|
||||
#include "physics.hpp"
|
||||
#include "render.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <entt/entt.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
void increment_ticks(entt::registry& registry)
|
||||
{
|
||||
auto& game = registry.ctx().get<Game>();
|
||||
// auto& translate_system = registry.ctx().get<TranslateSystem>();
|
||||
|
||||
game.ticks += 1;
|
||||
|
||||
if (game.ticks % 60 == 0)
|
||||
if (game.ticks % 60 == 0 && game.time > 0)
|
||||
{
|
||||
game.time = std::max(0, game.time - 1);
|
||||
game.time--;
|
||||
}
|
||||
|
||||
// if (game.time == 0)
|
||||
// translate_system.translate_system.disable();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
entt::registry registry;
|
||||
|
||||
registry.ctx().emplace<Game>(Game{.ticks = 0, .time = 60, .score = 0, .random_engine = {}});
|
||||
registry.ctx().emplace<ButtonInput>(ButtonInput{});
|
||||
auto sdl_handles = registry.ctx().emplace<SdlHandles>(
|
||||
SdlHandles{.window = window, .renderer = renderer, .text_engine = text_engine});
|
||||
|
||||
AssetModule asset_module(registry);
|
||||
AudioModule audio_module(registry);
|
||||
@@ -77,56 +41,55 @@ int main()
|
||||
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;
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
// AudioModule::FeedAudioStreams(registry);
|
||||
|
||||
if (registry.ctx().get<Game>().time != 0)
|
||||
{
|
||||
increment_ticks(registry);
|
||||
LevelModule::MoveBasket(registry);
|
||||
LevelModule::SpawnFruits(registry);
|
||||
LevelModule::CollectFruit(registry);
|
||||
LevelModule::CollectSpider(registry);
|
||||
// LevelModule::DespawnItems(registry);
|
||||
}
|
||||
|
||||
PhysicsModule::TranslatePhysicsObject(registry);
|
||||
PhysicsModule::PropagatePosition(registry);
|
||||
PhysicsModule::RemoveCollisionMarker(registry);
|
||||
PhysicsModule::CollisionCheck(registry);
|
||||
|
||||
SDL_RenderClear(sdl_handles.renderer);
|
||||
// SDL_RenderClear(sdl_handles.renderer);
|
||||
RenderModule::RenderSprites(registry);
|
||||
RenderModule::RenderScore(registry);
|
||||
SDL_RenderPresent(sdl_handles.renderer);
|
||||
// SDL_RenderPresent(sdl_handles.renderer);
|
||||
}
|
||||
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#include "physics.hpp"
|
||||
#include "level.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
void PhysicsModule::TranslatePhysicsObject(entt::registry& registry)
|
||||
{
|
||||
auto view = registry.view<Position, Velocity const>();
|
||||
|
||||
@@ -13,7 +13,7 @@ RenderModule::RenderModule(entt::registry& registry)
|
||||
|
||||
void RenderModule::RenderSprites(entt::registry& registry)
|
||||
{
|
||||
auto const& sdl_handles = registry.ctx().get<SdlHandles>();
|
||||
// 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>);
|
||||
@@ -24,17 +24,17 @@ void RenderModule::RenderSprites(entt::registry& registry)
|
||||
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 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_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);
|
||||
}
|
||||
|
||||
for (auto [entity, pos, size, sprite] : sprites_view.each())
|
||||
@@ -42,31 +42,31 @@ void RenderModule::RenderSprites(entt::registry& registry)
|
||||
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 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_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);
|
||||
}
|
||||
}
|
||||
|
||||
void RenderModule::RenderScore(entt::registry& registry)
|
||||
{
|
||||
auto const& sdl_handles = registry.ctx().get<SdlHandles>();
|
||||
// auto const& sdl_handles = registry.ctx().get<SdlHandles>();
|
||||
auto const& game = registry.ctx().get<Game>();
|
||||
auto const& font_assets = registry.ctx().get<FontAssets>();
|
||||
// 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);
|
||||
// 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);
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "assets.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <cstdint>
|
||||
|
||||
struct Sprite
|
||||
|
||||
Reference in New Issue
Block a user