2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
15 * Functions to access Mouse and Cyberman...
19 #define WIN32_LEAN_AND_MEAN
34 // These are to kludge up a bit my slightly broken GCC directx port.
36 #define E_FAIL (HRESULT)0x80004005L
39 #define SUCCEEDED(a) ((HRESULT)(a) >= 0)
45 #ifndef SEVERITY_SUCCESS
46 #define SEVERITY_SUCCESS 0
47 #define SEVERITY_ERROR 1
49 #ifndef FACILITY_WIN32
50 #define FACILITY_WIN32 7
53 #define FIELD_OFFSET(type, field) ((LONG)&(((type *)0)->field))
56 #define ME_CURSOR_MOVED (1<<0)
57 #define ME_LB_P (1<<1)
58 #define ME_LB_R (1<<2)
59 #define ME_RB_P (1<<3)
60 #define ME_RB_R (1<<4)
61 #define ME_MB_P (1<<5)
62 #define ME_MB_R (1<<6)
63 #define ME_OB_P (1<<7)
64 #define ME_OB_R (1<<8)
66 #define ME_Y_C (1<<10)
67 #define ME_Z_C (1<<11)
68 #define ME_P_C (1<<12)
69 #define ME_B_C (1<<13)
70 #define ME_H_C (1<<14)
71 #define ME_O_C (1<<15)
73 typedef struct event_info {
81 ushort device_dependant;
84 typedef struct mouse_info {
88 ubyte pressed[MOUSE_MAX_BUTTONS];
89 fix time_went_down[MOUSE_MAX_BUTTONS];
90 fix time_held_down[MOUSE_MAX_BUTTONS];
91 uint num_downs[MOUSE_MAX_BUTTONS];
92 uint num_ups[MOUSE_MAX_BUTTONS];
93 // ubyte went_down; /* Not in PC version, not needed with 'num_downs' etc */
98 typedef struct cyberman_info {
105 ubyte pitch_descriptor;
106 ubyte roll_descriptor;
107 ubyte yaw_descriptor;
111 static mouse_info Mouse;
113 static int Mouse_installed = 0;
115 int WMMouse_Handler_Ready = 0;
116 int mouse_wparam, mouse_lparam, mouse_msg;
120 static double mouse_x, mouse_y;
121 static double mouse_saved_x, mouse_saved_y; //used when hiding/unhiding to reset the real (displayed) postion
122 double mouse_accel=1.0;
124 void DrawMouse(void);
125 void EraseMouse(void);
126 void MoveMouse(/*int button,*/ int x, int y);
128 #define WIN_WIDTH 640
129 #define WIN_HEIGHT 480
130 #define SCR_WIDTH 640
131 #define SCR_HEIGHT 480
133 LPDIRECTINPUT g_lpdi = NULL;
134 LPDIRECTINPUTDEVICE g_lpdidMouse = NULL;
138 HRESULT ReadMouse (DIDEVICEOBJECTDATA *pdidod)
143 if (g_lpdidMouse == NULL)
146 hr = IDirectInputDevice_GetDeviceData (
153 if (hr == DIERR_INPUTLOST)
155 hr = IDirectInputDevice_Acquire (g_lpdidMouse);
158 hr = IDirectInputDevice_GetDeviceData (
167 if (SUCCEEDED (hr) && cElements != 1)
174 void UpdateMouseState (DIDEVICEOBJECTDATA *pdidod)
176 // fix timeNow = timer_get_fixed_seconds ();
178 ULONG iEvt = pdidod->dwOfs;
186 BOOL bPressed = pdidod->dwData & 0x80;
187 ULONG iButton = (iEvt - DIMOFS_BUTTON0) + MB_LEFT;
190 if (!Mouse.pressed [iButton])
192 Mouse.pressed [iButton] = 1;
193 Mouse.time_went_down [iButton] = Mouse.ctime;
194 Mouse.num_downs [iButton]++;
195 // Mouse.went_down = 1;
197 Mouse.num_downs [iButton] ++;
201 if (Mouse.pressed [iButton])
203 Mouse.pressed [iButton] = 0;
204 Mouse.time_held_down [iButton] += Mouse.ctime - Mouse.time_went_down [iButton];
205 Mouse.num_ups [iButton]++;
206 // Mouse.went_down = 0;
212 mouse_x += (double) ((LONG) pdidod->dwData);
216 mouse_y += (double) ((LONG) pdidod->dwData);
219 break;//hm, handle this?
221 mprintf((0,"unknown mouse event %i\n",iEvt));
222 // exit (iEvt);//not happy.
229 DIDEVICEOBJECTDATA didod;
231 Mouse.ctime = timer_get_fixed_seconds();
233 while (SUCCEEDED (ReadMouse (&didod)))
235 UpdateMouseState (&didod);
244 if (!Mouse_installed)
249 CurTime = timer_get_fixed_seconds();
250 for (i = 0; i < MOUSE_MAX_BUTTONS; i++) {
251 Mouse.pressed[i] = 0;
252 Mouse.time_went_down[i] = CurTime;
253 Mouse.time_held_down[i] = 0;
254 Mouse.num_downs[i] = 0;
255 Mouse.num_ups[i] = 0;
257 // Mouse.went_down = 0; /* mac only */
261 DWORD cElements = INFINITE;
263 IDirectInputDevice_GetDeviceData (
265 sizeof (DIDEVICEOBJECTDATA),
273 void mouse_close(void)
275 // if (Mouse_installed) // DPH: Unnecessary...
276 WMMouse_Handler_Ready=Mouse_installed = 0;
278 if (g_lpdidMouse != NULL)
280 IDirectInputDevice_Unacquire (g_lpdidMouse);
281 IDirectInputDevice_Release (g_lpdidMouse);
286 IDirectInput_Release (g_lpdi);
293 int mouse_init(int unused)
295 if (FindArg("-nomouse"))
298 return Mouse.num_buttons;
303 if (SUCCEEDED (hr = DirectInputCreate (GetModuleHandle (NULL), DIRECTINPUT_VERSION, &g_lpdi, NULL)))
305 if (SUCCEEDED (hr = IDirectInput_CreateDevice (g_lpdi,(void *) &GUID_SysMouse, &g_lpdidMouse, NULL)))
308 dipdw.diph.dwSize = sizeof (DIPROPDWORD);
309 dipdw.diph.dwHeaderSize = sizeof (DIPROPHEADER);
310 dipdw.diph.dwObj = 0;
311 dipdw.diph.dwHow = DIPH_DEVICE;
314 if (SUCCEEDED (hr = IDirectInputDevice_SetDataFormat (g_lpdidMouse, &c_dfDIMouse)) &&
315 //changed on 9/4/99 by Victor Rachels NONEX -> Exclusive
316 SUCCEEDED (hr = IDirectInputDevice_SetCooperativeLevel (g_lpdidMouse, g_hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND)) &&
317 //end this section edit -VR
318 SUCCEEDED (hr = IDirectInputDevice_SetProperty (g_lpdidMouse, DIPROP_BUFFERSIZE, &dipdw.diph)) &&
319 SUCCEEDED (hr = IDirectInputDevice_Acquire (g_lpdidMouse)))
324 IDirectInputDevice_Release (g_lpdidMouse);
331 Mouse.num_buttons = 3;
333 WMMouse_Handler_Ready=Mouse_installed = 1;
336 // mouse_set_center();
338 return Mouse.num_buttons;
342 void mouse_center() {
343 mouse_x=mouse_saved_x=WIN_WIDTH/2;
344 mouse_y=mouse_saved_y=WIN_HEIGHT/2;
347 void mouse_get_pos( int *x, int *y)
349 mouse_handler(); //temp
355 void mouse_get_delta( int *dx, int *dy )
357 if (!Mouse_installed) {
361 mouse_handler(); //temp
363 *dx = (int) mouse_x - WIN_WIDTH/2;
364 *dy = (int) mouse_y - WIN_HEIGHT/2;
366 //Now reset the mouse position to the center of the screen
367 mouse_x = (double) WIN_WIDTH/2;
368 mouse_y = (double) WIN_HEIGHT/2;
371 void mouse_get_delta_no_reset( int *dx, int *dy )
373 if (!Mouse_installed) {
377 mouse_handler(); //temp
379 *dx = (int) mouse_x - WIN_WIDTH/2;
380 *dy = (int) mouse_y - WIN_HEIGHT/2;
389 if (!Mouse_installed)
392 mouse_handler(); //temp
394 for (i = 0; i < MOUSE_MAX_BUTTONS; i++) {
395 if (Mouse.pressed[i])
402 /* This should be replaced with mouse_button_down_count(int button) */
403 int mouse_went_down(int button)
407 if (!Mouse_installed)
410 mouse_handler(); //temp
412 if ((button < 0) || (button >= MOUSE_MAX_BUTTONS))
416 count = Mouse.num_downs[button];
417 Mouse.num_downs[button] = 0;
423 // Returns how many times this button has went down since last call.
424 int mouse_button_down_count(int button)
428 if (!Mouse_installed)
431 mouse_handler(); //temp
433 if ((button < 0) || (button >= MOUSE_MAX_BUTTONS))
437 count = Mouse.num_downs[button];
438 Mouse.num_downs[button] = 0;
443 // Returns 1 if this button is currently down
444 int mouse_button_state(int button)
448 if (!Mouse_installed)
451 mouse_handler(); //temp
453 if ((button < 0) || (button >= MOUSE_MAX_BUTTONS))
457 state = Mouse.pressed[button];
462 // Returns how long this button has been down since last call.
463 fix mouse_button_down_time(int button)
467 if (!Mouse_installed)
470 mouse_handler(); //temp
472 if ((button < 0) || (button >= MOUSE_MAX_BUTTONS))
476 if (!Mouse.pressed[button]) {
477 time_down = Mouse.time_held_down[button];
478 Mouse.time_held_down[button] = 0;
480 time = timer_get_fixed_seconds();
481 time_down = time - Mouse.time_held_down[button];
482 Mouse.time_held_down[button] = 0;
489 void mouse_get_cyberman_pos( int *x, int *y )
495 //WHS: I made this :)
507 //#include "cursor.h" /* cursor and mask */
509 typedef struct Sprite {
516 //Sprite mouse_sprite = { cursor_width, cursor_height, cursor_bits, cursor_mask_bits};
518 //byte *behind_mouse;
519 //byte behind_mouse[cursor_width*cursor_height];
527 void EraseMouse(void)
531 //Should add a mode, relative, absolute
534 //void MoveMouse(int mode, int x, int y) {
536 void MoveMouse(int x, int y)