2

I make a scene with parallax effect. I can’t make the sprite move smoothly along the X axis. The problem is that regardless of whether I use SDL_RenderCopyF or SDL_RenderCopy, the sprite is drawn according to the pixel grid of the monitor when moving slowly, which is accompanied by a jitter effect, when there are many layers and everyone moves at different speeds.

My problem is like SDL2 Smooth texture(sprite) animation between points in time function But there was a version of SDL2 that did not support floating-point rendering, and the author had to patch it. Starting from version 2.0.10.0, SDL_RenderCopyF, SDL_FRect and other functions have been added to the SDL, with which you can implement subpixel moving sprites, but I can not get them to work in my scene.

There are 10 layers in FullHD resolution. Rendering occurs in the FullHD window.

Window and render initialization code:

void CreateWidnow()
{
    var windowFlags = Settings.Fullscreen ?
        SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN : Settings.Resizeble ? 
        SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE : SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN;
        
    renderWindow = SDL.SDL_CreateWindow(
        Settings.DebugMode ? $"{Settings.Title} [DEBUG]" : Settings.Title, SDL.SDL_WINDOWPOS_CENTERED, 
        SDL.SDL_WINDOWPOS_CENTERED,
        Settings.Resolution.Width,
        Settings.Resolution.Height,              
        windowFlags);
}

void CreateRenderer()
{
    if(Settings.VSinc) {
        renderer = SDL.SDL_CreateRenderer(
            renderWindow, -1, 
            SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC |
            SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED |
            SDL.SDL_RendererFlags.SDL_RENDERER_TARGETTEXTURE
        );
    }
    else
    {
        renderer = SDL.SDL_CreateRenderer(
            renderWindow, -1, 
            SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED |
            SDL.SDL_RendererFlags.SDL_RENDERER_TARGETTEXTURE
        );
    }   
}

The main cycle with the calculation of deltaTime:

void RenderLoop()
{
    bool cap = Settings.FPS > 0;
    var timer_fps = new Timer();
        
    ulong now_counter = SDL.SDL_GetPerformanceCounter();
    ulong last_counter = 0;
    double deltaTime = 0;
        
    while (renderLoop)
    {
        timer_fps.Start();
            
        last_counter = now_counter;
        now_counter = SDL.SDL_GetPerformanceCounter();
            
        deltaTime = (double)(now_counter - last_counter) / SDL.SDL_GetPerformanceFrequency();
            
        OnPreUpdate(deltaTime);
        OnUpdate(deltaTime);
        SDL.SDL_RenderPresent(renderer);
        OnPostUpdate(deltaTime);
            
        if( ( cap ) && ( timer_fps.GetTicks() < 1000 / Settings.FPS ) )
        {
            SDL.SDL_Delay( ( 1000 / Settings.FPS  ) - timer_fps.GetTicks() );
        }
            
    }
}

Sprite loading and rendering code:

public Sprite(string path)
{
    Path = path;
    Transform = new Transform(new Point(0, 0));
    sprite = Image.IMG_LoadTexture(Game.RenderContext, path);
    SDL.SDL_QueryTexture(sprite, out format, out access, out width, out height); // get the width and height of the texture

    draw_rect = new SDL.SDL_FRect();
    draw_rect.w = width;
    draw_rect.h = height;
    scr_rect.x = 0; scr_rect.y = 0; scr_rect.w = width; scr_rect.h = height; 
}

public Transform Transform
{
    get; set;
}

public void Draw()
{
    draw_rect.x = (float)Transform.Position.X;
    draw_rect.y = (float)Transform.Position.Y;
    SDL.SDL_RenderCopyExF(Game.RenderContext, sprite, ref scr_rect, ref draw_rect, Transform.Degrees, IntPtr.Zero, SDL.SDL_RendererFlip.SDL_FLIP_NONE);
}

Moving Function (Transform.Translate):

public void Translate(double x, double y)
{
    position.X += x;
    position.Y += y;
}

Parallax implementation function (move the sprites back to the right side when they leave the screen):

void layerDraw(Sprite l1, Sprite l2, double speed)
{
    l1.Transform.Translate(speed, 0);
    l1.Draw();


    l2.Transform.Translate(speed, 0);
    l2.Draw();

    if (l1.Transform.Position.X <= -Settings.Resolution.Width)
        l1.Transform.SetPosition(Settings.Resolution.Width + l2.Transform.Position.X, 0);
    if (l2.Transform.Position.X <= -Settings.Resolution.Width)
        l2.Transform.SetPosition(Settings.Resolution.Width + l1.Transform.Position.X, 0);  
    }

Parallax Rendering Function:

double speed_09 = -2.0, speed_08 = -4.0, speed_07 = -8.0, speed_06 = -16.0;
double speed_05 = -24.0, speed_04 = -32.0, speed_03 = -64.0, speed_02 = -96.0, speed_01 = -128.0;

protected override void Update(double deltaTime)
{
    background.Draw();
    layerDraw(forest_091, forest_092, speed_09 * deltaTime);
    layerDraw(forest_081, forest_082, speed_08 * deltaTime);
    layerDraw(forest_071, forest_072, speed_07 * deltaTime);
    layerDraw(forest_061, forest_062, speed_06 * deltaTime);
    layerDraw(particles051, particles052, speed_05 * deltaTime);
    layerDraw(forest_041, forest_042, speed_04 * deltaTime);
    layerDraw(particles_031, particles_032, speed_03 * deltaTime);
    layerDraw(bushes_021, bushes_022, speed_02 * deltaTime);
    layerDraw(mist_011, mist_012, speed_01 * deltaTime);
}

Outputting forest_091 coordinates to the console:

[11:31:07:32] (x,y): = (1902,7461329999965:0), deltaTime = 0,001391, speed = -0,002782
[11:31:07:32] (x:y): = (1902,7430913999965:0), deltaTime = 0,0015208, speed = -0,0030416
[11:31:07:32] (x:y): = (1902,7400399999965:0), deltaTime = 0,0015257, speed = -0,0030514
[11:31:07:32] (x:y): = (1902,7370409999965:0), deltaTime = 0,0014995, speed = -0,002999
[11:31:07:32] (x:y): = (1902,7339605999964:0), deltaTime = 0,0015402, speed = -0,0030804
[11:31:07:33] (x:y): = (1902,7300727999964:0), deltaTime = 0,0019439, speed = -0,0038878
[11:31:07:33] (x:y): = (1902,7271281999963:0), deltaTime = 0,0014723, speed = -0,0029446
[11:31:07:33] (x:y): = (1902,7241953999962:0), deltaTime = 0,0014664, speed = -0,0029328
[11:31:07:33] (x:y): = (1902,7212207999962:0), deltaTime = 0,0014873, speed = -0,0029746
[11:31:07:33] (x:y): = (1902,7181395999962:0), deltaTime = 0,0015406, speed = -0,0030812
[11:31:07:33] (x:y): = (1902,715346599996:0), deltaTime = 0,0013965, speed = -0,002793
[11:31:07:33] (x:y): = (1902,712221399996:0), deltaTime = 0,0015626, speed = -0,0031252
[11:31:07:34] (x:y): = (1902,709382799996:0), deltaTime = 0,0014193, speed = -0,0028386

Link to youtube with a demonstration of the problemы

I use nectcore3.1, SDL2-CS и SDL2 v2.0.10.0

UPD: log deltaTime calculations:

[08:48:34:25] now_counter = 1097310517365, last_counter = 1097310516099, now_counter-last_counter = 1266
[08:48:34:25] now_counter = 1097310519141, last_counter = 1097310517365, now_counter-last_counter = 1776
[08:48:34:25] now_counter = 1097310521406, last_counter = 1097310519141, now_counter-last_counter = 2265
[08:48:34:25] now_counter = 1097310532746, last_counter = 1097310521406, now_counter-last_counter = 11340
[08:48:34:25] now_counter = 1097310534069, last_counter = 1097310532746, now_counter-last_counter = 1323
[08:48:34:25] now_counter = 1097310535356, last_counter = 1097310534069, now_counter-last_counter = 1287
[08:48:34:25] now_counter = 1097310536628, last_counter = 1097310535356, now_counter-last_counter = 1272
[08:48:34:25] now_counter = 1097310537897, last_counter = 1097310536628, now_counter-last_counter = 1269
[08:48:34:25] now_counter = 1097310539169, last_counter = 1097310537897, now_counter-last_counter = 1272
[08:48:34:25] now_counter = 1097310540441, last_counter = 1097310539169, now_counter-last_counter = 1272`
  • 1
    Could you add integer values of your timer to log as well? `now_counter - last_counter` or just `now_counter`. – keltar Jul 09 '20 at 06:53
  • @keltar Post update – Eduard Design Jul 13 '20 at 09:47
  • 1
    Sorry but it could only make sense if both float and integer values are displayed in the same log, so I'll explain why I asked for it instead. My guess is you have accumulated imprecision with your additive calculation, so sum of your delta movements differs from expected results. I always recomment integer calculations where possible; simplier solution would probably be using total passed time instead of dt (and roll it over at around 2 hours to avoid precision loss spike). – keltar Jul 13 '20 at 12:26

1 Answers1

3

Thanks to keltar for the answers and the time spent. In fact, I struggled for a long time to solve this problem and sincerely did not understand why even after adding the ability to draw floating-point sprites in SDL2, they still drew me according to the pixel grid of the monitor.

The good news: deltaTime is calculated correctly, And there are no problems with rounding double when transferring coordinates to the SDL render!

Bad news: the solution is too simple to spend a few days on it.

SDL.SDL_SetHint (SDL.SDL_HINT_RENDER_SCALE_QUALITY, "2");

This line tells SDL2 to use Anisatropic filtering when rendering sprites. It made my animation flow smoothly.