-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris.cxx
2909 lines (2867 loc) · 77.6 KB
/
tetris.cxx
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
#include<cstdint>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cmath>
#include<map>
#include<windows.h>
#include<windowsx.h>
#include<shlwapi.h>
#define __IN_CODE__
#include"tetris.rc"
using namespace std;
//{{{ Constants
const uint32_t red = 0x0000FF;
const uint32_t orange = 0x007FFF;
const uint32_t yellow = 0x00BFBF;
const uint32_t green = 0x007F00;
const uint32_t blue = 0xFF0000;
const uint32_t cyan = 0xBFBF00;
const uint32_t purple = 0x7F007F;
const uint32_t white = 0xFFFFFF;
const uint32_t black = 0x000000;
const uint32_t gray = 0x3F3F3F;
const uint32_t gold = 0x00D7FF;
const uint32_t lime = 0x00FF00;
const uint32_t null = (uint32_t)-1;
const unsigned int width = 10;
const unsigned int height = 20;
const int unit = 30;
const int border = unit / 10;
const int client_width = (width + 8) * unit;
const int client_height = (height + 2) * unit;
//}}}
//{{{ Memory control
typedef void (*collector)(void *);
//{{{ These wrappings must be done to fill up the gaps of ABI
void delete_object (void *obj) { DeleteObject ((HGDIOBJ) obj); }
void delete_dc (void *obj) { DeleteDC ((HDC) obj); }
void destroy_icon (void *obj) { DestroyIcon ((HICON) obj); }
//}}}
class gc {
public:
gc(void *obj, collector fun) { cons(false, obj, fun); }
gc(collector fun, void *obj) { cons(false, obj, fun); }
gc(bool st, void *obj, collector fun) { cons(st, obj, fun); }
~gc(void) {
collector(object);
if (dynamic)
delete next_gc;
}
static void garbage_collect_dynamic(void);
static void garbage_collect_static(void);
static void garbage_collect(void) {
garbage_collect_dynamic();
garbage_collect_static();
}
private:
static gc *allocated_objects;
static gc *allocated_objects_static;
void cons(bool, void *, collector);
bool dynamic;
void *object;
collector collector;
gc *next_gc;
};
gc *gc::allocated_objects;
gc *gc::allocated_objects_static;
void gc::cons(bool st, void *obj, ::collector fun) {
object = obj;
collector = fun;
dynamic = !st;
if (st) {
next_gc = allocated_objects_static;
allocated_objects_static = this;
} else {
next_gc = allocated_objects;
allocated_objects = this;
}
}
void gc::garbage_collect_dynamic(void) {
delete allocated_objects;
allocated_objects = NULL;
}
void gc::garbage_collect_static(void) {
for (gc *ptr = allocated_objects_static; ptr; ptr = ptr->next_gc)
ptr->collector(ptr->object);
}
//}}}
//{{{ Graphic control
HWND window;
HINSTANCE instance;
HDC hdc_main, hdc_sub;
typedef enum { normal, translucent, dim } draw_type;
const int num_type= 3;
//{{{ Color operation
uint32_t color_mix(uint32_t color_bg, uint32_t color, double alpha) {
if (alpha < 0)
alpha = 0;
else if (alpha > 1)
alpha = 1;
uint32_t c = 0;
c |= (uint8_t)((color_bg >> 16 & 0xFF) * (1 - alpha) +\
(color >> 16 & 0xFF) * alpha);
c <<= 8;
c |= (uint8_t)((color_bg >> 8 & 0xFF) * (1 - alpha) +\
(color >> 8 & 0xFF) * alpha);
c <<= 8;
c |= (uint8_t)((color_bg & 0xFF) * (1 - alpha) +\
(color & 0xFF) * alpha);
return c;
}
uint32_t color_saturation(uint32_t color, double alpha) {
// We use HSL model's saturation
if (alpha < 0)
alpha = 0;
uint8_t r = color & 0xFF;
uint8_t g = color >> 8 & 0xFF;
uint8_t b = color >> 16 & 0xFF;
uint8_t *max, *min, *mid;
if (r < g)
max = &g, min = &r;
else
max = &r, min = &g;
if (*max < b)
mid = max, max = &b;
else if (*min > b)
mid = min, min = &b;
else
mid = &b;
if (*max == *min)
return color;
double cc1, c1 = ((double)(*min) + .5) / 256;
double c2 = ((double)(*mid) + .5) / 256;
double cc3, c3 = ((double)(*max) + .5) / 256;
double l = (c1 + c3) / 2;
if (alpha > 1) {
if (l < .5) {
if ((l - c1) * alpha > l - 1./512)
alpha = (l - 1./512) / (l - c1);
} else {
if ((c3 - l) * alpha > 1-1./512 - l)
alpha = (1-1./512 - l) / (c3 - l);
}
}
*min = (uint8_t)(256 * (cc1 = l + (c1 - l) * alpha));
*max = (uint8_t)(256 * (cc3 = l + (c3 - l) * alpha));
*mid = (uint8_t)(256 * (c2 < l ? cc3 - (c3 - c2) * alpha :\
cc1 + (c2 - c1) * alpha));
return (uint32_t)r | (uint32_t)g << 8 | (uint32_t)b << 16;
}
//}}}
//{{{ Draw a 3D frame
void draw_frame(HDC hdc, int pos_x, int pos_y, int width, int height, int border,\
uint32_t color, uint32_t color_u, uint32_t color_d) {
RECT r1, r2;
if (border * 2 > width || border * 2 > height)
return;
if (border > 0) {
HBRUSH brush_u = CreateSolidBrush(color_u);
HBRUSH brush_d = CreateSolidBrush(color_d);
HPEN pen = CreatePen(PS_SOLID, 1, color_mix(color_u, color_d, .5));
HPEN old_pen = (HPEN)SelectObject(hdc, pen);
POINT p[3];
p[1].x = r1.left = (p[0].x = p[2].x = r2.left = pos_x) + border;
r2.right = (r1.right = pos_x + width) - border;
p[1].y = p[2].y = r2.top = (p[0].y = r2.bottom = pos_y + height) - border;
r1.bottom = (r1.top = pos_y) + border;
FillRect(hdc, &r1, brush_u);
FillRect(hdc, &r2, brush_d);
HRGN region = CreatePolygonRgn(p, 3, ALTERNATE);
FillRgn(hdc, region, brush_u);
DeleteObject(region);
p[0].y--; p[1].y--;
Polyline(hdc, p, 2);
int t = r1.right;
r1.right = r1.left;
r1.left = r2.left;
p[1].x = r2.left = r2.right;
p[0].x = p[2].x = r2.right = t;
t = r1.bottom;
r1.bottom = r2.top;
p[1].y = p[2].y = r2.top = t;
p[0].y = r1.top;
FillRect(hdc, &r1, brush_u);
FillRect(hdc, &r2, brush_d);
region = CreatePolygonRgn(p, 3, ALTERNATE);
FillRgn(hdc, region, brush_d);
DeleteObject(region);
p[0].x--; p[1].x--;
Polyline(hdc, p, 2);
SelectObject(hdc, old_pen);
DeleteObject(pen);
DeleteObject(brush_u);
DeleteObject(brush_d);
r1.left = r1.right;
r1.right = r2.left;
r1.top = r2.top;
} else {
r1.left = pos_x;
r1.right = pos_x + width;
r1.top = pos_y;
r1.bottom = pos_y + height;
}
HBRUSH brush = CreateSolidBrush(color);
FillRect(hdc, &r1, brush);
DeleteObject(brush);
}
inline void fill_bg(HDC hdc, int width, int height, uint32_t color) {
draw_frame(hdc, 0, 0, width, height, 0, color, 0, 0);
}
inline void fill_bg(HDC hdc, int width, int height) {
fill_bg(hdc, width, height, black);
}
//}}}
//{{{ New bitmap
HBITMAP new_bitmap(int width, int height) {
HDC hdc = GetDC(window);
HBITMAP bmp = CreateCompatibleBitmap(hdc, width, height);
ReleaseDC(window, hdc);
return bmp;
}
//}}}
//{{{ Font control
extern unsigned char ascii_16[] asm ("_binary_ASC16_start");
extern unsigned char ascii_24[] asm ("_binary_ASC24_start");
void put_char(HDC hdc, bool big, bool slant, int scale,\
int pos_x, int pos_y, uint8_t c, uint32_t color) {
const int slant_ratio = 5;
bool mirror = c & 0x80;
int i, j;
c &= 0x7F;
if (big) {
uint16_t *ptr, t;
int k = 24 * scale;
int mask = 8 - mirror;
ptr = (uint16_t *)(ascii_24 + c * 48);
for (j = 0; j < 24; j++, k -= scale, ptr++)
for (t = 0x8000, i = 0; i < 16; i++, t >>= 1)
if (*ptr & t) {
int x = pos_x + (i ^ mask) * scale;
int y = pos_y + j * scale;
int offset = k;
for (int j = 0; j < scale; j++, offset--)
for (int i = 0; i < scale; i++)
SetPixel(hdc,\
x + (slant ? offset / slant_ratio : 0) + i,\
y + j, color);
}
} else {
uint8_t *ptr, t;
int k = 16 * scale;
int mask = mirror ? 7 : 0;
ptr = ascii_16 + c * 16;
for (j = 0; j < 16; j++, k -= scale, ptr++)
for (t = 0x80, i = 0; i < 8; i++, t >>= 1)
if (*ptr & t) {
int x = pos_x + (i ^ mask) * scale;
int y = pos_y + j * scale;
int offset = k;
for (int j = 0; j < scale; j++, offset--)
for (int i = 0; i < scale; i++)
SetPixel(hdc,\
x + (slant ? offset / slant_ratio : 0) + i,\
y + j, color);
}
}
}
void put_string(HDC hdc, bool big, bool slant, int scale,\
int pos_x, int pos_y, const char *s, int len, int space, uint32_t color) {
if (scale <= 0)
return;
int i, x, step = scale * (big ? 16 : 8) + space;
for (i = 0, x = pos_x; (len == -1 || i < len) && s[i]; x += step, i++)
put_char(hdc, big, slant, scale, x, pos_y, s[i], color);
}
//}}}
//}}}
//{{{ Timer control
typedef enum {
timer_drop, timer_lock, timer_animation,
timer_action, timer_second, timer_key
} timer_id;
const int num_timer = 6;
const int initial_num_controlled = 5;
int num_controlled = initial_num_controlled;
bool clear_timer_queue;
void kill_all_timer(void);
void set_timer(timer_id timer, int delay) {
static clock_t start[num_timer];
static int remain[num_timer];
static gc *dummy = new gc(NULL, [] (void *) {
kill_all_timer();
});
if (delay > 0) {
start[timer] = clock();
remain[timer] = delay;
SetTimer(window, timer + 1, delay, NULL);
} else if (delay == -2) {
remain[timer] = 0;
start[timer] = (clock_t)0;
KillTimer(window, timer + 1);
} else if (delay == -1) {
if (start[timer] != (clock_t)0) {
if ((remain[timer] -=\
(clock() - start[timer]) /\
(CLOCKS_PER_SEC / 1000)) <= 0)
remain[timer] = 1;
start[timer] = (clock_t)0;
KillTimer(window, timer + 1);
}
} else {
if (start[timer] == (clock_t)0 && remain[timer] > 0) {
start[timer] = clock();
SetTimer(window, timer + 1, remain[timer], NULL);
}
}
}
inline void kill_timer(timer_id timer) {
set_timer(timer, -2);
}
inline void kill_all_timer(void) {
for (int i = 0; i < num_timer; i++)
kill_timer((timer_id)i);
}
inline void stop_timer(timer_id timer) {
set_timer(timer, -1);
}
inline void resume_timer(timer_id timer) {
set_timer(timer, 0);
}
//}}}
//{{{ Definition of board
//{{{ Base class of board
class board {
public:
board(int x, int y, int w, int h) { init_board(x, y, w, h); }
board(const char *title, int x, int y, int w, int h) {
init_board(x, y, w, h);
init_title(title);
}
~board(void) { if (saved_bmp) DeleteObject(saved_bmp); }
void draw_bitmap(HBITMAP, int, int, int, int, int, int);
void draw_bitmap(HBITMAP bmp, int x, int y, int w, int h) {
draw_bitmap(bmp, 0, 0, x, y, w, h);
}
void draw_bitmap(HBITMAP bmp, int w, int h) {
draw_bitmap(bmp, 0, 0, w, h);
}
void save_bitmap(void);
void restore_bitmap(void);
void flush_board(bool, RECT *);
void flush_board(RECT *area) { flush_board(true, area); }
void flush_board(bool erase) { flush_board(erase, NULL); }
void flush_board(void) { flush_board((RECT *)NULL); }
int get_width(void) { return width; }
int get_height(void) { return height; }
protected:
int pos_x, pos_y;
int width, height;
private:
void init_board(int, int, int, int);
void init_title(const char *);
RECT rect;
HBITMAP saved_bmp = NULL;
};
//{{{ Initialize board and title
void board::init_board(int x, int y, int w, int h) {
rect.left = pos_x = x;
rect.top = pos_y = y;
rect.right = x + (width = w);
rect.bottom = y + (height = h);
draw_frame(hdc_main, x - border, y - border,\
w + 2 * border, h + 2 * border, border, black,\
color_mix(black, gray, .5), color_mix(white, gray, .5));
}
void board::init_title(const char *title) {
put_string(hdc_main, true, false, 1,\
pos_x + (width - strlen(title) * (16 - 3)) / 2,\
pos_y - 3 * border - 24, title, -1, -3, white);
}
//}}}
//{{{ Draw a bitmap to board area with truncation
void board::draw_bitmap(HBITMAP bmp, int dx, int dy, int x, int y, int w, int h) {
if (x < 0)
dx -= x, w += x, x = 0;
if (y < 0)
dy -= y, h += y, y = 0;
if (w > width - x)
w = width - x;
if (h > height - y)
h = height - y;
if (w <= 0 || h <= 0)
return;
HBITMAP old_bmp = (HBITMAP)SelectObject(hdc_sub, bmp);
BitBlt(hdc_main, pos_x + x, pos_y + y, w, h, hdc_sub, dx, dy, SRCCOPY);
SelectObject(hdc_sub, old_bmp);
RECT area;
area.right = (area.left = x) + w;
area.bottom = (area.top = y) + h;
flush_board(false, &area);
}
//}}}
//{{{ Save/Restore content of board area
void board::save_bitmap(void) {
if (!saved_bmp) {
saved_bmp = new_bitmap(width, height);
}
HBITMAP old_bmp = (HBITMAP)SelectObject(hdc_sub, saved_bmp);
BitBlt(hdc_sub, 0, 0, width, height, hdc_main, pos_x, pos_y, SRCCOPY);
SelectObject(hdc_sub, old_bmp);
}
void board::restore_bitmap(void) {
if (saved_bmp) {
draw_bitmap(saved_bmp, width, height);
flush_board(false, NULL);
}
}
//}}}
//{{{ Flush board with given area
void board::flush_board(bool erase, RECT *area) {
if (!area) {
area = ▭
} else {
if (area->left < 0)
area->left = 0;
if (area->top < 0)
area->top = 0;
if (area->right > width)
area->right = width;
if (area->bottom > height)
area->bottom = height;
if (area->left >= area->right || area->top >= area->bottom)
return;
area->left += pos_x;
area->right += pos_x;
area->top += pos_y;
area->bottom += pos_y;
}
if (erase)
FillRect(hdc_main, area, (HBRUSH)GetStockObject(BLACK_BRUSH));
InvalidateRect(window, area, false);
}
//}}}
//}}}
//{{{ Derived class of game board
class game_board : public board {
public:
game_board(int x, int y, int w, int h) : board(x, y, w * unit, h * unit) {}
game_board(const char *title, int x, int y, int w, int h) :\
board(title, x, y, w * unit, h * unit) {}
void show_block(int, int, int, int, uint32_t, draw_type);
void show_block(int, int, uint32_t, draw_type);
void erase_block(int, int, int, int);
void erase_block(int, int);
void erase_lines(int, int);
void move_line(int, int);
};
void game_board_collector(void *p) {
delete (game_board *)p;
}
//{{{ Show a block
void game_board::show_block(int offset_x, int offset_y, int index_x, int index_y,\
uint32_t color, draw_type type) {
static map<uint32_t, HBITMAP> stored_bmp[num_type];
static gc dummy(true, stored_bmp, [] (void *p) {
map<uint32_t, HBITMAP>* array = (map<uint32_t, HBITMAP>*)p;
for (int i = 0; i < num_type; i++) {
for (auto e : array[i])
DeleteObject(e.second);
array[i].clear();
}
});
map<uint32_t, HBITMAP>::iterator element;
HBITMAP block;
if ((element = stored_bmp[type].find(color)) == stored_bmp[type].end()) {
uint32_t color_c, color_u, color_d;
color_u = color_mix(white, color, .5);
color_d = color_mix(black, color, .5);
switch (type) {
case normal:
color_c = color;
break;
case translucent:
color_c = color_mix(black, color, .3);
color_u = color_mix(black, color_u, .3);
color_d = color_mix(black, color_d, .3);
break;
case dim:
color_c = color_saturation(color, .7);
color_u = color_saturation(color_u, .7);
color_d = color_saturation(color_d, .7);
break;
}
block = new_bitmap(unit, unit);
HBITMAP old_bmp = (HBITMAP)SelectObject(hdc_sub, block);
draw_frame(hdc_sub, 0, 0, unit, unit, unit / 10,\
color_c, color_u, color_d);
SelectObject(hdc_sub, old_bmp);
stored_bmp[type][color] = block;
} else {
block = element->second;
}
draw_bitmap(block,\
offset_x + index_x * unit,\
height - offset_y - (index_y + 1) * unit,\
unit, unit);
}
inline void game_board::show_block(int index_x, int index_y, uint32_t color, draw_type type) {
show_block(0, 0, index_x, index_y, color, type);
}
//}}}
//{{{ Erase a block
void game_board::erase_block(int offset_x, int offset_y, int index_x, int index_y) {
RECT area;
area.right = (area.left = offset_x + index_x * unit) + unit;
area.bottom = (area.top = height - offset_y - (index_y + 1) * unit) + unit;
flush_board(&area);
}
void game_board::erase_block(int index_x, int index_y) {
erase_block(0, 0, index_x, index_y);
}
//}}}
//{{{ Erase lines
void game_board::erase_lines(int start, int end) {
RECT area;
area.left = 0;
area.right = width;
area.top = height - (end + 1) * unit;
area.bottom = height - start * unit;
flush_board(&area);
}
//}}}
//{{{ Move line src to line dst
void game_board::move_line(int dst, int src) {
if (src == -1) {
erase_lines(dst, dst);
} else if (src >= 0 && src * unit < height && dst >= 0 && dst * unit < height) {
BitBlt(hdc_main, pos_x, pos_y + height - (dst + 1) * unit, width, unit,\
hdc_main, pos_x, pos_y + height - (src + 1) * unit, SRCCOPY);
RECT area;
area.left = 0;
area.right = width;
area.top = (area.bottom = height - dst * unit) - unit;
flush_board(false, &area);
}
}
//}}}
//}}}
//{{{ Derived class of stat board
class stat_board : public board {
public:
stat_board(int x, int y) : board(x, y, 150, 30) {}
stat_board(const char *title, int x, int y) : board(title, x, y, 150, 30) {}
void show_digit(int);
};
void stat_board_collector(void *p) {
delete (stat_board *)p;
}
//{{{ Show a digit
void stat_board::show_digit(int digit) {
const int max_digit = 1000000000;
static HBITMAP stored_num[10];
static gc dummy(true, stored_num, [] (void *p) {
HBITMAP* array = (HBITMAP*)p;
for (int i = 0; i < 10; i++)
if (array[i])
DeleteObject(array[i]);
});
if (digit < 0)
digit = digit % max_digit + max_digit;
else if (digit >= max_digit)
digit %= max_digit;
HBITMAP num;
for (int i = 8; i >= 0; digit /= 10, i--) {
uint8_t d = digit % 10;
if (!(num = stored_num[d])) {
num = new_bitmap(16, 30);
HBITMAP old_bmp = (HBITMAP)SelectObject(hdc_sub, num);
fill_bg(hdc_sub, 16, 30);
put_char(hdc_sub, false, false, 2, 0, 1, '0' + d, lime);
SelectObject(hdc_sub, old_bmp);
stored_num[d] = num;
}
draw_bitmap(num, 4 + 16 * i, 0, 16, 30);
}
}
//}}}
//}}}
//}}}
//{{{ UI control -- Part I
game_board *main_board, *next_board;
stat_board *level_board, *score_board, *lines_board;
//{{{ Animation
void (*animation)(void);
void (*continuation)(void);
inline void stop_animation(void) { animation = continuation = NULL; }
//{{{ Default animation end
void default_animation_end(void) {
animation = NULL;
if (continuation) {
void (*cont)(void) = continuation;
continuation = NULL;
cont();
}
}
//}}}
//{{{ Show various languages
void show_language(void) {
static int frame, offset;
static HBITMAP bmp = LoadBitmap(instance, MAKEINTRESOURCE(IDB_LANG));
static gc *dummy = new gc(bmp, delete_object);
const int num = 13;
const int w = 150;
const int h = 30;
const int8_t fix[] = { 0, 0, 0, 0, 0, 0, 5, 5, 0, 5, 0, -5, 0 };
int wd = next_board->get_width();
int ht = next_board->get_height();
int x = (wd - w) / 2;
int y = (ht - h) / 2;
if (!animation) {
frame = offset = 0;
animation = show_language;
}
if (frame >= num)
frame = offset = 0;
next_board->flush_board();
next_board->draw_bitmap(bmp, 0, offset, x, y - fix[frame] / 2, w, h + fix[frame]);
offset += h + fix[frame++];
set_timer(timer_animation, 2000);
}
//}}}
//{{{ Ready Go!
void ready_go(void) {
static int frame;
HBITMAP bmp, old_bmp;
const char *str[] = { "READY", "GO!" };
const int delay[] = { 1000, 500 };
const int size = 3;
const int h = 16 * size;
int wd = main_board->get_width();
int ht = main_board->get_height();
int y = ht / 5 - h / 2;
if (!animation) {
frame = 0;
animation = ready_go;
main_board->flush_board();
}
if (frame < sizeof(str) / sizeof(str[0])) {
bmp = new_bitmap(wd, h);
old_bmp = (HBITMAP)SelectObject(hdc_sub, bmp);
fill_bg(hdc_sub, wd, h);
put_string(hdc_sub, false, false, size,\
(wd - strlen(str[frame]) * 8 * size) / 2, 0,\
str[frame], -1, 0, white);
SelectObject(hdc_sub, old_bmp);
main_board->draw_bitmap(bmp, 0, y, wd, h);
DeleteObject(bmp);
set_timer(timer_animation, delay[frame++]);
} else {
main_board->flush_board();
default_animation_end();
}
}
//}}}
//}}}
//{{{ Initialize game board
void init_board(void) {
fill_bg(hdc_main, client_width, client_height, gray);
new gc(game_board_collector,
main_board = new game_board(unit, unit, width, height));
int x = unit * (width + 2);
int y = unit + 30;
int size = 5;
new gc(game_board_collector,
next_board = new game_board("NEXT", x, y, size, size));
new gc(stat_board_collector,
level_board = new stat_board("LEVEL", x, y + unit * size + 60));
new gc(stat_board_collector,
score_board = new stat_board("SCORE", x, y + unit * size + 150));
new gc(stat_board_collector,
lines_board = new stat_board("LINES", x, y + unit * size + 240));
level_board->show_digit(0);
score_board->show_digit(0);
lines_board->show_digit(0);
}
//}}}
//{{{ Welcome screen
void welcome(void) {
const char *title = "TET\xD2IS";
const char *fun = "From Russia with Fun!";
const uint32_t logo[6][7] = {
{ null, red, red, red, red, green, null },
{ yellow, yellow, blue, blue, blue, green, green },
{ yellow, yellow, blue, orange, orange, orange, green },
{ null, cyan, cyan, cyan, purple, orange, null },
{ null, null, cyan, purple, purple, null, null },
{ null, null, null, purple, null, null, null }
};
const int space = 8;
const int size = 5;
const int word = -4;
int wd = main_board->get_width();
int ht = main_board->get_height();
int x = (wd - strlen(title) * (8 * size + space) + space) / 2;
int y = (ht - 16 * size) / 5;
HBITMAP bmp = new_bitmap(wd, ht);
HBITMAP old_bmp = (HBITMAP)SelectObject(hdc_sub, bmp);
fill_bg(hdc_sub, wd, ht);
put_string(hdc_sub, false, false, size,\
x + 2, y + 1, title, -1, space, color_mix(black, gold, .5));
put_string(hdc_sub, false, false, size,\
x - 1, y - 1, title, -1, space, gold);
put_string(hdc_sub, true, true, 1,\
(wd - strlen(fun) * (16 + word)) / 2,\
(ht - 24) / 2, fun, -1, word, white);
SelectObject(hdc_sub, old_bmp);
main_board->draw_bitmap(bmp, wd, ht);
DeleteObject(bmp);
for (int j = 0; j < 6; j++)
for (int i = 0; i < 7; i++)
if (logo[j][i] != (uint32_t)-1)
main_board->show_block((wd - unit * 7) / 2, 0, i, j, logo[j][i], normal);
show_language();
}
//}}}
//{{{ Clear lines
void clear_line(int *clear, int top_line) {
static int *static_clear, static_top_line;
static void (*ani_clear_line)(void) = [] (void) {
static int frame;
static int num_clear;
static int clear[5];
if (!animation) {
int i;
frame = (width + 1) / 2;
animation = ani_clear_line;
for (i = 0; static_clear[i] < height; i++)
clear[i] = static_clear[i];
clear[num_clear = i] = height;
}
if (--frame >= 0) {
for (int i = 0; i < num_clear; i++) {
main_board->erase_block(frame, clear[i]);
main_board->erase_block(width - 1 - frame, clear[i]);
}
set_timer(timer_animation, 100);
} else {
int i, y, y0;
y = y0 = clear[0];
i = 1;
while (y < static_top_line) {
if (++y < clear[i])
main_board->move_line(y0++, y);
else
i++;
}
main_board->erase_lines(y0, static_top_line);
default_animation_end();
}
};
static_clear = clear;
static_top_line = top_line;
ani_clear_line();
}
//}}}
//{{{ Draw info frame
void draw_info_frame(const char *title) {
HBITMAP bmp, old_bmp;
const int size = 3;
const int h = 16 * size;
int wd = main_board->get_width();
int ht = main_board->get_height();
int y = ht / 5 - h / 2;
bmp = new_bitmap(wd, h);
old_bmp = (HBITMAP)SelectObject(hdc_sub, bmp);
fill_bg(hdc_sub, wd, h);
put_string(hdc_sub, false, false, size,\
(wd - strlen(title) * 8 * size) / 2, 0,\
title, -1, 0, white);
SelectObject(hdc_sub, old_bmp);
main_board->flush_board();
main_board->draw_bitmap(bmp, 0, y, wd, h);
DeleteObject(bmp);
}
inline void draw_pause(void) { draw_info_frame("PAUSE"); }
inline void draw_exception(bool write) {
draw_info_frame(write ? "ERR: WRITE" : "ERR: READ");
}
//}}}
//{{{ Draw help frame
void draw_help(void) {
static int frame, offset;
static HBITMAP bmp = LoadBitmap(instance, MAKEINTRESOURCE(IDB_INFO));
static gc *dummy = new gc(bmp, delete_object);
const int w = 300;
const int h = 600;
int wd = main_board->get_width();
int ht = main_board->get_height();
int x = (wd - w) / 2;
int y = (ht - h) / 2;
main_board->flush_board();
main_board->draw_bitmap(bmp, 0, 0, x, y, w, h);
}
//}}}
//{{{ Block operation used by game kernel
inline void draw_block(int x, int y, uint32_t color, draw_type type) {
main_board->show_block(x, y, color, type);
}
inline void sweep_block(int x, int y, uint32_t) {
main_board->erase_block(x, y);
}
inline void draw_block_normal(int x, int y, uint32_t color) {
draw_block(x, y, color, normal);
}
inline void draw_block_translucent(int x, int y, uint32_t color) {
draw_block(x, y, color, translucent);
}
inline void draw_block_dim(int x, int y, uint32_t color) {
draw_block(x, y, color, dim);
}
//}}}
//{{{ Process operation used by game kernel
void next_round(uint8_t);
void game_over(void);
const int id_start = 1;
const int id_help = 2;
const int id_terminate = 3;
void notify_next_round(uint8_t up) {
static uint8_t static_up;
static void (*wait_for_next_round)(void) = [] (void) {
num_controlled = initial_num_controlled - 1;
clear_timer_queue = true;
next_round(static_up);
};
static_up = up;
if (animation) {
continuation = wait_for_next_round;
} else {
wait_for_next_round();
}
}
void notify_game_over(void) {
kill_all_timer();
clear_timer_queue = true;
stop_animation();
SendMessage(window, WM_COMMAND,\
(WPARAM)BN_CLICKED << 16 | id_terminate, (LPARAM)window);
game_over();
}
//}}}
//}}}
//{{{ Record control -- Part I
typedef enum {
event_T, event_O, event_I, event_S,
event_Z, event_L, event_J,
event_lock, event_drop, event_hold,
event_shift_left, event_shift_right,
event_rotate_ccw, event_rotate_cw,
event_drop_sonic, event_drop_hard,
event_pause, event_resume
} log_event;
const log_event event_piece = event_J;
void (*tetris_log)(log_event);
char *record_file_name;
HANDLE record_file = INVALID_HANDLE_VALUE;
DWORD last_io;
void end_record(void);
int global_exception;
const int exception_write = 1;
const int exception_read = 2;
//{{{ Record bar
const int bar_top = 7;
const int bar_time_left = 80;
void flush_record_bar(bool time, bool clear) {
RECT rect;
rect.left = time ? bar_time_left : 0;
rect.top = bar_top;
rect.right = client_width;
rect.bottom = bar_top + 16;
if (clear) {
HBRUSH brush = CreateSolidBrush(gray);
FillRect(hdc_main, &rect, brush);
DeleteObject(brush);
}
InvalidateRect(window, &rect, false);
}
inline void flush_record_bar(bool clear) {
flush_record_bar(false, clear);
}
void bar_show_record(bool record) {
const int dot_left = 15;
const int tri_left = dot_left + 2;
int color;
const char *text;
HRGN region;
if (record) {
color = red;
text = "REC";
region = CreateEllipticRgn(\
dot_left, bar_top + 4,\
dot_left + 7, bar_top + 11);
} else {
POINT p[3];
color = lime;
text = "PLAY";
p[0].x = p[2].x = tri_left;
p[1].x = tri_left + 4;
p[0].y = bar_top + 2;
p[1].y = bar_top + 6;
p[2].y = bar_top + 10;
region = CreatePolygonRgn(p, 3, ALTERNATE);
}
HBRUSH brush = CreateSolidBrush(color);
FillRgn(hdc_main, region, brush);
DeleteObject(brush);
DeleteObject(region);
put_string(hdc_main, false, false, 1,\
27, bar_top, text, -1, 0, color);
flush_record_bar(false);
}
void bar_show_time(unsigned int second) {
const unsigned int max_digit = 1000000000;
unsigned int minute, hour;
int pos_x = bar_time_left, offset;
char buffer[11];
flush_record_bar(true, true);
minute = second / 60;
second %= 60;
if (hour = minute / 60) {
minute %= 60;
if (hour > max_digit)
hour %= max_digit;
offset = snprintf(buffer, 11, "%u:", hour);
put_string(hdc_main, false, false, 1,\
pos_x, bar_top, buffer, offset, 0, white);
pos_x += offset * 8;
}
sprintf(buffer, "%02u:%02u", minute, second);
put_string(hdc_main, false, false, 1,\
pos_x, bar_top, buffer, 5, 0, white);
}
//}}}
//{{{ Record second
void msecond_clock(int duration) {
static bool reverse;
static int second;
static clock_t end;
if (duration == -1) {
if (reverse) {
duration = (end - clock()) /\
(CLOCKS_PER_SEC / 1000) - 500;
int rem = duration % 1000;
duration /= 1000;
if (rem <= 0)
rem += 1000;
else
duration++;
bar_show_time(duration);
if (duration > 0)
set_timer(timer_second, rem + 500);
} else {
duration = (clock() - end) /\
(CLOCKS_PER_SEC / 1000) + 500;
int rem = duration % 1000;
duration /= 1000;
bar_show_time(duration);
set_timer(timer_second, 1500 - rem);
}
} else if (duration < -1) {
end += (-1 - duration) * (CLOCKS_PER_SEC / 1000);
} else if (duration == 0) {
reverse = false;
end = clock();