1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #define SG_BUILD_LIBRARY |
16 | #include <siege/input/mouse.h> |
17 | #include <siege/core/window.h> |
18 | #include <siege/core/entity.h> |
19 | |
20 | #include <stdlib.h> |
21 | #include <string.h> |
22 | |
23 | #include <SDL/SDL.h> |
24 | |
25 | void SG_CALL__cdecl _sg_cbMouseButton(SGuint button, SGbool down) |
26 | { |
27 | if(button - 1 >= _sg_mouseButtonNum) |
| |
28 | return; |
29 | |
30 | _sg_mouseButtonCurr[button - 1] = down; |
31 | |
32 | SGbool pressed = sgMouseGetButtonPress(button); |
33 | |
34 | size_t numevents = 2; |
35 | SGenum events[2]; |
36 | |
37 | if(pressed) |
| |
38 | events[0] = SG_EVF_MOUSEBUTP(0x00080000U | 0x0002); |
39 | else if(!down) |
| 3 | | Assuming 'down' is not equal to 0 | |
|
| |
40 | events[0] = SG_EVF_MOUSEBUTR(0x00080000U | 0x0004); |
41 | else |
42 | { |
43 | events[1] = 0; |
44 | numevents--; |
45 | } |
46 | switch(button) |
| 5 | | Control jumps to 'case 3:' at line 60 | |
|
47 | { |
48 | case SG_MOUSE_BUTTON_LEFT1: |
49 | if(pressed) |
50 | events[numevents-1] = SG_EVF_MOUSEBUTLP(0x00080000U | 0x0010); |
51 | else if(!down) |
52 | events[numevents-1] = SG_EVF_MOUSEBUTLR(0x00080000U | 0x0020); |
53 | break; |
54 | case SG_MOUSE_BUTTON_RIGHT2: |
55 | if(pressed) |
56 | events[numevents-1] = SG_EVF_MOUSEBUTRP(0x00080000U | 0x0080); |
57 | else if(!down) |
58 | events[numevents-1] = SG_EVF_MOUSEBUTRR(0x00080000U | 0x0100); |
59 | break; |
60 | case SG_MOUSE_BUTTON_MIDDLE3: |
61 | if(pressed) |
| |
62 | events[numevents-1] = SG_EVF_MOUSEBUTMP(0x00080000U | 0x0400); |
63 | else if(!down) |
| |
64 | events[numevents-1] = SG_EVF_MOUSEBUTMR(0x00080000U | 0x0800); |
65 | break; |
| 8 | | Execution continues on line 71 | |
|
66 | default: |
67 | events[--numevents] = 0; |
68 | break; |
69 | } |
70 | |
71 | sgEntityEventSignal(numevents, events[0], button, events[1]); |
| 9 | | Function call argument is an uninitialized value |
|
72 | } |
73 | void SG_CALL__cdecl _sg_cbMouseMove(SGint x, SGint y) |
74 | { |
75 | _sg_mouseXPrev = _sg_mouseX; |
76 | _sg_mouseYPrev = _sg_mouseY; |
77 | _sg_mouseX = x; |
78 | _sg_mouseY = y; |
79 | |
80 | sgEntityEventSignal(1, (SGenum)SG_EVF_MOUSEMOVE(0x00080000U | 0x1000), x, y); |
81 | } |
82 | void SG_CALL__cdecl _sg_cbMouseWheel(SGint w) |
83 | { |
84 | _sg_mouseWheelPrev = _sg_mouseWheel; |
85 | _sg_mouseWheel = w; |
86 | |
87 | sgEntityEventSignal(1, (SGenum)SG_EVF_MOUSEWHEEL(0x00080000U | 0x2000), w); |
88 | } |
89 | |
90 | void SG_CALL__cdecl _sgMouseUpdate(void) |
91 | { |
92 | size_t numevents; |
93 | SGenum events[2]; |
94 | SGuint i; |
95 | for(i = 0; i < _sg_mouseButtonNum; i++) |
96 | { |
97 | if(_sg_mouseButtonCurr[i]) |
98 | { |
99 | numevents = 2; |
100 | events[0] = SG_EVF_MOUSEBUTH(0x00080000U | 0x0001); |
101 | switch(i + 1) |
102 | { |
103 | case SG_MOUSE_BUTTON_LEFT1: |
104 | events[1] = SG_EVF_MOUSEBUTLH(0x00080000U | 0x0008); |
105 | break; |
106 | case SG_MOUSE_BUTTON_RIGHT2: |
107 | events[1] = SG_EVF_MOUSEBUTRH(0x00080000U | 0x0040); |
108 | break; |
109 | case SG_MOUSE_BUTTON_MIDDLE3: |
110 | events[1] = SG_EVF_MOUSEBUTMH(0x00080000U | 0x0200); |
111 | break; |
112 | default: |
113 | events[1] = 0; |
114 | numevents--; |
115 | break; |
116 | } |
117 | sgEntityEventSignal(numevents, events[0], i + 1, events[1]); |
118 | } |
119 | } |
120 | memcpy(_sg_mouseButtonPrev, _sg_mouseButtonBuff, _sg_mouseButtonNum * sizeof(SGbool)); |
121 | memcpy(_sg_mouseButtonBuff, _sg_mouseButtonCurr, _sg_mouseButtonNum * sizeof(SGbool)); |
122 | } |
123 | |
124 | SGbool SG_CALL__cdecl _sgMouseInit(void) |
125 | { |
126 | _sg_mouseButtonNum = 7; |
127 | _sg_mouseButtonPrev = malloc(_sg_mouseButtonNum * sizeof(SGbool)); |
128 | memset(_sg_mouseButtonPrev, 0, _sg_mouseButtonNum * sizeof(SGbool)); |
129 | _sg_mouseButtonBuff = malloc(_sg_mouseButtonNum * sizeof(SGbool)); |
130 | memset(_sg_mouseButtonBuff, 0, _sg_mouseButtonNum * sizeof(SGbool)); |
131 | _sg_mouseButtonCurr = malloc(_sg_mouseButtonNum * sizeof(SGbool)); |
132 | memset(_sg_mouseButtonCurr, 0, _sg_mouseButtonNum * sizeof(SGbool)); |
133 | |
134 | _sg_mouseXPrev = 0; |
135 | _sg_mouseYPrev = 0; |
136 | _sg_mouseX = 0; |
137 | _sg_mouseY = 0; |
138 | _sg_mouseWheelPrev = 0; |
139 | _sg_mouseWheel = 0; |
140 | |
141 | return SG_TRUE1; |
142 | } |
143 | SGbool SG_CALL__cdecl _sgMouseDeinit(void) |
144 | { |
145 | free(_sg_mouseButtonPrev); |
146 | free(_sg_mouseButtonBuff); |
147 | free(_sg_mouseButtonCurr); |
148 | _sg_mouseButtonNum = 0; |
149 | |
150 | return SG_TRUE1; |
151 | } |
152 | |
153 | void SG_CALL__cdecl sgMouseMove(SGint x, SGint y) |
154 | { |
155 | SDL_WarpMouse(x, y); |
156 | _sg_mouseXPrev = _sg_mouseX; |
157 | _sg_mouseYPrev = _sg_mouseY; |
158 | _sg_mouseX = x; |
159 | _sg_mouseY = y; |
160 | } |
161 | |
162 | void SG_CALL__cdecl sgMouseSetVisible(SGbool vis) |
163 | { |
164 | SDL_ShowCursor(vis); |
165 | } |
166 | void SG_CALL__cdecl sgMouseShow(void) |
167 | { |
168 | sgMouseSetVisible(SG_TRUE1); |
169 | } |
170 | void SG_CALL__cdecl sgMouseHide(void) |
171 | { |
172 | sgMouseSetVisible(SG_FALSE0); |
173 | } |
174 | |
175 | void SG_CALL__cdecl sgMouseGetPosPrev(SGint* x, SGint* y) |
176 | { |
177 | if(x != NULL((void*)0)) |
178 | *x = _sg_mouseXPrev; |
179 | if(y != NULL((void*)0)) |
180 | *y = _sg_mouseYPrev; |
181 | } |
182 | SGint SG_CALL__cdecl sgMouseGetPosPrevX(void) |
183 | { |
184 | return _sg_mouseXPrev; |
185 | } |
186 | SGint SG_CALL__cdecl sgMouseGetPosPrevY(void) |
187 | { |
188 | return _sg_mouseYPrev; |
189 | } |
190 | |
191 | void SG_CALL__cdecl sgMouseGetPos(SGint* x, SGint* y) |
192 | { |
193 | if(x != NULL((void*)0)) |
194 | *x = _sg_mouseX; |
195 | if(y != NULL((void*)0)) |
196 | *y = _sg_mouseY; |
197 | } |
198 | SGint SG_CALL__cdecl sgMouseGetPosX(void) |
199 | { |
200 | return _sg_mouseX; |
201 | } |
202 | SGint SG_CALL__cdecl sgMouseGetPosY(void) |
203 | { |
204 | return _sg_mouseY; |
205 | } |
206 | |
207 | SGint SG_CALL__cdecl sgMouseGetWheelPrev(void) |
208 | { |
209 | return _sg_mouseWheelPrev; |
210 | } |
211 | SGint SG_CALL__cdecl sgMouseGetWheel(void) |
212 | { |
213 | return _sg_mouseWheel; |
214 | } |
215 | |
216 | SGbool SG_CALL__cdecl sgMouseGetButtonPrev(SGuint button) |
217 | { |
218 | if(button - 1 < _sg_mouseButtonNum) |
219 | return _sg_mouseButtonPrev[button - 1]; |
220 | return SG_FALSE0; |
221 | } |
222 | SGbool SG_CALL__cdecl sgMouseGetButton(SGuint button) |
223 | { |
224 | if(button - 1 < _sg_mouseButtonNum) |
225 | return _sg_mouseButtonCurr[button - 1]; |
226 | return SG_FALSE0; |
227 | } |
228 | SGbool SG_CALL__cdecl sgMouseGetButtonPress(SGuint button) |
229 | { |
230 | if(button - 1 < _sg_mouseButtonNum) |
231 | return !_sg_mouseButtonPrev[button - 1] && _sg_mouseButtonCurr[button - 1]; |
232 | return SG_FALSE0; |
233 | } |
234 | SGbool SG_CALL__cdecl sgMouseGetButtonRelease(SGuint button) |
235 | { |
236 | if(button - 1 < _sg_mouseButtonNum) |
237 | return _sg_mouseButtonPrev[button - 1] && !_sg_mouseButtonCurr[button - 1]; |
238 | return SG_FALSE0; |
239 | } |
240 | |
241 | SGbool SG_CALL__cdecl sgMouseGetButtonLeftPrev(void) |
242 | { |
243 | return sgMouseGetButtonPrev(SG_MOUSE_BUTTON_LEFT1); |
244 | } |
245 | SGbool SG_CALL__cdecl sgMouseGetButtonLeft(void) |
246 | { |
247 | return sgMouseGetButton(SG_MOUSE_BUTTON_LEFT1); |
248 | } |
249 | SGbool SG_CALL__cdecl sgMouseGetButtonLeftPress(void) |
250 | { |
251 | return sgMouseGetButtonPress(SG_MOUSE_BUTTON_LEFT1); |
252 | } |
253 | SGbool SG_CALL__cdecl sgMouseGetButtonLeftRelease(void) |
254 | { |
255 | return sgMouseGetButtonRelease(SG_MOUSE_BUTTON_LEFT1); |
256 | } |
257 | |
258 | SGbool SG_CALL__cdecl sgMouseGetButtonRightPrev(void) |
259 | { |
260 | return sgMouseGetButtonPrev(SG_MOUSE_BUTTON_RIGHT2); |
261 | } |
262 | SGbool SG_CALL__cdecl sgMouseGetButtonRight(void) |
263 | { |
264 | return sgMouseGetButton(SG_MOUSE_BUTTON_RIGHT2); |
265 | } |
266 | SGbool SG_CALL__cdecl sgMouseGetButtonRightPress(void) |
267 | { |
268 | return sgMouseGetButtonPress(SG_MOUSE_BUTTON_RIGHT2); |
269 | } |
270 | SGbool SG_CALL__cdecl sgMouseGetButtonRightRelease(void) |
271 | { |
272 | return sgMouseGetButtonRelease(SG_MOUSE_BUTTON_RIGHT2); |
273 | } |
274 | |
275 | SGbool SG_CALL__cdecl sgMouseGetButtonMiddlePrev(void) |
276 | { |
277 | return sgMouseGetButtonPrev(SG_MOUSE_BUTTON_MIDDLE3); |
278 | } |
279 | SGbool SG_CALL__cdecl sgMouseGetButtonMiddle(void) |
280 | { |
281 | return sgMouseGetButton(SG_MOUSE_BUTTON_MIDDLE3); |
282 | } |
283 | SGbool SG_CALL__cdecl sgMouseGetButtonMiddlePress(void) |
284 | { |
285 | return sgMouseGetButtonPress(SG_MOUSE_BUTTON_MIDDLE3); |
286 | } |
287 | SGbool SG_CALL__cdecl sgMouseGetButtonMiddleRelease(void) |
288 | { |
289 | return sgMouseGetButtonRelease(SG_MOUSE_BUTTON_MIDDLE3); |
290 | } |