Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / src/core/game.cpp

sdl_runtime_compiler git · main

SDL3 game for running and compiling code at runtime

sdl3 c++ compiler dlopen cmake · first commit 2026-04-19 · last commit 2026-07-03 (3 months ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-runtime-compiler

C++ 72.3% C 26.2%
git clone https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler.gitwget https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler/archive/sdl_runtime_compiler.tar.gz
src/core/game.cpp 1.9 KB · 89 lines raw
#include <filesystem>
#include <system_error>

#include "entities/world.hpp"
#include "game.hpp"
#include "states/game_state.hpp"
#include "states/istate.hpp"
#include "states/menu_state.hpp"
#include "systems/input_system.hpp"
#include "systems/physics_system.hpp"
#include "utils/config.hpp"

namespace
Nimbin
{

Game::~Game() = default;


bool
Game::init()
{
    std::error_code ec;
    std::filesystem::create_directories("/tmp/game_mod_include", ec);
    std::filesystem::copy_file         ("src/systems/game_mod_abi.h",
                                        "/tmp/game_mod_include/game_mod_abi.h",
                                        std::filesystem::copy_options::overwrite_existing, ec);
    if (ec) SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
                        "Could not stage game_mod_abi.h: %s", ec.message().c_str());

    _gl_renderer = cpputils::make_unique<GlRenderer>();
    _gl_renderer->pre_Config();

    _window = SDL_CreateWindow("game", 640, 480, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
    if (!_window) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindow failed: %s", SDL_GetError());
        return false;
    }

    if (!_gl_renderer->init(_window))
        return false;

    SDL_SetWindowRelativeMouseMode(_window, true);
    SDL_StartTextInput            (_window);

    change_State(cpputils::make_unique<MenuState>());
    return true;
}


void
Game::change_State(cpputils::unique_ptr<IState> next)
{
    if (_state) _state->on_Exit(*this);

    _state = std::move(next);

    if (_state) _state->on_Enter(*this);
}


void
Game::handle_Event(const SDL_Event& event)
{
    if (_state) _state->handle_Event(*this, event);
}


void
Game::update(double dt_ns)
{
    if (_state) _state->update(dt_ns * 1e-9, _gl_renderer.get());
}


void
Game::render()
{
    if (_state) _state->render(*_window, *_gl_renderer);
}


void
Game::shutdown()
{
    SDL_Log("Power off!");
}

} // namespace Nimbin