Detect mouse coords relative to desktop on Mac

BlitzMax Forums/BlitzMax Programming/Detect mouse coords relative to desktop on Mac

Grey Alien(Posted 2007) [#1]
Hiya, I'm able to detect the mouse coords relative to the desktop on the PC via an API call, this allows me to slide the mouse smoothly out of the game window. Does anyone know how to do this on the Mac please? Thx!


Brucey(Posted 2007) [#2]
Busy today, aren't we? ;-)


It just so happens I never got around to releasing a cross-platform module/hack which gets screen mouse coords...
... and I doubt you'll get anyone else posting, so in good old Blue Peter tradition...

cocoa_mouse.m
#include <AppKit/AppKit.h>

void NSGetCursorPos(int *x,int *y){
	NSPoint mouseLoc = [NSEvent mouseLocation];
	*x = mouseLoc.x;
	*y = mouseLoc.y;
}


A max gui example of usage :
SuperStrict

Import "cocoa_mouse.m"


Function GetScreenPointerPos(x:Int Var, y:Int Var)
	NSGetCursorPos(x, y)
End Function

Local window:TGadget

window=CreateWindow("My Window",40,40,320,240, Null)

CreateTimer 30

While True
	WaitEvent 
	
	Select EventID()
		Case EVENT_WINDOWCLOSE
			End
	End Select
	
	Local x:Int
	Local y:Int
	GetScreenPointerPos(x, y)
	SetStatusText(window, "~t" + x + "," + y)
Wend

Apply as you wish :-)


Grey Alien(Posted 2007) [#3]
Busy every day :-) Just done the Vista stuff so now I'm onto Mac stuff.

Thanks! Very kind. I'll try to figure this out.

Will it work WITHOUT MaxGUI as somepeople don't have that?


Brucey(Posted 2007) [#4]
Yes, only the example is MaxGUI'ish, as it was the one I was testing the code with at the time.

The cocoa code will work on a non-MaxGUI app.
Try it and see ;-)


Brucey(Posted 2007) [#5]
this is untested, and may not even compile, but hopefully you'll get the idea :

SuperStrict

Import "cocoa_mouse.m"


Extern
	Function NSGetCursorPos(x:Int Ptr, y:Int Ptr)
End Extern

Function GetScreenPointerPos(x:Int Var, y:Int Var)
	NSGetCursorPos(VarPtr x, VarPtr y)
End Function



Graphics 640,480, 0

While Not KeyDown(KEY_ESCAPE)

    Cls

    Local x:Int, y:Int
    GetScreenPointerPos(x, y)

    DrawText "Pos = " + x + ", " + y, 50, 50

    Flip

Wend

End



Grey Alien(Posted 2007) [#6]
Thanks, but it won't compile. It falls down on the import with multiple errors, the first being: "invalid preprocessing directive #Include". Pretty strange as I'm importing another c file in my game game with include and that's fine!

By the way, what is AppKit.h, is that yours?

Thanks for any help you may offer.


Brucey(Posted 2007) [#7]
#include should be a small "i". And make sure you have the file as a .m suffix.

AppKit is a set of classes for developing applications, mostly concerned with the user interface.


Grey Alien(Posted 2007) [#8]
Thanks, small i hey, who'd have thought it?!

So I have to download appkit from your site? I'll look into it.


Brucey(Posted 2007) [#9]
No.. it's part of Apple's GUI framework :-)

ie. it should already be installed on your Mac.


Grey Alien(Posted 2007) [#10]
Ah I see, thanks. OK I'll try it on the Mac. thanks for putting up with all my questions.


Grey Alien(Posted 2007) [#11]
Hi Bruce, OK finally got to test this on the Mac, but ran into problems compiling the non-gui version. I get this:

cocoa_mouse.m:3: error: parse error before '*' token


then whole bunch more errors related to that one. Any ideas? Doesn't it like Int? weird.

Thanks for any help.


Brucey(Posted 2007) [#12]
It should be a small i, like "int".
Going by your original problem with "Include" (where it should have been "include", that may be the problem.

C is a case-sensitive language.


Grey Alien(Posted 2007) [#13]
harrah! that was it, thanks.

Uh next prob:

Linking:mousetest
/usr/bin/ld: multiple definitions of symbol _NSGetCursorPos
/Users/jakebirkett/Desktop/FS/Include/.bmx/cocoa_mouse.m.release.macos.x86.o definition of _NSGetCursorPos in section (__TEXT,__text)
/Applications/BlitzMax/mod/brl.mod/cocoamaxgui.mod/cocoamaxgui.release.macos.x86.a(cocoa.macos.m.release.macos.x86.o) definition of _NSGetCursorPos in section (__TEXT,__text)
collect2: ld returned 1 exit status
Build Error: Failed to link /Users/jakebirkett/Desktop/FS/Include/mousetest.app/Contents/MacOS/mousetest


However, I figured this was due to the extern so I took that out and those errors went but I get this:

Compile Error: Unable to convert from 'Int Ptr' to 'TNSGadget'
with the non-GUI code AND the GUI code. What is a TNSGadget? Is it an apple OS thing or maybe a Max GUI thing? Any ideas?


Brucey(Posted 2007) [#14]
It's just non-stop with you, isn't it? ;-)

Okay... rename the function "NSGetCursorPos" (the one in the file cocoa_mouse.m) to something else - whatever you like - as it seems BRL have used a function of the same name in MaxGUI (imaging that!), though I do believe mine is more useful :-p

You will need to re-add the extern bit, and then your BlitzMax code should make the call to the newly named function.

Sorry for the confusion :-)

I seem to have a million things going on at the moment, what with trying to fix all the memory leaks in the Mac MaxGUI, and the rest of my pet projects... not that's me trying to give excuses or anything...


Grey Alien(Posted 2007) [#15]
It's just non-stop with you, isn't it? ;-)
Yeah sorry dude. I REALLY appreciate your help on this though.

TOP MAN! It works, thanks very much! Strangely the y coordinate is Inversed and one-based not zero-based. It starts at 1 at the bottom of the screen and goes up to 900 a the top of the screen on mine. I'll have to "jiggery pokery" that a little bit to get it into a sensible range ;-)


Brucey(Posted 2007) [#16]
Oh yeah... OS X uses a standard coordinate system (bottom left is origin), rather than BlitzMax's top-left origin.

It's surprising how few coordinate systems start at the top left...