-
Notifications
You must be signed in to change notification settings - Fork 3
/
st.c
142 lines (115 loc) · 2.53 KB
/
st.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
#include <time.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include "tetris.h"
void end_cb(struct game_state *gs)
{
int l = gs->lines;
printf("%d line%s\n", l, l == 1 ? "" : "s");
SDL_Quit();
exit(0);
}
static void tick_cb(struct game_state *gs)
{
if (clock() - gs->lastdrop >= gs->delay) {
#if TICK
drop_cb(gs);
#endif
gs->lastdrop = clock();
}
SDL_RenderPresent(gs->surface);
}
static void draw_cb(struct game_state *gs)
{
int width = W_WIDTH, heigth = W_HEIGHT;
int w;
int cw = width / G_WIDTH;
int ch = heigth / G_HEIGHT;
if (cw > ch) {
w = ch;
cw = (width - w * G_WIDTH) / 2;
ch = 0;
} else {
w = cw;
cw = 0;
ch = (heigth - w * G_HEIGHT) / 2;
}
SDL_SetRenderDrawColor(gs->surface, 100, 100, 100, 255);
SDL_RenderClear(gs->surface);
int i, j;
for (i = 0; i < G_WIDTH; i++) {
for (j = 0; j < G_HEIGHT; j++) {
int col, clr;
switch (gs->grid[i][j]) {
case 1: clr = col = 128;
break;
case 4: clr = col = 84;
break;
default:
col = j < 4 ? 26 : 26;
clr = j < 4 ? 52 : 26;
}
SDL_SetRenderDrawColor(gs->surface, clr, col, col, 255);
SDL_Rect rect;
rect.x = cw + i * w;
rect.y = ch + j * w;
rect.w = rect.h = w;
SDL_RenderFillRect(gs->surface, &rect);
}
}
SDL_RenderPresent(gs->surface);
}
static void key_cb(SDL_Keycode k, int shift, struct game_state *gs)
{
switch (k)
{
case SDLK_r: init_game_state(gs); break;
case SDLK_q: if (shift) end_cb(gs); break;
case SDLK_j:
case SDLK_s: drop_block(gs); break;
case SDLK_h:
case SDLK_a: move_x(gs, -1); break;
case SDLK_l:
case SDLK_d: move_x(gs, 1); break;
case SDLK_k:
case SDLK_w: rot_block(gs); break;
}
draw_cb(gs);
}
static struct game_state *init_window(int delay)
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS);
SDL_Window *screen = SDL_CreateWindow("Tetris",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, W_WIDTH, W_HEIGHT, 0);
SDL_Renderer *r = SDL_CreateRenderer(screen, -1, 0);
struct game_state *gs = malloc(sizeof(struct game_state));
gs->surface = r;
gs->delay = delay;
gs->log = stdout;
init_game_state(gs);
/*
g_timeout_add(10, (GSourceFunc) tick_cb, (gpointer) gs);
*/
return gs;
}
int main(int argc, char **argv)
{
struct game_state *gs = init_window(125000);
SDL_Event ev;
int mods;
draw_cb(gs);
while (1) {
SDL_RenderPresent(gs->surface);
SDL_WaitEvent(&ev);
switch (ev.type) {
case SDL_QUIT:
end_cb(gs);
break;
case SDL_KEYDOWN:
mods = SDL_GetModState();
key_cb(ev.key.keysym.sym, mods & KMOD_SHIFT, gs);
break;
}
}
return 0;
}