aboutsummaryrefslogtreecommitdiff
path: root/tetris.c
blob: 1d90846bcfc99c0467c6a23a4ac5e5013ad56a51 (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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <time.h>

#include "colour.h"
#include "tetromino.h"
#include "plot.h"

#define FONT_FILE "/usr/share/fonts/TTF/arial.ttf"
#define FONT_SIZE 16

#define VERSION "1.0"
#define TETROMINO_WIDTH  4
#define TETROMINO_HEIGHT 4
#define TETROMINO_AREA   (TETROMINO_HEIGHT*TETROMINO_WIDTH)
#define MIN(a,b)         (a<b? a : b)

#define INTERVAL_SPEED   50
#define INTERVAL_NORMAL  500

static unsigned int speed_mode;
static unsigned int pause_mode;

struct colour palette[] = {
	{.r=0x28,.g=0x28,.b=0x28}, /* black */
	{.r=0xCC,.g=0x24,.b=0x1D}, /* red */
	{.r=0x98,.g=0x97,.b=0x1A}, /* green */
	{.r=0xD7,.g=0x99,.b=0x21}, /* yellow */
	{.r=0x45,.g=0x85,.b=0x88}, /* blue */
	{.r=0xB1,.g=0x62,.b=0x86}, /* purple */
	{.r=0x68,.g=0x9d,.b=0x6a}, /* aqua */
	{.r=0xA8,.g=0x99,.b=0x84}, /* grey */
	{.r=0x92,.g=0x83,.b=0x74}, /* grey2 */
	{.r=0xFB,.g=0x49,.b=0x23}, /* bright red */
	{.r=0xB8,.g=0xBB,.b=0x26}, /* bright green */
	{.r=0xFA,.g=0xBD,.b=0x2F}, /* bright yellow  */
	{.r=0x83,.g=0xA5,.b=0x98}, /* bright blue */
	{.r=0xD3,.g=0x86,.b=0x9B}, /* bright purple */
	{.r=0x8E,.g=0xC0,.b=0x7C}, /* bright aqua */
	{.r=0xD6,.g=0x5D,.b=0x0E}, /* orange */
	{.r=0xFE,.g=0x80,.b=0x19}, /* bright orange */
	{.r=0xEB,.g=0xDB,.b=0xB2}, /* whitish */
};

void
draw_board(struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS])
{
	unsigned int x,y;
	plot_clear();
	for (y = 0; y < HEIGHT_CELLS; y++) {
		for (x = 0; x < WIDTH_CELLS; x++) {
			plot_cell(x,y, (*board)[x][y]);
		}
	}
}

void
draw_piece(int x, int y, struct colour *c, char (*piece)[4][4])
{
	int px, py, ipx, ipy, end_x, end_y, wx, wy;
	ipx = (x < 0)? -x : 0;
	ipy = (y < 0)? -y : 0;
	end_y = MIN(HEIGHT_CELLS, y + 4);
	end_x = MIN(WIDTH_CELLS, x + 4);

	for (wy = (y < 0)? 0 : y, py = ipy; wy < end_y; wy++, py++)
		for (wx = (x < 0)? 0 : x, px = ipx; wx < end_x; wx++, px++)
			if ((*piece)[px][py])
				plot_cell(wx, wy, c);
}

void
drop_piece(int x, int y, struct piece *piece, struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS])
{
	int px, py, ipx, ipy, end_x, end_y, wx, wy;
	ipx = (x < 0)? -x : 0;
	ipy = (y < 0)? -y : 0;
	end_y = MIN(HEIGHT_CELLS, y + 4);
	end_x = MIN(WIDTH_CELLS, x + 4);

	for (wy = (y < 0)? 0 : y, py = ipy; wy < end_y; wy++, py++)
		for (wx = (x < 0)? 0 : x, px = ipx; wx < end_x; wx++, px++)
			if ((*piece->bitmap)[px][py])
				(*board)[wx][wy] = piece->colour;
}

Uint32
gravity_callback(Uint32 interval, void *param)
{
	(void)interval;
	(void)param;
	SDL_Event e;
	SDL_UserEvent ue;
	ue.type = SDL_USEREVENT;
	ue.code = 0;
	ue.data1 = 0;
	ue.data2 = 0;

	e.type = SDL_USEREVENT;
	e.user = ue;

	SDL_PushEvent(&e);
	return speed_mode ? INTERVAL_SPEED : INTERVAL_NORMAL;
}

int
hit_floor(int x, int y, struct piece *held, struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS])
{
	int px,py;
	for (px = 0; px < 4; px++) {
		/* seek to first cell of column */
		py = 0;
		while (py < 4 && (*held->bitmap)[px][py] == 0)
			py++;

		/* column has no cells? no collision possible. NEXT! */
		if (py == 4)
			continue;

		while (py < 4 && (*held->bitmap)[px][py])
			py++;

		if (   y + py >= HEIGHT_CELLS
			|| (*board)[x + px][y + py] != &(palette[0]))
			return 1;
	}
	return 0;
}

int
hit_side(int x, int y, struct piece *held, struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS])
{
	int px,py;
	for (py = 0; py < 4; py++) {
		for (px = 0; px < 4; px++) {
			if ((*held->bitmap)[px][py]
			    &&
			    (  x + px >= WIDTH_CELLS
			    || x + px < 0
			    || (*board)[x + px][y + py] != &(palette[0])
			    ))
				return 1;
		}
	}
	return 0;
}

void
update_bitmap(struct piece *held)
{
	held->bitmap = &(tetrominoes[0][held->type][held->rotation]);
}

void
new_piece(struct piece *held)
{
	held->colour = &palette[rand() % (sizeof(palette)/sizeof(*palette) - 1) + 1];
	held->type = rand()%7;
	held->rotation= rand()%4;
	update_bitmap(held);
}

void
rotate(struct piece *held, int direction)
{
	held->rotation += direction;
	/* FIXME need to handle direction not in [-4, 4] */
	if (held->rotation >= 4)
		held->rotation -= 4;

	if (held->rotation < 0)
		held->rotation += 4;

	update_bitmap(held);
}

int
clear_rows(struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS])
{
	int rows = 0;
	char row;
	int x, y, x1, y1;
	for (y = 0; y < HEIGHT_CELLS; y++) {
		row = 1;
		for (x = 0; x < WIDTH_CELLS; x++) {
			if ((*board)[x][y] == &(palette[0])) {
				row = 0;
				break;
			}
		}
		if (!row)
			continue;

		rows++;

		for (y1 = y; y1 > 0; y1--)
			for (x1 = 0; x1 < WIDTH_CELLS; x1++)
				(*board)[x1][y1] = (*board)[x1][y1-1];

		for (x1 = 0; x1 < WIDTH_CELLS; x1++)
			(*board)[x1][y1] = &(palette[0]);
	}
	return rows;
}

int
piece_overlaps(struct piece *held, struct colour *(*board)[WIDTH_CELLS][HEIGHT_CELLS], int piece_x, int piece_y)
{
	int x = 0;
	int y = 0;

	for (y = 0; y < TETROMINO_HEIGHT; y++)
		for (x = 0; x < TETROMINO_WIDTH; x++)
			if (held->bitmap[x][y] && (*board)[piece_x + x][piece_y + y] != &(palette[0]))
				return 1;

	return 0;
}

void
main_loop()
{
	struct colour *board[WIDTH_CELLS][HEIGHT_CELLS];
	SDL_Event e = {0};
	bool running = false;
	bool game_over = false;
	int i = 0;
	int x = 0;
	int y = 0;
	int score = 0;
	int last_x = 0;
	int last_y = 1;
	struct piece held;
	new_piece(&held);

	TTF_Font *font = TTF_OpenFont(FONT_FILE, FONT_SIZE);
	if (!font) {
		printf("TTF_OpenFont: %s\n", TTF_GetError());
		return;
	}

	for (y = 0; y < HEIGHT_CELLS; y++)
		for (x = 0; x < WIDTH_CELLS; x++)
			board[x][y] = &(palette[0]);

	running = true;
	last_x = last_y = x = y = 0;
	SDL_AddTimer(INTERVAL_NORMAL, &gravity_callback, NULL);
	char lockout;

	while (running) {
		lockout = 0;
		score += clear_rows(&board);

		if (hit_side(x, y, &held, &board))
			x = last_x;

		if (hit_floor(x, y, &held, &board))
			lockout = 1;

		draw_board(&board);

		if (!game_over)
			draw_piece(x, y, held.colour, held.bitmap);

		char score_string[16];
		snprintf(score_string, sizeof(score_string), "Score: %d", score);
		score_string[sizeof(score_string) - 1] = '\0';
		plot_text(score_string, font, ((SDL_Color){255,255,255,255}), 10, 10);

		if (game_over)
			plot_text("Game over", font, ((SDL_Color){255,255,255,255}), 90, 50);

		plot_update();
		SDL_WaitEvent(&e);
		if (pause_mode) {
			if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_p) {
				pause_mode = 0;
			}
			continue;
		}

		switch (e.type) {
		case SDL_USEREVENT:
			if (lockout && !game_over) {
				drop_piece(x, y, &held, &board);
				last_x = last_y = x = y = 0;
				new_piece(&held);
				if (piece_overlaps(&held, &board, x, y)) {
					game_over = true;
				}

				lockout = 0;
			} else {
				last_y = y++; /* gravity */
				last_x = x;
			}
			break;
		case SDL_QUIT:
			fprintf</