summaryrefslogtreecommitdiffstats
path: root/tetris.c
blob: aae62ed0f6b843563a46786ecef03ece8ca8ba0c (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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
/**
 * \file tetris.c
 * \brief Simple gaming board - Tetris
 *
 * \subsection Copyright
 * Copyright (c) 2013-2022 Michael Buesch <m@bues.ch>
 *
 * \subsection License
 * Licensed under the terms of the GNU General Public License version 2.
 */

#include "tetris.h"
#include "ks0108.h"
#include "shr3.h"
#include "util.h"
#include "music.h"
#include "font.h"

#include <string.h>

#include <avr/pgmspace.h>


/** Tetris Spielzustand. */
enum tetris_state {
	/** Im Intro. */
	TETRIS_INTRO = 0,
	/** Im Spiel. */
	TETRIS_INGAME,
	/** Game over */
	TETRIS_GAMEOVER,
};

/** Tetris Konstanten */
enum tetris_constants {
	/** Breite des Spielbretts, in Steinen. */
	BOARD_WIDTH		= 8,
	/** Hoehe des Spielbretts, in Steinen. */
	BOARD_HEIGHT		= 16,

	/** Maximale Breite und Hoehe eines Blocks, in Steinen. */
	MAX_BLOCK_SIZE		= 4,

	/** Breite und Hoehe eines Steins, in Pixel. */
	TOKEN_SIZE_PIX		= 8,

	/** Naechstes Level nach X geloeschten Zeilen. */
	NR_ZAPPED_NEXT_LEVEL	= 4,
	/** Multiplikator fuer Punktzahl. */
	SCORE_MUL		= 8,
};

/** 100 Hz Zeitgeber-Intervalle. */
enum tetris_timer_intervals {
	/** Normaler Intervall. */
	TIMER_INTER_DEFAULT	= 100,
	/** Intervall fuer schnell fallenden Block. */
	TIMER_INTER_FASTDOWN	= 5,
	/** Intervall fuer links/rechts-Bewegungen. */
	TIMER_INTER_MOVE	= 13,
	/** Intervall fuer Lautstaerke hoch/runter. */
	TIMER_INTER_VOLUPDOWN	= 30,
};

/** Stein-flags. */
enum token_flags {
	/** Stein wird angezeigt. */
	TOKEN_FILLED		= (1 << 0),
};

/** Ein Tetris "Stein". */
struct tetris_token {
	uint8_t flags;
};

/** Ein Tetris "Block" (mehrere "Steine"). */
struct tetris_block {
	/* Die Steine des Blocks */
	struct tetris_token tokens[MAX_BLOCK_SIZE][MAX_BLOCK_SIZE];
	/* Die X Koordinate des Blocks im Spielfeld. */
	uint8_t x;
	/* Die Y Koordinate des Blocks im Spielfeld. */
	uint8_t y;
	/* Die Breite des Blocks (<= MAX_BLOCK_SIZE). */
	uint8_t width;
	/* Die Hoehe des Blocks (<= MAX_BLOCK_SIZE). */
	uint8_t height;
};

/** Zeilen-flags. */
enum line_flags {
	/** Zeile wurde veraendert. */
	LINE_MODIFIED		= (1 << 0),
};

/** Ein Tetris "Spielfeld".
 *
 *	Der Nullpunkt ist oben links:
 *
 *	0#### x (8) >
 *	#####
 *	#####
 *	#####
 *	y (16)
 #	v
 */
struct tetris_board {
	/** Zweidimensionales Spielfeld */
	struct tetris_token tokens[BOARD_HEIGHT][BOARD_WIDTH];
	/** Zeilenflags. */
	uint8_t line_flags[BOARD_HEIGHT];
};

/** Tetris Spielstatus */
struct tetris_game {
	/** Der Spielstatus. */
	enum tetris_state state;

	/** Das Spielfeld. */
	struct tetris_board board;

	/** Ist ein fallender Block vorhanden? */
	bool have_falling_block;
	/** Der fallende Block. */
	struct tetris_block falling_block;
	/** Fallenden Block schnell nach unten bewegen. */
	bool fast_down;

	/** Der Spieltimer zaehler.
	 * Zaehlt mit 100Hz runter. */
	uint8_t timer_count;
	/** Spieltimer Intervall. */
	uint8_t timer_inter;

	/** Anzahl der geloeschten vollen Zeilen. */
	uint8_t nr_zapped;
	/** Schwierigkeitsgrad. */
	uint8_t level;
	/** Gesamtpunktzahl. */
	uint16_t score;

	/** LCD Update Flag. */
	bool lcd_update;
};

#define TOK(_flags)	{ .flags = (_flags), }
#define FILLED		TOK(TOKEN_FILLED)
#define EMPTY		TOK(0)

static const struct tetris_block __flash tetris_blocks[] = {
	{
		/*  Block:  ####
		 */
		.tokens		= {
			{ FILLED, FILLED, FILLED, FILLED, },
		},
		.width		= 4,
		.height		= 1,
	}, {
		/*  Block:  #
		 *          ###
		 */
		.tokens		= {
			{ FILLED, EMPTY, EMPTY, },
			{ FILLED, FILLED, FILLED, },
		},
		.width		= 3,
		.height		= 2,
	}, {
		/*  Block:    #
		 *          ###
		 */
		.tokens		= {
			{ EMPTY, EMPTY, FILLED, },
			{ FILLED, FILLED, FILLED, },
		},
		.width		= 3,
		.height		= 2,
	}, {
		/*  Block:  ##
		 *          ##
		 */
		.tokens		= {
			{ FILLED, FILLED, },
			{ FILLED, FILLED, },
		},
		.width		= 2,
		.height		= 2,
	}, {
		/*  Block:   ##
		 *          ##
		 */
		.tokens		= {
			{ EMPTY, FILLED, FILLED, },
			{ FILLED, FILLED, EMPTY, },
		},
		.width		= 3,
		.height		= 2,
	}, {
		/*  Block:  ##
		 *           ##
		 */
		.tokens		= {
			{ FILLED, FILLED, EMPTY, },
			{ EMPTY, FILLED, FILLED, },
		},
		.width		= 3,
		.height		= 2,
	}, {
		/*  Block:   #
		 *          ###
		 */
		.tokens		= {
			{ EMPTY, FILLED, EMPTY, },
			{ FILLED, FILLED, FILLED, },
		},
		.width		= 3,
		.height		= 2,
	},
};

/** Instanz des Spielstatus. */
static struct tetris_game tetris;


/** \brief	Einen Zufallsblock generieren.
 *
 * \param block		Der Zielpuffer fuer den zufaelligen Block.
 */
static void get_random_block(struct tetris_block *block)
{
	uint8_t index;

	/* Zufaelligen Index generieren. */
	index = shr3_get_random_value_byte(ARRAY_SIZE(tetris_blocks) - 1);

	/* Block am Index in Puffer kopieren. */
	*block = tetris_blocks[index];
}

/** \brief	Block um 90 Grad rotieren.
 *
 * \param to_block	Der Puffer in den der rotierte Block geschrieben wird.
 *
 * \param from_block	Der Block der rotiert werden soll.
 *
 * \param direction	1 -> Nach rechts rotieren.
 *			-1 -> Nach links rotieren.
 */
static void block_rotate(struct tetris_block *to_block,
			 const struct tetris_block *from_block,
			 int8_t direction)
{
	uint8_t x, y;
	int8_t sx, sy;
	uint8_t w, h;
	int8_t offs;

	if (!direction) {
		/* Keine Rotation. */
		*to_block = *from_block;
		return;
	}

	if (direction < 0) {
		/* Rotation nach links. */
		for (y = 0; y < from_block->height; y++) {
			for (x = 0; x < from_block->width; x++) {
				to_block->tokens[from_block->width - x - 1][y] =
					from_block->tokens[y][x];
			}
		}
	} else {
		/* Rotation nach rechts. */
		for (y = 0; y < from_block->height; y++) {
			for (x = 0; x < from_block->width; x++) {
				to_block->tokens[x][from_block->height - y - 1] =
					from_block->tokens[y][x];
			}
		}
	}

	sx = (int8_t)from_block->x;
	sy = (int8_t)from_block->y;
	/* Hoehe/Breite tauschen. */
	h = from_block->width;
	w = from_block->height;

	/* Position korrigieren. */
	offs = ((int8_t)h - (int8_t)w) / 2;
	sx += offs;
	sy -= offs;
	/* Randlage korrigieren. */
	if (sx < 0)
		sx = 0;
	if (sx + (int8_t)w > BOARD_WIDTH)
		sx = BOARD_WIDTH - w;
	if (sy < 0)
		sy = 0;
	if (sy + (int8_t)h > BOARD_HEIGHT)
		sy = BOARD_HEIGHT - h;

	to_block->x = (uint8_t)sx;
	to_block->y = (uint8_t)sy;
	to_block->height = h;
	to_block->width = w;
}

/** \brief	Einen Spielstein im Bitmap zeichnen. */
static void draw_token(struct bitmap *bitmap,
		       uint8_t x_offset, uint8_t y_offset,
		       const struct tetris_token *t)
{
	/* Hintergrundfarbe zeichnen:
	 * Schwarz wenn Stein sichtbar, sonst weiss. */
	bitmap_area_fill(bitmap,
			 x_offset, y_offset,
			 TOKEN_SIZE_PIX, TOKEN_SIZE_PIX,
			 !!(t->flags & TOKEN_FILLED));

	/* Linken Rand weiss zeichnen. */
	bitmap_draw_line(bitmap,
			 x_offset, y_offset,
			 x_offset + TOKEN_SIZE_PIX - 1, y_offset,
			 0);

	/* Oberen Rand weiss zeichnen. */
	bitmap_draw_line(bitmap,
			 x_offset + TOKEN_SIZE_PIX - 1, y_offset,
			 x_offset + TOKEN_SIZE_PIX - 1, y_offset + TOKEN_SIZE_PIX - 1,
			 0);
}

/** \brief	Prueft ob die gegebenenen globalen Koordinaten sich
 *		innerhalb eines Blocks befinden.
 *
 * \param block		Der Block.
 *
 * \param line		Die globale Y Koordinate.
 *
 * \param col		Die globale X Koordinate.
 *
 * \return		Gibt 1 zurueck, wenn die Koordinaten im Block sind.
 *			Sonst 0.
 */
static bool coord_is_in_block(const struct tetris_block *block,
			      uint8_t line, uint8_t col)
{
	if (col < block->x || col >= block->x + block->width)
		return 0;
	if (line < block->y || line >= block->y + block->height)
		return 0;
	return 1;
}

/** \brief	Pruefen ob ein Block mit Steinen auf dem Spielbrett kollidiert.
 *
 * \param block		Der Block.
 *
 * \return		Gibt 1 zurueck, wenn der Block kollidiert. Sonst 0.
 */
static bool check_block_collision(const struct tetris_block *block)
{
	const struct tetris_token *t_block, *t_board;
	uint8_t x, y;

	if (block->y >= BOARD_HEIGHT ||
	    block->y + block->height > BOARD_HEIGHT ||
	    block->x >= BOARD_WIDTH ||
	    block->x + block->width > BOARD_WIDTH) {
		/* Kollision mit Rand. */
		return 1;
	}

	/* Jeden Stein des Blocks pruefen. */
	for (y = 0; y < block->height; y++) {
		for (x = 0; x < block->width; x++) {
			t_block = &block->tokens[y][x];
			t_board = &tetris.board.tokens[block->y + y][block->x + x];
			if ((t_block->flags & TOKEN_FILLED) &&
			    (t_board->flags & TOKEN_FILLED)) {
				/* Sowohl der Block-Stein als auch der
				 * Spielfeld-Stein sind gefuellt.
				 * -> Kollision. */
				return 1;
			}
		}
	}

	/* Keine Kollision. */
	return 0;
}

/** \brief	Das "veraendert" Flag fuer eine Zeile setzen.
 *
 * \param line		Die Zeilennummer.
 */
static void line_set_modified(uint8_t line)
{
	/* "veraendert" Flag fuer Zeile setzen. */
	tetris.board.line_flags[line] |= LINE_MODIFIED;
	/* LCD update flag setzen. */
	tetris.lcd_update = 1;
}

/** \brief	Das "veraendert" Flag fuer alle Zeilen setzen. */
static void all_lines_set_modified(void)
{
	uint8_t line;

	for (line = 0; line < BOARD_HEIGHT; line++)
		line_set_modified(line);
}

/** \brief	Das "veraendert" Flag fuer alle Zeilen auf denen
 *		der Block liegt setzen.
 *
 * \param block		Der Block.
 */
static void block_lines_set_modified(const struct tetris_block *block)
{
	uint8_t y;

	for (y = 0; y < block->height; y++)
		line_set_modified(block->y + y);
}

/** \brief	Kopiert den Inhalt einer Zeile in eine andere.
 *
 * \param to_line	Die Zielzeile, in die kopiert werden soll.
 *			Wenn to_line ausserhalb des gueltigen Bereiches
 *			liegt, wird nicht kopiert.
 *
 * \param from_line	Die Quellzeile, von der kopiert werden soll.
 *			Wenn from_line ausserhalb des gueltigen Bereiches
 *			liegt, wird to_line geloescht.
 */
static void line_copy(int8_t to_line, int8_t from_line)
{
	if (to_line == from_line) {
		/* Quell- und Zielzeile sind gleich.
		 * -> Nicht kopieren. */
		return;
	}
	if (to_line < 0 || to_line >= BOARD_HEIGHT) {
		/* Zielzeile ist ausserhalb des Boards.
		 * -> Nicht kopieren. */
		return;
	}
	if (from_line < 0 || from_line >= BOARD_HEIGHT) {
		/* Quellzeile ist ausserhalb des Boards.
		 * -> Zielzeile "nullen". */
		memset(&tetris.board.tokens[to_line][0],
		       0, BOARD_WIDTH * sizeof(struct tetris_token));
	} else {
		/* Quell- und Zielzeile sind ok.
		 * -> kopieren. */
		memcpy(&tetris.board.tokens[to_line][0],
		       &tetris.board.tokens[from_line][0],
		       BOARD_WIDTH * sizeof(struct tetris_token));
	}
	/* "veraendert" Flag fuer die Zielzeile setzen. */
	line_set_modified(to_line);
}

/** \brief	Pruefen ob eine Zeile voll und zusammenhaengend ist.
 *		Die Zeile ist voll und zusammenhaengend, wenn alle Steine
 *		gefuellt sind.
 *
 * \param line		Die Zeilennummer.
 */
static bool line_is_coherent(uint8_t line)
{
	uint8_t col, flags;

	/* Alle TOKEN_FILLED Flags der Zeile ver-UND-en. */
	flags = TOKEN_FILLED;
	for (col = 0; col < BOARD_WIDTH; col++)
		flags &= tetris.board.tokens[line][col].flags;

	/* Wenn TOKEN_FILLED immer noch gesetzt ist, dann waren
	 * alle TOKEN_FILLED Flags in der Zeile gesetzt. */
	return !!(flags & TOKEN_FILLED);
}

/** \brief	Zusammenhaengende Zeilen im Spielfeld loeschen
 *		und den Rest nachruecken.
 */
static void zap_coherent_lines(void)
{
	int8_t from_line, to_line;
	bool line_zapped = 0, speed_increased = 0;

	/* Unten anfangen. */
	from_line = BOARD_HEIGHT - 1;
	to_line = BOARD_HEIGHT - 1;

	/* Alle Zeilen durchlaufen. */
	for ( ; from_line >= -1; from_line--) {
		if (from_line >= 0 && line_is_coherent(from_line)) {
			/* Zeile ist zusammenhaengend.
			 * Zeile nicht kopieren (= loeschen). */

			/* "zap"-Merker setzen. */
			line_zapped = 1;

			/* Gesamtpunktzahl erhoehen. */
			if (tetris.score <= UINT16_MAX - (tetris.level * SCORE_MUL))
				tetris.score += tetris.level * SCORE_MUL;

			/* Naechstes Level? */
			tetris.nr_zapped++;
			if (tetris.nr_zapped >= NR_ZAPPED_NEXT_LEVEL) {
				tetris.nr_zapped = 0;
				/* Geschwindigkeit und Level erhoehen. */
				if (tetris.timer_inter > 10)
					tetris.timer_inter -= 10;
				if (tetris.level < UINT8_MAX / SCORE_MUL)
					tetris.level++;
				/* "geschwindigkeit erhoeht"-Merker setzen. */
				speed_increased = 1;
			}
			continue;
		}
		/* Zeile kopieren. */
		line_copy(to_line, from_line);
		to_line--;
	}

	/* Ggf. Ton ausgeben. */
	if (speed_increased)
		buzzer_play(sound_incspeed, 1500, 0);
	else if (line_zapped)
		buzzer_play(sound_zapline, 1500, 0);
}

/** \brief	Einen Block mit dem Spielfeld verschmelzen.
 *
 * \param block		Der Block der verschmolzen werden soll.
 */
static void merge_block_with_board(const struct tetris_block *block)
{
	uint8_t x, y;

	/* Alle Zeilen auf denen der Block liegt als "veraendert" markieren. */
	block_lines_set_modified(block);

	/* Jeden gefuellten Stein des Blocks auf das Spielfeld kopieren. */
	for (y = 0; y < block->height; y++) {
		for (x = 0; x < block->width; x++) {
			if (!(block->tokens[y][x].flags & TOKEN_FILLED)) {
				/* Stein ist nicht gefuellt. */
				continue;
			}
			/* Stein ins Spielfeld kopieren. */
			tetris.board.tokens[block->y + y][block->x + x] = block->tokens[y][x];
		}
	}
}

/** \brief	Verschmelzt den fallenden Block mit dem Spielfeld. */
static void merge_falling_block(void)
{
	/* Stein verschmelzen. */
	merge_block_with_board(&tetris.falling_block);
	/* Fallender-Block-Merker ruecksetzen. */
	tetris.have_falling_block = 0;
	/* Zusammenhaengende Zeilen finden, zaehlen und loeschen. */
	zap_coherent_lines();
}

/** \brief	Schiebt den fallenden Block um eine Einheit herunter. */
static void falling_block_move_down(void)
{
	/* Position des Blocks inkrementieren. (= nach unten schieben) */
	tetris.falling_block.y++;

	/* Auf Kollision pruefen. */
	if (check_block_collision(&tetris.falling_block)) {
		/* Kollision! Block ist am Ende angekommen.
		 * Block in der vorherigen Position mit dem
		 * Spielfeld verschmelzen. */
		tetris.falling_block.y--;
		merge_falling_block();
		tetris.fast_down = false;
	} else {
		/* Keine Kollision.
		 * Veraenderte Zeilen als "veraendert" markieren. */
		line_set_modified(tetris.falling_block.y - 1);
		block_lines_set_modified(&tetris.falling_block);
	}
}

/** \brief	Tetris Spielbrett auf LCD neuzeichnen. */
static void tetris_game_screen_update(void)
{
	DEFINE_BITMAP(line_bitmap,
		      TOKEN_SIZE_PIX, BOARD_WIDTH * TOKEN_SIZE_PIX);
	struct tetris_token *t;
	uint8_t line, col;
	uint8_t x;

	/* Spielfeld Zeilenweise zeichnen und auf LCD ausgeben */
	for (line = 0; line < BOARD_HEIGHT; line++) {
		if (!(tetris.board.line_flags[line] & LINE_MODIFIED)) {
			/* Zeile wurde nicht veraendert.
			 * -> nicht neuzeichnen. */
			continue;
		}

		/* Eine Zeile zeichnen. */
		for (col = 0; col < BOARD_WIDTH; col++) {
			t = NULL;
			/* Wenn wir im Bereich des fallenden Blocks sind,
			 * Steine aus diesem zeichnen. */
			if (tetris.have_falling_block &&
			    coord_is_in_block(&tetris.falling_block, line, col)) {
				t = &tetris.falling_block.tokens
						[line - tetris.falling_block.y]
						[col - tetris.falling_block.x];
				if (!(t->flags & TOKEN_FILLED))
					t = NULL;
			}
			/* Wenn nicht im Bereich des fallenden Blocks,
			 * Steine aus dem Spielfeld zeichnen. */
			if (!t)
				t = &tetris.board.tokens[line][col];
			/* Den Stein zeichnen. */
			draw_token(BITMAP_PTR(line_bitmap),
				   0, col * TOKEN_SIZE_PIX, t);
		}

		/* Zeile auf LCD ausgeben. */
		x = (BOARD_HEIGHT - line - 1) * TOKEN_SIZE_PIX;
		ks0108_draw_bitmap(BITMAP_PTR(line_bitmap), x, 0);

		/* "veraendert" Flag fuer Zeile ruecksetzen. */
		tetris.board.line_flags[line] &= ~LINE_MODIFIED;
	}
}

/** \brief	Intro auf LCD neuzeichnen. */
static void tetris_intro_screen_update(void)
{
	struct tetris_block block;
	DEFINE_BITMAP(bm_horiz, 14, 48);
	DEFINE_BITMAP(bm_vert, 23, 16);
	char buffer[12];

	/* Z-Block zeichnen. */
	block = tetris_blocks[6];
	block.x = 3;
	block.y = 0;
	merge_block_with_board(&block);

	/* L-Block zeichnen. */
	block = tetris_blocks[1];
	block.x = 1;
	block.y = 9;
	merge_block_with_board(&block);

	/* O-Block zeichnen. */
	block = tetris_blocks[3];
	block.x = 5;
	block.y = 12;
	merge_block_with_board(&block);

	/* Bloecke auf LCD ausgeben. */
	tetris_game_screen_update();

	/* Horizontales "Tetr" auf LCD zeichnen. */
	BITMAP_PTR(bm_horiz)->rotation = BITMAP_ROT_90DEG;
	bitmap_draw_string(BITMAP_PTR(bm_horiz), 0, 0, &font_10x14,
			   to_memx(PSTR("Tetr")));
	ks0108_draw_bitmap(BITMAP_PTR(bm_horiz), 79, 8);

	/* Vertikales "is" auf LCD zeichnen. */
	BITMAP_PTR(bm_vert)->rotation = BITMAP_ROT_180DEG;
	bitmap_draw_string(BITMAP_PTR(bm_vert), 0, 0, &font_10x14,
			   to_memx(PSTR("is")));
	ks0108_draw_bitmap(BITMAP_PTR(bm_vert), 55, 40);

	/* Versionsnummer auf LCD zeichnen. */
	bitmap_clear(BITMAP_PTR(bm_horiz));
	buffer[0] = 'v';
	utoa(VERSION, buffer + 1, 10);
	bitmap_draw_string(BITMAP_PTR(bm_horiz), 0, 0, &font_10x14,
			   to_memx(buffer));
	ks0108_draw_bitmap(BITMAP_PTR(bm_horiz), 0, 0);
}

/** \brief	Gameover Status auf LCD neuzeichnen. */
static void tetris_gameover_screen_update(void)
{
	DEFINE_BITMAP(bm, 18, 64);
	char buffer[6];

	/* Bitmap rotieren. */
	BITMAP_PTR(bm)->rotation = BITMAP_ROT_90DEG;

	/* String "Score" auf LCD ausgeben. */
	bitmap_draw_string(BITMAP_PTR(bm), 0, 2, &font_10x14,
			   to_memx(PSTR("Score")));
	ks0108_draw_bitmap(BITMAP_PTR(bm), 79, 0);

	/* Bitmap loeschen. */
	bitmap_clear(BITMAP_PTR(bm));

	/* Anzahl der geloeschten Zeilen als Score ausgeben. */
	utoa(tetris.score, buffer, 10);
	bitmap_draw_string(BITMAP_PTR(bm), 0, 2, &font_10x14, to_memx(buffer));
	ks0108_draw_bitmap(BITMAP_PTR(bm), 61, 0);
}

/** \brief	LCD Update durchfuehren, wenn noetig. */
static void tetris_lcd_update(void)
{
	if (!tetris.lcd_update) {
		/* Kein LCD Update angefordert. */
		return;
	}

	/* LCD Update je nach Spielzustand durchfuehren. */
	switch (tetris.state) {
	case TETRIS_INTRO:
		tetris_intro_screen_update();
		break;
	case TETRIS_INGAME:
		tetris_game_screen_update();
		break;
	case TETRIS_GAMEOVER:
		tetris_gameover_screen_update();
		break;
	}

	/* LCD Update Merker ruecksetzen. */
	tetris.lcd_update = 0;
}

/** \brief	Einen neuen "fallenden Block" erzeugen. */
static void create_new_falling_block(void)
{
	/* Fallender-Block-Merker ruecksetzen. */
	tetris.have_falling_block = 1;
	/* Zufaelligen fallenden Block generieren. */
	get_random_block(&tetris.falling_block);
	/* Block in X zentrieren. */
	tetris.falling_block.x = BOARD_WIDTH / 2 -
				 tetris.falling_block.width / 2;
	/* Zeilen als "veraendert" markieren. */
	block_lines_set_modified(&tetris.falling_block);
}

/** \brief	TETRIS_INTRO Status einleiten. */
static void tetris_enter_state_intro(void)
{
	/* Spielbrett loeschen. */
	memset(&tetris.board, 0, sizeof(tetris.board));
	/* Alle Zeilen als "veraendert" markieren. */
	all_lines_set_modified();
	/* Spielstatus initialisieren. */
	tetris.state = TETRIS_INTRO;
	tetris.have_falling_block = 0;
	tetris.lcd_update = 1;
	/* Intro-Musik abspielen. */
	buzzer_play(music_korobeiniki, 1500, 1);
}

/** \brief	TETRIS_INGAME Status einleiten. */
static void tetris_enter_state_ingame(void)
{
	/* Spielbrett loeschen. */
	memset(&tetris.board, 0, sizeof(tetris.board));
	/* Alle Zeilen als "veraendert" markieren. */
	all_lines_set_modified();
	/* Spielstatus initialisieren. */
	tetris.state = TETRIS_INGAME;
	tetris.timer_inter = TIMER_INTER_DEFAULT;
	tetris.timer_count = tetris.timer_inter;
	create_new_falling_block();
	tetris.nr_zapped = 0;
	tetris.score = 0;
	tetris.level = 1;
	tetris.lcd_update = 1;
	/* Intro-Musik abbrechen. */
	buzzer_stop();
}

/** \brief	TETRIS_GAMEOVER Status einleiten. */
static void tetris_enter_state_gameover(void)
{
	/* Spielstatus initialisieren. */
	tetris.state = TETRIS_GAMEOVER;
	tetris.lcd_update = 1;
}

/** \brief	Spieltimer verarbeiten. */
static void tetris_timer_handle(void)
{
	if (tetris.have_falling_block) {
		/* Fallenden Block nach unten verschieben. */
		falling_block_move_down();
	} else {
		/* Einen neuen fallenden Block erzeugen. */
		create_new_falling_block();
		/* Auf Kollision des neuen Blocks pruefen. */
		if (check_block_collision(&tetris.falling_block)) {
			/* Neuer Block kollidiert sofort nach der Erstellung.
			 * -> Game over. */
			merge_block_with_board(&tetris.falling_block);
			/* Spielfeld neuzeichnen. */
			tetris_game_screen_update();
			/* Game-over Ton abspielen. */
			buzzer_play(sound_gameover, 1500, 0);
			/* Spielstatus auf Game-over setzen.  */
			tetris_enter_state_gameover();
		}
	}
}

/** \brief	Tasterereignis fuer das Spiel verarbeiten. */
static void tetris_game_handle_button(const struct button_event *event)
{
	struct tetris_block rotated_block;

	switch (event->button) {
	case BTN_UP:
		break;
	case BTN_LEFT:
		if (event->action != BTNACT_PRESSED)
			break;
		if (!tetris.have_falling_block)
			break;
		/* "links"-Taster gedrueckt.
		 * Fallenden Block nach links verschieben. */
		tetris.falling_block.x--;
		if (check_block_collision(&tetris.falling_block))
			tetris.falling_block.x++;
		else
			block_lines_set_modified(&tetris.falling_block);
		break;
	case BTN_RIGHT:
		if (event->action != BTNACT_PRESSED)
			break;
		if (!tetris.have_falling_block)
			break;
		/* "rechts"-Taster gedrueckt.
		 * Fallenden Block nach rechts verschieben. */
		tetris.falling_block.x++;
		if (check_block_collision(&tetris.falling_block))
			tetris.falling_block.x--;
		else
			block_lines_set_modified(&tetris.falling_block);
		break;
	case BTN_DOWN:
		if (event->action == BTNACT_PRESSED) {
			if (!tetris.have_falling_block)
				break;
			/* "unten"-Taster gedrueckt.
			 * Fallenden Block schnell nach unten verschieben. */
			tetris.timer_count = min(TIMER_INTER_FASTDOWN, tetris.timer_inter);
			tetris.fast_down = true;
		} else {
			tetris.fast_down = false;
		}
		break;
	case BTN_A:
		if (event->action != BTNACT_PRESSED)
			break;
		if (!tetris.have_falling_block)
			break;
		/* "A"-Taster gedrueckt.
		 * Fallenden Block nach rechts rotieren. */
		block_rotate(&rotated_block, &tetris.falling_block, 1);
		if (!check_block_collision(&rotated_block)) {
			block_lines_set_modified(&tetris.falling_block);
			block_lines_set_modified(&rotated_block);
			tetris.falling_block = rotated_block;
		}
		break;
	case BTN_B:
		if (event->action != BTNACT_PRESSED)
			break;
		if (!tetris.have_falling_block)
			break;
		/* "B"-Taster gedrueckt.
		 * Fallenden Block nach links rotieren. */
		block_rotate(&rotated_block, &tetris.falling_block, -1);
		if (!check_block_collision(&rotated_block)) {
			block_lines_set_modified(&tetris.falling_block);
			block_lines_set_modified(&rotated_block);
			tetris.falling_block = rotated_block;
		}
		break;
	case NR_BTN:
		break;
	}
}

/** \brief	Tasterereignis fuer das Intro verarbeiten. */
static void tetris_intro_handle_button(const struct button_event *event)
{
	switch (event->button) {
	case BTN_UP:
		if (event->action != BTNACT_PRESSED)
			break;
		/* "up"-Taster gedrueckt.
		 * Lautstaerke erhoehen. */
		buzzer_volume_set(buzzer_volume_get() + 1);
		break;
	case BTN_LEFT:
		break;
	case BTN_RIGHT:
		break;
	case BTN_DOWN:
		if (event->action != BTNACT_PRESSED)
			break;
		/* "down"-Taster gedrueckt.
		 * Lautstaerke verringern. */
		buzzer_volume_set(buzzer_volume_get() - 1);
		break;
	case BTN_A:
	case BTN_B:
		if (event->action != BTNACT_PRESSED)
			break;
		/* "A"- oder "B"-Taster gedrueckt.
		 * Spiel starten. */
		tetris_enter_state_ingame();
		break;
	case NR_BTN:
		break;
	}
}

/** \brief	Tasterereignis fuer die Statusanzeige verarbeiten. */
static void tetris_gameover_handle_button(const struct button_event *event)
{
	switch (event->button) {
	case BTN_UP:
	case BTN_LEFT:
	case BTN_RIGHT:
	case BTN_DOWN:
		break;
	case BTN_A:
	case BTN_B:
		if (event->action != BTNACT_PRESSED)
			break;
		/* "A"- oder "B"-Taster gedrueckt.
		 * Intro anzeigen. */
		tetris_enter_state_intro();
		break;
	case NR_BTN:
		break;
	}
}

/** \brief	Tasterereignis verarbeiten. */
static void tetris_handle_button(const struct button_event *event)
{
	/* Tasterereignis je nach Spielzustand verarbeiten. */
	switch (tetris.state) {
	case TETRIS_INTRO:
		tetris_intro_handle_button(event);
		break;
	case TETRIS_INGAME:
		tetris_game_handle_button(event);
		break;
	case TETRIS_GAMEOVER:
		tetris_gameover_handle_button(event);
		break;
	}
}

/** \brief	Tasternverzoegerungen fuer autofire. */
static uint8_t tetris_get_autofire_delay(enum button button)
{
	switch (button) {
	case BTN_LEFT:
	case BTN_RIGHT:
		if (tetris.state == TETRIS_INGAME)
			return TIMER_INTER_MOVE;
		break;
	case BTN_UP:
	case BTN_DOWN:
		if (tetris.state == TETRIS_INTRO)
			return TIMER_INTER_VOLUPDOWN;
		break;
	case BTN_A:
	case BTN_B:
	case NR_BTN:
		break;
	}

	return 0;
}

/** \brief	Periodische 100 Hz Abarbeitung. */
static void tetris_100hz_tick(void)
{
	switch (tetris.state) {
	case TETRIS_INTRO:
		break;
	case TETRIS_INGAME:
		/* Spiel-Tick dekrementieren und auswerten. */
		tetris.timer_count--;
		if (!tetris.timer_count) {
			/* Spiel-Tick verarbeiten und ruecksetzen. */
			tetris_timer_handle();
			if (tetris.fast_down) {
				tetris.timer_count = TIMER_INTER_FASTDOWN;
			} else {
				tetris.timer_count = tetris.timer_inter;
			}
		}
		break;
	case TETRIS_GAMEOVER:
		break;
	}
}

/** \brief	Periodische Abarbeitung. */
static void tetris_work(void)
{
	/* LCD ggf. neuzeichnen. */
	tetris_lcd_update();
}

/** \brief	Tetris initialisieren. */
static void tetris_init(void)
{
	memset(&tetris, 0, sizeof(tetris));
	tetris_enter_state_intro();
}

/** Schnittstelle zur Tetris Spiellogik anlegen. */
struct game_interface tetris_game_interface = {
	.handle_button		= tetris_handle_button,
	.get_autofire_delay	= tetris_get_autofire_delay,
	.do_100hz_tick		= tetris_100hz_tick,
	.periodic_work		= tetris_work,
	.init			= tetris_init,
};
bues.ch cgit interface