• 13
• 15
• 27
• 9
• 9

SDL2 and mouse movement to move screen

This topic is 1403 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Anyone here have some code to post to show how to use a mouse motion event to move the screen when the mouse hits the edge of the screen edges? e.g. a RTS game?

Thanks!

Share on other sites
What part are you having trouble with? The SDL function SDL_SetWindowPosition seems like a candidate for moving the window. Do you know how to get the position of the cursor? Do you know how to tell if the cursor is at/near the edge of the window? What have you tried?

Share on other sites

Do you know how to tell if the cursor is at/near the edge of the window?

I need help with the logic of it...

Share on other sites

What part are you having trouble with? The SDL function SDL_SetWindowPosition seems like a candidate for moving the window. Do you know how to get the position of the cursor? Do you know how to tell if the cursor is at/near the edge of the window? What have you tried?

I think he wants to move the camera, not the window that displays the game.

Do you know how to tell if the cursor is at/near the edge of the window?

I need help with the logic of it...

To get the position of the cursor relative to the top left corner of the window look at https://wiki.libsdl.org/SDL_GetMouseState

On the other hand, when you create the window you're setting the width and height.

Now, for every update, check if x and y position of the SDL_GetMouseState function are close to some edge (zero or the width and height values) and then call the logic to move the camera in the direction of that edge, or whatever you use to keep track of what part of the game level should be displayed.

Edited by DiegoSLTS

Share on other sites

What part are you having trouble with? The SDL function SDL_SetWindowPosition seems like a candidate for moving the window. Do you know how to get the position of the cursor? Do you know how to tell if the cursor is at/near the edge of the window? What have you tried?

I think he wants to move the camera, not the window that displays the game.

Do you know how to tell if the cursor is at/near the edge of the window?

I need help with the logic of it...

To get the position of the cursor relative to the top left corner of the window look at https://wiki.libsdl.org/SDL_GetMouseState

On the other hand, when you create the window you're setting the width and height.

Now, for every update, check if x and y position of the SDL_GetMouseState function are close to some edge (zero or the width and height values) and then call the logic to move the camera in the direction of that edge, or whatever you use to keep track of what part of the game level should be displayed.

Yeah I get that but does anyone have a code post of this in action? I can never figure out how to wrap the movement to a certain area e.g. 0,0 to 1024,768 e.g. a AABB  box. BTW this is 2D and not in 3D.

I am looking to move mouse to screen edges to move around the map e.g. Total Annihilation ect...

Thanks!

Edit

I can't find a SDL_SetMousePosition()? Do I need this to keep the mouse confined with in the aabb size I setup?

Bah I see SDL_WarpMouseInWindow() is the new function....

Here is what I have so far...

case SDL_MOUSEMOTION:
{
int x = 0, y = 0;
SDL_GetMouseState(&x, &y);
if(x > 1440)
{
SDL_WarpMouseInWindow(MainWindow, 1440, y);
}
if(x < 0)
{
SDL_WarpMouseInWindow(MainWindow, 0, y);
}
if(y > 1050)
{
SDL_WarpMouseInWindow(MainWindow, x, 1050);
}
if(y < 0)
{
SDL_WarpMouseInWindow(MainWindow, x, 0);
}
}


Edited by MARS_999

Share on other sites

Yeah I get that but does anyone have a code post of this in action? I can never figure out how to wrap the movement to a certain area e.g. 0,0 to 1024,768 e.g. a AABB  box. BTW this is 2D and not in 3D.

I am looking to move mouse to screen edges to move around the map e.g. Total Annihilation ect...

For each frame, get the position of the mouse relative to the screen. If the mouse is within 10 pixels of the edge of the screen, move the camera in that same direction.

if(mouse.x < 10) cameraPos.x -= (CameraSpeedPerSecond * deltaTimeInSeconds);
else if(mouse.x > (ScreenWidth-10)) cameraPos.x += (CameraSpeedPerSecond * deltaTimeInSeconds);

...and similar for mouse.y and cameraPos.y.

After each change in camera, do:

if(cameraPos.x < 0) cameraPos.x = 0;
else if((cameraPos.x + cameraPos.width) > MapWidth) cameraPos.x = (MapWidth - cameraPos.width);

...and similar for cameraPos.y.

Share on other sites

I don't have code doing that, but you can start doing something and post here the part that's not working.

Once you have the x and y position of the mouse I guess you can do something like this in your update function:

if (mouseX == 0) {
//move the camera to the left or everything else to the right
} else if (mouseX == width) {
//camera to the right
}

if (mouseY == 0) {
//camera up
} else if (mouseY == height) {
//camera down
}


What and how you do the part of moving the camera depends a lot on your code, but I guess you have some information to know what portion of the field is being displayed so making a check to know when to stop moving would be to check that information to see if the move in that direction is valid. But really, you should share some code if you want a less abstract answer.

EDIT: Sorry, didn't see the answer above while I was writing this one.

Edited by DiegoSLTS

Share on other sites
I think he wants to move the camera, not the window that displays the game.

Yep that makes way more sense.

Edited by nobodynews

Share on other sites

Here is what I came up with... thanks for everyones help!!!

void NX::App::onLoop()
{
int x = 0, y = 0;
SDL_GetMouseState(&x, &y);
if(x > (SW - 100))
{
SDL_WarpMouseInWindow(MainWindow, SW, y);
moveX +=5;
}
if(x < 100)
{
SDL_WarpMouseInWindow(MainWindow, 0, y);
moveX -=5;
}
if(y > (SH - 100))
{
SDL_WarpMouseInWindow(MainWindow, x, SH);
moveY +=5;
}
if(y < 100)
{
SDL_WarpMouseInWindow(MainWindow, x, 0);
moveY -=5;
}
}