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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SFML/System/Clock.h>
#include <SFML/Graphics/RenderWindow.h>
#include <SFML/Graphics/Text.h>
#include <SFML/Graphics/RectangleShape.h>
#include "common.h"
#include "tet_conf.h"
#include "text.h"
#include "field.h"
#include "engine.h"
/* Externs from main.c */
extern struct game game;
extern struct shape active, next;
extern struct field fld;
extern sfFont *fontScore;
extern char arrKeys; // Arrow keys states byte container
extern sfClock *gameTick;
extern sfClock *putTick;
extern sfClock *mTick;
extern sfClock *repPushDown; // Clock for repeat latency when Down key held
extern sfClock *repKeyLeft; // Clock for repeat latency when Left key held
extern sfClock *repKeyRight; // Clock for repeat latency when Left key held
void valueAfterTextDisplay(int value, List *texts, char *type)
{
List *l = texts;
while (l) {
Text *text = (Text *)l->obj;
if (text->type)
if (!strcmp(text->type, type)) {
char a[64];
if (text->text) {
if (strlen(text->text) > 52) {
memset(text->text+52, '\0', 1);
strcpy(a, text->text);
}
sprintf(a, "%s%d", text->text, value);
sfText_setString(text->sfText, (char *)&a);
break;
}
}
l = l->next;
}
}
void checkLevelUp(struct game *game)
{
while (game->lines >= LEVELUP_LINES) {
game->level++;
game->lines -= LEVELUP_LINES;
game->moveLatency = getMoveLatencyOfLevel(game->level);
}
}
int getMoveLatencyOfLevel(int level)
{
if (level >= 29)
return L29LATENCY;
else if (level >= 19)
return L19LATENCY;
else if (level >= 16)
return L16LATENCY;
else if (level >= 13)
return L13LATENCY;
else if (level >= 10)
return L10LATENCY;
else if (level == 9)
return L09LATENCY;
else if (level == 8)
return L08LATENCY;
else if (level == 7)
return L07LATENCY;
else if (level == 6)
return L06LATENCY;
else if (level == 5)
return L05LATENCY;
else if (level == 4)
return L04LATENCY;
else if (level == 3)
return L03LATENCY;
else if (level == 2)
return L02LATENCY;
else if (level == 1)
return L01LATENCY;
else if (level == 0)
return L00LATENCY;
return L00LATENCY;
}
/*
* Game tick
*
*/
void tTick()
{ // If tick exceeds current level tick latency
if (sfClock_getElapsedTime(gameTick).microseconds >= game.moveLatency) {
sfClock_restart(gameTick);
active.y--; // try
if (collide(&fld, &active))
active.y++; // fallback
else
sfClock_restart(putTick);
if (sfClock_getElapsedTime(putTick).microseconds >= PUT_LATENCY) {
if (out_of_field(&fld, &active)) {
gameover(&game);
return;
} else {
putShape(&fld, &active);
int removedLines = rm_lines(&fld);
game.lines += removedLines;
switch (removedLines) {
case 1:
game.scoreCurrent += RM_1LINES_SCORE * game.level;
break;
case 2:
game.scoreCurrent += RM_2LINES_SCORE * game.level;
break;
case 3:
game.scoreCurrent += RM_3LINES_SCORE * game.level;
break;
case 4:
game.scoreCurrent += RM_4LINES_SCORE * game.level;
break;
}
active.t = next.t;
resetActiveShape(&fld, &active);
gen_shape(&next);
checkLevelUp(&game);
}
sfClock_restart(putTick);
}
}
}
/*
* Keys hold handler
*
*/
void tKeyCtrl()
{
/* Up arrow key 'hold' handler */
if (sfKeyboard_isKeyPressed(sfKeyUp)) {
if (!(arrKeys & UP)) {
arrKeys = arrKeys | UP;
rotate_shape(&fld, &active);
}
} else {
if ((arrKeys & UP)) {
arrKeys = arrKeys & ~UP;
}
}
/* Down Arrow Key 'hold' handler */
if (sfKeyboard_isKeyPressed(sfKeyDown)) {
if (!(arrKeys & DOWN)) {
arrKeys = arrKeys | DOWN;
active.y--;
if (collide(&fld, &active))
active.y++;
else {
// Avoid excess move down by gameTick
sfClock_restart(putTick);
sfClock_restart(gameTick);
game.scoreCurrent++;
}
sfClock_restart(repPushDown);
} else {
if (sfClock_getElapsedTime(repPushDown).microseconds
>= moveRepeatLatency2)
arrKeys = arrKeys & ~DOWN;
}
} else {
if ((arrKeys & DOWN)) {
arrKeys = arrKeys & ~DOWN;
}
}
/* Left Arrow Key 'hold' handler */
if (sfKeyboard_isKeyPressed(sfKeyLeft)
&& !sfKeyboard_isKeyPressed(sfKeyRight)) {
if (!(arrKeys & LEFT)) {
arrKeys = arrKeys | LEFT;
active.x--;
if (collide(&fld, &active))
active.x++;
else
sfClock_restart(putTick);
sfClock_restart(repKeyLeft);
} else {
if (!(arrKeys & LEFTHOLD)) {
if (sfClock_getElapsedTime(repKeyLeft).microseconds
>= moveRepeatLatency1) {
arrKeys = arrKeys | LEFTHOLD;
arrKeys = arrKeys & ~LEFT;
}
} else {
if (sfClock_getElapsedTime(repKeyLeft).microseconds
>= moveRepeatLatency2)
arrKeys = arrKeys & ~LEFT;
}
}
} else if (!sfKeyboard_isKeyPressed(sfKeyLeft)) {
if ((arrKeys & LEFT)) {
arrKeys = arrKeys & ~LEFT;
arrKeys = arrKeys & ~LEFTHOLD;
}
}
/* Right Arrow Key 'hold' handler */
if (sfKeyboard_isKeyPressed(sfKeyRight)
&& !sfKeyboard_isKeyPressed(sfKeyLeft)) {
if (!(arrKeys & RIGHT)) {
arrKeys = arrKeys | RIGHT;
active.x++;
if (collide(&fld, &active))
active.x--;
else
sfClock_restart(putTick);
sfClock_restart(repKeyRight);
} else {
if (!(arrKeys & RIGHTHOLD)) {
if (sfClock_getElapsedTime(repKeyRight).microseconds
>= moveRepeatLatency1) {
arrKeys = arrKeys | RIGHTHOLD;
arrKeys = arrKeys & ~RIGHT;
}
} else if (!sfKeyboard_isKeyPressed(sfKeyLeft)) {
if (sfClock_getElapsedTime(repKeyRight).microseconds
>= moveRepeatLatency2) // Wait short time
arrKeys = arrKeys & ~RIGHT;
}
}
} else {
if ((arrKeys & RIGHT)) {
arrKeys = arrKeys & ~RIGHT;
arrKeys = arrKeys & ~RIGHTHOLD;
}
}
}
/*
* Draw all fld cells
*
*/
void drawFld(sfRenderWindow *window)
{
for (int j = 0; j < fld.size.y; j++)
for (int i = 0; i < fld.size.x; i++)
sfRenderWindow_drawRectangleShape(window, fld.p[j][i], NULL);
}
void gameover(struct game *game)
{
game->isStarted = 0;
game->scoreCurrent = 0;
game->level = 0;
game->moveLatency = L00LATENCY;
game->lines = 0;
}
void drawNextShape(sfRenderWindow *window)
{
for (int j = 0; j < 4; j++)
for (int i = 0; i < 4; i++)
if (next.c[j][i]) {
sfRectangleShape_setFillColor(next.p[j][i], next.fColor);
sfRectangleShape_setOutlineColor(next.p[j][i], UIFGACTIVECOLOR);
sfRenderWindow_drawRectangleShape(window, next.p[j][i], NULL);
}
}
|