Split up main.cpp into multiple files
This commit is contained in:
@@ -9,7 +9,7 @@ find_package(SDL3_image CONFIG REQUIRED)
|
|||||||
find_package(flecs CONFIG REQUIRED)
|
find_package(flecs CONFIG REQUIRED)
|
||||||
find_package(spdlog 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)
|
target_link_libraries(HansTheGatherer SDL3::SDL3 SDL3_image::SDL3_image flecs::flecs spdlog::spdlog)
|
||||||
|
|
||||||
|
|||||||
28
assets.cpp
Normal file
28
assets.cpp
Normal 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
11
assets.hpp
Normal 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
BIN
fruits.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
|
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 41 KiB |
11
input.hpp
Normal file
11
input.hpp
Normal 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;
|
||||||
|
};
|
||||||
|
|
||||||
42
main.cpp
42
main.cpp
@@ -1,51 +1,25 @@
|
|||||||
|
#include "assets.hpp"
|
||||||
|
#include "sdl_types.hpp"
|
||||||
|
#include "input.hpp"
|
||||||
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <SDL3/SDL_error.h>
|
#include <SDL3/SDL_error.h>
|
||||||
#include <SDL3/SDL_events.h>
|
#include <SDL3/SDL_events.h>
|
||||||
#include <SDL3/SDL_iostream.h>
|
#include <SDL3/SDL_iostream.h>
|
||||||
#include <SDL3/SDL_render.h>
|
#include <SDL3/SDL_render.h>
|
||||||
#include <SDL3/SDL_video.h>
|
#include <SDL3/SDL_video.h>
|
||||||
#include <SDL3_image/SDL_image.h>
|
|
||||||
#include <flecs.h>
|
#include <flecs.h>
|
||||||
#include <set>
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
const uint8_t background_data[] = {
|
|
||||||
#embed "jungle.jpg"
|
|
||||||
};
|
|
||||||
|
|
||||||
static constexpr int WINDOW_WIDTH = 400;
|
static constexpr int WINDOW_WIDTH = 400;
|
||||||
static constexpr int WINDOW_HEIGHT = 280;
|
static constexpr int WINDOW_HEIGHT = 280;
|
||||||
|
|
||||||
struct SpriteAssets {
|
struct Position {
|
||||||
SDL_Texture *background;
|
int x;
|
||||||
|
int y;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SdlHandles {
|
int main() {
|
||||||
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 {
|
|
||||||
spdlog::info("Initialize SDL...");
|
spdlog::info("Initialize SDL...");
|
||||||
|
|
||||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
|
|||||||
9
sdl_types.hpp
Normal file
9
sdl_types.hpp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
struct SdlHandles {
|
||||||
|
SDL_Window *window;
|
||||||
|
SDL_Renderer *renderer;
|
||||||
|
};
|
||||||
|
|
||||||
Reference in New Issue
Block a user