How to handle mouse movement when zooming in and out?

Hey there,

I have built a zoom feature in my tile based game but I can’t figure out how to handle the mouse movement when the game is zoomed in.

The way I do this is I draw the target scene to a texture with a fixed size (say 1920x1080) and then I RenderCopy as,

SDL_RenderCopy(renderer, sceneTexture, &zoomRect, nullptr);

where zoomRect is initialized at the texture resolution and then decrements by scrolling the mouse wheel. The effect is that the user can magnify the scene as a smaller and smaller source rectangle is drawn to the screen.

The issue I have is that when the user is magnified in, he or she can no longer move properly because moving the mouse one tile is now moving the mouse by several tiles (due to the magnification). I think the affect is something similar to having a different screen and logical resolution. Do I need some kind of mouse scaling like SDL_RenderWindowToLogical?

What scaling factors you support and what tile sizes in pixels you support? What is your default tile size in pixels depending on dpi?

Hi,

I was able to resolve the issue on my own eventually by normalizing the mouse position to the logical resolution of the game. So the new mouse position is:

mouseX = zoomRect.x + zoomRect.w*mouseX/logicalWidth;   
mouseY = zoomRect.y + zoomRect.h*mouseY/logicalHeight;

where zoomRect is the rectangle of the “zoomed” view. When the user is not zoomed the ZoomRect is at (0,0,logicalWidth, logicalHeight), which just gives the plain old coords on the screen.