use "const" where able to + random readability changes

This commit is contained in:
2025-02-04 12:17:42 +01:00
parent c8d43d9564
commit be09d57a27
14 changed files with 84 additions and 92 deletions

View File

@@ -7,7 +7,7 @@
#define MAX_STR_LEN 128
void error(const ErrorCode error_code, const char* format, ...) {
void error(ErrorCode const error_code, char const* const format, ...) {
char buffer[MAX_STR_LEN] = {0}; // contains the buffer of the final string
va_list args = {0};
@@ -21,7 +21,7 @@ void error(const ErrorCode error_code, const char* format, ...) {
exit(error_code);
}
void warn(const char* format, ...) {
void warn(char const* const format, ...) {
char buffer[MAX_STR_LEN] = {0}; // contains the buffer of the final string
va_list args = {0};

View File

@@ -21,5 +21,5 @@ enum {
};
// call when a fatal error has occurred, the program will immediately terminate when called
void error(const ErrorCode error_code, const char* format, ...);
void warn(const char* format, ...);
void error(ErrorCode error_code, char const* format, ...);
void warn(char const* format, ...);

View File

@@ -14,10 +14,10 @@
// shuffle the array using a FisherYates shuffle
static inline void shuffle(uint8_t size, ShapeId* elmnts) {
static inline void shuffle(uint8_t const size, ShapeId* const elmnts) {
for (uint8_t i = 0; i < (size - 1); i++) {
const uint8_t j = i + rand() % (size - i);
const ShapeId cache = elmnts[i];
uint8_t const j = i + rand() % (size - i);
ShapeId const cache = elmnts[i];
elmnts[i] = elmnts[j];
elmnts[j] = cache;
}
@@ -63,7 +63,7 @@ void game_init(GameData* const game_data) {
}
// called every time the game's state is updated
void game_update(GameData* game_data, const uint8_t* keys) {
void game_update(GameData* const game_data, uint8_t const* const keys) {
if (keys[SDL_SCANCODE_ESCAPE])
stop();
@@ -74,8 +74,6 @@ void game_update(GameData* game_data, const uint8_t* keys) {
if (keys[SDL_SCANCODE_Q]) move |= MOVE_ROTLEFT;
if (keys[SDL_SCANCODE_E]) move |= MOVE_ROTRIGHT;
place_update(game_data, move);
// dbg_set_all(game_data);
}
void game_free(GameData* const game_data) {

View File

@@ -9,7 +9,7 @@
#define COLUMNS ((int8_t)10)
#define ROWS ((int8_t)24)
typedef const Colour8* CRow;
typedef const Colour8* const CRow;
typedef Colour8* Row;
typedef struct {
@@ -22,5 +22,5 @@ typedef struct {
void next_shape(GameData* game_data);
void game_init(GameData* game_data); // initializes the game
void game_update(GameData* game_data, const uint8_t* keys); // updates the game's state
void game_update(GameData* game_data, uint8_t const* keys); // updates the game's state
void game_free(GameData* game_data); // free all data stored with the game

View File

@@ -7,7 +7,7 @@
#include "shapes.h"
static bool is_filled(CRow row) {
static bool is_filled(CRow const row) {
for (int8_t x = 0; x < COLUMNS; x++) {
if (row[x].packed == 0) {
return false;
@@ -17,7 +17,7 @@ static bool is_filled(CRow row) {
return true;
}
static void clear_rows(Row* rows) {
static void clear_rows(Row* const rows) {
Row cache[4] = {0}; // you can only clear four rows at a time
struct {
uint8_t filled : 3; // values will only ever be 0..4 (use extra bit for carry)
@@ -26,7 +26,7 @@ static void clear_rows(Row* rows) {
// loop through each row (excluding the empty rows at the top when clearing a line)
for (int8_t y = 0; y < (ROWS - dat.filled); y++) {
const int8_t i = (ROWS - 1) - y; // get the index starting from the bottom
int8_t const i = (ROWS - 1) - y; // get the index starting from the bottom
rows[i] = rows[i - dat.filled]; // set the row to the new or same address
@@ -53,11 +53,11 @@ static void clear_rows(Row* rows) {
}
// sets a shape to the screen
static void set_shape_i(Row* row, const ShapeId id, const int8_t pos_x) {
const Shape shape = shape_from_id(id);
const Colour8 colour = colour_from_id(id);
static void set_shape_i(Row const* const row, ShapeId const id, int8_t const pos_x) {
Shape const shape = shape_from_id(id);
Colour8 const colour = colour_from_id(id);
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
ShapeRow shape_row = shape_get_row(shape, y);
ShapeRow const shape_row = shape_get_row(shape, y);
if (shape_row == 0)
continue;
@@ -68,21 +68,21 @@ static void set_shape_i(Row* row, const ShapeId id, const int8_t pos_x) {
}
}
static inline void set_shape(Row* row, const ShapeId id, const int8_t pos_x, const int8_t pos_y) {
static inline void set_shape(Row const* const row, ShapeId const id, int8_t const pos_x, int8_t const pos_y) {
set_shape_i(&row[pos_y], id, pos_x); // calls itself, but omitting the pos_y argument, instead opting for specifying the row
}
static bool shape_intersects(const Row* rows, const ShapeId id, const int8_t x, const int8_t y) {
const Shape shape = shape_from_id(id);
static bool shape_intersects(Row const* const rows, ShapeId const id, int8_t const x, int8_t const y) {
Shape const shape = shape_from_id(id);
for (int8_t y0 = 0; y0 < SHAPE_HEIGHT; y0++) {
const ShapeRow shape_row = shape_get_row(shape, y0); // get the shape row
ShapeRow const shape_row = shape_get_row(shape, y0); // get the shape row
if (shape_row == 0) continue; // if the row doesn't contain data; continue
for (int8_t x0 = 0; x0 < SHAPE_WIDTH; x0++) {
if (is_set(shape_row, x0) == false) continue; // if the bit isn't set at this index; continue
const int8_t x1 = x + x0;
const int8_t y1 = y + y0;
int8_t const x1 = x + x0;
int8_t const y1 = y + y0;
if (x1 < 0 || x1 >= COLUMNS) return true; // if X is out of bounds
if (y1 < 0 || y1 >= ROWS) return true; // if Y is out of bounds
@@ -92,19 +92,21 @@ static bool shape_intersects(const Row* rows, const ShapeId id, const int8_t x,
return false;
}
static inline ShapeId rotate_id(const ShapeId id, const int dir) {
static inline ShapeId rotate_id(ShapeId const id, int const dir) {
return (id + dir) & 31;
}
void place_update(GameData* const game_data, const InputData move) {
// store the current index, only changes when placed (which yields no movement) and rotation (which occurs last)
ShapeId const curr_idx = game_data->curr_idx;
void place_update(GameData* const game_data, InputData const move) {
// store the current index and ID, only changes when placed (which yields no movement) and rotation (which occurs last)
uint8_t const curr_idx = game_data->curr_idx;
ShapeId const curr_id = game_data->nxt[curr_idx];
// set the shape if we moved vertically and intersected
if (move & 4) {
const int8_t y = game_data->sel_y + 1;
if (shape_intersects(game_data->rows, game_data->nxt[curr_idx], game_data->sel_x, y)) {
set_shape(game_data->rows, game_data->nxt[curr_idx], game_data->sel_x, game_data->sel_y); // if the shape intersects vertically, write the shape at the current position and return
int8_t const y = game_data->sel_y + 1;
if (shape_intersects(game_data->rows, curr_id, game_data->sel_x, y)) {
set_shape(game_data->rows, curr_id, game_data->sel_x, game_data->sel_y); // if the shape intersects vertically, write the shape at the current position and return
clear_rows(game_data->rows); // clear the rows that have been completed
next_shape(game_data);
@@ -117,27 +119,19 @@ void place_update(GameData* const game_data, const InputData move) {
// update shape's X coordinate movement
if ((move & 3) != 3 && (move & 3)) {
const int8_t x = game_data->sel_x + ((move & 3) == 1 ? -1 : 1); // either move along -x or +x
if (shape_intersects(game_data->rows, game_data->nxt[curr_idx], x, game_data->sel_y) == false) {
int8_t const x = game_data->sel_x + ((move & 3) == 1 ? -1 : 1); // either move along -x or +x
if (shape_intersects(game_data->rows, curr_id, x, game_data->sel_y) == false) {
game_data->sel_x = x; // set X if the shape does not intersect
}
}
// update the shape's rotation
if (move & 8 || move & 16) {
const ShapeId id = move & 8 // check which direction we should move
? rotate_id(game_data->nxt[curr_idx], -8)
: rotate_id(game_data->nxt[curr_idx], 8);
ShapeId const id = move & 8 // check which direction we should move
? rotate_id(curr_id, -8)
: rotate_id(curr_id, 8);
if (shape_intersects(game_data->rows, id, game_data->sel_x, game_data->sel_y) == false) {
game_data->nxt[curr_idx] = id;
}
}
}
#ifdef DEBUG
void dbg_set_all(GameData* game_data) {
for (uint8_t i = 0; i < TETROMINO_COUNT; i++)
for (uint8_t r = 0; r < 4; r++)
set_shape(game_data->rows, i | (r << 3), r * 4, i * 4);
}
#endif

View File

@@ -15,7 +15,7 @@ enum {
MOVE_ROTRIGHT = 16,
};
void place_update(GameData* game_data, const InputData move);
void place_update(GameData* game_data, InputData move);
#ifdef DEBUG
void dbg_set_all(GameData* game_data);

View File

@@ -35,9 +35,9 @@
#define SHAPE_J_180 ((Shape)0x6440) // 0110 0100 0100 0000 the J tetromino with a 180° rotation
#define SHAPE_J_270 ((Shape)0x0E20) // 0000 1110 0010 0000 the J tetromino with a 270° rotation
Shape shape_from_id(ShapeId id) {
Shape shape_from_id(ShapeId const id) {
// clang-format off
static const Shape shapes[TETROMINO_COUNT][4] = {
static Shape const shapes[TETROMINO_COUNT][4] = {
// 0° 90° 180° 170°
{SHAPE_O, SHAPE_O, SHAPE_O, SHAPE_O},
{SHAPE_I, SHAPE_I_90, SHAPE_I_180, SHAPE_I_270},
@@ -53,7 +53,7 @@ Shape shape_from_id(ShapeId id) {
return shapes[id & 7][id >> 3];
}
Colour8 colour_from_id(ShapeId id) {
Colour8 colour_from_id(ShapeId const id) {
switch (id & 7) {
case TETROMINO_O: return COLOUR_YELLOW;
case TETROMINO_I: return COLOUR_CYAN;

View File

@@ -27,11 +27,11 @@ enum {
static inline ShapeRow shape_get_row(Shape shape, uint8_t index) {
static inline ShapeRow shape_get_row(Shape const shape, uint8_t const index) {
return shape >> (((SHAPE_HEIGHT - 1) - index) * SHAPE_WIDTH) & 0xF;
}
static inline bool is_set(ShapeRow row, uint8_t index) {
static inline bool is_set(ShapeRow const row, uint8_t const index) {
return (row >> ((SHAPE_WIDTH - 1) - index) & 1) != 0;
}

View File

@@ -78,7 +78,7 @@ void stop(void) {
}
// entry point of the application
int main(int argc, char** argv) {
int main(int const argc, char const* const* const argv) {
(void)argc;
(void)argv;

View File

@@ -13,15 +13,15 @@
#define MAX_SOUNDS 64
typedef struct {
AudioData* playing_audio;
AudioDevice audio_device;
AudioData const* const playing_audio;
AudioDevice const audio_device;
} AudioCallbackData;
// audio callback from SDL_AudioSpec; called when the audio device needs more data
static void audio_mixer(void* userdata, uint8_t* stream, int32_t len) {
static void audio_mixer(void* const userdata, uint8_t* const stream, int32_t const len) {
memset(stream, 0, len); // clear the playing audio
AudioDevice* device = userdata; // get the callback data
AudioData* audio = device->playing_audio;
AudioDevice const* const device = userdata; // get the callback data
AudioData* const audio = device->playing_audio;
for (int32_t i = 0; i < MAX_SOUNDS; i++) {
// skip if the audio doesn't conain any further data
@@ -30,7 +30,7 @@ static void audio_mixer(void* userdata, uint8_t* stream, int32_t len) {
}
// get the length of which we shall be mixing
uint32_t mix_length = SDL_min(audio[i].length, len);
uint32_t const mix_length = SDL_min(audio[i].length, (uint32_t)len);
// mix the audio with the stream
SDL_MixAudioFormat(stream, audio[i].buffer, device->format, mix_length, SDL_MIX_MAXVOLUME);
@@ -40,7 +40,7 @@ static void audio_mixer(void* userdata, uint8_t* stream, int32_t len) {
}
// converts the audio to the format of the audio device
static void convert_audio(const AudioDevice* audio_device, const SDL_AudioSpec wav_spec, uint8_t** wav_buffer, uint32_t* wav_length) {
static void convert_audio(AudioDevice const* const audio_device, SDL_AudioSpec const wav_spec, uint8_t* const* const wav_buffer, uint32_t* const wav_length) {
// build the audio converter with the audio given
SDL_AudioCVT cvt = {0};
SDL_BuildAudioCVT(&cvt, wav_spec.format, wav_spec.channels, wav_spec.freq, audio_device->format, audio_device->channels, audio_device->freq);
@@ -61,7 +61,7 @@ static void convert_audio(const AudioDevice* audio_device, const SDL_AudioSpec w
}
// loads a WAV file and returns the relevant information
AudioData audio_load_wav(const AudioDevice* audio_device, const char* file_path) {
AudioData audio_load_wav(AudioDevice const* const audio_device, char const* const file_path) {
SDL_AudioSpec wav_spec = {0};
AudioData audio = {0};
@@ -73,12 +73,12 @@ AudioData audio_load_wav(const AudioDevice* audio_device, const char* file_path)
}
// initializes the audio device
AudioDevice* audio_device_init(const int32_t freq, const SDL_AudioFormat format, const uint8_t channels, const Uint16 samples) {
AudioDevice const* audio_device_init(int32_t const freq, SDL_AudioFormat const format, uint8_t const channels, Uint16 const samples) {
// allocate memory for the audio device
AudioDevice* audio_device = malloc(sizeof(AudioDevice));
AudioDevice* const audio_device = malloc(sizeof(AudioDevice));
// define the audio specification
SDL_AudioSpec spec = {freq, format, channels, samples};
SDL_AudioSpec spec = {freq, format, channels, 0, samples, 0, 0, NULL, NULL};
spec.callback = audio_mixer;
spec.userdata = audio_device;
@@ -103,8 +103,8 @@ AudioDevice* audio_device_init(const int32_t freq, const SDL_AudioFormat format,
}
// plays the audio
void audio_play(const AudioDevice* audio_device, const AudioData audio) {
AudioData* playing_audio = audio_device->playing_audio;
void audio_play(AudioDevice const* const audio_device, AudioData const audio) {
AudioData* const playing_audio = audio_device->playing_audio;
for (int32_t i = 0; i < MAX_SOUNDS; i++) {
// overrite audio that has been deallocated

View File

@@ -18,6 +18,6 @@ typedef struct {
} AudioDevice;
AudioData audio_load_wav(const AudioDevice* audio_device, const char* file_path);
AudioDevice* audio_device_init(const int freq, const SDL_AudioFormat format, const uint8_t channels, const uint16_t samples);
void audio_play(const AudioDevice* audio_device, const AudioData audio);
AudioData audio_load_wav(AudioDevice const* audio_device, char const* file_path);
AudioDevice const* audio_device_init(int freq, SDL_AudioFormat format, uint8_t channels, uint16_t samples);
void audio_play(AudioDevice const* audio_device, AudioData const audio);

View File

@@ -23,16 +23,16 @@ typedef union {
#define COLOUR_WHITE ((Colour8){0xFF}) // 1111 1111
// gets the red channel in 32 bit colour space
static inline uint8_t colour8_red32(Colour8 colour) {
static inline uint8_t colour8_red32(Colour8 const colour) {
return colour.r * (255 / 7);
}
// gets the green channel in 32 bit colour space
static inline uint8_t colour8_green32(Colour8 colour) {
static inline uint8_t colour8_green32(Colour8 const colour) {
return colour.g * (255 / 7);
}
// gets the blue channel in 32 bit colour space
static inline uint8_t colour8_blue32(Colour8 colour) {
static inline uint8_t colour8_blue32(Colour8 const colour) {
return colour.b * (255 / 3);
}

View File

@@ -15,7 +15,7 @@
#include "renderer.h"
int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
int renderer_init(SDL_Window** const window, SDL_Renderer** const renderer) {
// create a new window
*window = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (*window == NULL) {
@@ -34,22 +34,22 @@ int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
}
// draws a block at the specified position
static inline int draw_block(SDL_Renderer* renderer, int8_t x, int8_t y) {
static inline int draw_block(SDL_Renderer* const renderer, int8_t const x, int8_t const y) {
return SDL_RenderFillRect(renderer, &(SDL_Rect){x * BLOCK_WIDTH + 1 + TET_PADDING, y * BLOCK_HEIGHT + 1 + TET_PADDING, BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1});
}
// sets the colour32 from the colour8
static inline void set_colour(SDL_Renderer* renderer, Colour8 c) {
static inline void set_colour(SDL_Renderer* const renderer, Colour8 const c) {
(void)SDL_SetRenderDrawColor(renderer, colour8_red32(c), colour8_green32(c), colour8_blue32(c), 0xFF);
}
// draws a shape at the specified position
static void draw_shape(SDL_Renderer* const renderer, const ShapeId id, const int8_t pos_x, const int8_t pos_y) {
const Shape shape = shape_from_id(id);
static void draw_shape(SDL_Renderer* const renderer, ShapeId const id, int8_t const pos_x, int8_t const pos_y) {
Shape const shape = shape_from_id(id);
set_colour(renderer, colour_from_id(id));
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
const ShapeRow shape_row = shape_get_row(shape, y);
ShapeRow const shape_row = shape_get_row(shape, y);
if (shape_row == 0)
continue;
@@ -61,9 +61,9 @@ static void draw_shape(SDL_Renderer* const renderer, const ShapeId id, const int
}
// draw the block data in the level
static void render_level(SDL_Renderer* renderer, GameData* data) {
static void render_level(SDL_Renderer* const renderer, GameData const* const data) {
for (int8_t y = 0; y < ROWS; y++) {
Row row = data->rows[y];
CRow const row = data->rows[y];
for (int8_t x = 0; x < COLUMNS; x++) {
if (row[x].packed != 0) {
@@ -74,9 +74,9 @@ static void render_level(SDL_Renderer* renderer, GameData* data) {
}
}
void renderer_update(const RenderData* render_data) {
void renderer_update(const RenderData* const render_data) {
SDL_Renderer* const renderer = render_data->renderer;
const GameData* game_data = render_data->game_data;
GameData const* const game_data = render_data->game_data;
int success = 0; // if an error occurs, this value is <0

View File

@@ -10,7 +10,7 @@
#define TET_WIDTH (COLUMNS * PX_DENS - TET_PADDING) // tetris playing field width
#define TET_HEIGHT (TET_WIDTH / COLUMNS * ROWS) // tetris playing field height
#define SCREEN_WIDTH ((COLUMNS + 6) * PX_DENS) // window width
#define SCREEN_HEIGHT ((COLUMNS ) * PX_DENS / COLUMNS * ROWS) // window height
#define SCREEN_HEIGHT ((COLUMNS) * PX_DENS / COLUMNS * ROWS) // window height
#define BLOCK_WIDTH (TET_WIDTH / COLUMNS) // width of a block
#define BLOCK_HEIGHT (TET_HEIGHT / ROWS) // height of a block
@@ -21,4 +21,4 @@ typedef struct {
} RenderData;
int renderer_init(SDL_Window** window, SDL_Renderer** renderer);
void renderer_update(const RenderData* render_data);
void renderer_update(RenderData const* render_data);