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.
P.S. I am pretty new to C++ and SDL, so offer any miscellaneous advice if you would like to.
SDL_GetGlobalMouseState(&xMouse,&yMouse);without checking for a SDL_MOUSEMOTION event? So just get the mouse position in every execution of your main loop and check if that fixes the problem. - lmNt