summaryrefslogtreecommitdiff
path: root/input.c
blob: 6e9994d1a7ec0a4a71de48c9b2a0c80c48b968b7 (plain)
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
#include <SDL2/SDL.h>
#include "./constants.h"
#include "./structs.h"

extern App app;

static void handle_key_down(SDL_KeyboardEvent *event) {
	if(event->repeat == 0) {
		if(event->keysym.sym == SDLK_ESCAPE) {
			app.input |= INPUT_RESET;
		}
		if(event->keysym.sym == SDLK_a) {
			app.input |= PADDLE_1_UP;
		}
		if(event->keysym.sym == SDLK_z) {
			app.input |= PADDLE_1_DOWN;
		}
		if(event->keysym.sym == SDLK_QUOTE) {
			app.input |= PADDLE_2_UP;
		}
		if(event->keysym.sym == SDLK_SLASH) {
			app.input |= PADDLE_2_DOWN;
		}
	}
}

static void handle_key_up(SDL_KeyboardEvent *event) {
	if(event->repeat == 0) {
		if(event->keysym.sym == SDLK_a) {
			app.input ^= PADDLE_1_UP;
		}
		if(event->keysym.sym == SDLK_z) {
			app.input ^= PADDLE_1_DOWN;
		}
		if(event->keysym.sym == SDLK_QUOTE) {
			app.input ^= PADDLE_2_UP;
		}
		if(event->keysym.sym == SDLK_SLASH) {
			app.input ^= PADDLE_2_DOWN;
		}
	}
}

void process_input() {
	SDL_Event event;
	while(SDL_PollEvent(&event)) {
		switch(event.type) {
			case SDL_QUIT:
				app.input |= INPUT_RESET;
				break;
			case SDL_KEYDOWN:
				handle_key_down(&event.key);
			break;
			case SDL_KEYUP:
				handle_key_up(&event.key);
			break;
		}
	}
}