Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL2 render texture visibly lags behind mouse

I'm using SDL2 on Windows (I've tested both Windows 7 and Windows 8). I was playing around with rendering a texture locked to the mouse coordinates to create a sort of "crosshair" effect.

It works, but the texture visibly lags behind the mouse which creates an awkward delay between mouse motion and the rendered updates. Honestly, the delay is very minor, but to someone who cares about absolute accuracy, it would drive the person insane.

My question is basically, is this normal? I'm guessing that the delay is due to the time it takes for Windows to deliver the event to SDL and then SDL to deliver the event to me. How can one achieve a locked "crosshair" effect via SDL?

My code for reference:

#include "SDL.h"

int main( int argc, char* args[] ) 
{ 
    SDL_Init( SDL_INIT_EVERYTHING ); 

    SDL_Window* window = SDL_CreateWindow("SDL", 100, 100, 640, 480, SDL_WINDOW_SHOWN);

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    SDL_Surface* surface = SDL_LoadBMP("mouse.bmp");

    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
    SDL_FreeSurface(surface);

    bool isExiting = false;
    int x = 0;
    int y = 0;

    while(!isExiting)
    {
        SDL_Event e;
        while(SDL_PollEvent(&e))
        {
            if(e.type == SDL_QUIT)
            {
                isExiting = true;
                break;
            }
            else if(e.type == SDL_MOUSEMOTION)
            {
                x = e.motion.x;
                y = e.motion.y;
            }
        }

        SDL_Rect destRect;
        destRect.h = 19;
        destRect.w = 19;
        destRect.x = x;
        destRect.y = y;

        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, texture, NULL, &destRect);
        SDL_RenderPresent(renderer);
    }

    SDL_Quit(); 

    return 0; 
}
like image 583
Justin Skiles Avatar asked Sep 04 '13 12:09

Justin Skiles


2 Answers

While I cannot be sure why your loop lags, SDL has support for changing the mouse surface, as well as some other functions you may be interested in. It seems like you could put SDL_CreateColorCursor(SDL_Surface* surface, int hot_x, int hot_y) to good use. Here's a link to the wiki page on mouse support: http://wiki.libsdl.org/CategoryMouse

Happy coding!

like image 75
BrainSteel Avatar answered Nov 10 '22 08:11

BrainSteel


In a project I'm working on, I do something like this:

(Outside the main game loop):

SDL_Texture* cursor = //blah;
SDL_Rect cursor_hitbox;
SDL_QueryTexture(cursor, NULL, NULL, &cursor_hitbox.w, &cursor_hitbox.h);

(In the main game loop):

SDL_GetMouseState(&cursor_hitbox.x, &cursor_hitbox.y);

While using this I haven't really noticed any input lag. Maybe it's just the event type?

Note that this is most likely not as efficient though, since you would be getting the mouse state every frame, instead of just when you move the mouse.

like image 1
HelpMe000 Avatar answered Nov 10 '22 08:11

HelpMe000