-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
137 lines (119 loc) · 2.78 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
#include "tetris.h"
#include <time.h>
int main(int argc, char **argv)
{
int ret = 0;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO))
{
SDL_Log("unable to initialize SDL2: %s", SDL_GetError());
ret = -1;
goto cleanup;
}
SDL_Window *window = SDL_CreateWindow("tetris", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 300, 600, 0);
if (!window)
{
SDL_Log("unable to create window: %s", SDL_GetError());
ret = -1;
goto cleanup2;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (!renderer)
{
SDL_Log("unable to create renderer: %s", SDL_GetError());
ret = -1;
goto cleanup3;
}
tetris_map_t map;
tetris_map_init(&map, 10, 20);
tetramino_t tetramini[4];
int pivot;
random_spawn_types(tetramini, &pivot, &map);
int timer = 1000;
Uint32 last_ticks = SDL_GetTicks();
for (;;)
{
SDL_Event event;
Uint32 current_ticks = SDL_GetTicks();
timer -= current_ticks - last_ticks;
last_ticks = current_ticks;
if (timer <= 0)
{
if (tetramino_move_all_down(tetramini, &map) == TETRAMINO_DEAD)
{
for (int i = 0; i < 4; i++)
{
if (tetramini[i].y == -1)
{
goto cleanup4;
}
}
int rows[4];
int rows_to_remove = check_for_full_lines(tetramini, &map, rows);
if (rows_to_remove > 0)
{
remove_full_lines(&map, rows, rows_to_remove);
}
random_spawn_types(tetramini, &pivot, &map);
}
timer = 1000;
}
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
goto cleanup4;
}
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_DOWN)
{
if (tetramino_move_all_down(tetramini, &map) == TETRAMINO_DEAD)
{
for (int i = 0; i < 4; i++)
{
if (tetramini[i].y == -1)
{
goto cleanup4;
}
}
int rows[4];
int rows_to_remove = check_for_full_lines(tetramini, &map, rows);
if (rows_to_remove > 0)
{
remove_full_lines(&map, rows, rows_to_remove);
}
random_spawn_types(tetramini, &pivot, &map);
}
timer = 1000;
}
else if (event.key.keysym.sym == SDLK_SPACE)
{
rotate_tetraminoes_types(tetramini, pivot, &map);
}
else if (event.key.keysym.sym == SDLK_LEFT)
{
tetramino_move_all_left(tetramini, &map);
}
else if (event.key.keysym.sym == SDLK_RIGHT)
{
tetramino_move_all_right(tetramini, &map);
}
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// draw map
tetris_map_draw(&map, renderer, 30);
//tetramino draw
tetraminoes_draw(tetramini, renderer, 30);
SDL_RenderPresent(renderer);
}
cleanup4:
SDL_DestroyRenderer(renderer);
cleanup3:
SDL_DestroyWindow(window);
cleanup2:
SDL_Quit();
cleanup:
return ret;
}