Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / How to find mouse position of the mouse click in GDK 2D game?

Author
Message
MxM111
15
Years of Service
User Offline
Joined: 3rd Oct 2009
Location:
Posted: 6th Oct 2009 08:40
Hello

GDK provides either check that the mouse button is pressed or check its current position, however, it does not give the position of the mouse click itself. If I right something like
if (dbMouseClick())
{ read positions using dbMouseX() and dbMouseY() }
then it will not read the right position, because of possible time lag between when I clicked and when the check is performed. So, can somebody direct how can I get mouse position at the time of the click?
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 6th Oct 2009 13:52
I'd think that's pretty much as accurate as you can get...remember this is checking for this 60 or so times a second, so for a simple 2d game, I wouldn't worry about this.

You could put

Mouseclick = dbMouseClick();
MouseX = dbMouseX();
MouseY = dbMouseY();

at the beginning of your loop and use Mousex,MouseY etc instead of the darkgdk functions.
Mista Wilson
16
Years of Service
User Offline
Joined: 27th Aug 2008
Location: Brisbane, Australia
Posted: 6th Oct 2009 16:56 Edited at: 6th Oct 2009 17:03
Easiest way to get the mouse position at the time of a click in GDK is similar to puppy of kosh's suggestion, which is actually the preferred/suggested way to work with these methods(assign them to variables and then use the variables instead of the function calls - this is to preserve the value on a "per loop" basis, so once a mouse value has been set at the top of a loop, it stays the same till the start of the next loop, getting different values in a single loop can cause strange behaviour depending on how they r used).

Assign dbMouseClick() to a variable at the start of your main loop(eg. int iMClick = dbMouseClick(); ) and define 2 empty variables to hold you mouse position( int iMouseX, iMouseY; ) . Then have a conditional lower in the loop to grab the position like this :



EDIT : Bare in mind that a single "Game Loop" can be thought of as approx. 1/60th of a second at 60 FPS. Your program(with default settings) will loop ~60 times per second, so everything in your main loop will be called 60 times per second.

If it ain't broke.... DONT FIX IT !!!
MxM111
15
Years of Service
User Offline
Joined: 3rd Oct 2009
Location:
Posted: 7th Oct 2009 05:51
Thanks for the suggestions, but I am worried about those cases when program can not maintain 60fps. I think I will have lot's of things like path-finding, which may slowdown the 2D application significant. I wonder if there are alternative ways of getting the click position.
SFCBias
15
Years of Service
User Offline
Joined: 8th Aug 2009
Location: Hephzibah, GA, USA
Posted: 8th Oct 2009 00:21
that IS the best way IMO im running a 3D game with a (2d menu)etc... the method Mista Wilson suggested will not (or should not) cause any kind of lag.
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 8th Oct 2009 00:51
You could always use the DirectX functions, which are probably faster.
MxM111
15
Years of Service
User Offline
Joined: 3rd Oct 2009
Location:
Posted: 8th Oct 2009 06:58
I do not worry that the functions themselves are slow, I worry that there is noticeable time passed between my click and when the functions are called, if there are lots of calculation in background.

As I understand, each time when mouse makes click, there is interrupt called and the coordinates are stored somewhere. Am I right? What is the way to access those coordinates stored by the interrupt?

Login to post a reply

Server time is: 2024-11-20 13:20:32
Your offset time is: 2024-11-20 13:20:32