PDA

View Full Version : Mouse Speed



cocoflop
28-01-2008, 03:26
Hello, are there any functions ready for games that are based on heavy mouse input?

1. How can I get the speed of mouse movements (x and y)?
2. How can I hide the system cursor?
3. How can I position the cursor in the middle of the window?

matthew
28-01-2008, 05:01
I don't know if you can get the speed of the mouse movements but if you look on this (http://www.thinbasic.com/public/products/thinBasic/help/html/mouseptr.htm) page of the help documentation it tells you how to change the mouse style (http://www.thinbasic.com/public/products/thinBasic/help/html/mouseptr_equates.htm).

ErosOlmi
28-01-2008, 09:08
I'm in hurry at the moment but I will try to reply now and get it back later when I will have more time.
_________________________________________________
There are many things that can be done on this area. Mainly it depends on what you need to do.
Are you referring to standard Windows or TBGL module windows (OpenGl)? This make some difference.

To get the mouse position you can use GetCursorPos API.
To hide the mouse you can use MOUSEPTR(0) but mouse cursor can be restored by many other process running or by the OS itself.

For the rest I will get you back later, sorry.

Eros

Petr Schreiber
28-01-2008, 12:07
Hi,

following answers are only for TBGL:

1. How can I get the speed of mouse movements (x and y)?


...
type tMouseCur
NewX as long
NewY as long
OldX as long
OldY as long
end type

dim Mouse as tMouseCur

While TBGL_IsWindow(hWnd)
Mouse.NewX = tbgl_MouseGetPosX
Mouse.NewY = tbgl_MouseGetPosY

TBGL_ClearFrame

TBGL_DrawFrame

If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Mouse.OldX = Mouse.NewX
Mouse.OldY = Mouse.NewY
Wend
...


Difference between Mouse.Old and Mouse.New will give you the "speed"


2. How can I hide the system cursor?
TBGL_ShowCursor(%FALSE)


3. How can I position the cursor in the middle of the window?
TBGL_CenterCursor
Remark - if you want to measure speed then you must not store the old coordinate as you will measure only difference from the center, see complete code here:


'
' TBGL Mouse handling
'
Uses "TBGL"

Dim hWnd As Dword

hWnd = TBGL_CreateWindowEx("Move mouse to watch the speed scaled movement, [ESC] to quit", 640, 480, 32, 0)
TBGL_ShowWindow


type tMouseSpeed
SpeedX as double
SpeedY as double
end type

dim Mouse as tMouseSpeed
dim CameraX, CameraY as double
dim FrameRate as double
dim i, j as long
dim ScreenX, ScreenY as long

TBGL_GetAsyncKeyState(-1) '-- Resets status of all keys

' -- No cursor
tbgl_ShowCursor(%False)

While TBGL_IsWindow(hWnd)

' -- Center cursor
tbgl_CenterCursor

FrameRate = tbgl_GetFrameRate

TBGL_ClearFrame
tbgl_Camera CameraX, CameraY, 10, CameraX, CameraY, 0

' -- Draw grid
tbgl_BeginPoly %GL_POINTS
for i = -10 to 10
for j = -10 to 10
tbgl_Vertex i, j, 0
next
next
tbgl_EndPoly

TBGL_DrawFrame

' -- Get client are resolution
tbgl_GetWindowClient hWnd, ScreenX, ScreenY

' -- Get delta from center and scale using framerate
Mouse.SpeedX = (tbgl_MouseGetPosX - ScreenX/2) / FrameRate
Mouse.SpeedY = (ScreenY/2 -tbgl_MouseGetPosY) / FrameRate

' -- Shift camera
CameraX += Mouse.SpeedX
CameraY += Mouse.SpeedY

If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow



Hope it helps,
Petr

Petr Schreiber
28-01-2008, 16:21
Hmm,

and for the classic dialog programming ( UI module ) few tips, mainly based on what Eros said.

1. How can I get the speed of mouse movements (x and y)?
GetCursorPos function is very good.

As it is Windows function, you must declare it from DLL.


TYPE POINT
X AS LONG
Y AS LONG
END TYPE

DECLARE FUNCTION GetCursorPos LIB "USER32.DLL" ALIAS "GetCursorPos" (lpPoint AS POINT) AS LONG


Then you need to declare UDT variable the info will be returned to:


DIM MyMouse AS POINT


... and call the API


GetCursorPos(MyMouse)


Now MyMouse.x and MyMouse.y will contain coordinates of mouse cursor.
You can retrieve the "speed" using mechanism showed in previous post.


2. How can I hide the system cursor?
Another thing from Win32 - ShowCursor could serve:



DECLARE FUNCTION ShowCursor LIB "USER32.DLL" ALIAS "ShowCursor" (BYVAL bShow AS LONG) AS LONG




Then you can disable mouse ( for your dialog only! ) like:


ShowCursor(%FALSE)



3. How can I position the cursor in the middle of the window?
First thing is to be able to set curosr position, for this you need SetCursorPos:




DECLARE FUNCTION SetCursorPos LIB "USER32.DLL" ALIAS "SetCursorPos" (BYVAL x AS LONG, BYVAL y AS LONG) AS LONG




Then you just need to:

Retrieve window position
Retrieve window size
Put cursor in center


This can do for you following function:


sub Dialog_CenterCursor( dHandle as long )
local wx, wy, wposx, wposy as long

if win_GetForeground = dHandle then ' -- Only if our dialog is topmost
' -- Get dialog size and position in dialog units
dialog get size dHandle to wx, wy
dialog get loc dHandle to wposx, wposy

' -- Convert to pixels
dialog units dHandle, wx, wy to pixels wx, wy
dialog units dHandle, wposx, wposy to pixels wposx, wposy

' -- Set cursor to center
SetCursorPos(wposx+wx/2, wposy+wy/2)
end if
end sub


As you can see, procedure works only if window is top, else you could not control other programs after alt-tab.


Bye,
Petr

P.S. To see Win32 arsenal, good to visit MSDN (http://msdn2.microsoft.com/en-us/library/ms648390.aspx).

cocoflop
28-01-2008, 17:08
TBGL will do the trick for me, thank you guys for answering.

P.S.: Psch's reply (with examples) is ready for documentation. Generally it would be a wise idea to use many of the forum posts for this purpose.

Bye!

Petr Schreiber
28-01-2008, 18:51
Hi cocoflop,

also good to mention with thinBASIC you are not limited to mouse and keyboard.
TBDI module by Mike Hartlef allows control over joysticks, gamepads and wheels including ForceFeedback as well.

Functions for TBGL mouse and keyboard handling you can find in TBGL helpfile / Functions list / Input handling.

You should have your TBGL help file in thinAir under Help / User help / TBGL + keyboard sensitive help anytime using [F1] on keyword.


Bye,
Petr

Michael Hartlef
28-01-2008, 19:24
Mmmh, with Petr's 2nd example, I get an unbalanced parenthesis error in line 50 on the token hwnd . Any ideas? TB downloaded on the 22th.

Michael Clease
28-01-2008, 19:31
read this mike

http://community.thinbasic.com/index.php?topic=1463.msg10457;topicseen#new

Petr Schreiber
28-01-2008, 19:45
Hi Mike,

my apologies, I work with next TBGL preview which has this fixed.
I corrected the sample to be 0.2.1 compliant now.

Work on 0.2.2 goes well, stay tuned!


Bye,
Petr

Michael Hartlef
28-01-2008, 20:07
No problem, thanks!