Split up main.cpp into multiple files

This commit is contained in:
2025-05-24 20:29:56 +02:00
parent 3bc879a5ab
commit 7fc0c47c69
8 changed files with 68 additions and 35 deletions

View File

@@ -9,7 +9,7 @@ find_package(SDL3_image CONFIG REQUIRED)
find_package(flecs CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
add_executable(HansTheGatherer main.cpp)
add_executable(HansTheGatherer main.cpp assets.cpp)
target_link_libraries(HansTheGatherer SDL3::SDL3 SDL3_image::SDL3_image flecs::flecs spdlog::spdlog)

28
assets.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include "assets.hpp"
#include "sdl_types.hpp"
#include <SDL3_image/SDL_image.h>
#include <cstdint>
#include <spdlog/spdlog.h>
static constexpr uint8_t BACKGROUND_DATA[] = {
#embed "jungle.jpg"
};
static constexpr uint8_t FRUITS_DATA[] = {
#embed "fruits.jpg"
};
void init_assets(flecs::world &world) {
auto *renderer = world.get<SdlHandles>()->renderer;
auto *background_iostream =
SDL_IOFromConstMem(BACKGROUND_DATA, sizeof(BACKGROUND_DATA));
SDL_Texture *background =
IMG_LoadTexture_IO(renderer, background_iostream, false);
if (background == nullptr) {
spdlog::error("Failed to load SDL texture!\nCause: {}", SDL_GetError());
}
world.set<SpriteAssets>(SpriteAssets{.background = background});
}

11
assets.hpp Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include <SDL3/SDL.h>
#include <flecs.h>
struct SpriteAssets {
SDL_Texture *background;
SDL_Texture *fruits;
};
void init_assets(flecs::world &world);

BIN
fruits.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

View File

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 41 KiB

11
input.hpp Normal file
View File

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

View File

@@ -1,51 +1,25 @@
#include "assets.hpp"
#include "sdl_types.hpp"
#include "input.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 <SDL3_image/SDL_image.h>
#include <flecs.h>
#include <set>
#include <spdlog/spdlog.h>
const uint8_t background_data[] = {
#embed "jungle.jpg"
};
static constexpr int WINDOW_WIDTH = 400;
static constexpr int WINDOW_HEIGHT = 280;
struct SpriteAssets {
SDL_Texture *background;
struct Position {
int x;
int y;
};
struct SdlHandles {
SDL_Window *window;
SDL_Renderer *renderer;
};
struct ButtonInput {
std::set<SDL_Keycode> pressed;
std::set<SDL_Keycode> just_pressed;
std::set<SDL_Keycode> just_released;
};
auto init_assets(flecs::world &world) -> void {
auto *renderer = world.get<SdlHandles>()->renderer;
auto *background_iostream =
SDL_IOFromConstMem(background_data, sizeof(background_data));
SDL_Texture *background =
IMG_LoadTexture_IO(renderer, background_iostream, false);
if (background == nullptr) {
spdlog::error("Failed to load SDL texture!\nCause: {}", SDL_GetError());
}
world.set<SpriteAssets>(SpriteAssets{.background = background});
}
auto main() -> int {
int main() {
spdlog::info("Initialize SDL...");
if (!SDL_Init(SDL_INIT_VIDEO)) {

9
sdl_types.hpp Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include <SDL3/SDL.h>
struct SdlHandles {
SDL_Window *window;
SDL_Renderer *renderer;
};