PDA

View Full Version : Cocoa - Reset Mouse Position to center of View


CactusJack
2002.12.11, 08:36 PM
Cocoa First Personal Viewpoints:

First, I'm not a hard-core game programmer: I am a beginner. I'm experienced in Windows programming, but I wanted to learn Cocoa, so I deceided I'd port some Open GL tutorials to Cocoa. This is why I would like Cocoa specific help. I've learned quite a bit about Cocoa, but I've hit some snags.

My question is this: I can track the position of the mouse in the glWindow through the mouseMoved event. What I'd like to do in each render cycle, take the coordinates of the mouse from this event and use the distance from them to the center of the screen to rotate my viewpoint and then set the mouse coordinates back to the center of the screen to start over again. Does anyone have any ideas on how to do this in Cocoa?

Also, how do it make it if I push Escape, the program quits? (because if I start moving the cursor back to the center all the time, I'll never be able to muse the Application menu to quit. appreciate

One last question. Do you know how to hide the cursor?

Thanks for any help you may be able to give me. I appreciate your time!

John Baker

OneSadCookie
2002.12.11, 08:47 PM
You should never move the mouse pointer, as that would be against the HI guidelines. You should never mess with standard mouse behavior for a non-full-screen application. That's why you can't do it from Cocoa.

When you receive a mouse moved event, you can call

void CGGetLastMouseDelta( CGMouseDelta * deltaX, CGMouseDelta * deltaY );

which returns the amount the mouse moved disregarding screen bounds. This will get you the information you want.

If you do decide to go full-screen, you can hide and show the cursor using

CGDisplayErr CGDisplayHideCursor(CGDirectDisplayID display); /* increments hide cursor count */
CGDisplayErr CGDisplayShowCursor(CGDirectDisplayID display); /* decrements hide cursor count */

#include <ApplicationServices/ApplicationServices.h> for these functions.

You can quit a Cocoa application by doing

[NSApp terminate:self];

CactusJack
2002.12.11, 09:46 PM
Thank you SO much for your quick reply. I'm new to Mac Game programming and had no idea about those methods. That really helped!

John Baker

CactusJack
2002.12.11, 10:30 PM
I can use CGGetLastMouseDelta now fine for tracking the distance the mouse traveled, but it stops tracking when I click a mouse button and drag. Is this easily avoided? Is this a problem with a first responser or something?

Thanks ahead of time.

OneSadCookie
2002.12.11, 10:42 PM
I suspect it's simply that you're getting mouseDragged (or similar) events when the mouse button is down as opposed to the mouseMoved events you get when no buttons are pressed.

If you look in the NSResponder docs/header file you should be able to find all the different events you can be sent.

Justin Brimm
2002.12.11, 10:47 PM
Try your code in mouseDragged

CactusJack
2002.12.11, 10:51 PM
I feel stupid. I thought this is what was happening but wasn't sure.

Thanks again!

Cactus