Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / src/text/token.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/text/token.hpp 5.8 KB · 227 lines raw
#pragma once

#include <SDL3/SDL.h>
#include <cstdint>
#include <string>

#include "entities/game_object.hpp"
#include "text/text.hpp"
#include "text/mesh.hpp"
#include "utils/config.hpp"
#include "utils/globals.hpp"
#include "utils/light.hpp"

namespace
Nimbin
{

class Camera;
class GlRenderer;

namespace
Text
{


namespace
Token
{
    struct Ai;
    struct Data;
    struct Description;

    enum class Type : uint8_t
    {
        Delimiter,
        Identifier,
        Keyword,
        Literal,
        Operator,
        Preprocessor,
        Type,
        Function,
        Variable,
        Comment,
        StringLit,
        NumberLit,
        Namespace,
        Whitespace,
    };

    // token_db.cpp START
    inline const std::string font_path    = "assets/fonts/DejaVuSansMono.ttf";

    inline constexpr float depth_wall     = 0.06f;
    inline constexpr float depth_floor    = 0.14f;
    inline constexpr float depth_compiler = 0.02f;

    const char* get_TypeName   (Type type);
    SDL_FColor  get_Color      (Type t);
    Description get_Description(const std::string& text, Type type);
    DynArray<std::string>
                get_TestArrayAllKeywords();
    // token_db.cpp END

} // namespace Token::Data



template<typename... Args>
TokenPtr make_Token(Args&&... args)
{
    return cpputils::make_unique<Token::Data>(std::forward<Args>(args)...);
}

} // namespace Text



namespace
Text
{


namespace
Token
{

    struct Description
    {
        std::string short_desc;
        std::string long_desc;
    };

    struct Ai
    {
        enum class State : uint8_t { Idle, Attack, Flee, Stunned };

        bool   in_pause           = false;
        bool   has_stolen         = false;
        State  state              = State::Idle;
        double state_timer        = 0.0;
        double aggressiveness     = 0.0;
        double phase_timer        = 0.0;
        double wiggle_phase       = 0.0;
        double wiggle_freq        = 4.0;
        double speed              = 2.0;

        double hover_height_saved = 0.0;
        double orbit_angle        = 0.0;
        double orbit_dir          = 1.0;

        DynArray<cpputils::unique_ptr<Data>> stolen;
        Vec3 trail_dir { 1.0, 0.0, 0.0 };
    };

    struct Data final : public GameObject
    {
        std::string text;
        SDL_FColor  color;
        SDL_FColor  single_color  {};
        SDL_FColor  context_color {};
        bool        has_context { false };
        bool        is_space    { false };
        bool        is_tab      { false };
        TextMesh    mesh;

        bool is_Whitespace() const { return is_space || is_tab; }

        ColorSource color_src { ColorSource::Lint };

        float depth;
        float scale;
        float half_extent   { 0.0f };
        float half_extent_y { 0.2f };
        float pitch         { 0.0f };

        bool  loaded        { false };
        bool  front_tint    { true };
        bool  glow_enabled  { false };
        float glow_strength { 0.2f };
        float back_dim      { 0.45f };
        Vec3  phrase_offset {};   // projectile layout scratch: word's offset within a
                                  // thrown phrase (proj right/up frame), then rebased
                                  // in-place to the absolute wall stick-target. z unused.


        size_t byte_begin { 0 };
        size_t byte_end   { 0 };

        Vec3 jiggle_off {};
        Vec3 jiggle_vel {};


        Ai ai;


        mutable bool         gpu_dirty       { true };
        mutable int          gpu_index_count { 0 };
        mutable unsigned int gpu_vbo         { 0 };
        mutable unsigned int gpu_ebo         { 0 };
        mutable unsigned int gpu_vbo_sparkle { 0 };
        mutable unsigned int gpu_ebo_sparkle { 0 };
        mutable int          gpu_sparkle_index_count { 0 };

        static constexpr int
                       SPARKLE_TRAIL       = 14;
        mutable int    sparkle_trail_count { 0 };
        mutable Uint64 sparkle_last_ms     { 0 };
        mutable Vec3   sparkle_trail[SPARKLE_TRAIL];

        DynArray<int>   packed_indices;
        DynArray<float> packed_verts;


        Data (const std::string& txt, float depth, float scale,
              Vec3  position, SDL_FColor colr = Config::Color::blue);
        ~Data() override;


        void set_Color      (SDL_FColor c);
        void set_Context    (SDL_FColor c);
        void clear_Context  ();
        void set_Look       (SDL_FColor c, float new_depth);
        void set_ColorSource(ColorSource src) { color_src = src; }
        void recolor        ();
        void retext         (const std::string& s);

        SDL_FColor get_ActiveColor() const {
            switch (color_src) {
                case ColorSource::Static: return color;
                case ColorSource::Lexed:  return has_context ? context_color : single_color;
                case ColorSource::Lint:   return single_color;
            }
            return single_color;
        }

        void set_Depth   (float depth);
        void rebuild_Mesh();


        static Vec3 words_centroid(const DynArray<cpputils::unique_ptr<Data>>& words);
        Vec3 get_Distance(Vec3 position) const;
        void update      (const Vec3& camera_pos, const double view_mat[9], float carry_dist = 1.5f);
        void draw        (GlRenderer& gl, const Camera& camera, const Light& light) const override;
    };




    struct SpikeTimer {
        const char* name; Uint64 t0;
        SpikeTimer(const char* n) : name(n), t0(SDL_GetPerformanceCounter()) {}
        ~SpikeTimer() {
            double ms = 1000.0 * double(SDL_GetPerformanceCounter() - t0)
                               / double(SDL_GetPerformanceFrequency());
            if (ms > 1.0) SDL_Log("SPIKE %-24s %.2f ms", name, ms);
        }
    };
#define SPIKE(n) Text::Token::SpikeTimer _spk_##__LINE__(n)


} // namespace Token::Data


} // namespace Text

} // namespace Nimbin