[comp.sys.mac.programmer] How do you set the mouse position?

6500stom@hub.UUCP (Josh Pritikin) (02/26/90)

I need to be able to set the position of the mouse for a game that
I'm writting. Actually, limiting the mouse movement to a rectangle
on the screen would work as well but I remember seeing a lot of
letters in MacTutor argueing the point. Does anyone have the code
to put the mouse somewhere the "correct way" or limit the mouse to
a rect?

Please send me mail but you might want to post too because other
people might have the same question but think the answer is probably
so obvious that they'd look stupid which they would. Hee hee.

/            Josh Pritikin             T The C++ programming language  \
| Internet:  6500stom@ucsbuxa.ucsb.edu | is at worst, the second best  |
| AppleLink: Josh.P                    | for a given application.      |
\ GEnie:     J.Pritikin                ! But usually, it is the best.  /

jackiw@cs.swarthmore.edu (Nick Jackiw) (02/26/90)

6500stom@hub.UUCP (Josh Pritikin) writes:
> I need to be able to set the position of the mouse for a game that
> I'm writting. Actually, limiting the mouse movement to a rectangle
> on the screen would work as well but I remember seeing a lot of
> letters in MacTutor argueing the point. Does anyone have the code
> to put the mouse somewhere the "correct way" or limit the mouse to
> a rect?
> 

No. You bad, bad rabbit.

The incorrect way, in that it violates all sorts of interface rules
(like: the user decides where the mouse goes) and compatibility
guidelines (like: don't access undocumented low-memory globals) was
discussed here quite some time ago.  Try:

procedure SetMouse(where:point);

{Thanks to Chris Muir, pacbell!well!cbm, for posting the original
version of this.}

var LowGlob:integer; LowMem:ptr; PointPtr:^point; finalTicks:longint;

const
   MBState=$172; MTemp=$828; RawMouse=$82C; Mouse=$830; 
   CrsrNew=$8ce; 

begin
 LocalToGlobal(where);
 LowMem:=pointer(RawMouse);
 PointPtr:=@LowMem^;
 PointPtr^:=where;
 LowMem:=pointer(MTemp);
 PointPtr:=@LowMem^;
 PointPtr^:=where;
 LowMem:=pointer(CrsrNew);
 LowMem^:=$ffff;
 Delay(5,finalTicks) 
end;

If you're after user friendliness, you might want to check that your app is
running on an architecture (hardware+System Version) with which you've
tested this works, and if not, warn them of a possible incompatibility before
letting them start the game.

-Nick
jackiw@cs.swarthmore.edu

--
"'I have seen too often your filthy teeth snap with rage and your august
countenance, covered with the fungus of time, blush like a flaming coal
because of some microscopic futility that man had commited, to be able
to pause longer before the signpost of that silly hypothesis.'" -Maldoror

mithomas@bsu-cs.bsu.edu (Michael Thomas Niehaus) (02/27/90)

If you are writing a game, there is no reason to move the mouse.  Just hide the
real cursor and have your program put up a fake one that will stay inside the
game window (it can even look like the real one if you like).

I used this approach in a game that I wrote where I wanted to restrict the
movement of the mouse to a two-dimensional path, and it worked quite well.
(Actually, I later modified the game so that the real cursor turned into
a cross-hair, and the fake cursor just mirrored the real cursor's movements
in a horizontal-only manner.)

If you want easy to implement, just use the normal local coordinates.  If they
are outside of the visible region of the window, set the fake cursor to the
boundary position.

If you want a better feel, register the change in the mouse position (delta)
and move your fake pointer by those amounts.  (This is better when the real
cursor leaves the window area.  When you return, the fake pointer will move
even though the real pointer is not yet back in the window.)  You can even
do your own scaling here (if for some reason you really wanted to).

-Michael

-- 
Michael Niehaus        UUCP: <backbones>!{iuvax,pur-ee}!bsu-cs!mithomas
Apple Student Rep      ARPA:  mithomas@bsu-cs.bsu.edu
Ball State University  AppleLink: ST0374 (from UUCP: st0374@applelink.apple.com)

jmunkki@kampi.hut.fi (Juri Munkki) (02/27/90)

In <10829@bsu-cs.bsu.edu> mithomas@bsu-cs.UUCP (Michael Thomas Niehaus) writes:
>If you are writing a game, there is no reason to move the mouse.

All games are not alike...

Suppose you are using the mouse to rotate something. Most users will stay
within certain limits, since their desk space is limited and they are not
used to picking up the mouse and moving it to a new place. More advanced
users handle the mouse expertly and will only be annoyed if there's a limit.

The way I would do this is to continuously move the mouse pointer to the
center of the screen. There might be other ways, but usually they lead to
even more trouble. I once tried expanding the constraints of the mouse,
which worked ok just as long as the cursor was hidden. If it was visible:
kaboom...

_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
|     Juri Munkki jmunkki@hut.fi  jmunkki@fingate.bitnet        I Want   Ne   |
|     Helsinki University of Technology Computing Centre        My Own   XT   |
^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^

cbm@well.sf.ca.us (Chris Muir) (03/01/90)

Ok, here it is again (in C this time):

/* ------------------------- SetMouse ------------------------- */
/* some dangerous low-memory-global equates */
extern  Point   MTemp           :   0x828;
extern  Point   RawMouse        :   0x82c;
extern  Point   Mouse           :   0x830;
extern  Byte    MBState         :   0x172;
extern  int     CrsrNewCouple   :   0x8ce;  /* both New & Couple */
extern  Byte    CrsrNew         :   0x8ce;
extern  Byte    CrsrCouple      :   0x8cf;

#define     Couple      0xff;   /* value for CrsrCouple */
#define     Uncouple    0x00;   /* value for CrsrCouple */

void    SetMouse(where)
Point   where;
{
long    finalTicks;
    LocalToGlobal(&where);      /* Get ready to store mouse position */
    RawMouse = where;           /* into RawMouse */
    MTemp = where;              /* and MTemp */
    CrsrNewCouple = 0xffff;     /* Hit CrsrNew & CrsrCouple */
    Delay(5, &finalTicks);      /* let the cursor catch up */
}   /* SetMouse */




-- 
_______________________________________________________________________________
Chris Muir                             |   "There is no language in our lungs
{hplabs,pacbell,ucbvax,apple}          |    to tell the world just how we feel" 
!well!cbm                              |                         - A. Partridge