Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


SDL2 and mouse movement to move screen


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 MARS_999   Members   -  Reputation: 1288

Like
0Likes
Like

Posted 12 May 2014 - 10:40 PM

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!

 



Sponsor:

#2 nobodynews   Crossbones+   -  Reputation: 2023

Like
0Likes
Like

Posted 13 May 2014 - 09:30 AM

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?

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


#3 MARS_999   Members   -  Reputation: 1288

Like
0Likes
Like

Posted 13 May 2014 - 12:44 PM

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... 



#4 DiegoSLTS   Members   -  Reputation: 1733

Like
4Likes
Like

Posted 13 May 2014 - 01:17 PM

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, 13 May 2014 - 01:18 PM.


#5 MARS_999   Members   -  Reputation: 1288

Like
0Likes
Like

Posted 13 May 2014 - 06:18 PM

 

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, 13 May 2014 - 07:18 PM.


#6 Servant of the Lord   Crossbones+   -  Reputation: 20304

Like
3Likes
Like

Posted 13 May 2014 - 07:15 PM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#7 DiegoSLTS   Members   -  Reputation: 1733

Like
1Likes
Like

Posted 13 May 2014 - 07:22 PM

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, 13 May 2014 - 07:24 PM.


#8 nobodynews   Crossbones+   -  Reputation: 2023

Like
0Likes
Like

Posted 13 May 2014 - 08:07 PM

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

Yep that makes way more sense.


Edited by nobodynews, 13 May 2014 - 08:08 PM.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


#9 MARS_999   Members   -  Reputation: 1288

Like
1Likes
Like

Posted 14 May 2014 - 12:57 AM

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;
}
}


#10 dejaime   Crossbones+   -  Reputation: 4052

Like
0Likes
Like

Posted 14 May 2014 - 08:01 PM


Here is what I came up with... thanks for everyones help!!!
You could add some elses to that code!

#11 Servant of the Lord   Crossbones+   -  Reputation: 20304

Like
0Likes
Like

Posted 14 May 2014 - 09:06 PM

I don't think SDL_WarpMouseInWindow is the correct function to use. I think you should be moving the camera, not the mouse pointer.

 

I've never played Total Annihilation in specific, but I've played plenty of RTS games, and they don't teleport the mouse pointer around.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS