Simple SDL game loop

This commit is contained in:
2025-05-24 13:37:23 +02:00
parent 2cd1d4c0bf
commit fb1cc8b452
2 changed files with 50 additions and 4 deletions

View File

@@ -5,9 +5,10 @@ project(HansTheGatherer)
# Option to switch real platform vs. SDL implementation...
find_package(SDL3 REQUIRED)
find_package(spdlog REQUIRED)
add_executable(HansTheGatherer main.cpp)
target_link_libraries(HansTheGatherer SDL3::SDL3)
target_link_libraries(HansTheGatherer SDL3::SDL3 spdlog::spdlog)
set_property(TARGET HansTheGatherer PROPERTY CXX_STANDARD 23)
set_property(TARGET HansTheGatherer PROPERTY CXX_STANDARD 20)

View File

@@ -1,7 +1,52 @@
#include <print>
#include <SDL3/SDL.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_render.h>
#include <spdlog/spdlog.h>
const uint8_t image_data[] =
{
#embed "main.cpp"
};
auto main() -> int {
std::println("Hello World!");
spdlog::info("Initialize SDL...");
bool sdl_success = SDL_Init(SDL_INIT_VIDEO);
if (!sdl_success) {
spdlog::critical("Failed to initialize SDL!\nCause: {}", SDL_GetError());
std::terminate();
}
auto *sdl_window = SDL_CreateWindow("HansTheGatherer", 400, 280, 0);
if (sdl_window == nullptr) {
spdlog::critical("Failed to create SDL window!\nCause: {}", SDL_GetError());
}
auto *sdl_renderer = SDL_CreateRenderer(sdl_window, nullptr);
if (sdl_renderer == nullptr) {
spdlog::critical("Failed to create SDL renderer!\nCause: {}",
SDL_GetError());
}
bool exit_gameloop = false;
while (!exit_gameloop) {
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;
}
break;
}
}
}
SDL_DestroyRenderer(sdl_renderer);
SDL_DestroyWindow(sdl_window);
SDL_Quit();
return 0;
}