简体   繁体   中英

C++ SDL2 Get Mouse Coordinates Without Delay

I am making a 2D bird's-eye view shooter with C++ using the SDL2 library in Code::Blocks , compiled with MinGW , running on Windows 7 .

Everything is going along smoothly, but I've encountered one major problem and I have not been able to find any working solution. Being a shooter, the mouse is involved A LOT, but I can never return the mouse position at the exact right frame... It always seems to be around 2-5 frames behind.

For example, I need a cross hair image to always be on the (invisible) mouse. I know I could just use one of the Mouse functions like CreateCursor, but I still need accurate coordinates so that the aim is not off. I'm using a basic script like this:

bool quit = false;
SDL_Event e;
int xMouse, yMouse;

while(quit == false)
{
    while(SDL_PollEvent(&e) != 0)
    {
        if(e.type == SDL_MOUSEMOTION)
        {
            SDL_GetGlobalMouseState(&xMouse,&yMouse);
        }
    }
}

Now, if I had a cross hair image rendered at (xMouse-10, yMouse-10) (subtraction used for origin) , it will always trail behind the cursor. Also, the coordinates will take a couple frames to stop moving once the mouse has already stopped moving.

Offer me a better method of precise mouse tracking.

PS I am pretty new to C++ and SDL, so offer any miscellaneous advice if you would like to.

This is a bit of a long shot, but on my Linux SDL2 project I had similar mouse lag, which was caused by setting the SDL_RENDERER_PRESENTVSYNC flag on my renderer. Removing that flag fixed the lag. Might be worth trying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM