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
|
#include "include/functions.h"
#include "include/common.h"
/* Externs from main.c */
extern sfRectangleShape *fld[20][10]; // Array of field rectangles
extern sfVector2f fldCPos[20][10]; // Array of absolute coordinates of field rectangles
extern sfVector2i fldSize;
extern sfVector2i fldPos;
extern sfVector2f fldCSize; // Field rectangles size variable x/y
extern int fldCOutThick; // Field rectangles outline thickness
extern sfVector2f textScore_pos;
extern sfText *textScore;
extern sfFont *font;
extern int gameIsStarted;
extern uint8_t arrKeys; // arrow keys states byte container
/* arrKeys = ...n|7|6|5|4|3|2|1|0| (just a little bit of such called "bit fucking")
* 0 - Right arrow pushed and held
* 1 - Down arrow pushed and held
* 2 - N/A
* 3 - Left arrow pushed and held
* 4 - Right arrow short repeat activated (after once long repeat)
* 5 - N/A
* 6 - N/A
* 7 - Right arrow short repeat activated (after once long repeat)
*/
sfClock *gameTick;
sfClock *mTick;
sfClock *repPushDown; // Clock for repeat latency when Down arrow long push
sfClock *repKeyLeft; // Clock for repeat latency when Left arrow long push
sfClock *repKeyRight; // Clock for repeat latency when Left arrow long push
extern int lvlLatency;
extern int scoreCurrent;
extern char *scoreDisp;
/* Shapes maps */
extern uint8_t arrShapeL_a1[4][4];
extern uint8_t arrShapeL_a2[4][4];
extern uint8_t arrShapeL_a3[4][4];
extern uint8_t arrShapeL_a4[4][4];
extern uint8_t arrShapeRL_a1[4][4];
extern uint8_t arrShapeRL_a2[4][4];
extern uint8_t arrShapeRL_a3[4][4];
extern uint8_t arrShapeRL_a4[4][4];
extern uint8_t arrShapeZ_a1[4][4];
extern uint8_t arrShapeZ_a2[4][4];
extern uint8_t arrShapeZ_a3[4][4];
extern uint8_t arrShapeZ_a4[4][4];
extern uint8_t arrShapeS_a1[4][4];
extern uint8_t arrShapeS_a2[4][4];
extern uint8_t arrShapeS_a3[4][4];
extern uint8_t arrShapeS_a4[4][4];
extern uint8_t arrShapeB_a1[4][4];
extern uint8_t arrShapeB_a2[4][4];
extern uint8_t arrShapeB_a3[4][4];
extern uint8_t arrShapeB_a4[4][4];
extern uint8_t arrShapeI_a1[4][4];
extern uint8_t arrShapeI_a2[4][4];
extern uint8_t arrShapeI_a3[4][4];
extern uint8_t arrShapeI_a4[4][4];
extern uint8_t arrShapeT_a1[4][4];
extern uint8_t arrShapeT_a2[4][4];
extern uint8_t arrShapeT_a3[4][4];
extern uint8_t arrShapeT_a4[4][4];
/*
* Init routine. Performs once per program run
*
*/
void initAll()
{
/*
* Dimensions of every fld's block
* 19px - fill color 1px - for outline, 20 - at all
*
*/
fldCSize.x = 23;
fldCSize.y = 23;
fldPos.x = 10; // Field's bottom left corner coordinates
fldPos.y = 10+500-25;
fldSize.x = 10; // Field size in blocks
fldSize.y = 20;
srand( time(NULL) );
gameTick = sfClock_create();
mTick = sfClock_create();
resetActiveShape();
/* Create field */
for (int j = 0; j < fldSize.y; j++) {
for(int i = 0; i < fldSize.x; i++) {
fldCAtt[j][i].a = 0; // Inactive = empty
fldCPos[j][i].x = fldPos.x
+ (i * (fldCSize.x + 2 * fldCOutThick));
fldCPos[j][i].y = fldPos.y -
(j * (fldCSize.y + 2 * fldCOutThick));
fld[j][i] = sfRectangleShape_create();
sfRectangleShape_setFillColor(fld[j][i], uiColor1);
sfRectangleShape_setSize(fld[j][i], fldCSize);
sfRectangleShape_setPosition(fld[j][i], fldCPos[j][i]);
sfRectangleShape_setOutlineColor(fld[j][i],
uiColor3);
sfRectangleShape_setOutlineThickness(fld[j][i],
fldCOutThick);
}
}
font = sfFont_createFromFile("dat/arial.ttf");
if (!font) {
printf("dat/arial.ttf font load failed");
exit(-1);
}
textScore_pos.x = 250+10+10;
textScore_pos.y = 485;
textScore = sfText_create();
sfText_setFont(textScore, font);
sfText_setCharacterSize(textScore, 20);
sfText_setPosition(textScore, textScore_pos);
}
/*
* Refreshes score
*
*/
void scoreDisplay(int score, sfText *textScore)
{
char a[64];
char b[8];
sprintf(b, "Score: ");
sprintf(a, "%d", score);
for (int i = 63; i >= 7; i--)
a[i] = a[i-7];
for (int i = 0; i < 7; i++)
a[i] = b[i];
sfText_setString(textScore, (char *)&a);
}
/*
* Removes line when cells all are in row in it
*
*/
int linesRmScore()
{
int k = 0; // "Filled line" indicator
int s = 0;
for (int j = 0; j < 20; j++) {
for (int i = 0; i < 10; i++) {
if (fldCAtt[j][i].a != 0)
k++;
}
if (k >= 10) { // If line is full
s++; // Give scores for line
for (int n = j; n < 20; n++) { // Drop all lines down
if (n == 19) {
for (int m = 0; m < 10; m++) {
fldCAtt[n][m].a = 0;
fldCAtt[n][m].fColor = uiColor1;
}
break;
}
for (int m = 0; m < 10; m++) {
fldCAtt[n][m].a = fldCAtt[n+1][m].a;
fldCAtt[n][m].fColor
= fldCAtt[n+1][m].fColor;
}
}
j--; // Do not let loop to go to next line because
// next line go down by itself =)
}
k = 0; // Clear line fill indicator
}
return s; // Return number of deleted lines
}
/*
* Inserts shape into field, runs filled lines searching, puts new shape
* into the game at the top.
*
*/
void putShape()
{
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
if (actiSh.c[j][i]) {
fldCAtt[j+actiSh.y][i+actiSh.x].a
= actiSh.c[j][i];
if ((j+actiSh.y >= 0)
&& (i+actiSh.x >= 0)) {
fldCAtt[j+actiSh.y][i+actiSh.x].fColor
= actiSh.fColor;
}
}
}
}
scoreCurrent += linesRmScore()*100; // Remove filled lines and
// get score;
resetActiveShape();
}
void resetActiveShape()
{
actiSh.x = 3;
actiSh.y = 16;
actiSh.r = 1;
actiSh.t = (rand()%7)+1; // Insert new random shape of 7 variants
switch (actiSh.t) { // Copy cell active/inactive state
case 1 :
memcpy(&actiSh.c[0][0],
&arrShapeL_a1[0][0],
sizeof(uint8_t)*4*4);
actiSh.fColor = tOrange;
break;
case 2 :
memcpy(&actiSh.c[0][0],
&arrShapeRL_a1[0][0],
sizeof(uint8_t)*4*4);
actiSh.fColor = tBlue;
break;
case 3 :
memcpy(&actiSh.c[0][0],
&arrShapeZ_a1[0][0],
sizeof(uint8_t)*4*4);
actiSh.fColor = tRed;
break;
case 4 :
memcpy(&actiSh.c[0][0],
&arrShapeS_a1[0][0],
sizeof(uint8_t)*4*4);
actiSh.fColor = tGreen;
break;
case 5 :
memcpy(&actiSh.c[0][0],
&arrShapeB_a1[0][0],
sizeof(uint8_t)*4*4);
actiSh.fColor = tYellow;
break;
case 6 :
memcpy(&actiSh.c[0][0],
&arrShapeI_a1[0][0],
sizeof(uint8_t)*4*4);
actiSh.fColor = tCyan;
break;
case 7 :
memcpy(&actiSh.c[0][0],
&arrShapeT_a1[0][0],
sizeof(uint8_t)*4*4);
actiSh.fColor = tMagneta;
break;
}
}
/*
* Game tick
*
*/
void tTick()
{ // If tick exceeds current level tick latency
if (sfClock_getElapsedTime(gameTick).microseconds >= lvlLatency) {
sfClock_restart(gameTick); // Restart gameTick
/* if bottom not reached */
if ((wallCollisionCheck(0b0010) == 0) &&
(cellCollisionCheck(0b0010) == 0))
actiSh.y--; // Move
else
putShape(); // Just put the shape
}
}
/*
* Rotate matrix left routine
*
*/
void rotateLeft()
{
uint8_t arr[4][4];
memcpy(&arr[0][0], &actiSh.c[0][0], sizeof(uint8_t)*4*4);
if (actiSh.t == 5)
return;
if (actiSh.t == 6) {
for (int j = 3; j >= 0; j--)
for (int i = 0; i < 4; i++)
actiSh.c[j][i] = arr[3-i][3-j];
return;
}
for (int j = 3; j > 0; j--)
for (int i = 0; i < 3; i++)
actiSh.c[j][i] = arr[3-i][j-1];
}
/*
* Rotate matrix right routine
*
*/
void rotateRight()
{
uint8_t arr[4][4];
memcpy(&arr[0][0], &actiSh.c[0][0], sizeof(uint8_t)*4*4);
if (actiSh.t == 5)
return;
if (actiSh.t == 6) {
for (int j = 3; j >= 0; j--)
for (int i = 0; i < 4; i++)
actiSh.c[j][i] = arr[3-i][3-j];
return;
}
for (int j = 3; j > 0; j--)
for (int i = 0; i < 3; i++)
actiSh.c[j][i] = arr[i+1][3-j];
}
/*
* Rotates active shape with callling collision checkers
*
*/
void rotateShape()
{
rotateRight(); // Make rotate
if ((wallRotCollisionCheck() == 1)
|| (cellRotCollisionCheck() == 1))
rotateLeft(); // Just rotate back =)
}
int cellRotCollisionCheck()
{
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
if (actiSh.c[j][i]
&& fldCAtt[j+actiSh.y][i+actiSh.x].a)
return 1; // Collision happens
}
}
return 0; // Else it do not
}
int wallRotCollisionCheck()
{
if(actiSh.y < 0) //If shape has crossed Bottom border
for(int i = 0; i < 4; i++)
if (actiSh.c[-1-actiSh.y][i])
return 1; // Collision happens
if(actiSh.x < 0) //If shape has crossed Left border
for(int i = 0; i < 4; i++)
if (actiSh.c[i][-1-actiSh.x])
return 1; // Collision happens
if(actiSh.x > 6) //If shape has crossed Right border
for(int i = 0; i < 4; i++)
if (actiSh.c[i][3-(actiSh.x-7)])
return 1; // Collision happens
return 0; // If no conditions are met collision is absent
}
int cellCollisionCheck(int dir)
{
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
if ((dir & 0b0001) // Check Right
&& (j+actiSh.y >= 0) // Avoiding nonexisting fld cells
&& (i+actiSh.x+1 >= 0) // ---
&& actiSh.c[j][i] // Check activity
&& fldCAtt[j+actiSh.y][i+actiSh.x+1].a)
return 1; // Collision happens
if ((dir & 0b1000) // Check Left
&& (j+actiSh.y >= 0) // Avoiding nonexisting fld cells
&& (i+actiSh.x-1 >= 0) // ---
&& actiSh.c[j][i] // Check activity
&& fldCAtt[j+actiSh.y][i+actiSh.x-1].a)
return 1; // Collision happens
if ((dir & 0b0010) // Check Bottom
&& (j+actiSh.y-1 >= 0) // Avoiding nonexisting fld cells
&& (i+actiSh.x >= 0) // ---
&& actiSh.c[j][i] // Check activity
&& fldCAtt[j+actiSh.y-1][i+actiSh.x].a)
return 1; // Collision happens
}
}
return 0; // Else it do not
}
int wallCollisionCheck(int dir)
{
if (dir & 0b0001) { // Right collision request
if (actiSh.x >= 6) // If shape has reached Right boreder
for (int i = 0 ; i < 4; i++)
if (actiSh.c[i][3-(actiSh.x-6)])
return 1; // Collision happens
} else if (dir & 0b0010) { // Bottom collision request
if (actiSh.y <= 0) //If shape has reached Bottom border
for (int i = 0; i < 4; i++)
if (actiSh.c[-actiSh.y][i])
return 1; // Collision happens
} else if (dir & 0b1000) // Left collision request
if (actiSh.x <= 0) // If shape has reached Left border
for (int i = 0; i < 4; i++)
if (actiSh.c[i][-actiSh.x])
return 1; // Collision happens
return 0;
}
/*
* Keys hold handler
*
*/
void tKeyCtrl()
{
/* Up arrow key 'hold' handler */
if (sfKeyboard_isKeyPressed(sfKeyUp) == 1) {
if ((arrKeys & 0b0100) == 0) {
arrKeys = arrKeys | 0b0100;
rotateShape();
}
} else {
if ((arrKeys & 0b0100) != 0) {
arrKeys = arrKeys & ~0b0100;
}
}
/* Down Arrow Key 'hold' handler */
if (sfKeyboard_isKeyPressed(sfKeyDown) == 1) {
if ((arrKeys & 0b0010) == 0) {
arrKeys = arrKeys | 0b0010;
if ((wallCollisionCheck(0b0010) == 0)
&& (cellCollisionCheck(0b0010) == 0)) {
actiSh.y--;
// Avoid excess move down by gameTick
sfClock_restart(gameTick);
scoreCurrent++;
}
repPushDown = sfClock_create();
} else {
if (sfClock_getElapsedTime(repPushDown).microseconds
>= moveRepeatLatency2)
arrKeys = arrKeys & ~0b0010;
}
} else {
if ((arrKeys & 0b0010) != 0) {
arrKeys = arrKeys & ~0b0010;
arrKeys = arrKeys & ~0b100000;
}
}
/* Left Arrow Key 'hold' handler */
if ((sfKeyboard_isKeyPressed(sfKeyLeft) == 1)
&& (sfKeyboard_isKeyPressed(sfKeyRight) == 0)) {
if ((arrKeys & 0b1000) == 0){
arrKeys = arrKeys | 0b1000;
if ((wallCollisionCheck(0b1000) == 0)
&& (cellCollisionCheck(0b1000) == 0))
actiSh.x--;
repKeyLeft = sfClock_create();
} else {
if ((arrKeys & 0b10000000) == 0) {
if (sfClock_getElapsedTime(repKeyLeft).microseconds
>= moveRepeatLatency1) {
arrKeys = arrKeys | 0b10000000;
arrKeys = arrKeys & ~0b1000;
}
} else {
if (sfClock_getElapsedTime(repKeyLeft).microseconds
>= moveRepeatLatency2)
arrKeys = arrKeys & ~0b1000;
}
}
} else if (sfKeyboard_isKeyPressed(sfKeyLeft) == 0) {
if ((arrKeys & 0b1000) != 0){
arrKeys = arrKeys & ~0b1000;
arrKeys = arrKeys & ~0b10000000;
}
}
/* Right Arrow Key 'hold' handler */
if ((sfKeyboard_isKeyPressed(sfKeyRight) == 1)
&& (sfKeyboard_isKeyPressed(sfKeyLeft) == 0)) {
if ((arrKeys & 0b0001) == 0){
arrKeys = arrKeys | 0b0001;
if ((wallCollisionCheck(0b0001) == 0)
&& (cellCollisionCheck(0b0001) == 0))
actiSh.x++;
repKeyRight = sfClock_create();
} else {
if ((arrKeys & 0b10000) == 0) {
if (sfClock_getElapsedTime(repKeyRight).microseconds
>= moveRepeatLatency1) {
arrKeys = arrKeys | 0b10000;
arrKeys = arrKeys & ~0b0001;
}
} else if (sfKeyboard_isKeyPressed(sfKeyLeft) == 0) {
if (sfClock_getElapsedTime(repKeyRight).microseconds
>= moveRepeatLatency2) // Wait short time
arrKeys = arrKeys & ~0b0001;
}
}
} else {
if ((arrKeys & 0b0001) != 0) {
arrKeys = arrKeys & ~0b0001;
arrKeys = arrKeys & ~0b10000;
}
}
}
/*
* Colorize active cells of field
*
*/
void colorizeFld()
{
for(int j = 0; j < 20; j++) {
for(int i = 0; i < 10; i++) {
if (fldCAtt[j][i].a) {
sfRectangleShape_setFillColor(
fld[j][i],
fldCAtt[j][i].fColor);
sfRectangleShape_setOutlineColor(
fld[j][i],
uiColor3);
} else {
sfRectangleShape_setFillColor(
fld[j][i],
uiColor1);
sfRectangleShape_setOutlineColor(
fld[j][i],
uiColor2);
}
}
}
}
/*
* Colorize active cells of active shape (overlay only
* active cells above background of fld)
*
*/
void colorizeActiSh()
{
for(int j = 0; j < 4; j++) {
for(int i = 0; i < 4; i++) {
if (actiSh.c[j][i]) {
sfRectangleShape_setFillColor(
fld[j+actiSh.y][i+actiSh.x],
actiSh.fColor);
sfRectangleShape_setOutlineColor(
fld[j+actiSh.y][i+actiSh.x],
uiColor3);
}
}
}
}
void menuTick()
{
if(sfClock_getElapsedTime(mTick).microseconds >= lvlLatency){
sfClock_restart(mTick);
colorizeRandom();
}
}
void colorizeRandom()
{
for (int j = 0; j < fldSize.y; j++) {
for (int i = 0; i < fldSize.x; i++) {
int a;
a = rand()%7+1;
switch (a) {;
case 1 :
sfRectangleShape_setFillColor(fld[j][i],
tOrange);
break;
case 2 :
sfRectangleShape_setFillColor(fld[j][i],
tBlue);
break;
case 3 :
sfRectangleShape_setFillColor(fld[j][i],
tRed);
break;
case 4 :
sfRectangleShape_setFillColor(fld[j][i],
tGreen);
break;
case 5 :
sfRectangleShape_setFillColor(fld[j][i],
tYellow);
break;
case 6 :
sfRectangleShape_setFillColor(fld[j][i],
tCyan);
break;
case 7 :
sfRectangleShape_setFillColor(fld[j][i],
tMagneta);
break;
}
sfRectangleShape_setOutlineColor(fld[j][i], uiColor3);
}
}
}
|