Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / src/entities/pult.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/entities/pult.cpp 2.7 KB · 102 lines raw
#include "entities/camera.hpp"
#include "pult.hpp"
#include "rendering/gl_renderer.hpp"

namespace
Nimbin
{

///////////////////////////
// PultButton

PultButton::PultButton(const std::string& lbl, PultAction act, Vec3 world_pos)
           : SolidBox(
                 world_pos,
                 Vec3      { 0.30, 0.10, 0.30 },
                 SDL_FColor{ 0.35f, 0.75f, 0.35f, 1.0f },
                 SDL_FColor{ 0.55f, 0.95f, 0.55f, 1.0f },
                 true)
           , label(lbl), action(act)
{}


void
PultButton::press()
{ pressed = true; press_timer = Config::Pult::press_dur; }


void
PultButton::tick(double dt)
{ if (pressed) { press_timer -= dt; if (press_timer <= 0.0) pressed = false; } }


void
PultButton::draw(GlRenderer& gl, const Camera& cam, const Light& light) const
{
    Mat4 view  = buildViewMat4    (cam);
    Mat4 model = buildModelMat4   (pos, yaw);
    Mat4 proj  = gl.get_Projection();
    Mat4 mvp   = Mat4::mul        (proj, Mat4::mul(view, model));

    const float hx = static_cast<float>(shape.half_extents.x);
    const float hy = static_cast<float>(shape.half_extents.y);
    const float hz = static_cast<float>(shape.half_extents.z);

    gl.set_Wireframe(false);
    gl.set_MVP      (model, mvp);
    gl.set_NormalMat(model);
    gl.set_Light    (light);
    gl.set_Color    (pressed ? 1.0f : 0.35f,
                     pressed ? 1.0f : 0.75f,
                     pressed ? 0.2f : 0.35f);
    gl.draw_Box     (hx, hy, hz);
}



///////////////////////////
// Pult

Pult::Pult(const std::string& n, Vec3 world_pos,
           float half_width, float half_depth)
     : SolidBox(
           world_pos,
           Vec3{ half_width, H, half_depth },
           SDL_FColor{ 0.45f, 0.45f, 0.65f, 1.0f },
           SDL_FColor{ 0.70f, 0.70f, 0.90f, 1.0f },
           false)
     , name(n), half_w(half_width), half_d(half_depth)
{
    label.set_FaceCamera (true);
    label.set_Style     (Config::Color::teal, Text::Token::depth_wall, 0.0025f);
    label.set_Collidable(true);
    label.attach_To     (&pos, Vec3{ 0.0, Config::Pult::label_height, 0.0 });
    label.set_Text      (n);
}


void
Pult::tick(double dt)
{ for (PultButton* b : buttons) b->tick(dt); }


void
Pult::draw(GlRenderer& gl, const Camera& cam, const Light& light) const
{
    Mat4 view  = buildViewMat4    (cam);
    Mat4 model = buildModelMat4   (pos, yaw);
    Mat4 proj  = gl.get_Projection();
    Mat4 mvp   = Mat4::mul        (proj, Mat4::mul(view, model));

    gl.set_Wireframe(false);
    gl.set_MVP      (model, mvp);
    gl.set_NormalMat(model);
    gl.set_Light    (light);
    gl.set_Color    (0.45f, 0.45f, 0.65f);
    gl.draw_Box     (half_w, H, half_d);

    for (const PultButton* b : buttons)
        b->draw(gl, cam, light);
}

} // namespace Nimbin