don't use exact integer types where it's not necessary

This commit is contained in:
2025-04-17 09:45:27 +02:00
parent 4bba7bda9f
commit 1ee8c69d1b
4 changed files with 38 additions and 40 deletions

View File

@@ -2,58 +2,57 @@
#include <stdint.h> #include <stdint.h>
#include "../../window/audio.h"
#include "../../window/colour/colour8.h" #include "../../window/colour/colour8.h"
#include "../game.h" #include "../game.h"
#include "shapes.h" #include "shapes.h"
static bool is_filled(row_const const row) { static int is_filled(row_const const row) {
for (int8_t x = 0; x < COLUMNS; x++) { for (int8_t x = 0; x < COLUMNS; x++) {
if (row[x] == 0) { if (row[x] == 0) {
return false; return 0;
} }
} }
return true; return 1;
} }
static void clear_rows(row* const rows, uint16_t* const score) { static void clear_rows(row* const rows, uint16_t* const score) {
row cache[4] = {0}; // you can only clear four rows at a time row cache[4] = {0}; // you can only clear four rows at a time
struct { unsigned filled = 0;
uint8_t filled : 3; // values will only ever be 0..4 unsigned checked = 0;
uint8_t checked : 3; // values will only ever be 0..4
} dat = {0};
// loop through each row (excluding the empty rows at the top when clearing a line) // 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++) { for (unsigned y = 0; y < (ROWS - filled); y++) {
int8_t const i = (ROWS - 1) - y; // get the index starting from the bottom int 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 rows[i] = rows[i - filled]; // set the row to the new or same address
// continue if the line isn't filled or the max amount has been reached // continue if the line isn't filled or the max amount has been reached
if (dat.checked >= 4 || !is_filled(rows[i])) { if (checked >= 4 || !is_filled(rows[i])) {
if (dat.filled > 0 && dat.checked < 4) dat.checked++; if (filled > 0 && checked < 4) checked++;
continue; // continue to the next line continue; // continue to the next line
} }
cache[dat.filled] = rows[i]; // cache the current row address cache[filled] = rows[i]; // cache the current row address
dat.filled++; // increase filled, and keep the row in the cache filled++; // increase filled, and keep the row in the cache
dat.checked++; // increase the checked count checked++; // increase the checked count
y--; // decrease y to check this line again y--; // decrease y to check this line again
} }
if (dat.filled == 0) return; if (filled == 0) return;
*score += 8 << dat.filled; *score += 8 << filled;
// do while, as we already know that entering the loop that filled is non-zero // do while, as we already know that entering the loop that filled is non-zero
do { do {
dat.filled--; filled--;
rows[dat.filled] = cache[dat.filled]; rows[filled] = cache[filled];
// zero out the filled row // zero out the filled row
for (int8_t x = 0; x < COLUMNS; x++) for (unsigned x = 0; x < COLUMNS; x++)
cache[dat.filled][x] = 0; cache[filled][x] = 0;
} while (dat.filled > 0); } while (filled > 0);
} }
// sets a shape to the screen // sets a shape to the screen
@@ -76,24 +75,24 @@ static inline void set_shape(row const* const row, shape_id const id, int8_t con
set_shape_i(&row[pos_y], id, pos_x); // calls itself, but omitting the pos_y argument, instead opting for specifying the row 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(row const* const rows, shape_id const id, int8_t const x, int8_t const y) { static int shape_intersects(row const* const rows, shape_id const id, int8_t const x, int8_t const y) {
shape const shape = shape_from_id(id); shape const shape = shape_from_id(id);
for (int8_t y0 = 0; y0 < SHAPE_HEIGHT; y0++) { for (int y0 = 0; y0 < SHAPE_HEIGHT; y0++) {
shape_row const shape_row = shape_get_row(shape, y0); // get the shape row shape_row 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 if (shape_row == 0) continue; // if the row doesn't contain data; continue
for (int8_t x0 = 0; x0 < SHAPE_WIDTH; x0++) { for (int x0 = 0; x0 < SHAPE_WIDTH; x0++) {
if (shape_is_set(shape_row, x0) == false) continue; // if the bit isn't set at this index; continue if (shape_is_set(shape_row, x0) == false) continue; // if the bit isn't set at this index; continue
int8_t const x1 = x + x0; int const x1 = x + x0;
int8_t const y1 = y + y0; int const y1 = y + y0;
if (x1 < 0 || x1 >= COLUMNS) return true; // if X is out of bounds if (x1 < 0 || x1 >= COLUMNS) return 1; // if X is out of bounds
if (y1 < 0 || y1 >= ROWS) return true; // if Y is out of bounds if (y1 < 0 || y1 >= ROWS) return 1; // if Y is out of bounds
if (rows[y1][x1] != 0) return true; // if there is a block here if (rows[y1][x1] != 0) return 1; // if there is a block here
} }
} }
return false; return 0;
} }
static inline shape_id rotate_id(shape_id const id, int const dir) { static inline shape_id rotate_id(shape_id const id, int const dir) {

View File

@@ -2,7 +2,6 @@
#include <SDL_error.h> #include <SDL_error.h>
#include <SDL_events.h> #include <SDL_events.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
#include <stdint.h>
#include "error.h" #include "error.h"
#include "game/game.h" #include "game/game.h"
@@ -45,7 +44,7 @@ static void update(void) {
} }
// entry-point of the application // entry-point of the application
int32_t main(int32_t argc, char** argv) { int main(int argc, char** argv) {
(void)argc, (void)argv; (void)argc, (void)argv;
init(); init();

View File

@@ -19,7 +19,7 @@
#include "../error.h" #include "../error.h"
static void audiomixer(void* const userdata, uint8_t* const stream, int32_t const len) { static void audiomixer(void* const userdata, uint8_t* const stream, int const len) {
memset(stream, 0, len); // clear the playing audio memset(stream, 0, len); // clear the playing audio
audiodevice* const dev = userdata; // retreive the callback data audiodevice* const dev = userdata; // retreive the callback data
@@ -48,7 +48,7 @@ static void audiomixer(void* const userdata, uint8_t* const stream, int32_t cons
} }
// calculate how much of the current audio player we should mix into the stream // calculate how much of the current audio player we should mix into the stream
uint32_t const mixlen = SDL_min(curr->len, (uint32_t)len); int const mixlen = SDL_min(curr->len, (unsigned)len);
// mix the current buffer into the stream, and update the audio player values accordingly // mix the current buffer into the stream, and update the audio player values accordingly
SDL_MixAudioFormat(stream, curr->buf, dev->fmt, mixlen, SDL_MIX_MAXVOLUME); SDL_MixAudioFormat(stream, curr->buf, dev->fmt, mixlen, SDL_MIX_MAXVOLUME);
@@ -63,7 +63,7 @@ static void audiomixer(void* const userdata, uint8_t* const stream, int32_t cons
// converts the inputted audio to the format of dev // converts the inputted audio to the format of dev
// returns 1 upon failure, 0 upon success. When 1 is returned *bufptr will be freed. Otherwise *bufptr is reallocated // returns 1 upon failure, 0 upon success. When 1 is returned *bufptr will be freed. Otherwise *bufptr is reallocated
static int8_t audio_cvt(audiodevice const* dev, SDL_AudioSpec const* spec, uint8_t** bufptr, uint32_t* len) { static int8_t audio_cvt(audiodevice const* dev, SDL_AudioSpec const* spec, uint8_t** bufptr, unsigned* len) {
// init the converter // init the converter
SDL_AudioCVT cvt; SDL_AudioCVT cvt;
if (SDL_BuildAudioCVT(&cvt, spec->format, spec->channels, spec->freq, dev->fmt, dev->channels, dev->freq) < 0) { if (SDL_BuildAudioCVT(&cvt, spec->format, spec->channels, spec->freq, dev->fmt, dev->channels, dev->freq) < 0) {
@@ -101,7 +101,7 @@ static int8_t audio_cvt(audiodevice const* dev, SDL_AudioSpec const* spec, uint8
return 0; return 0;
} }
audiodevice* audio_device_init(int32_t freq, SDL_AudioFormat fmt, uint8_t channels, uint16_t samples) { audiodevice* audio_device_init(int freq, SDL_AudioFormat fmt, uint8_t channels, uint16_t samples) {
audiodevice* dev = malloc(sizeof(audiodevice)); audiodevice* dev = malloc(sizeof(audiodevice));
if (dev == NULL) { if (dev == NULL) {

View File

@@ -19,7 +19,7 @@ struct audioplayer {
struct audiodevice { struct audiodevice {
struct audioplayer* audio_players; struct audioplayer* audio_players;
SDL_AudioDeviceID id; SDL_AudioDeviceID id;
int32_t freq; int freq;
SDL_AudioFormat fmt; SDL_AudioFormat fmt;
uint8_t channels; uint8_t channels;
}; };
@@ -27,7 +27,7 @@ struct audiodevice {
typedef struct audiodata audiodata; typedef struct audiodata audiodata;
typedef struct audiodevice audiodevice; typedef struct audiodevice audiodevice;
audiodevice* audio_device_init(int32_t, SDL_AudioFormat, uint8_t, uint16_t); audiodevice* audio_device_init(int, SDL_AudioFormat, uint8_t, uint16_t);
void audio_play(audiodevice*, audiodata const*); void audio_play(audiodevice*, audiodata const*);
void audio_device_free(audiodevice*); void audio_device_free(audiodevice*);