-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
611 lines (520 loc) · 13.6 KB
/
input.c
File metadata and controls
611 lines (520 loc) · 13.6 KB
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#include <stdio.h>
#include <stdlib.h>
#include "engine.h"
#include "input.h"
#include "console.h"
#include "ff_vector2.h"
//Relates a Key to an Action
ActionKeyPair action_dictionary[] =
{
{SDLK_ESCAPE, ACTION_QUIT},
{SDLK_BACKQUOTE, ACTION_TOGGLE_CONSOLE},
{SDLK_RETURN, ACTION_CONFIRM_CONSOLE},
{SDLK_KP_ENTER, ACTION_CONFIRM_CONSOLE},
{SDLK_w, ACTION_FORWARD},
{SDLK_s, ACTION_BACKWARD},
{SDLK_d, ACTION_STRAFE_RIGHT},
{SDLK_a, ACTION_STRAFE_LEFT},
{SDLK_RIGHT, ACTION_TURN_RIGHT},
{SDLK_LEFT, ACTION_TURN_LEFT}
};
const static uint32_t action_dictionary_size = sizeof(action_dictionary)/sizeof(action_dictionary[0]);
//Relates an Action to a function (And its data)
Action default_registered_actions[] =
{
{ACTION_QUIT, signal_quit, &engine},
{ACTION_TOGGLE_CONSOLE, toggle_console, &console},
{ACTION_CONFIRM_CONSOLE, enter_console, &input.text_input_character_loc}
};
const static uint32_t default_registered_actions_size = sizeof(default_registered_actions)/sizeof(Action);
Input input;
//Inits state.
void init_input()
{
//Gets a snapshot of the keyboardstate
input.is_scancode_down = SDL_GetKeyboardState(NULL);
input.mouse_sensitivity = 0.4f;
SDL_SetRelativeMouseMode(SDL_TRUE);
set_input_actions(default_registered_actions, default_registered_actions_size);
input.text_input_enabled = false;
input.text_input_destination = NULL;
input.text_input_buffer_size = 0;
input.text_input_character_loc = 0;
input.disabled_actions = NULL;
input.disabled_actions_size = 0;
}
//Called once per frame
void update_input()
{
SDL_Event event;
input.mouse_buttons = SDL_GetMouseState(&input.mouse_x, &input.mouse_y);
//Polls all events (also updates input.is_scancode_down by calling SDL_PumpEvents internally)
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
signal_quit(&engine);
break;
case SDL_KEYDOWN:
on_keydown(event.key.keysym.sym, false);
break;
case SDL_MOUSEMOTION:
on_mouse_movement(scale_v2(vector2f(event.motion.xrel, -event.motion.yrel), input.mouse_sensitivity));
break;
case SDL_MOUSEWHEEL:
on_mouse_wheel(event.wheel.y);
break;
}
}
if(!input.text_input_enabled)
{
for(int i = 0; i < 284; i++)
{
if(input.is_scancode_down[i])
{
on_keydown(SDL_GetKeyFromScancode(i), true);
}
}
}
}
//Returns true if keycode is in the dictionary and puts the corresponding ActionCode in out_code
bool get_actioncode_from_keycode(SDL_Keycode keycode, ActionCode* outcode)
{
for(int i = 0; i < action_dictionary_size; i++)
{
if(action_dictionary[i].keycode == keycode)
{
*outcode = action_dictionary[i].actioncode;
return true;
}
}
return false;
}
//Returns true if actioncode is in the dictionary and puts the corresponding Keycode in out_code
bool get_keycode_from_actioncode(ActionCode actioncode, SDL_Keycode* outcode)
{
for(int i = 0; i < action_dictionary_size; i++)
{
if(action_dictionary[i].actioncode == actioncode)
{
*outcode = action_dictionary[i].keycode;
return true;
}
}
return false;
}
//Sets current input action context.
void set_input_actions(Action* actions, const uint32_t number_of_actions)
{
if(actions == NULL)
{
input.registered_actions = default_registered_actions;
input.registered_actions_size = default_registered_actions_size;
}
else
{
input.registered_actions = actions;
input.registered_actions_size = number_of_actions;
}
}
//Disables some actions from being triggered.
void set_disabled_actions(ActionCode* actions, const uint32_t number_of_actions)
{
if(actions == NULL)
{
input.disabled_actions = NULL;
input.disabled_actions_size = 0;
}
else
{
input.disabled_actions = actions;
input.disabled_actions_size = number_of_actions;
}
}
//Disables some actions from being triggered.
void set_enabled_actions(ActionCode* actions, const uint32_t number_of_actions)
{
if(actions == NULL)
{
input.enabled_actions = NULL;
input.enabled_actions_size = 0;
}
else
{
input.enabled_actions = actions;
input.enabled_actions_size = number_of_actions;
}
}
//Returns true if ActionCode is in the disabled actions list.
bool check_disabled_actions(ActionCode action)
{
for(int i = 0; i < input.disabled_actions_size; i++)
{
if(action == input.disabled_actions[i])
{
return true;
}
}
return false;
}
//Returns true if ActionCode is in the enabled actions list.
bool check_enabled_actions(ActionCode action)
{
if(input.enabled_actions == NULL) return true;
for(int i = 0; i < input.enabled_actions_size; i++)
{
if(action == input.enabled_actions[i])
{
return true;
}
}
return false;
}
//Performs action of action_code specified (Only keydowns)
void on_action(Action action)
{
if(!check_disabled_actions(action.action) &&
check_enabled_actions(action.action))
{
action.function.default_func(action.user_data);
}
}
bool check_registered_actions(ActionCode code, Action* out)
{
for(int i = 0; i < input.registered_actions_size; i++)
{
if( code == input.registered_actions[i].action )
{
*out = input.registered_actions[i];
return true;
}
}
return false;
}
//Searches the Action Dictionary and Registered actions for the key that was pressed
//And executes it.
void on_keydown(SDL_Keycode keycode, bool continuous)
{
//If text input is enabled, write to text out.
if(input.text_input_enabled) on_text_input(keycode);
bool has_action = false;
ActionCode found_action_code;
Action found_action;
if(!get_actioncode_from_keycode(keycode, &found_action_code)) return;
has_action = check_registered_actions(found_action_code, &found_action);
//Do nothing if on continuous mode and action is not continuous
if(continuous && !(found_action.flags & ACT_FLAG_CONTINUOUS)) return;
//This is the normal on_action
if(has_action) on_action(found_action);
}
//Performs a Mouse_movement action.
void on_mouse_movement(const Vector2f amount)
{
if(check_disabled_actions(ACTION_MOUSE_MOVE) || !check_enabled_actions(ACTION_MOUSE_MOVE)) return;
for(int i = 0; i < input.registered_actions_size; i++)
{
if(input.registered_actions[i].action == ACTION_MOUSE_MOVE)
{
input.registered_actions[i].function.mouse_move_func(input.registered_actions[i].user_data, amount);
}
if( input.registered_actions[i].action == ACTION_MOUSE_DRAG_RIGHT &&
is_mouse_held(INPUT_MOUSE_RIGHT))
{
input.registered_actions[i].function.mouse_move_func(input.registered_actions[i].user_data, neg_v2(amount));
}
}
}
void on_mouse_wheel(const int direction)
{
if(check_disabled_actions(ACTION_SCROLL_WHEEL) || !check_enabled_actions(ACTION_SCROLL_WHEEL)) return;
for(int i = 0; i < input.registered_actions_size; i++)
{
if(input.registered_actions[i].action == ACTION_SCROLL_WHEEL)
{
input.registered_actions[i].function.mouse_wheel_func(input.registered_actions[i].user_data, direction);
}
}
}
void on_text_input(SDL_Keycode keycode)
{
char current_char;
SDL_Keycode console_open_keycode;
get_keycode_from_actioncode(ACTION_TOGGLE_CONSOLE, &console_open_keycode);
current_char = keycode;
if(keycode >= 0x20 && keycode <= 0x7E && keycode != console_open_keycode)
{
if(keycode >= 0x61 && keycode <= 0x7A && (SDL_GetModState() & KMOD_SHIFT))
{
current_char = keycode - 0x20;
}
if(input.text_input_character_loc < input.text_input_buffer_size)
{
*(input.text_input_destination + input.text_input_character_loc) = current_char;
input.text_input_character_loc++;
}
}
if(current_char == '\b' && input.text_input_character_loc > 0)
{
input.text_input_character_loc -= 1;
*(input.text_input_destination + input.text_input_character_loc) = ' ';
}
}
void start_text_input(char* text_destination, uint32_t buffer_size)
{
input.text_input_enabled = true;
input.text_input_destination = text_destination;
input.text_input_buffer_size = buffer_size;
input.text_input_character_loc = 0;
}
void end_text_input()
{
input.text_input_enabled = false;
input.text_input_destination = NULL;
input.text_input_buffer_size = 0;
input.text_input_character_loc = 0;
}
/*
#include "SDL2/SDL.h"
#include "console.h"
#include "time.h"
#include "vector2.h"
#include "math.h"
#include "editor.h"
#include "player.h"
#include "world.h"
#include "input.h"
/*
extern bool e_running;
extern bool game_mode;
extern bool edit_mode;
extern int show_texture_select;
extern int selecting_texture_for;
char* console_buffer;
int console_buffer_cursor_pos;
SDL_Event event;
char lower_case_symbols[20] = {'`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '[', ']', ';', '\'', ',', '.', '/'};
char upper_case_symbols[20] = {'~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', ':', '\"', '<', '>', '?'};
extern clock_t current_frame_start;
extern PLAYER * player;
bool show_fps = 0;
bool show_map = 0;
extern VECTOR2 closest_vector;
extern EDGE * closest_edge;
extern SECTOR * closest_sector;
char get_upper_case_symbol(char lower_case)
{
for(int i = 0; i < 20; i ++)
{
if(lower_case_symbols[i] == lower_case)
{
return upper_case_symbols[i];
}
}
return lower_case;
}
void clear_console_buffer()
{
for(int i = 0; i < CONSOLE_CHAR_LIMIT; i++)
{
*(console_buffer + i) = '\0';
}
}
char* get_console_buffer()
{
return console_buffer;
}
void INPUT_Handle_Console()
{
while(SDL_PollEvent(&event) != 0)
{
if(event.type == SDL_QUIT)
{
engine.running = false;
}
else if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == '`' || event.key.keysym.sym == '\'' || event.key.keysym.sym == 'q')
{
set_console_open(!is_console_open());
}
else if(event.key.keysym.sym == SDLK_BACKSPACE && console_buffer_cursor_pos > 0)
{
*(console_buffer + console_buffer_cursor_pos - 1) = '\0';
console_buffer_cursor_pos -= 1;
}
else if(event.key.keysym.sym == 32)
{
*(console_buffer + console_buffer_cursor_pos) = ' ';
console_buffer_cursor_pos += 1;
}
else if(event.key.keysym.sym >= 97 && event.key.keysym.sym <= 122)
{
if(SDL_GetModState() & (KMOD_LSHIFT | KMOD_RSHIFT))
{
*(console_buffer + console_buffer_cursor_pos) = event.key.keysym.sym - 32;
}
else
{
*(console_buffer + console_buffer_cursor_pos) = event.key.keysym.sym;
}
console_buffer_cursor_pos += 1;
}
else if(event.key.keysym.sym >= 33 && event.key.keysym.sym <= 126)
{
if(SDL_GetModState() & (KMOD_LSHIFT | KMOD_RSHIFT))
{
*(console_buffer + console_buffer_cursor_pos) = get_upper_case_symbol(event.key.keysym.sym);
}
else
{
*(console_buffer + console_buffer_cursor_pos) = event.key.keysym.sym;
}
console_buffer_cursor_pos += 1;
}
if(event.key.keysym.sym == SDLK_RETURN)
{
printf_console("\n");
printf_console(console_buffer);
parse_console(console_buffer);
console_buffer_cursor_pos = 0;
clear_console_buffer();
}
}
}
}
void INPUT_Handle()
{
float delta_time;
delta_time = (float)(clock() - engine.current_frame_start)/(float)CLOCKS_PER_SEC;
if(is_console_open())
{
INPUT_Handle_Console();
}
else if(engine.edit_mode == 1)
{
EDIT_MODE_Handle_Input();
}
else
{
unsigned char * keystate = SDL_GetKeyState(NULL);
if(keystate[SDLK_LSHIFT])
{
player->speed = player->run_speed;
player->turn_speed = player->run_turn_speed;
}
else
{
player->speed = player->walk_speed;
player->turn_speed = player->walk_turn_speed;
}
if(keystate[SDLK_UP])
{
PLAYER_Move(player, scale_v2(rot_v2(vector2(0, 1), -(player->facing)), player->speed * engine_delta_time()));
}
if(keystate[SDLK_DOWN])
{
PLAYER_Move(player, scale_v2(rot_v2(vector2(0, -1), -(player->facing)), player->speed * engine_delta_time()));
}
if(keystate[SDLK_RIGHT])
{
if(keystate[SDLK_LALT])
{
PLAYER_Move(player, scale_v2(rot_v2(vector2(1, 0), -(player->facing)), player->speed * engine_delta_time()));
}
else
{
PLAYER_Turn(player, player->turn_speed * engine_delta_time());
}
}
if(keystate[SDLK_LEFT])
{
if(keystate[SDLK_LALT])
{
PLAYER_Move(player, scale_v2(rot_v2(vector2(-1, 0), -(player->facing)), player->speed * engine_delta_time()));
}
else
{
PLAYER_Turn(player, -player->turn_speed * engine_delta_time());
}
}
if(keystate[SDLK_PAGEUP])
{
player->pos_height += player->speed * engine_delta_time();
}
if(keystate[SDLK_PAGEDOWN])
{
player->pos_height -= player->speed * engine_delta_time();
}
while(SDL_PollEvent(&event) != 0)
{
if(event.type == SDL_QUIT)
{
engine.running = false;
}
else if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case '`':
case '\'':
case 'q':
set_console_open(!is_console_open());
break;
case 'i':
show_fps = !show_fps;
break;
case 'm':
show_map = !show_map;
break;
case 'p':
if(engine.edit_mode == 1 && show_texture_select == 0) engine.game_mode = !engine.game_mode;
break;
case '0':
player->current_weapon = 0;
break;
case '1':
player->current_weapon = 1;
break;
case '2':
player->current_weapon = 2;
break;
case '3':
player->current_weapon = 3;
break;
case '4':
player->current_weapon = 4;
break;
case '5':
player->current_weapon = 5;
break;
case '6':
player->current_weapon = 6;
break;
case '7':
player->current_weapon = 7;
break;
case '8':
player->current_weapon = 8;
break;
case '9':
player->current_weapon = 9;
break;
case SDLK_SPACE:
PLAYER_Jump();
break;
}
}
}
}
}
void INPUT_Init()
{
console_buffer = malloc(CONSOLE_CHAR_LIMIT*sizeof(char));
clear_console_buffer();
console_buffer_cursor_pos = 0;
}
void INPUT_Quit()
{
free(console_buffer);
}*/