Nimbin[12]?SDL & Graphics / sdl3_3d_oop_basic_game / src/entities/world.cpp

sdl3_3d_oop_basic_game git · main

SDL3 3D OOP basic game (entities, systems, CMake)

sdl3 3d c++ game cmake · first commit 2026-04-19 · last commit 2026-04-19 (7 months ago) · synced 1 hour ago · upstream: git.ide3.de/chfis005/sdl3_3d_oop_basic_game

C++ 99%
git clone https://git.christianimmanuel.de/sdl-graphics/sdl3_3d_oop_basic_game.gitwget https://git.christianimmanuel.de/sdl-graphics/sdl3_3d_oop_basic_game/archive/sdl3_3d_oop_basic_game.tar.gz
src/entities/world.cpp 5 KB · 195 lines raw
#include "entities/world.hpp"

World::World()
{
    initEdges();
}

void World::initEdges()
{
    const float r = world_scale;

    const int map[24] = {
        0,1 , 1,3 , 3,2 , 2,0 ,
        7,6 , 6,4 , 4,5 , 5,7 ,
        6,2 , 3,7 , 0,4 , 5,1
    };

    for (size_t i = 0; i < 12; i++) {
        Vec3 a, b;

        for (int j = 0; j < 3; j++) {
            float va = (map[i*2+0] & (1 << j)) ? r : -r;
            float vb = (map[i*2+1] & (1 << j)) ? r : -r;

            if (j == 0) { a.x = va; b.x = vb; }
            if (j == 1) { a.y = va; b.y = vb; }
            if (j == 2) { a.z = va; b.z = vb; }
        }

        edges[i] = { a, b };
    }

    size_t scale = static_cast<size_t>(world_scale);
    for (size_t i = 0; i < scale; i++) {
        float d = static_cast<float>(i * 2) - world_scale;

        // line z axis
        edges[12 + i] = {
            { -r, -r, d },
            {  r, -r, d }
        };

        // line x axis
        edges[12 + scale + i] = {
            { d, -r, -r },
            { d, -r,  r }
        };
    }
}


static double sweepAxis(double end, double min, double max, bool& hit)
{
    hit = false;

    if (end < min) {
        hit = true;
        return min;
    }

    if (end > max) {
        hit = true;
        return max;
    }

    return end;
}

PlayerCollision World::resolvePlayer(const Player& player) const
{
    PlayerCollision out;
    out.vel = player.vel;

    const double min = -world_scale + player.radius;
    const double max =  world_scale - player.radius;

    const double minY = player.height - world_scale;
    const double maxY = world_scale;

    out.pos.x = sweepAxis(player.pos.x, min,  max,  out.hit_x);
    out.pos.y = sweepAxis(player.pos.y, minY, maxY, out.hit_y);
    out.pos.z = sweepAxis(player.pos.z, min,  max,  out.hit_z);

    if (out.hit_x) out.vel.x = 0;
    if (out.hit_y) out.vel.y = 0;
    if (out.hit_z) out.vel.z = 0;

    return out;
}


static void drawClippedSegment(
    SDL_Renderer *renderer,
    float ax, float ay, float az,
    float bx, float by, float bz,
    float x, float y, float z, float w)
{
    if (az >= -w && bz >= -w) return;

    float dx = ax - bx;
    float dy = ay - by;

    if (az > -w) {
        float t = (-w - bz) / (az - bz);
        ax = bx + dx * t;
        ay = by + dy * t;
        az = -w;
    } else if (bz > -w) {
        float t = (-w - az) / (bz - az);
        bx = ax - dx * t;
        by = ay - dy * t;
        bz = -w;
    }

    ax = -z * ax / az;
    ay = -z * ay / az;
    bx = -z * bx / bz;
    by = -z * by / bz;

    SDL_RenderLine(renderer, x + ax, y - ay, x + bx, y - by);
}

static Vec3 toCameraSpace(const Vec3& p, const double mat[9])
{
    Vec3 out;
    out.x = mat[0] * p.x + mat[1] * p.y + mat[2] * p.z;
    out.y = mat[3] * p.x + mat[4] * p.y + mat[5] * p.z;
    out.z = mat[6] * p.x + mat[7] * p.y + mat[8] * p.z;
    return out;
}

void World::draw(SDL_Renderer* renderer, const Camera& camera) const
{
    int w, h;
    if (!SDL_GetRenderOutputSize(renderer, &w, &h)) {
        return;
    }
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(renderer);
    float wf = (float)w;
    float hf = (float)h;
    int part_hor = 1;
    int part_ver = 1;
    float size_hor = wf / (static_cast<float>(part_hor));
    float size_ver = hf / (static_cast<float>(part_ver));
    float hor_origin = size_hor * 0.5f;
    float ver_origin = size_ver * 0.5f;
    float cam_origin = 0.5f * static_cast<float>(SDL_sqrt(size_hor * size_hor + size_ver * size_ver));
    float hor_offset = 0.0f;
    float ver_offset = 0.0f;
    SDL_Rect rect;
    rect.x = static_cast<int>(hor_offset);
    rect.y = static_cast<int>(ver_offset);
    rect.w = static_cast<int>(size_hor);
    rect.h = static_cast<int>(size_ver);
    SDL_SetRenderClipRect(renderer, &rect);
    double x0 = camera.pos.x;
    double y0 = camera.pos.y;
    double z0 = camera.pos.z;

    double mat[9];
    buildViewMatrix(camera, mat);

    SDL_SetRenderDrawColor(renderer, 64, 64, 64, 255);

    for (size_t k = 0; k < MAP_BOX_EDGES_LEN; k++) {
        const Edge& e = edges[k];

        Vec3 a = { e.a.x - x0, e.a.y - y0, e.a.z - z0 };
        Vec3 b = { e.b.x - x0, e.b.y - y0, e.b.z - z0 };

        Vec3 ca = toCameraSpace(a, mat);
        Vec3 cb = toCameraSpace(b, mat);

        drawClippedSegment(
                renderer,
                static_cast<float>(ca.x), static_cast<float>(ca.y), static_cast<float>(ca.z),
                static_cast<float>(cb.x), static_cast<float>(cb.y), static_cast<float>(cb.z),
                hor_origin, ver_origin, cam_origin, 1
            );
    }

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    SDL_RenderLine(renderer, hor_origin, ver_origin-10, hor_origin, ver_origin+10);
    SDL_RenderLine(renderer, hor_origin-10, ver_origin, hor_origin+10, ver_origin);

    SDL_SetRenderClipRect(renderer, NULL);
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
}

Vec3 World::spawnPoint() const
{ return {0.0, 2.0, 0.0}; }

float World::scale() const
{ return world_scale; }