Nimbin2git.christianimmanuel.de / Games / strichmaennchen / test_opengl.c

strichmaennchen git · main

git clone https://git.christianimmanuel.de/games/strichmaennchen.gitwget https://git.christianimmanuel.de/games/strichmaennchen/archive/strichmaennchen.tar.gz
test_opengl.c 3.3 KB · 114 lines raw
#include <SDL3/SDL.h>
#include <stdbool.h>
#include <GL/gl.h>
#include "renderer.h"
#include "linalg.h"

// Window
SDL_Window *window = NULL;
SDL_GLContext gl_context = NULL;
int windowWidth = 800;
int windowHeight = 600;

// Camera
Vec3 cameraPos   = {0.0f, 0.0f, 0.0f};
Vec3 cameraTarget= {0.0f, 0.0f, -1.0f};
Vec3 cameraUp    = {0.0f, 1.0f, 0.0f};

// Placeholder input functions
void processInput(bool *running, float *move_forward, float *move_strafe, float *yaw_delta, float *pitch_delta) {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_EVENT_QUIT) *running = false;
    }
    *move_forward = 0.0f;
    *move_strafe = 0.0f;
    *yaw_delta = 0.0f;
    *pitch_delta = 0.0f;
}

// Placeholder camera update
void updateCamera(float dt, float move_forward, float move_strafe, float yaw_delta, float pitch_delta) {
    // Simple forward/backward along z-axis for testing
    cameraPos.z += move_forward * dt;
    cameraTarget.z += move_forward * dt;
}

int main(void) {
    if (!SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("SDL_Init failed: %s", SDL_GetError());
        return 1;
    }

    window = SDL_CreateWindow("SDL3 OpenGL Test",
        windowWidth, windowHeight, SDL_WINDOW_OPENGL);

    if (!window) {
        SDL_Log("Window creation failed: %s", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    gl_context = SDL_GL_CreateContext(window);
    if (!gl_context) {
        SDL_Log("GL context creation failed: %s", SDL_GetError());
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    if (!Renderer_init()) {
        SDL_Log("Renderer init failed");
        SDL_GL_DestroyContext(gl_context);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    bool running = true;
    Uint64 prev_ticks = SDL_GetTicks();
    while (running) {
        Uint64 now = SDL_GetTicks();
        float dt = (now - prev_ticks) / 1000.0f;
        prev_ticks = now;

        float move_forward = 0.0f;
        float move_strafe = 0.0f;
        float yaw_delta = 0.0f;
        float pitch_delta = 0.0f;

        processInput(&running, &move_forward, &move_strafe, &yaw_delta, &pitch_delta);
        updateCamera(dt, move_forward, move_strafe, yaw_delta, pitch_delta);

        // Clear screen
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Compute MVP
        Mat4 model      = mat4_translate(vec3(0.0f, 0.0f, -3.0f));
        Mat4 view       = mat4_lookAt(cameraPos, cameraTarget, cameraUp);
        Mat4 projection = mat4_perspective(70.0f, windowWidth/(float)windowHeight, 0.1f, 100.0f);
        //Mat4 mvp        = mat4_mul(projection, mat4_mul(view, model));
        Mat4 mvp = mat4_translate(vec3(0.0f, 0.0f, -0.5f));



        // Draw triangle
        Renderer_drawTriangle(mvp);

        SDL_GL_SwapWindow(window);
    }

    Renderer_shutdown();
    SDL_GL_DestroyContext(gl_context);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}