Files
HansTheGatherer/main.cpp

159 lines
4.5 KiB
C++

#include "assets.hpp"
#include "input.hpp"
#include "sdl_types.hpp"
#include "sprite.hpp"
#include <SDL3/SDL.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_iostream.h>
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_video.h>
#include <flecs.h>
#include <spdlog/spdlog.h>
static constexpr int WINDOW_WIDTH = 400;
static constexpr int WINDOW_HEIGHT = 280;
struct Position {
int x;
int y;
};
int main() {
spdlog::info("Initialize SDL...");
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
if (!SDL_Init(SDL_INIT_VIDEO)) {
spdlog::critical("Failed to initialize SDL!\nCause: {}", SDL_GetError());
std::terminate();
}
auto *window =
SDL_CreateWindow("HansTheGatherer", WINDOW_WIDTH, WINDOW_HEIGHT, 0);
if (window == nullptr) {
spdlog::critical("Failed to create SDL window!\nCause: {}", SDL_GetError());
}
auto *renderer = SDL_CreateRenderer(window, nullptr);
if (renderer == nullptr) {
spdlog::critical("Failed to create SDL renderer!\nCause: {}",
SDL_GetError());
}
flecs::world world;
world.set<ButtonInput>(ButtonInput{});
world.set<SdlHandles>(SdlHandles{.window = window, .renderer = renderer});
init_assets(world);
auto *texture_assets = world.get<TextureAssets>();
world.entity("Background")
.set<Position>(Position{.x = 0, .y = 0})
.set<Sprite>(
Sprite{.texture = &texture_assets->background,
.texture_atlas_index = 0,
.custom_size = Vec2{.x = WINDOW_WIDTH, .y = WINDOW_HEIGHT}});
struct Fruit {};
world.entity("Sprite1")
.set<Position>(Position{.x = 0, .y = 0})
.set<Sprite>(
Sprite{.texture = &texture_assets->fruits, .texture_atlas_index = 0})
.add<Fruit>();
world.entity("Sprite2")
.set<Position>(Position{.x = 32, .y = 32})
.set<Sprite>(
Sprite{.texture = &texture_assets->fruits, .texture_atlas_index = 5});
world.system<SdlHandles const, Position const, Sprite const>("RenderSprites")
.term_at(0)
.singleton()
.each([](SdlHandles const &sdl_handles, Position const &pos,
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)};
Vec2 size = sprite.custom_size.value_or(
{.x = layout.width, .y = layout.height});
SDL_FRect dstrect{static_cast<float>(pos.x), static_cast<float>(pos.y),
static_cast<float>(size.x),
static_cast<float>(size.y)};
SDL_RenderTexture(sdl_handles.renderer, sprite.texture->sdl_texture,
&srcrect, &dstrect);
});
world.system<ButtonInput const, Position, Sprite const, Fruit>("MoveSprites")
.term_at(0)
.singleton()
.each([](ButtonInput const &input, Position &pos, Sprite const &sprite,
Fruit) {
if (input.pressed.contains(SDLK_LEFT)) {
pos.x -= 1;
}
if (input.pressed.contains(SDLK_RIGHT)) {
pos.x += 1;
}
if (input.pressed.contains(SDLK_UP)) {
pos.y -= 1;
}
if (input.pressed.contains(SDLK_DOWN)) {
pos.y += 1;
}
});
bool exit_gameloop = false;
while (!exit_gameloop) {
auto *input = world.get_mut<ButtonInput>();
// Clear just pressed/released
input->just_pressed.clear();
input->just_released.clear();
// Input
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT:
exit_gameloop = true;
break;
case SDL_EVENT_KEY_DOWN:
if (event.key.key == SDLK_ESCAPE) {
exit_gameloop = true;
}
if (input->pressed.insert(event.key.key).second) {
input->just_pressed.insert(event.key.key);
}
break;
case SDL_EVENT_KEY_UP:
if (input->pressed.erase(event.key.key) != 0) {
input->just_released.insert(event.key.key);
}
break;
}
}
// Game Logic
// Render
SDL_RenderClear(renderer);
world.progress();
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}