Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/05/23 14:22
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
4 registered members (AndrewAMD, Quad, soulman3, Ayumi), 675 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Limiting mouse cursor movement to a box/window? #422536
05/12/13 07:19
05/12/13 07:19
Joined: Apr 2013
Posts: 19
Zyro_Falcon Offline OP
Newbie
Zyro_Falcon  Offline OP
Newbie

Joined: Apr 2013
Posts: 19
Thus far in my learning journey... I haven't seen any way to influence the Windows mouse cursor itself via Lite-C code.

Say I have a game window on my desktop of size 640x480. I want to make it such that the mouse cannot move outside of that window. How should the code be written?

Re: Limiting mouse cursor movement to a box/window? [Re: Zyro_Falcon] #422538
05/12/13 08:17
05/12/13 08:17
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
do you want to keep the mouse-coordinates in the window or do you want to modify the windows cursor position?


POTATO-MAN saves the day! - Random
Re: Limiting mouse cursor movement to a box/window? [Re: Kartoffel] #422540
05/12/13 10:22
05/12/13 10:22
Joined: Apr 2013
Posts: 19
Zyro_Falcon Offline OP
Newbie
Zyro_Falcon  Offline OP
Newbie

Joined: Apr 2013
Posts: 19
The latter (modifying windows cursor position) sounds closer to what most games seem to be doing. I would also like to know how we'd do the first one you suggested... May I know how we'd do both, one method per script/game?

Re: Limiting mouse cursor movement to a box/window? [Re: Zyro_Falcon] #422545
05/12/13 13:51
05/12/13 13:51
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
for the second one you have to use the windows api, (windows.h) but I have no idea, how the functions you need are called.

the first method is very easy, you can use simple macros for that:
Code:
#define mouse_x_limited     clamp(mouse_cursor.x, 0, screen_size.x)
#define mouse_y_limited     clamp(mouse_cursor.y, 0, screen_size.y)



if you defined these two in your code you can access the mouse position by using mouse_x_limited and mouse_y_limited


POTATO-MAN saves the day! - Random
Re: Limiting mouse cursor movement to a box/window? [Re: Kartoffel] #422546
05/12/13 14:33
05/12/13 14:33
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
I don't know what is the best/ correct way to do it but I normally use the following code:

Code:
#include <windows.h>

void lock_mouse()
{
	RECT rect;
	GetClientRect(hWnd,&rect);
	ClientToScreen(hWnd,&rect);
	ClientToScreen(hWnd,&rect.right);
	ClipCursor(&rect);
}



"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: Limiting mouse cursor movement to a box/window? [Re: Superku] #422559
05/12/13 19:20
05/12/13 19:20
Joined: Nov 2011
Posts: 274
de
lemming Offline
Member
lemming  Offline
Member

Joined: Nov 2011
Posts: 274
de
@Superku: I didn't know it's THAT easy. Thank you!

(The function has to be called every frame you want to lock it to the screen by the way. So it's recommended to not execute it while in pause mode or something.)

Re: Limiting mouse cursor movement to a box/window? [Re: lemming] #422563
05/12/13 20:35
05/12/13 20:35
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
@lemming: it only needs to be called once confused (edit: well if the position changes it has to be called again)

...this should be a bit easier:

Code:
byte mouse_lock_on_off = 0;
void lock_mouse(var on_off)
{
	mouse_lock_on_off = (byte)(on_off != NULL);
	
	byte _toggle = window_focus;
	
	RECT temp_rect;
	GetClientRect(hWnd, &temp_rect);
	ClientToScreen(hWnd, &temp_rect);
	ClientToScreen(hWnd, &temp_rect.right);
	
	if(_toggle)
	{
		ClipCursor(&temp_rect);
	}
	
	while(mouse_lock_on_off)
	{
		GetClientRect(hWnd, &temp_rect);
		ClientToScreen(hWnd, &temp_rect);
		ClientToScreen(hWnd, &temp_rect.right);
		
		if(_toggle && !window_focus)
		{
			_toggle = 0;
			ClipCursor(NULL);
		}
		
		if(!_toggle && window_focus)
		{
			_toggle = 1;
			ClipCursor(&temp_rect);
		}
		
		wait(1);
	}
	
	ClipCursor(NULL);
}



clipping can be enabled or disabled by calling the function with 1 or 0, it will be disabled automatically if the window looses focus (and it gets enabled again if the engine window is the active window)

Last edited by Kartoffel; 05/12/13 20:42.

POTATO-MAN saves the day! - Random
Re: Limiting mouse cursor movement to a box/window? [Re: Kartoffel] #422570
05/13/13 00:53
05/13/13 00:53
Joined: Apr 2013
Posts: 19
Zyro_Falcon Offline OP
Newbie
Zyro_Falcon  Offline OP
Newbie

Joined: Apr 2013
Posts: 19
Awesome, I'll try these out.

On another hand, Workshop14 mentions setting the mouse to coords 200,300 using mouse_pos... I tried that (mouse_pos) and it didn't work? What do they mean?

Re: Limiting mouse cursor movement to a box/window? [Re: Zyro_Falcon] #422572
05/13/13 05:21
05/13/13 05:21
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
mouse_cursor is the position of the windows cursor (read only, synced automatically)

mouse_pos is used as internal mouse position for buttons and mouse events
and has to be either set manually or automatically by using mouse_mode = 4.

also, buttons and mouse events only work if mouse_mode is enabled.


Originally Posted By: Workshop 14
[...] The predefined variables named mouse_pos.x and mouse_pos.y control the position of the mouse pointer on the screen; if I set mouse_pos.x = 200 and mouse_pos.y = 300 (just an example), the pointer appears on the screen just like in the picture below: [...]

This is not said very clear but this only sets the engine mouse position, not the windows cursor.


POTATO-MAN saves the day! - Random
Re: Limiting mouse cursor movement to a box/window? [Re: Kartoffel] #422585
05/13/13 10:17
05/13/13 10:17
Joined: Apr 2013
Posts: 19
Zyro_Falcon Offline OP
Newbie
Zyro_Falcon  Offline OP
Newbie

Joined: Apr 2013
Posts: 19
So the engine mouse position is... a second (non-Windows) mouse cursor? Using mouse_pos I can alter that second mouse cursor's position?

Page 1 of 2 1 2

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1