-
Notifications
You must be signed in to change notification settings - Fork 160
/
animation_user_control_discrete.c
109 lines (103 loc) · 3.45 KB
/
animation_user_control_discrete.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
/*
Use controls a square in a very discrete world, e.g. Tetris.
Using float coordinates would lead to an internal invisible state,
which is weird (you can be either at the left or right corner of the active square).
*/
#include "common.h"
#define WINDOW_WIDTH 600
#define RECTS_PER_WINDOW (10)
#define RECT_WIDTH (WINDOW_WIDTH / RECTS_PER_WINDOW)
#define SCREENS_PER_SECOND 1.0
#define KEYPRESS_DELAY 0.25
unsigned int get_pos(int x, int speed) {
x = (x + speed) % RECTS_PER_WINDOW;
if (x < 0)
x += RECTS_PER_WINDOW;
return x;
}
int main(void) {
SDL_Event event;
SDL_Rect rect;
SDL_Renderer *renderer;
SDL_Window *window;
double current_time_s, last_key_pressed_time_s;
unsigned int x = RECTS_PER_WINDOW / 2,
y = RECTS_PER_WINDOW / 2;
int key_pressed,
quit = 0,
speed_x = 0,
speed_y = 0;
unsigned int current_time, last_time;
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
SDL_SetWindowTitle(window, "arrow keys: move");
rect.w = RECT_WIDTH;
rect.h = RECT_WIDTH;
last_key_pressed_time_s = SDL_GetTicks() / 1000.0;
common_fps_init();
while (!quit) {
current_time = SDL_GetTicks();
current_time_s = current_time / 1000.0;
while (SDL_PollEvent(&event) == 1) {
key_pressed = 0;
if (event.type == SDL_QUIT) {
quit = 1;
} else if (event.type == SDL_KEYDOWN) {
switch(event.key.keysym.sym) {
case SDLK_LEFT:
speed_x = -1;
break;
case SDLK_RIGHT:
speed_x = 1;
break;
case SDLK_UP:
speed_y = -1;
break;
case SDLK_DOWN:
speed_y = 1;
break;
default:
break;
}
key_pressed = 1;
} else if (event.type == SDL_KEYUP) {
switch(event.key.keysym.sym) {
case SDLK_LEFT:
speed_x = 0;
break;
case SDLK_RIGHT:
speed_x = 0;
break;
case SDLK_UP:
speed_y = 0;
break;
case SDLK_DOWN:
speed_y = 0;
break;
default:
break;
}
}
}
if (current_time != last_time) {
if (key_pressed && current_time_s - last_key_pressed_time_s > KEYPRESS_DELAY) {
x = get_pos(x, speed_x);
y = get_pos(y, speed_y);
last_key_pressed_time_s = current_time_s;
}
rect.x = x * RECT_WIDTH;
rect.y = y * RECT_WIDTH;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);
}
last_time = current_time;
common_fps_update_and_print();
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}