jag

tetoris_c

public

Small block game in C

Star0
Watch0

Commit c4f522359c37

tarball

Jaglekon <58274093+Jaglekon@users.noreply.github.com>2026-07-25 13:13 UTC

3 files changed+61-18
redid ticks
Modified file: source/main.c+59 additions−18 deletions
@@ -57,15 +57,24 @@ int main () {
5757 piece current_piece = {};
5858 piece next_piece = init_piece((rand() % Z_PIECE) + 1);
5959
60- /* Game Loop */
60+
61+ /* Time keeping */
6162
6263 struct timespec start, current;
6364 clock_gettime(CLOCK_MONOTONIC_RAW, &start);
6465
66+ uint64_t time_acc = 0;
67+
68+ uint16_t tick_fall = 0;
69+ uint16_t tick_blink = 0;
70+ uint16_t tick_spin = 0;
71+
6572
6673 uint8_t i, j;
6774 bool is_tetris = false;
6875
76+ /* Game Loop */
77+
6978 while (true) {
7079
7180 int8_t move = 0;
@@ -73,16 +82,40 @@ int main () {
7382 bool draw = false;
7483 bool rotate = false;
7584
76- clock_gettime(CLOCK_MONOTONIC_RAW, &current);
7785
78- uint64_t time_diff =
79- (current.tv_sec - start.tv_sec) * 1000000000ULL +
80- (current.tv_nsec - start.tv_nsec);
86+ /* Polling for input */
87+
88+ int is_poll = poll(polling, 1, paused ? -1 : 17);
89+
90+
91+ /* Dealing with Ticks */
92+
93+ uint16_t ticks = 0;
94+
95+ {
96+
97+ clock_gettime(CLOCK_MONOTONIC_RAW, &current);
98+
99+ uint64_t time_diff =
100+ (current.tv_sec - start.tv_sec) * 1000000000ULL +
101+ (current.tv_nsec - start.tv_nsec) + time_acc;
102+
103+ if (!paused) {
81104
105+ while (time_diff >= FRAME) {
106+
107+ time_diff -= FRAME;
108+ ticks++;
109+ }
110+
111+ time_acc = time_diff;
112+ }
113+
114+ start = current;
115+ }
82116
83117 /* Take care of the input */
84118
85- int is_poll = poll(polling, 1, paused ? -1 : 17);
86119
87120 if (is_poll > 0 && polling[0].revents & POLLIN) {
88121
@@ -97,12 +130,10 @@ int main () {
97130 if (tolower(buf[0]) == 'p') {
98131 if (paused) {
99132
100- time_diff = paused_diff;
101133
102134 paused = false;
103135 } else {
104136
105- paused_diff = time_diff;
106137
107138 draw_pause();
108139
@@ -136,12 +167,12 @@ int main () {
136167
137168 if (is_tetris) {
138169
139- if (time_diff >= FRAME * BLINK && i < 4) {
170+ tick_blink += ticks;
140171
141- time_diff -= time_diff % (FRAME * BLINK);
142- start.tv_sec += time_diff / 1000000000ULL;
143- start.tv_nsec += time_diff % 1000000000ULL;
172+ while (tick_blink >= BLINK && i < 4) {
144173
174+ tick_blink -= BLINK;
175+
145176 draw_board(current_piece, next_piece, (i % 2 == 0) ? NULL : blink, is_piece);
146177 i++;
147178 }
@@ -186,6 +217,8 @@ int main () {
186217 next_piece = init_piece((rand() % Z_PIECE) + 1);
187218 is_piece = true;
188219 draw = true;
220+
221+ tick_fall = tick_blink = tick_spin = 0;
189222 }
190223
191224
@@ -209,8 +242,10 @@ int main () {
209242
210243
211244 /* Piece rotates */
212-
213- if (rotate && current_piece.positions > 1) {
245+
246+ tick_spin += ticks;
247+
248+ if (rotate && current_piece.positions > 1 && tick_spin >= SPIN) {
214249
215250 piece rotated = current_piece;
216251
@@ -220,17 +255,18 @@ int main () {
220255 current_piece = rotated;
221256 draw = true;
222257 }
258+
259+ tick_spin = 0;
223260 }
224261
225262
226263 /* Piece falls */
227264
228- if (time_diff >= (FRAME * FALL) || down) {
265+ tick_fall += ticks;
266+
267+ while (tick_fall >= FALL || down) {
229268 current_piece.y++;
230269
231- time_diff -= time_diff % (FRAME * FALL);
232- start.tv_sec += time_diff / 1000000000ULL;
233- start.tv_nsec += time_diff % 1000000000ULL;
234270
235271 uint8_t j, k;
236272
@@ -249,6 +285,11 @@ int main () {
249285 }
250286 }
251287
288+ if (tick_fall >= FALL)
289+ tick_fall -= FALL;
290+
291+ down = false;
292+
252293 draw = true;
253294 }
254295
Modified file: source/main.h+2 additions
@@ -33,6 +33,8 @@
3333
3434 #define FALL 58
3535 #define BLINK 12
36+#define SPIN 5
37+
3638
3739 typedef enum {
3840 EMPTY, I_PIECE, J_PIECE, L_PIECE, O_PIECE, S_PIECE, T_PIECE, Z_PIECE, TETRIS
Binary file: source/main.obinary

Binary file not shown.