Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / src/states/game_state.hpp

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/states/game_state.hpp 6.1 KB · 185 lines raw
#pragma once

#include "cpputils/unique_ptr.hpp"
#include "entities/plate.hpp"
#include "istate.hpp"
#include "levels/director.hpp"
#include "levels/goal.hpp"
#include "levels/scene.hpp"
#include "states/game_context.hpp"
#include "systems/carry_system.hpp"
#include "systems/celebration_system.hpp"
#include "systems/compiler_system.hpp"
#include "systems/cpp_highlight_system.hpp"
#include "systems/dyn_loader_system.hpp"
#include "systems/freelook_system.hpp"
#include "systems/hint_system.hpp"
#include "systems/input_system.hpp"
#include "systems/particle_system.hpp"
#include "systems/physics_system.hpp"
#include "systems/plate_system.hpp"
#include "systems/program_system.hpp"
#include "systems/projectile_system.hpp"
#include "systems/pult_system.hpp"
#include "systems/wall_connection.hpp"
#include "systems/word_ai_system.hpp"
#include "systems/word_anim_system.hpp"
#include "text/handler.hpp"
#include "text/vim_buffer.hpp"
#include "utils/globals.hpp"
#include "wall/label.hpp"
#include "wall/wall.hpp"

namespace
Nimbin
{

class GameState : public IState
{
    Text::Handler      _wall_editor;
    Text::Handler      _wall_program;
    Text::Handler      _wall_compiler;
    Text::Handler      _wall_info;
    Wall::Connection   _wall_connection;
    CarriedLine        _carried;
    FrameInput         _frame_input;
    GameContext        _ctx;

    DynArray<Text::Token::Data*>
                             _typing_words;
    DynArray<GameObject*>    _dyn_scenery;
    DynArray<Vec3>           _freelook_return_from;
    DynArray<Projectile>     _projectiles;
    DynArray<Wall::Label>    _wall_labels;
    DynArray<WordAnim_Add>   _anims_add;
    DynArray<WordAnim_Drop>  _anims_drop;
    DynArray<Text::Handler*> _text_walls;

    std::string        _typing_last;

    cpputils::unique_ptr<InputSystem>        _input_sys;
    cpputils::unique_ptr<FreelookSystem>     _freelook_sys;

    cpputils::unique_ptr<CompilerSystem>     _compiler_sys;
    cpputils::unique_ptr<ProgramSystem>      _program_sys;
    cpputils::unique_ptr<DynLoaderSystem>    _dyn_loader_sys;


    cpputils::unique_ptr<HintSystem>         _hint_sys;
    cpputils::unique_ptr<Level::Director>    _level_director;
    cpputils::unique_ptr<CelebrationSystem>  _celebration_sys;
    cpputils::unique_ptr<PlateSystem>        _plate_sys;
    cpputils::unique_ptr<PultSystem>         _pult_sys;

    cpputils::unique_ptr<ParticleSystem>     _particle_sys;
    cpputils::unique_ptr<PhysicsSystem>      _physics_sys;
    cpputils::unique_ptr<ProjectileSystem>   _projectiles_sys;
    cpputils::unique_ptr<CppHighlightSystem> _highlight_sys;
    cpputils::unique_ptr<WordAnimSystem>     _word_anim_sys;
    cpputils::unique_ptr<WordAiSystem>       _word_ai_sys;

    size_t _carried_last_size { 0 };
    double _pick_cooldown     { 0.0 };
    double _shoot_cooldown    { 0.0 };

    // editor-token projectile-hit id range (line numbers use Text::Handler::HIT_BASE)
    static constexpr size_t WALL_TOK_BASE  = size_t(1) << 40;
    static constexpr size_t LABEL_HIT_BASE = size_t(1) << 48;

    // focus wall jump START
    Vec3   _focus_center    {};
    Vec3   _focus_normal    {};
    Vec3   _focus_label_pos {};
    double _focus_t       { 0.0 };
    double _focus_dist    { 4.0 };
    double _focus_yaw_r   { 0.0 };
    float  _fov_y         { 0.0f };
    float  _aspect        { 0.0f };
    float  _focus_label_yaw  { 0.0f };
    Text::TokenPtr
           _focus_label;
    bool   _focus_active  { false };
    bool   _focus_locked  { false };
    bool   _focus_on      { false };
    bool           _focus_label_init { false };
    Wall::Role
           _focus_role    { Wall::Role::Editor };
    // focus wall jump END

    void wallconnect_BeginEditor ();
    void wallconnect_BeginProgram();

    std::string get_EditorSource() const;


    bool on_Pick         ();
    bool on_ShootOne     ();
    void on_Click        ();
    void on_Compile      ();
    void on_DebugPrint   ();
    void on_FocusToggle  ();
    void on_DropLast     ();
    void on_FreelookEnd  ();
    void on_FreelookStart();
    void on_Jump         ();
    void on_NumberAdjust (int dt);
    void on_Run          ();
    void on_ShootAll     ();
    void on_TypingCancel (FrameInput&);
    void on_TypingCommit (FrameInput&);
    void on_TypingStart  (FrameInput&);
    void on_EditorScroll (int8_t dt);
    void on_EditorClear  ();
    void on_SandboxRestore   ();
    void on_WallConnectExit  ();
    void on_WallConnectSubmit();

    void carriedline_Update    ();
    void carry_BeginAnim       (Text::TokenPtr word, bool with_pitch);
    void level_TryAdvance      (double dt);
    void typing_ClearPreview   ();
    void typing_RebuildPreview (const std::string& s);


    void projectile_LandOnProgramInput(ProjectileResult&);
    void projectile_LandOnFloor       (ProjectileResult&);
    void projectile_LandOnWall        (ProjectileResult&);
    void projectile_LandOnWallText    (ProjectileResult&);

    void pult_RunAction     (PultAction act);

    void debug_SpawnWords   (size_t n);

    void update_Input       (FrameInput& input, double dt);
    void update_Physics     (double dt);
    void update_Objects     (double dt, const GlRenderer* gl);
    void update_Compiler    ();
    void update_Highlighting();
    void update_Progression (double dt);

    // dynamic-lib sandbox START
    void dynlib_Clear                ();
    void dynlib_Apply                (const DynArray<DynObject>& objs);
    void sandbox_LoadFileToWall      ();
    std::string sandbox_GetSourcePath() const;
    std::string sandbox_BackupPath   () const;
    // dynamic-lib sandbox END

    // focus wall jump START
    bool focus_ComputeTarget();
    void focus_UpdateWall   (FrameInput& input, double dt);
    void focus_UpdateLabel  (double dt, const Camera& cam, float aspect, float fov_y);
    // focus wall jump END

    Text::Handler*
         get_Handler(Wall::Role r);

public:
    explicit GameState    (size_t starting_level = 0);
            ~GameState    () = default;
    void     handle_Event (Game&, const SDL_Event&)      override;
    void     update       (double dt, const GlRenderer*) override;
    void     render       (SDL_Window&, GlRenderer&)     override;
};

} // namespace Nimbin