]> icculus.org git repositories - btb/d2x.git/blob - arch/sdl/mouse.c
do away with Mouse.buttons
[btb/d2x.git] / arch / sdl / mouse.c
1 /*
2  *
3  * SDL mouse driver.
4  *
5  *
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <conf.h>
10 #endif
11
12 #include <string.h>
13
14 #include <SDL.h>
15
16 #include "fix.h"
17 #include "timer.h"
18 #include "event.h"
19 #include "mouse.h"
20 #include "key.h"
21
22 #ifdef _WIN32_WCE
23 # define LANDSCAPE
24 #endif
25
26 #define Z_SENSITIVITY 100
27
28 static struct mouseinfo {
29         int delta_x, delta_y, delta_z;
30         int x,y,z;
31 } Mouse;
32
33 void d_mouse_init(void)
34 {
35         memset(&Mouse,0,sizeof(Mouse));
36 }
37
38 void mouse_button_handler(SDL_MouseButtonEvent *mbe)
39 {
40         // to bad, SDL buttons use a different mapping as descent expects,
41         // this is at least true and tested for the first three buttons 
42         int button_remap[11] = {
43                 MB_LEFT,
44                 MB_MIDDLE,
45                 MB_RIGHT,
46                 MB_Z_UP,
47                 MB_Z_DOWN,
48                 MB_PITCH_BACKWARD,
49                 MB_PITCH_FORWARD,
50                 MB_BANK_LEFT,
51                 MB_BANK_RIGHT,
52                 MB_HEAD_LEFT,
53                 MB_HEAD_RIGHT
54         };
55
56         int button = button_remap[mbe->button - 1]; // -1 since SDL seems to start counting at 1
57
58         vkey_handler(KEY_MB1 + button, mbe->state == SDL_PRESSED);
59
60         if (mbe->state == SDL_PRESSED) {
61                 if (button == MB_Z_UP) {
62                         Mouse.delta_z += Z_SENSITIVITY;
63                         Mouse.z += Z_SENSITIVITY;
64                 } else if (button == MB_Z_DOWN) {
65                         Mouse.delta_z -= Z_SENSITIVITY;
66                         Mouse.z -= Z_SENSITIVITY;
67                 }
68         }
69 }
70
71 void mouse_motion_handler(SDL_MouseMotionEvent *mme)
72 {
73 #ifdef LANDSCAPE
74         Mouse.delta_y += mme->xrel;
75         Mouse.delta_x += mme->yrel;
76         Mouse.y += mme->xrel;
77         Mouse.x += mme->yrel;
78 #else
79         Mouse.delta_x += mme->xrel;
80         Mouse.delta_y += mme->yrel;
81         Mouse.x += mme->xrel;
82         Mouse.y += mme->yrel;
83 #endif
84 }
85
86 void mouse_flush()      // clears all mice events...
87 {
88         int i;
89
90         event_poll();
91
92         Mouse.delta_x = 0;
93         Mouse.delta_y = 0;
94         Mouse.delta_z = 0;
95         Mouse.x = 0;
96         Mouse.y = 0;
97         Mouse.z = 0;
98         SDL_GetMouseState(&Mouse.x, &Mouse.y); // necessary because polling only gives us the delta.
99 }
100
101 //========================================================================
102 void mouse_get_pos( int *x, int *y )
103 {
104         event_poll();
105 #ifdef _WIN32_WCE // needed here only for touchpens?
106 # ifdef LANDSCAPE
107         SDL_GetMouseState(&Mouse.y, &Mouse.x);
108 # else
109         SDL_GetMouseState(&Mouse.x, &Mouse.y);
110 # endif
111 #endif
112         *x=Mouse.x;
113         *y=Mouse.y;
114 }
115
116 void mouse_get_delta( int *dx, int *dy )
117 {
118         event_poll();
119         *dx = Mouse.delta_x;
120         *dy = Mouse.delta_y;
121         Mouse.delta_x = 0;
122         Mouse.delta_y = 0;
123 }
124
125 void mouse_get_pos_z( int *x, int *y, int *z )
126 {
127         event_poll();
128         *x=Mouse.x;
129         *y=Mouse.y;
130         *z=Mouse.z;
131 }
132
133 void mouse_get_delta_z( int *dx, int *dy, int *dz )
134 {
135         event_poll();
136         *dx = Mouse.delta_x;
137         *dy = Mouse.delta_y;
138         *dz = Mouse.delta_z;
139         Mouse.delta_x = 0;
140         Mouse.delta_y = 0;
141         Mouse.delta_z = 0;
142 }
143
144 int mouse_get_btns()
145 {
146         int i;
147         uint flag=1;
148         int status = 0;
149
150         event_poll();
151
152         for (i=0; i<MOUSE_MAX_BUTTONS; i++ ) {
153                 if (keyd_pressed[KEY_MB1 + i])
154                         status |= flag;
155                 flag <<= 1;
156         }
157
158         return status;
159 }
160
161 void mouse_get_cyberman_pos( int *x, int *y )
162 {
163 }
164
165
166 // Returns 1 if this button is currently down
167 int mouse_button_state(int button)
168 {
169         event_poll();
170         return keyd_pressed[KEY_MB1 + button];
171 }