Compare commits

...

6 Commits

9 changed files with 212 additions and 266 deletions

View File

@@ -1,50 +0,0 @@
#pragma once
#include <stdint.h>
#include <stdnoreturn.h>
/* defines statuses in the 0..127, any higher/negative values are POSIX-reserved.
* The max value (or -1) shall mean the application is running, anything else shall mean an exit code of some kind */
enum gamestatus {
// clang-format off
STATUS_SUCCESS = 0, // 0; successful exit
STATUS_ERROR = 1, // miscellaneous error
ERROR_INIT = STATUS_ERROR | 2, // initialisation error
ERROR_STD = STATUS_ERROR | 64, // standard library error
ERROR_STD_INIT = ERROR_INIT | 64, // standard library initialisation error
ERROR_STD_MEMORY = ERROR_STD | 32, // memory error
ERROR_STD_MEMORY_INIT = ERROR_STD_INIT | 32, // memory initialization error
ERROR_SDL = STATUS_ERROR | 32, // SDL error
ERROR_SDL_INIT = ERROR_INIT | 32, // SDL initialization error
ERROR_SDL_RENDERING = ERROR_SDL | 16, // rendering error
ERROR_SDL_RENDERING_INIT = ERROR_SDL_INIT | 16, // rendering initialization error
ERROR_SDL_AUDIO = ERROR_SDL | 8, // audio error
ERROR_SDL_AUDIO_INIT = ERROR_SDL_INIT | 8, // audio initialization error
ERROR_SDL_FONT = ERROR_SDL | 4, // font error
ERROR_SDL_FONT_INIT = ERROR_SDL_INIT | 4, // font initialization error
STATUS_RUNNING = -1,
// clang-format on
};
#if __INCLUDE_LEVEL__ > 0
#include <SDL_messagebox.h>
#include <stdio.h>
#include <stdlib.h>
#include "util/macro.h"
#endif
#define debug(s, ...) printf("\033[95m" __FILE__ ":" MACRO_STR2(__LINE__) ": [DBG]: " s "\033[0m\n" __VA_OPT__(, __VA_ARGS__))
#define info(s, ...) printf(__FILE__ ":" MACRO_STR2(__LINE__) ": [INF]: " s "\n", __VA_OPT__(, __VA_ARGS__))
#define warn(s, ...) fprintf(stderr, "\033[93m" __FILE__ ":" MACRO_STR2(__LINE__) ": [WAR]: " s "\033[0m\n" __VA_OPT__(, __VA_ARGS__))
#define error(s, ...) fprintf(stderr, "\033[91m" __FILE__ ":" MACRO_STR2(__LINE__) ": [ERR]: " s "\033[0m\n" __VA_OPT__(, __VA_ARGS__))
#define fatal(c, s, ...) \
do { \
printf("\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n" __VA_OPT__(, __VA_ARGS__)); \
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", "view stderr for full details: \n" s, NULL); \
exit(c); \
} while (0)

View File

@@ -1,95 +0,0 @@
#include "placing.h"
#include <stdint.h>
#include <string.h>
#include <sys/cdefs.h>
#include "../../io/audio.h"
#include "../../io/input.h"
#include "../../util/types.h"
#include "../../util/vec.h"
#include "../game.h"
#include "shapes.h"
static int clear_rows(u8 *restrict *restrict rows) {
int count = 0;
u8 *cache[4]; /* the maximum amount of rows the user can clear at once is four */
for (int y = ROWS - 1; y >= 0; y--) {
int x = 0;
while (x < COLUMNS && rows[y][x] && count < 4) x++;
if (x >= COLUMNS) cache[count++] = rows[y];
else rows[y + count] = rows[y];
}
if (count) {
for (int i = 0; i < count; i++) {
memset(cache[i], 0, COLUMNS);
rows[i] = cache[i];
}
}
return count;
}
/* writes a shape to the screen */
static void plcmnt_place(u8 *restrict const *restrict row, u8 id, i8vec2 pos) {
u8 colour = colour_from_id(id);
i8vec2 bpos[4];
shape_getblocks(id, bpos);
bpos[0] += pos;
bpos[1] += pos;
bpos[2] += pos;
bpos[3] += pos;
row[bpos[0][VY]][bpos[0][VX]] = colour;
row[bpos[1][VY]][bpos[1][VX]] = colour;
row[bpos[2][VY]][bpos[2][VX]] = colour;
row[bpos[3][VY]][bpos[3][VX]] = colour;
}
static int plcmnt_valid(u8 *restrict const *restrict const rows, i8vec2 pos) {
return pos[VX] >= 0 && pos[VX] < COLUMNS &&
pos[VY] >= 0 && pos[VY] < ROWS &&
!rows[pos[VY]][pos[VX]];
}
static int plcmnt_intersect(u8 *restrict const *restrict const rows, const u8 id, i8vec2 pos) {
i8vec2 bpos[4];
shape_getblocks(id, bpos);
return !(plcmnt_valid(rows, pos + bpos[0]) &&
plcmnt_valid(rows, pos + bpos[1]) &&
plcmnt_valid(rows, pos + bpos[2]) &&
plcmnt_valid(rows, pos + bpos[3]));
}
int place_update(struct gamedata *gdat, int movdat) {
// store the current index and ID, only changes when placed (which yields no movement) and rotation (which occurs last)
int tmp;
u8 id = gdat->pdat.cur;
// update Y axis
tmp = !!(movdat & MOVD);
gdat->pdat.sel[VY] += tmp;
tmp = tmp && plcmnt_intersect(gdat->rows, id, gdat->pdat.sel);
if (tmp) {
gdat->pdat.sel[VY]--;
plcmnt_place(gdat->rows, id, gdat->pdat.sel);
gdat->pnts += clear_rows(gdat->rows) << 4; // clear the rows that have been completed
next_shape();
audio_play(AUDIO_ID_PLACE);
if (plcmnt_intersect(gdat->rows, gdat->pdat.cur, gdat->pdat.sel))
return 1;
}
// update X axis
tmp = !!(movdat & MOVR) - !!(movdat & MOVL);
gdat->pdat.sel[VX] += (tmp && !plcmnt_intersect(gdat->rows, id, (i8vec2){gdat->pdat.sel[VX] + tmp, gdat->pdat.sel[VY]})) * tmp;
// update roll
tmp = id ^ (((!!(movdat & MOVRR) - !!(movdat & MOVRL)) * 8 + id) & 31);
gdat->pdat.cur ^= (tmp && !plcmnt_intersect(gdat->rows, id ^ tmp, gdat->pdat.sel)) * tmp;
return 0;
}

View File

@@ -1,10 +0,0 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "../game.h"
/* updates the movement of the `pdat` structure, updating the rows when colliding downwards.
* returns `0` if we successfully updated. Returns 1 if we couldn't update. (e.g. when a next block immediately collides) */
int place_update(struct gamedata *gdat, int movdat);

View File

@@ -1,83 +0,0 @@
#include "shapes.h"
#include "../../io/colour/colour8.h"
#include "../../util/types.h"
#include "../../util/vec.h"
#include "../../error.h"
void shape_getblocks(u8 id, i8vec2 *restrict out) {
struct blockdat {
u8 ax : 2, ay : 2;
u8 bx : 2, by : 2;
u8 cx : 2, cy : 2;
u8 dx : 2, dy : 2;
} dat;
switch (id) {
// O tetromino
case TET_O | TET_R0:
case TET_O | TET_R90:
case TET_O | TET_R180:
case TET_O | TET_R270: dat = (struct blockdat){1, 1, 2, 1, 1, 2, 2, 2}; break;
// I tetromino
case TET_I | TET_R0: dat = (struct blockdat){0, 1, 1, 1, 2, 1, 3, 1}; break;
case TET_I | TET_R90: dat = (struct blockdat){2, 0, 2, 1, 2, 2, 2, 3}; break;
case TET_I | TET_R180: dat = (struct blockdat){0, 2, 1, 2, 2, 2, 3, 2}; break;
case TET_I | TET_R270: dat = (struct blockdat){1, 0, 1, 1, 1, 2, 1, 3}; break;
// S tetromino
case TET_S | TET_R0: dat = (struct blockdat){1, 0, 2, 0, 0, 1, 1, 1}; break;
case TET_S | TET_R90: dat = (struct blockdat){1, 0, 1, 1, 2, 1, 2, 2}; break;
case TET_S | TET_R180: dat = (struct blockdat){1, 1, 2, 1, 0, 2, 1, 2}; break;
case TET_S | TET_R270: dat = (struct blockdat){0, 0, 0, 1, 1, 1, 1, 2}; break;
// Z tetromino
case TET_Z | TET_R0: dat = (struct blockdat){0, 0, 1, 0, 1, 1, 2, 1}; break;
case TET_Z | TET_R90: dat = (struct blockdat){2, 0, 1, 1, 2, 1, 1, 2}; break;
case TET_Z | TET_R180: dat = (struct blockdat){0, 1, 1, 1, 1, 2, 2, 2}; break;
case TET_Z | TET_R270: dat = (struct blockdat){1, 0, 0, 1, 1, 1, 0, 2}; break;
// T tetromino
case TET_T | TET_R0: dat = (struct blockdat){0, 1, 1, 1, 2, 1, 1, 2}; break;
case TET_T | TET_R90: dat = (struct blockdat){1, 0, 0, 1, 1, 1, 1, 2}; break;
case TET_T | TET_R180: dat = (struct blockdat){1, 0, 0, 1, 1, 1, 2, 1}; break;
case TET_T | TET_R270: dat = (struct blockdat){1, 0, 1, 1, 2, 1, 1, 2}; break;
// L tetromino
case TET_L | TET_R0: dat = (struct blockdat){1, 0, 1, 1, 1, 2, 2, 2}; break;
case TET_L | TET_R90: dat = (struct blockdat){0, 1, 1, 1, 2, 1, 0, 2}; break;
case TET_L | TET_R180: dat = (struct blockdat){0, 0, 1, 0, 1, 1, 1, 2}; break;
case TET_L | TET_R270: dat = (struct blockdat){2, 0, 0, 1, 1, 1, 2, 1}; break;
// J tetromino
case TET_J | TET_R0: dat = (struct blockdat){1, 0, 1, 1, 0, 2, 1, 2}; break;
case TET_J | TET_R90: dat = (struct blockdat){0, 0, 0, 1, 1, 1, 2, 1}; break;
case TET_J | TET_R180: dat = (struct blockdat){1, 0, 2, 0, 1, 1, 1, 2}; break;
case TET_J | TET_R270: dat = (struct blockdat){0, 1, 1, 1, 2, 1, 2, 2}; break;
default:
#ifndef NDEBUG
fatal(1, "something went wrong; couldn't reconise the ID.", );
#endif
break;
}
out[0] = (i8vec2){dat.ax, dat.ay};
out[1] = (i8vec2){dat.bx, dat.by};
out[2] = (i8vec2){dat.cx, dat.cy};
out[3] = (i8vec2){dat.dx, dat.dy};
}
colour8 colour_from_id(u8 id) {
switch (id & 7) {
case TET_O: return COLOUR8_YELLOW;
case TET_I: return COLOUR8_CYAN;
case TET_S: return COLOUR8_GREEN;
case TET_Z: return COLOUR8_RED;
case TET_T: return COLOUR8_MAGENTA;
case TET_L: return COLOUR8_ORANGE;
case TET_J: return COLOUR8_BLUE;
default: return COLOUR8_BLACK;
}
}

View File

@@ -1,28 +0,0 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "../../io/colour/colour8.h"
#include "../../util/types.h"
#include "../../util/vec.h"
enum tetromino {
TET_O = 0,
TET_I = 1,
TET_S = 2,
TET_Z = 3,
TET_T = 4,
TET_L = 5,
TET_J = 6,
TET_R0 = 0,
TET_R90 = 8,
TET_R180 = 16,
TET_R270 = 24,
};
#define SHAPE_WIDTH 4
#define SHAPE_HEIGHT 4
#define TETC 7
void shape_getblocks(u8 id, i8vec2 *out);
colour8 colour_from_id(u8 id);

81
src/tetris.c Normal file
View File

@@ -0,0 +1,81 @@
#include "tetris.h"
#include <string.h>
#include "util/intdef.h"
/* Board co-ordinates:
* 0 1 2 3
* 4 5 6 7
* 8 9 A B
* C D E F */
const u16 tetromino_shapes[7][4] = {
/* NONE CW_90 CW_180 CW_270 */
[TET_I] = {0x159D, 0x89AB, 0x26AE, 0x4567},
[TET_O] = {0x569A, 0x569A, 0x569A, 0x569A},
[TET_T] = {0x1456, 0x1569, 0x4569, 0x1459},
[TET_J] = {0x0456, 0x1259, 0x456A, 0x1589},
[TET_L] = {0x4536, 0x159A, 0x4568, 0x0159},
[TET_S] = {0x1245, 0x156A, 0x5689, 0x0459},
[TET_Z] = {0x0156, 0x2569, 0x459A, 0x1458},
};
void tetris_get_blocks(u8 tetromino, uint *restrict positions) {
uint shape = tetromino_shapes[tetromino & 7][tetromino >> 3];
uint *end = positions + 8;
while (positions < end) {
*positions += shape & 3;
shape >>= 2;
positions++;
}
}
void tetris_init_rows(const u8 *restrict data, const u8 *restrict *restrict out) {
const u8 *restrict *end = out + TET_HEIGHT;
while (out < end) {
*out = data;
data += TET_WIDTH;
out++;
}
}
int tetris_clear_rows(u8 *restrict *restrict rows) {
int count = 0;
u8 *cache[4]; /* the maximum amount of rows the user can clear at once is four */
for (int y = 0; y < TET_HEIGHT; y++) {
int x = 0;
while (x < TET_WIDTH && rows[y][x] && count < 4) x++;
if (x >= TET_WIDTH) cache[count++] = rows[y];
else rows[y - count] = rows[y];
}
if (count) {
for (int i = 0; i < count; i++) {
memset(cache[i], 0, TET_WIDTH);
rows[TET_HEIGHT - 1 - i] = cache[i];
}
}
return count;
}
int tetris_intersect(const u8 *restrict const *restrict rows, u8 tetromino, uint x, uint y) {
uint *pos = (uint[8]){x, y, x, y, x, y, x, y};
uint *end = pos + 8;
tetris_get_blocks(tetromino, pos);
for (; pos < end; pos += 2) {
if (*(pos + 0) >= TET_WIDTH) break;
if (*(pos + 1) >= TET_HEIGHT) break;
if (rows[*(pos + 1)][*pos]) break;
}
return pos < end;
}
void tetris_place(u8 *restrict *restrict rows, u8 tetromino, uint x, uint y) {
uint *pos = (uint[8]){x, y, x, y, x, y, x, y};
uint *end = pos + 8;
tetris_get_blocks(tetromino, pos);
tetromino &= 7; /* strip rotation data */
tetromino |= TET_NON_EMPTY;
for (; pos < end; pos += 2)
rows[*(pos + 1)][*pos] = tetromino;
}

58
src/tetris.h Normal file
View File

@@ -0,0 +1,58 @@
#ifndef TETRIS_H
#define TETRIS_H 1
#include "util/intdef.h"
#include "util/atrb.h"
#define TET_WIDTH 10
#define TET_HEIGHT 24 /* height may be 16—24 */
/* Defines tetromino indices.
* The `TET_R*` definitions specify various rotations.
* This is designed to be OR'd with the tetromino shape index.
* Shape index can be obtained via `val & 7`, and rotation index is
* obtained via `val >> 3`. */
enum tetromino {
TET_I = 0x00,
TET_O = 0x01,
TET_T = 0x02,
TET_J = 0x03,
TET_L = 0x04,
TET_S = 0x05,
TET_Z = 0x06,
TET_R0 = 0x00,
TET_R90 = 0x08,
TET_R180 = 0x10,
TET_R270 = 0x18,
/* Used when storing blocks to the board;
* allows to identify non-empty indices from empty ones.
* This value should not be used during general computation. */
TET_NON_EMPTY = 0x80,
};
/* Stores the co-ordinates of the four blocks in a 4x4 plane.
* Each co-ordinate is 4 bits wide in total. (2 bits for each axis).
* This is done over encoding the shape directly within the 16 bits (4²),
* since this is more easily parsed in practice. */
extern const u16 tetromino_shapes[7][4];
/* Adds the block position data to the numbers in `positions`.
* it is assumed `positions` has at least eight members and is initialised. `(X,Y)*4 = 8` */
void tetris_get_blocks(u8 tetromino, uint *restrict positions) NONNULL((2));
/* Initialises the rows of the Tetris play field.
* `data` is assumed to point to data of at least `TET_WIDTH * TET_HEIGHT` members.
* `out` is assumed to point to data of at least `TET_HEIGHT` members; the row pointers shall be outputted here. */
void tetris_init_rows(const u8 *restrict data, const u8 *restrict *restrict out) NONNULL((1, 2));
/* Finds up to 4 filled rows in `rows`, moving succeeding rows down and adding the cleared row on top.
* It is assumed `rows[0]` is the bottom row and `rows[TET_WIDTH-1]` is the top row.
* Returns the amount of rows cleared. */
int tetris_clear_rows(u8 *restrict *restrict rows) NONNULL((1));
/* Checks if `tetromino` intersects at (`x`, `y`) in `rows`, returns `1` if so, `0` if not. */
int tetris_intersect(const u8 *restrict const *restrict rows, u8 tetromino, uint x, uint y) NONNULL((1));
/* Writes the blocks of `tetromino` to `rows` at position `x` and `y`. */
void tetris_place(u8 *restrict *restrict rows, u8 tetromino, uint x, uint y) NONNULL((1));
#endif

51
src/util/error.c Normal file
View File

@@ -0,0 +1,51 @@
#include "error.h"
#include <stdarg.h>
#include <stdio.h>
#include "intdef.h"
static void error_log(FILE *restrict stream, const char *restrict pfx, uint ln, const char *restrict file, const char *restrict fmt, va_list ap) {
fprintf(stream, "(%s:%iu) [%s] '", file, ln, pfx);
vfprintf(stream, fmt, ap);
fprintf(stream, "'\n");
}
void error_dbg(uint ln, const char *restrict file, const char *restrict fmt, ...) {
va_list ap;
va_start(ap, fmt);
error_log(stdout, "\033[95mDBG\033[0m", ln, file, fmt, ap);
va_end(ap);
}
void error_inf(uint ln, const char *restrict file, const char *restrict fmt, ...) {
va_list ap;
va_start(ap, fmt);
error_log(stdout, "\033[93mINF\033[0m", ln, file, fmt, ap);
va_end(ap);
}
void error_war(uint ln, const char *restrict file, const char *restrict fmt, ...) {
va_list ap;
va_start(ap, fmt);
error_log(stdout, "\033[91mWAR\033[0m", ln, file, fmt, ap);
va_end(ap);
}
void error_err(uint ln, const char *restrict file, const char *restrict fmt, ...) {
va_list ap;
va_start(ap, fmt);
error_log(stdout, "\033[mFAT\033[0m", ln, file, fmt, ap);
va_end(ap);
}
void error_fat(uint ln, const char *restrict file, const char *restrict fmt, ...) {
va_list ap;
va_start(ap, fmt);
error_log(stdout, "\033[mFAT\033[0m", ln, file, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}

22
src/util/error.h Normal file
View File

@@ -0,0 +1,22 @@
/* Copyright (c) 2025 Quinn
* Licensed under the MIT Licence. See LICENSE for details */
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include "atrb.h"
#include "intdef.h"
void error_dbg(uint ln, const char *restrict file, const char *restrict fmt, ...);
void error_inf(uint ln, const char *restrict file, const char *restrict fmt, ...);
void error_war(uint ln, const char *restrict file, const char *restrict fmt, ...);
void error_err(uint ln, const char *restrict file, const char *restrict fmt, ...);
void error_fat(uint ln, const char *restrict file, const char *restrict fmt, ...) NORET;
#define debug(s, ...) error_dbg(__LINE__, __FILE__, s, ##__VA_ARGS__)
#define info(s, ...) error_inf(__LINE__, __FILE__, s, ##__VA_ARGS__)
#define warn(s, ...) error_war(__LINE__, __FILE__, s, ##__VA_ARGS__)
#define error(s, ...) error_err(__LINE__, __FILE__, s, ##__VA_ARGS__)
#define fatal(s, ...) error_fat(__LINE__, __FILE__, s, ##__VA_ARGS__)