-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
436 lines (330 loc) · 11 KB
/
main.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
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <SDL2/SDL.h>
const int WINDOW_INIT_WIDTH = 1020;
const int WINDOW_INIT_HEIGHT = 680;
#define FIELD_WIDTH 10
#define FIELD_HEIGHT 20
const float FIELD_ASPECT_RATIO = (float)(FIELD_HEIGHT)/(float)(FIELD_WIDTH);
const float FIELD_HEIGHT_PERCENT = 0.8;
const uint64_t MOVE_TIME_MS = 800;
const SDL_Color RED = {255, 0 , 0 , 255};
const SDL_Color BLUE = {0 , 0 , 255, 255};
const SDL_Color CYAN = {0 , 255, 255, 255};
const SDL_Color PINK = {255, 0 , 127, 255};
const SDL_Color BLACK = {0 , 0 , 0 , 255};
const SDL_Color WHITE = {255, 255, 255, 255};
const SDL_Color GREEN = {0 , 255, 0 , 255};
const SDL_Color AZURE = {0 , 127, 255, 255};
const SDL_Color ORANGE = {255, 127, 0 , 255};
const SDL_Color YELLOW = {255, 255, 0 , 255};
const SDL_Color VIOLET = {127, 0 , 255, 255};
const SDL_Color MAGENTA = {255, 0 , 255, 255};
const SDL_Color SPRING_GREEN = {0 , 255, 127, 255};
const SDL_Color CHARTEUSE_GREEN = {127, 255, 0 , 255};
const SDL_Color color_pool[12] = {
RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, ORANGE, PINK, CHARTEUSE_GREEN, SPRING_GREEN, AZURE, VIOLET};
void SetRenderDrawColor(SDL_Renderer* renderer, SDL_Color color) {
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);}
SDL_Point add(SDL_Point l, SDL_Point r){
return (SDL_Point){l.x + r.x, l.y + r.y};}
#define max(x, y) (((x) > (y)) ? (x) : (y))
#define min(x, y) (((x) < (y)) ? (x) : (y))
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Rect field_rect;
SDL_Event event;
uint64_t last_move_time;
bool running = false;
size_t score = 0;
typedef struct{
size_t state;
SDL_Point field_position;
SDL_Point tile_positions[8][4]; // for each state
SDL_Color color;
} Tetromino;
Tetromino current_tetromino;
bool taken_positions[FIELD_HEIGHT][FIELD_WIDTH];
SDL_Color field_colors[FIELD_HEIGHT][FIELD_WIDTH];
const Tetromino tetromino_pool[5] = {
{0, {FIELD_WIDTH / 2 - 1, 0}, {
{{0, 2}, {1, 2}, {2, 2}, {3, 2}},
{{2, 0}, {2, 1}, {2, 2}, {2, 3}},
{{0, 2}, {1, 2}, {2, 2}, {3, 2}},
{{2, 0}, {2, 1}, {2, 2}, {2, 3}},
{{0, 2}, {1, 2}, {2, 2}, {3, 2}},
{{2, 0}, {2, 1}, {2, 2}, {2, 3}},
{{0, 2}, {1, 2}, {2, 2}, {3, 2}},
{{2, 0}, {2, 1}, {2, 2}, {2, 3}}
}, BLACK},
{0, {FIELD_WIDTH / 2 - 1, 0}, {
{{0, 0}, {0, 1}, {1, 0}, {1, 1}},
{{0, 0}, {0, 1}, {1, 0}, {1, 1}},
{{0, 0}, {0, 1}, {1, 0}, {1, 1}},
{{0, 0}, {0, 1}, {1, 0}, {1, 1}},
{{0, 0}, {0, 1}, {1, 0}, {1, 1}},
{{0, 0}, {0, 1}, {1, 0}, {1, 1}},
{{0, 0}, {0, 1}, {1, 0}, {1, 1}},
{{0, 0}, {0, 1}, {1, 0}, {1, 1}}
}, BLACK},
{0, {FIELD_WIDTH / 2 - 1, 0}, {
{{0, 1}, {1, 1}, {2, 1}, {2, 2}},
{{1, 0}, {1, 1}, {1, 2}, {0, 2}},
{{0, 0}, {0, 1}, {1, 1}, {2, 1}},
{{1, 2}, {1, 1}, {1, 0}, {2, 0}},
{{0, 1}, {1, 1}, {2, 1}, {2, 0}},
{{1, 0}, {1, 1}, {1, 2}, {0, 0}},
{{0, 2}, {0, 1}, {1, 1}, {2, 1}},
{{1, 2}, {1, 1}, {1, 0}, {2, 2}}
}, BLACK},
{0, {FIELD_WIDTH / 2 - 1, 0}, {
{{0, 1}, {1, 1}, {2, 1}, {1, 2}},
{{1, 0}, {1, 1}, {1, 2}, {0, 1}},
{{0, 1}, {1, 1}, {2, 1}, {1, 0}},
{{1, 0}, {1, 1}, {1, 2}, {2, 1}},
{{0, 1}, {1, 1}, {2, 1}, {1, 0}},
{{1, 0}, {1, 1}, {1, 2}, {0, 1}},
{{0, 1}, {1, 1}, {2, 1}, {1, 2}},
{{1, 0}, {1, 1}, {1, 2}, {2, 1}}
}, BLACK},
{0, {FIELD_WIDTH / 2 - 1, 0}, {
{{0, 1}, {1, 1}, {1, 2}, {2, 2}},
{{2, 0}, {2, 1}, {1, 1}, {1, 2}},
{{0, 1}, {1, 1}, {1, 2}, {2, 2}},
{{2, 0}, {2, 1}, {1, 1}, {1, 2}},
{{0, 2}, {1, 1}, {1, 2}, {2, 1}},
{{1, 0}, {2, 1}, {1, 1}, {2, 2}},
{{0, 2}, {1, 1}, {1, 2}, {2, 1}},
{{1, 0}, {2, 1}, {1, 1}, {2, 2}}
}, BLACK}
};
void render_tetromino(Tetromino* t);
void render_tile(SDL_Point p, SDL_Color c);
void render_placed();
SDL_Scancode scancode_pool[7] = {
SDL_SCANCODE_RIGHT,
SDL_SCANCODE_LEFT,
SDL_SCANCODE_DOWN,
SDL_SCANCODE_Z,
SDL_SCANCODE_X,
SDL_SCANCODE_C,
SDL_SCANCODE_V
};
void move(Tetromino* t, void (*move_function)(Tetromino*));
void move_right(Tetromino* t);
void move_left(Tetromino* t);
void move_down(Tetromino* t);
void rotate_clockwise(Tetromino* t);
void rotate_counterclockwise(Tetromino* t);
void mirror_vertical(Tetromino* t);
void mirror_horizontal(Tetromino* t);
bool is_line_full(size_t line);
bool is_fitted(Tetromino* t);
void place(Tetromino* t);
void remove_line(size_t line);
void init();
void input();
void update();
void render();
void quit();
int main(){
init();
while(running){
input();
update();
render();
}
quit();
}
bool is_line_full(size_t line){
for(size_t i = 0; i < FIELD_WIDTH; i++)
if(!taken_positions[line][i]) return false;
return true;
}
void move_line_down(size_t line){
for(size_t i = 0; i < FIELD_WIDTH; i++){
taken_positions[line + 1][i] = taken_positions[line][i];
field_colors[line + 1][i] = field_colors[line][i];
}
}
void remove_line(size_t line){
for(size_t j = line - 1; j != 0; j--)
move_line_down(j);
}
bool is_fitted(Tetromino* t){
for(size_t i = 0; i < 4; i++){
SDL_Point p = add(t->field_position, t->tile_positions[t->state][i]);
if(p.y == FIELD_HEIGHT - 1) return true;
if(taken_positions[p.y + 1][p.x]) return true;
}
return false;
}
void place(Tetromino* t){
for(size_t i = 0; i < 4; i++){
SDL_Point p = add(t->field_position, t->tile_positions[t->state][i]);
taken_positions[p.y][p.x] = true;
field_colors[p.y][p.x] = t->color;
}
}
bool is_valid_position(Tetromino* t){
for(size_t i = 0; i < 4; i++){
SDL_Point p = add(t->field_position, t->tile_positions[t->state][i]);
if(p.x < 0 || p.x >= FIELD_WIDTH || p.y < 0 || p.y >= FIELD_HEIGHT) return false;
if(taken_positions[p.y][p.x]) return false;
}
return true;
}
void move(Tetromino* t, void (*move_function)(Tetromino*)){
Tetromino t0 = *t;
move_function(t);
if(!is_valid_position(t)) *t = t0;
}
void move_right(Tetromino* t){
Tetromino t0 = *t;
int right_most = 0;
for(size_t i = 0; i < 4; i++)
right_most = max(right_most, (t->field_position.x +
t->tile_positions[t->state][i].x));
t->field_position.x++;
if(!is_valid_position(t)) *t = t0;
}
void move_left(Tetromino* t){
int left_most = FIELD_WIDTH;
for(size_t i = 0; i < 4; i++)
left_most = min(left_most, (t->field_position.x +
t->tile_positions[t->state][i].x));
t->field_position.x--;
}
void move_down(Tetromino* t){
int down_most = 0;
for(size_t i = 0; i < 4; i++)
down_most = max(down_most, (t->field_position.y +
t->tile_positions[t->state][i].y));
t->field_position.y++;
}
void rotate_clockwise(Tetromino* t){
if(t->state < 4) t->state = (t->state + 1) % 4;
else t->state = 4 + (t->state - 1) % 4;
}
void rotate_counterclockwise(Tetromino* t){
if(t->state < 4) t->state = (t->state + 3) % 4;
else t->state = 4 + (t->state + 1) % 4;
}
void mirror_horizontal(Tetromino* t){
t->state = (t->state + 4) % 8;
}
void mirror_vertical(Tetromino* t){
if(t->state % 4 < 2) t->state = (t->state + 6) % 8;
else t->state = (t->state + 2) % 8;
}
void render_tetromino(Tetromino* t){
for(size_t i = 0; i < 4; i++)
render_tile(add(t->field_position, t->tile_positions[t->state][i]), t->color);
}
Tetromino get_random_tetromino(){
srand(time(NULL));
Tetromino t = tetromino_pool[rand() % 5];
t.color = color_pool[rand() % 12];
t.state = rand() % 8;
return t;
}
void clear_field(){
for(size_t i = 0; i < FIELD_HEIGHT; i++)
for(size_t j = 0; j < FIELD_WIDTH; j++){
taken_positions[i][j] = false;
field_colors[i][j] = WHITE;
}
}
void render_placed(){
for(size_t i = 0; i < FIELD_HEIGHT; i++)
for(size_t j = 0; j < FIELD_WIDTH; j++){
if(!taken_positions[i][j]) continue;
render_tile((SDL_Point){j, i}, field_colors[i][j]);
}
}
void render_tile(SDL_Point p, SDL_Color c){
float tile_size = field_rect.h / (float)FIELD_HEIGHT;
SetRenderDrawColor(renderer, c);
SDL_Rect r = {
field_rect.x + p.x * tile_size,
field_rect.y + p.y * tile_size,
tile_size + 1, tile_size + 1};
SDL_RenderFillRect(renderer, &r);
}
void init(){
SDL_Init(SDL_INIT_EVERYTHING);
running = true;
SDL_CreateWindowAndRenderer(WINDOW_INIT_WIDTH, WINDOW_INIT_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
for(size_t i = 0; i < FIELD_HEIGHT; i++)
for(size_t j = 0; j < FIELD_WIDTH; j++){
taken_positions[i][j] = false;
field_colors[i][j] = WHITE;
}
current_tetromino = get_random_tetromino();
last_move_time = SDL_GetTicks64();
}
void input(){
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT: running = false; break;
}
}
}
void update(){
uint8_t* key_scancodes = (uint8_t*)SDL_GetKeyboardState(NULL);
if(key_scancodes[SDL_SCANCODE_RIGHT] == 1) move(¤t_tetromino, move_right);
if(key_scancodes[SDL_SCANCODE_LEFT] == 1) move(¤t_tetromino, move_left);
if(key_scancodes[SDL_SCANCODE_DOWN] == 1) move(¤t_tetromino, move_down);
if(key_scancodes[SDL_SCANCODE_X] == 1) move(¤t_tetromino, rotate_clockwise);
if(key_scancodes[SDL_SCANCODE_Z] == 1) move(¤t_tetromino, rotate_counterclockwise);
if(key_scancodes[SDL_SCANCODE_V] == 1) move(¤t_tetromino, mirror_vertical);
if(key_scancodes[SDL_SCANCODE_C] == 1) move(¤t_tetromino, mirror_horizontal);
for(size_t i = 0; i < 7; i++)
key_scancodes[scancode_pool[i]] = 0;
if(SDL_GetTicks64() - last_move_time > MOVE_TIME_MS){
last_move_time = SDL_GetTicks64();
move(¤t_tetromino, move_down);
}
for(size_t line = FIELD_HEIGHT - 1; line != 1; line--){
if(is_line_full(line)){
remove_line(line);
score++;
}
}
if(is_fitted(¤t_tetromino)){
place(¤t_tetromino);
for(size_t i = 0; i < 4; i++)
for(size_t j = 0; j < FIELD_WIDTH; j++)
if(taken_positions[i][j]){
score = 0;
clear_field();
}
current_tetromino = get_random_tetromino();
}
}
void quit(){
printf("\n");
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void render(){
SetRenderDrawColor(renderer, WHITE);
SDL_RenderClear(renderer);
int window_width, window_height;
SDL_GetWindowSize(window, &window_width, &window_height);
field_rect.h = FIELD_HEIGHT_PERCENT * window_height;
field_rect.w = field_rect.h / FIELD_ASPECT_RATIO;
field_rect.y = 0.5 * (window_height - field_rect.h);
field_rect.x = 0.5 * (window_width - field_rect.w);
render_tetromino(¤t_tetromino);
render_placed();
SetRenderDrawColor(renderer, BLACK);
SDL_RenderDrawRect(renderer, &field_rect);
SDL_RenderPresent(renderer);
printf("\rScore: %ld ", score);
}