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
|
#include "common.h"
#include "functions.h"
/* --- Variables --- */
int gameIsStarted = 0;
sfVideoMode mode = {450, 520, 32};
sfRenderWindow* window;
sfEvent event;
sfText *textMenu1;
sfVector2f textMenu1_pos;
sfFont *fontScore;
sfText *textScore;
sfVector2f textScore_pos;
char *scoreDisp;
int scoreCurrent = 0;
sfRectangleShape* ns[4][4]; // Array of next shape image rectangles
sfRectangleShape* fld[25][10]; // Array of fld rectangles
sfVector2f fldCPos[25][10]; // Array of absolute coordinates of fld
// rectangles
sfRectangleShape* ns[4][4]; // Array of next shape image rectangles
sfVector2f nsCSize; // Next shape rectangles size variable x/y
int fldCOutThick = 1; // Field rectangles outline thickness
sfVector2f fldCSize; // Field rectangles size variable x/y
sfVector2i fldSize;
sfVector2f fldPos;
uint8_t arrKeys = 0b00000000; // Arrow keys states byte container
int lvlLatency = 500000;
/* --- Variables End --- */
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
void prepare() {
srand( time(NULL) );
gameTick = sfClock_create();
mTick = sfClock_create();
fontScore = sfFont_createFromFile("dat/arial.ttf");
if (!fontScore) {
printf("dat/arial.ttf font load failed");
exit(-1);
}
/*
* Dimensions of every fld's cell
* 23px - fill color 1px - for outline, 25 - at all
*/
fldCSize = (sfVector2f){.x = 23, .y = 23}; //Fld's cell size in pixels
fldPos = (sfVector2f){.x = 10, .y = 10+500-20}; // Fld bot left corner
fldSize = (sfVector2i){.x = 10, .y = 25}; // Field's size in blocks
nsCSize = (sfVector2f){.x = 23, .y = 23};
nxtShape = (struct shapeSt){.x = 250+10+20, .y = 200};
initFld();
textScore_pos = (sfVector2f){.x = 250+10+10, .y = 10};
textScore = sfText_create();
sfText_setFont(textScore, fontScore);
sfText_setCharacterSize(textScore, 20);
sfText_setPosition(textScore, textScore_pos);
/*
* Menu texts
*
*/
textMenu1_pos.x = 10+250+30;
textMenu1_pos.y = 100;
textMenu1 = sfText_create();
sfText_setFont(textMenu1, fontScore);
sfText_setCharacterSize(textMenu1, 36);
sfText_setPosition(textMenu1, textMenu1_pos);
char b[7];
sprintf(b, "TETRIS");
sfText_setString(textMenu1, (char *)&b);
window = sfRenderWindow_create(mode,
windowName_conf,
sfResize | sfClose,
NULL);
if (!window)
exit(EXIT_FAILURE);
}
void handleWindowEvents() {
while (sfRenderWindow_pollEvent(window, &event))
if (event.type == sfEvtClosed)
sfRenderWindow_close(window);
}
void gameLoop() {
tTick();
tKeyCtrl();
scoreDisplay(scoreCurrent, textScore);
colorizeFld();
colorizeActiSh();
drawFld(window);
drawNextShape(window);
sfRenderWindow_drawText(window, textScore, NULL);
}
void menuLoop() {
menuTick();
drawFld(window);
sfRenderWindow_drawText(window, textMenu1, NULL);
if (sfKeyboard_isKeyPressed(sfKeyS) == 1) {
gameIsStarted = 1;
cleanupFld();
initFld();
}
}
void mainLoop() {
while (sfRenderWindow_isOpen(window)) {
handleWindowEvents();
sfRenderWindow_clear(window, sfBlack);
if (gameIsStarted) {
gameLoop();
} else {
menuLoop();
}
sfRenderWindow_display(window);
}
}
int main()
{
prepare();
colorizeRandom();
mainLoop();
/* Just senseless printf */
printf("%d\n", scoreCurrent);
cleanupFld();
sfRenderWindow_destroy(window);
sfText_destroy(textScore);
sfText_destroy(textMenu1);
return EXIT_SUCCESS;
}
|