Making a game in zig with SDL2
Go to file
2025-01-07 16:34:47 -07:00
assets WIP decoupled rendering 2025-01-07 16:34:47 -07:00
src WIP decoupled rendering 2025-01-07 16:34:47 -07:00
.envrc First window rendered, sdl here I come 2024-12-30 09:26:12 -07:00
.fflinks Add changes to control the bmp image with key events, added chatgpt to links ;) 2024-12-31 11:15:03 -07:00
.gitignore First window rendered, sdl here I come 2024-12-30 09:26:12 -07:00
build.zig Added animation and color changing support, bouncing slime!! 2025-01-02 17:20:39 -07:00
build.zig.zon Added scaling and ready to start with sdl_image 2025-01-02 08:28:46 -07:00
flake.lock Updated flake, removed seperate ZLS install and am just installing from nixpkgs now 2025-01-02 10:45:48 -07:00
flake.nix Updated flake, removed seperate ZLS install and am just installing from nixpkgs now 2025-01-02 10:45:48 -07:00
README.md WIP decoupled rendering 2025-01-07 16:34:47 -07:00

Physics and Timing

I want to have decoupled rendering with the ability to render partial physics simulations, so it needs to be deterministic.

Based on this snippet from this article

double t = 0.0;
double dt = 0.01;

double currentTime = hires_time_in_seconds();
double accumulator = 0.0;

State previous;
State current;

while ( !quit )
{
    double newTime = time();
    double frameTime = newTime - currentTime;
    if ( frameTime > 0.25 )
        frameTime = 0.25;
    currentTime = newTime;

    accumulator += frameTime;

    while ( accumulator >= dt )
    {
        previousState = currentState;
        integrate( currentState, t, dt );
        t += dt;
        accumulator -= dt;
    }

    const double alpha = accumulator / dt;

    State state = currentState * alpha + 
        previousState * ( 1.0 - alpha );

    render( state );
}

or from this medium article

while(running){
    computeDeltaTimeSomehow();
    accumulator += deltaTime;
    while(accumulator >= 1.0/60.0){
        previous_state = current_state;
        current_state = update();
        accumulator -= 1.0/60.0;
    }
    render_interpolated_somehow(previous_state, current_state, accumulator/(1.0/60.0));
    display();
}