-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
458 lines (354 loc) · 9.78 KB
/
input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
Gravit - A gravity simulator
Copyright 2003-2005 Gerald Kaszuba
Gravit is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Gravit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Gravit; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "gravit.h"
#ifndef NO_GUI
int processKeys() {
SDL_Event event;
while( SDL_PollEvent( &event ) ) {
if (event.type == SDL_QUIT) {
cmdQuit(NULL);
return 1;
}
if (event.type == SDL_MOUSEBUTTONDOWN) {
if (view.screenSaver) {
cmdQuit(0);
return 1;
}
if (event.button.button == SDL_BUTTON_WHEELDOWN)
view.zoom *= (1 + (view.deltaVideoFrame * 0.01f));
if (event.button.button == SDL_BUTTON_WHEELUP)
view.zoom *= (1 + (view.deltaVideoFrame * -0.01f));
}
if (event.type == SDL_KEYUP) {
view.keys[event.key.keysym.sym] = 0;
if (view.screenSaver) {
cmdQuit(0);
return 1;
}
}
if (event.type == SDL_KEYDOWN) {
view.keys[event.key.keysym.sym] = 1;
if (view.consoleMode) {
if (event.key.keysym.unicode) {
if ( (event.key.keysym.unicode & 0xFF80) == 0 ) {
conInput((SDLKey)(event.key.keysym.unicode & 0x7F));
} else {
conAdd(LERR, "Sorry no unicode");
}
} else {
conInput(event.key.keysym.sym);
}
return 0;
}
switch(event.key.keysym.sym) {
case SDLK_ESCAPE:
if (view.consoleMode) {
view.consoleMode = 0;
break;
} else if (view.textMode == TM_HELP1 || view.textMode == TM_HELP2) {
view.textMode = TM_STANDARD;
break;
}
cmdQuit(NULL);
return 1;
break;
case SDLK_SPACE:
view.screenSaver = 0;
view.drawOSD = 1;
view.showCursor = 1;
break;
case SDLK_RETURN:
if (SDL_GetModState() & KMOD_LALT || SDL_GetModState() & KMOD_RALT) {
video.screenFS = !video.screenFS;
cmdVideoRestart(0);
}
break;
case SDLK_F1:
if (view.textMode == TM_HELP1)
view.textMode = TM_STANDARD;
else
view.textMode = TM_HELP1;
break;
case SDLK_F2:
if (view.textMode == TM_HELP2)
view.textMode = TM_STANDARD;
else
view.textMode = TM_HELP2;
break;
case SDLK_F5:
cmdPlay(NULL);
break;
case SDLK_F6:
cmdRecord(NULL);
break;
case SDLK_F7:
cmdStop(NULL);
break;
case SDLK_F8:
cmdStart(NULL);
break;
case SDLK_F9:
cmdScreenshot(NULL);
break;
case SDLK_F10:
if (view.screenshotLoop) {
view.screenshotLoop = 0;
conAdd(LNORM, "Stopped taking many screenshots! Check your screenshots directory.");
} else {
view.screenshotLoop = 1;
conAdd(LNORM, "Taking a screenshot every frame. Careful how long you run this!");
}
break;
case SDLK_s:
// save (control-s)
if (SDL_GetModState() & KMOD_LCTRL || SDL_GetModState() & KMOD_RCTRL)
cmdSaveFrameDump(NULL);
// toggle stereo mode
else {
if (view.stereoMode)
view.stereoMode = 0;
else {
view.stereoMode = 1;
cmdStereoWarning(0);
}
conAdd(LNORM, "stereoMode set to %i", view.stereoMode);
}
break;
case SDLK_BACKQUOTE:
view.consoleMode = (!view.consoleMode)?1:0;
break;
case SDLK_t:
if (++view.drawTree == 3)
view.drawTree = 0;
conAdd(LNORM, "drawTree set to %i", view.drawTree);
break;
case SDLK_SLASH:
view.particleColourMode ++;
if (view.particleColourMode == CM_LAST)
view.particleColourMode = 0;
conAdd(LNORM, "Colour mode set to: %s" , colourModes[view.particleColourMode]);
setColours();
break;
case SDLK_l:
view.blendMode ++;
if (view.blendMode == 5)
view.blendMode = 0;
conAdd(LNORM, "Blend mode set to: %i", view.blendMode);
break;
case SDLK_BACKSLASH:
view.particleRenderMode++;
if (view.particleRenderMode == 3)
view.particleRenderMode = 0;
conAdd(LNORM, "particleRenderMode set to %i" , view.particleRenderMode);
break;
case SDLK_MINUS:
view.particleSizeMin /= 2;
if (view.particleSizeMin < 1)
view.particleSizeMin = 1;
conAdd(LLOW, "particlesizemin set to %.1f ", view.particleSizeMin);
break;
case SDLK_EQUALS:
if (view.particleSizeMin < 1)
view.particleSizeMin = 1;
else
view.particleSizeMin *= 2;
conAdd(LLOW, "particlesizemin set to %.1f ", view.particleSizeMin);
if (view.particleSizeMin > view.particleSizeMax)
conAdd(LNORM, "Careful! particlesizemin is bigger then particlesizemax!");
break;
case SDLK_LEFTBRACKET:
view.particleSizeMax /= 2;
if (view.particleSizeMax < 1)
view.particleSizeMax = 1;
conAdd(LLOW, "particlesizemax set to %.1f ", view.particleSizeMax);
if (view.particleSizeMin > view.particleSizeMax)
conAdd(LNORM, "Careful! particlesizemin is bigger then particlesizemax!");
break;
case SDLK_RIGHTBRACKET:
if (view.particleSizeMax < 1)
view.particleSizeMax = 1;
else
view.particleSizeMax *= 2;
conAdd(LLOW, "particlesizemax set to %.1f ", view.particleSizeMax);
break;
case SDLK_m:
if (view.tailLength == -1) {
view.tailLength = 0;
conAdd(LLOW, "tailLength set to 0");
} else {
view.tailLength = -1;
conAdd(LLOW, "tailLength set to infinite!");
}
break;
case SDLK_n:
if (view.tailLength <= 0)
view.tailLength = 1;
else
view.tailLength *= 2;
conAdd(LLOW, "tailLength set to %i", view.tailLength);
break;
case SDLK_b:
if (view.tailLength < 0)
view.tailLength = 0;
if (view.tailLength == 1)
view.tailLength = 0;
else
view.tailLength /= 2;
conAdd(LLOW, "tailLength set to %i", view.tailLength);
break;
case SDLK_v:
view.tailOpacity += 0.1f;
if (view.tailOpacity > 1)
view.tailOpacity = 1;
conAdd(LLOW, "tailOpacity set to %.1f", view.tailOpacity);
break;
case SDLK_c:
view.tailOpacity -= 0.1f;
if (view.tailOpacity < 0)
view.tailOpacity = 0;
conAdd(LLOW, "tailOpacity set to %.1f", view.tailOpacity);
break;
case SDLK_x:
view.tailFaded = (!view.tailFaded)?1:0;
conAdd(LLOW, "tailFaded set to %i", view.tailFaded);
break;
case SDLK_COMMA:
view.tailSkip /= 2;
if (view.tailSkip < 1)
view.tailSkip = 1;
conAdd(LLOW, "tailSkip set to %i", view.tailSkip);
break;
case SDLK_PERIOD:
view.tailSkip *= 2;
conAdd(LLOW, "tailSkip set to %i", view.tailSkip);
break;
case SDLK_QUOTE:
view.tailWidth += 1;
if (view.tailWidth > 10)
view.tailWidth = 10;
conAdd(LLOW, "tailWidth set to %.0f", view.tailWidth);
break;
case SDLK_SEMICOLON:
view.tailWidth -= 1;
if (view.tailWidth < 1)
view.tailWidth = 1;
conAdd(LLOW, "tailWidth set to %.0f", view.tailWidth);
break;
case SDLK_o:
view.drawOSD = (!view.drawOSD)?1:0;
conAdd(LLOW, "drawOSD set to %i", view.drawOSD);
break;
case SDLK_q:
view.frameSkip--;
view.frameSkipCounter = 0;
conAdd(LLOW, "frameSkip set to %i", view.frameSkip);
break;
case SDLK_w:
view.frameSkip++;
view.frameSkipCounter = 0;
conAdd(LLOW, "frameSkip set to %i", view.frameSkip);
break;
case SDLK_d:
view.stereoSeparation--;
conAdd(LLOW, "stereoSeparation set to %.1f", view.stereoSeparation);
break;
case SDLK_f:
view.stereoSeparation++;
conAdd(LLOW, "stereoSeparation set to %.1f", view.stereoSeparation);
break;
default:
break;
}
}
}
if (!view.consoleMode) {
if (view.keys[SDLK_a])
view.zoom /= (1 + (view.deltaVideoFrame * 0.01f));
if (view.keys[SDLK_z])
view.zoom *= (1 + (view.deltaVideoFrame * 0.01f));
/*
if (view.keys[SDLK_UP])
view.face[0] -= state.dt * 0.1f;
if (view.keys[SDLK_DOWN])
view.face[0] += state.dt * 0.1f;
if (view.keys[SDLK_LEFT])
view.face[1] -= state.dt * 0.1f;
if (view.keys[SDLK_RIGHT])
view.face[1] += state.dt * 0.1f;
*/
}
return 0;
}
void processMouse() {
int x,y;
Uint16 wx,wy;
view.mouseButtons[1] = view.mouseButtons[0];
memcpy(view.lastMousePosition, view.currentMousePosition, sizeof(view.currentMousePosition));
view.mouseButtons[0] = SDL_GetMouseState(&view.currentMousePosition[0], &view.currentMousePosition[1]);
x = view.currentMousePosition[0] - view.lastMousePosition[0];
y = view.currentMousePosition[1] - view.lastMousePosition[1];
// if it's a screensaver, and the mouse has moved, and the simulation has been running for a bit...
if (view.screenSaver && (x || y) && (getMS() - view.firstTimeStamp > 1000)) {
cmdQuit(0);
return;
}
if (view.mouseButtons[0]) {
wx = video.screenW / 2;
wy = video.screenH / 2;
SDL_WarpMouse(wx,wy);
view.currentMousePosition[0] = wx;
view.currentMousePosition[1] = wy;
// turn off cursor only after the 2nd warp mouse, otherwise showcursor does strange things.
if (view.mouseButtons[1]) {
SDL_ShowCursor(0);
}
view.rot[1] += x;
view.rot[0] += y;
} else {
SDL_ShowCursor(view.showCursor);
}
}
#else
int processKeys() {
int key;
#ifdef WIN32
while (_kbhit()) {
key = _getche();
conInput(key);
if (key == 13 || key == 10) {
break;
}
}
#else
char numbytes;
fd_set rfds;
struct timeval tv;
int retval;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 1;
while ((retval = select(1, &rfds, NULL, NULL, &tv))) {
if (retval) {
numbytes = read(0,&key, 1); // = getchar();
if(numbytes)
conInput(key);
}
}
#endif
return 0;
}
#endif