mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 05:55:46 +01:00
use "const" where able to + random readability changes
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#define MAX_STR_LEN 128
|
#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
|
char buffer[MAX_STR_LEN] = {0}; // contains the buffer of the final string
|
||||||
|
|
||||||
va_list args = {0};
|
va_list args = {0};
|
||||||
@@ -21,7 +21,7 @@ void error(const ErrorCode error_code, const char* format, ...) {
|
|||||||
exit(error_code);
|
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
|
char buffer[MAX_STR_LEN] = {0}; // contains the buffer of the final string
|
||||||
|
|
||||||
va_list args = {0};
|
va_list args = {0};
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ enum {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// call when a fatal error has occurred, the program will immediately terminate when called
|
// call when a fatal error has occurred, the program will immediately terminate when called
|
||||||
void error(const ErrorCode error_code, const char* format, ...);
|
void error(ErrorCode error_code, char const* format, ...);
|
||||||
void warn(const char* format, ...);
|
void warn(char const* format, ...);
|
||||||
|
|||||||
@@ -14,10 +14,10 @@
|
|||||||
|
|
||||||
|
|
||||||
// shuffle the array using a Fisher–Yates shuffle
|
// shuffle the array using a Fisher–Yates 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++) {
|
for (uint8_t i = 0; i < (size - 1); i++) {
|
||||||
const uint8_t j = i + rand() % (size - i);
|
uint8_t const j = i + rand() % (size - i);
|
||||||
const ShapeId cache = elmnts[i];
|
ShapeId const cache = elmnts[i];
|
||||||
elmnts[i] = elmnts[j];
|
elmnts[i] = elmnts[j];
|
||||||
elmnts[j] = cache;
|
elmnts[j] = cache;
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ void game_init(GameData* const game_data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// called every time the game's state is updated
|
// 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])
|
if (keys[SDL_SCANCODE_ESCAPE])
|
||||||
stop();
|
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_Q]) move |= MOVE_ROTLEFT;
|
||||||
if (keys[SDL_SCANCODE_E]) move |= MOVE_ROTRIGHT;
|
if (keys[SDL_SCANCODE_E]) move |= MOVE_ROTRIGHT;
|
||||||
place_update(game_data, move);
|
place_update(game_data, move);
|
||||||
|
|
||||||
// dbg_set_all(game_data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_free(GameData* const game_data) {
|
void game_free(GameData* const game_data) {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#define COLUMNS ((int8_t)10)
|
#define COLUMNS ((int8_t)10)
|
||||||
#define ROWS ((int8_t)24)
|
#define ROWS ((int8_t)24)
|
||||||
|
|
||||||
typedef const Colour8* CRow;
|
typedef const Colour8* const CRow;
|
||||||
typedef Colour8* Row;
|
typedef Colour8* Row;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -22,5 +22,5 @@ typedef struct {
|
|||||||
|
|
||||||
void next_shape(GameData* game_data);
|
void next_shape(GameData* game_data);
|
||||||
void game_init(GameData* game_data); // initializes the game
|
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
|
void game_free(GameData* game_data); // free all data stored with the game
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
#include "shapes.h"
|
#include "shapes.h"
|
||||||
|
|
||||||
|
|
||||||
static bool is_filled(CRow row) {
|
static bool is_filled(CRow const row) {
|
||||||
for (int8_t x = 0; x < COLUMNS; x++) {
|
for (int8_t x = 0; x < COLUMNS; x++) {
|
||||||
if (row[x].packed == 0) {
|
if (row[x].packed == 0) {
|
||||||
return false;
|
return false;
|
||||||
@@ -17,7 +17,7 @@ static bool is_filled(CRow row) {
|
|||||||
return true;
|
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
|
Row cache[4] = {0}; // you can only clear four rows at a time
|
||||||
struct {
|
struct {
|
||||||
uint8_t filled : 3; // values will only ever be 0..4 (use extra bit for carry)
|
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)
|
// 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 (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
|
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
|
// sets a shape to the screen
|
||||||
static void set_shape_i(Row* row, const ShapeId id, const int8_t pos_x) {
|
static void set_shape_i(Row const* const row, ShapeId const id, int8_t const pos_x) {
|
||||||
const Shape shape = shape_from_id(id);
|
Shape const shape = shape_from_id(id);
|
||||||
const Colour8 colour = colour_from_id(id);
|
Colour8 const colour = colour_from_id(id);
|
||||||
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
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)
|
if (shape_row == 0)
|
||||||
continue;
|
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
|
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) {
|
static bool shape_intersects(Row const* const rows, ShapeId const id, int8_t const x, int8_t const y) {
|
||||||
const Shape shape = shape_from_id(id);
|
Shape const shape = shape_from_id(id);
|
||||||
|
|
||||||
for (int8_t y0 = 0; y0 < SHAPE_HEIGHT; y0++) {
|
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
|
if (shape_row == 0) continue; // if the row doesn't contain data; continue
|
||||||
|
|
||||||
for (int8_t x0 = 0; x0 < SHAPE_WIDTH; x0++) {
|
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
|
if (is_set(shape_row, x0) == false) continue; // if the bit isn't set at this index; continue
|
||||||
const int8_t x1 = x + x0;
|
int8_t const x1 = x + x0;
|
||||||
const int8_t y1 = y + y0;
|
int8_t const y1 = y + y0;
|
||||||
|
|
||||||
if (x1 < 0 || x1 >= COLUMNS) return true; // if X is out of bounds
|
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
|
if (y1 < 0 || y1 >= ROWS) return true; // if Y is out of bounds
|
||||||
@@ -92,20 +92,22 @@ static bool shape_intersects(const Row* rows, const ShapeId id, const int8_t x,
|
|||||||
return false;
|
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;
|
return (id + dir) & 31;
|
||||||
}
|
}
|
||||||
|
|
||||||
void place_update(GameData* const game_data, const InputData move) {
|
void place_update(GameData* const game_data, InputData const move) {
|
||||||
// store the current index, only changes when placed (which yields no movement) and rotation (which occurs last)
|
// store the current index and ID, only changes when placed (which yields no movement) and rotation (which occurs last)
|
||||||
ShapeId const curr_idx = game_data->curr_idx;
|
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
|
// set the shape if we moved vertically and intersected
|
||||||
if (move & 4) {
|
if (move & 4) {
|
||||||
const int8_t y = game_data->sel_y + 1;
|
int8_t const y = game_data->sel_y + 1;
|
||||||
if (shape_intersects(game_data->rows, game_data->nxt[curr_idx], game_data->sel_x, y)) {
|
if (shape_intersects(game_data->rows, curr_id, 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
|
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
|
clear_rows(game_data->rows); // clear the rows that have been completed
|
||||||
|
|
||||||
next_shape(game_data);
|
next_shape(game_data);
|
||||||
return;
|
return;
|
||||||
@@ -117,27 +119,19 @@ void place_update(GameData* const game_data, const InputData move) {
|
|||||||
|
|
||||||
// update shape's X coordinate movement
|
// update shape's X coordinate movement
|
||||||
if ((move & 3) != 3 && (move & 3)) {
|
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
|
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, game_data->nxt[curr_idx], x, game_data->sel_y) == false) {
|
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
|
game_data->sel_x = x; // set X if the shape does not intersect
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// update the shape's rotation
|
// update the shape's rotation
|
||||||
if (move & 8 || move & 16) {
|
if (move & 8 || move & 16) {
|
||||||
const ShapeId id = move & 8 // check which direction we should move
|
ShapeId const id = move & 8 // check which direction we should move
|
||||||
? rotate_id(game_data->nxt[curr_idx], -8)
|
? rotate_id(curr_id, -8)
|
||||||
: rotate_id(game_data->nxt[curr_idx], 8);
|
: rotate_id(curr_id, 8);
|
||||||
if (shape_intersects(game_data->rows, id, game_data->sel_x, game_data->sel_y) == false) {
|
if (shape_intersects(game_data->rows, id, game_data->sel_x, game_data->sel_y) == false) {
|
||||||
game_data->nxt[curr_idx] = id;
|
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
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ enum {
|
|||||||
MOVE_ROTRIGHT = 16,
|
MOVE_ROTRIGHT = 16,
|
||||||
};
|
};
|
||||||
|
|
||||||
void place_update(GameData* game_data, const InputData move);
|
void place_update(GameData* game_data, InputData move);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
void dbg_set_all(GameData* game_data);
|
void dbg_set_all(GameData* game_data);
|
||||||
|
|||||||
@@ -35,9 +35,9 @@
|
|||||||
#define SHAPE_J_180 ((Shape)0x6440) // 0110 0100 0100 0000 the J tetromino with a 180° rotation
|
#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
|
#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
|
// clang-format off
|
||||||
static const Shape shapes[TETROMINO_COUNT][4] = {
|
static Shape const shapes[TETROMINO_COUNT][4] = {
|
||||||
// 0° 90° 180° 170°
|
// 0° 90° 180° 170°
|
||||||
{SHAPE_O, SHAPE_O, SHAPE_O, SHAPE_O},
|
{SHAPE_O, SHAPE_O, SHAPE_O, SHAPE_O},
|
||||||
{SHAPE_I, SHAPE_I_90, SHAPE_I_180, SHAPE_I_270},
|
{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];
|
return shapes[id & 7][id >> 3];
|
||||||
}
|
}
|
||||||
|
|
||||||
Colour8 colour_from_id(ShapeId id) {
|
Colour8 colour_from_id(ShapeId const id) {
|
||||||
switch (id & 7) {
|
switch (id & 7) {
|
||||||
case TETROMINO_O: return COLOUR_YELLOW;
|
case TETROMINO_O: return COLOUR_YELLOW;
|
||||||
case TETROMINO_I: return COLOUR_CYAN;
|
case TETROMINO_I: return COLOUR_CYAN;
|
||||||
|
|||||||
@@ -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;
|
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;
|
return (row >> ((SHAPE_WIDTH - 1) - index) & 1) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ void stop(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// entry point of the application
|
// entry point of the application
|
||||||
int main(int argc, char** argv) {
|
int main(int const argc, char const* const* const argv) {
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
|
||||||
|
|||||||
@@ -13,15 +13,15 @@
|
|||||||
#define MAX_SOUNDS 64
|
#define MAX_SOUNDS 64
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
AudioData* playing_audio;
|
AudioData const* const playing_audio;
|
||||||
AudioDevice audio_device;
|
AudioDevice const audio_device;
|
||||||
} AudioCallbackData;
|
} AudioCallbackData;
|
||||||
|
|
||||||
// audio callback from SDL_AudioSpec; called when the audio device needs more data
|
// 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
|
memset(stream, 0, len); // clear the playing audio
|
||||||
AudioDevice* device = userdata; // get the callback data
|
AudioDevice const* const device = userdata; // get the callback data
|
||||||
AudioData* audio = device->playing_audio;
|
AudioData* const audio = device->playing_audio;
|
||||||
|
|
||||||
for (int32_t i = 0; i < MAX_SOUNDS; i++) {
|
for (int32_t i = 0; i < MAX_SOUNDS; i++) {
|
||||||
// skip if the audio doesn't conain any further data
|
// 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
|
// 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
|
// mix the audio with the stream
|
||||||
SDL_MixAudioFormat(stream, audio[i].buffer, device->format, mix_length, SDL_MIX_MAXVOLUME);
|
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
|
// 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
|
// build the audio converter with the audio given
|
||||||
SDL_AudioCVT cvt = {0};
|
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);
|
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
|
// 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};
|
SDL_AudioSpec wav_spec = {0};
|
||||||
AudioData audio = {0};
|
AudioData audio = {0};
|
||||||
|
|
||||||
@@ -73,12 +73,12 @@ AudioData audio_load_wav(const AudioDevice* audio_device, const char* file_path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// initializes the audio device
|
// 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
|
// allocate memory for the audio device
|
||||||
AudioDevice* audio_device = malloc(sizeof(AudioDevice));
|
AudioDevice* const audio_device = malloc(sizeof(AudioDevice));
|
||||||
|
|
||||||
// define the audio specification
|
// 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.callback = audio_mixer;
|
||||||
spec.userdata = audio_device;
|
spec.userdata = audio_device;
|
||||||
|
|
||||||
@@ -103,8 +103,8 @@ AudioDevice* audio_device_init(const int32_t freq, const SDL_AudioFormat format,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// plays the audio
|
// plays the audio
|
||||||
void audio_play(const AudioDevice* audio_device, const AudioData audio) {
|
void audio_play(AudioDevice const* const audio_device, AudioData const audio) {
|
||||||
AudioData* playing_audio = audio_device->playing_audio;
|
AudioData* const playing_audio = audio_device->playing_audio;
|
||||||
|
|
||||||
for (int32_t i = 0; i < MAX_SOUNDS; i++) {
|
for (int32_t i = 0; i < MAX_SOUNDS; i++) {
|
||||||
// overrite audio that has been deallocated
|
// overrite audio that has been deallocated
|
||||||
|
|||||||
@@ -18,6 +18,6 @@ typedef struct {
|
|||||||
} AudioDevice;
|
} AudioDevice;
|
||||||
|
|
||||||
|
|
||||||
AudioData audio_load_wav(const AudioDevice* audio_device, const char* file_path);
|
AudioData audio_load_wav(AudioDevice const* audio_device, char const* file_path);
|
||||||
AudioDevice* audio_device_init(const int freq, const SDL_AudioFormat format, const uint8_t channels, const uint16_t samples);
|
AudioDevice const* audio_device_init(int freq, SDL_AudioFormat format, uint8_t channels, uint16_t samples);
|
||||||
void audio_play(const AudioDevice* audio_device, const AudioData audio);
|
void audio_play(AudioDevice const* audio_device, AudioData const audio);
|
||||||
|
|||||||
@@ -23,16 +23,16 @@ typedef union {
|
|||||||
#define COLOUR_WHITE ((Colour8){0xFF}) // 1111 1111
|
#define COLOUR_WHITE ((Colour8){0xFF}) // 1111 1111
|
||||||
|
|
||||||
// gets the red channel in 32 bit colour space
|
// 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);
|
return colour.r * (255 / 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
// gets the green channel in 32 bit colour space
|
// 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);
|
return colour.g * (255 / 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
// gets the blue channel in 32 bit colour space
|
// 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);
|
return colour.b * (255 / 3);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
#include "renderer.h"
|
#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
|
// create a new window
|
||||||
*window = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
*window = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||||
if (*window == NULL) {
|
if (*window == NULL) {
|
||||||
@@ -34,22 +34,22 @@ int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// draws a block at the specified position
|
// 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});
|
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
|
// 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);
|
(void)SDL_SetRenderDrawColor(renderer, colour8_red32(c), colour8_green32(c), colour8_blue32(c), 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
// draws a shape at the specified position
|
// 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) {
|
static void draw_shape(SDL_Renderer* const renderer, ShapeId const id, int8_t const pos_x, int8_t const pos_y) {
|
||||||
const Shape shape = shape_from_id(id);
|
Shape const shape = shape_from_id(id);
|
||||||
set_colour(renderer, colour_from_id(id));
|
set_colour(renderer, colour_from_id(id));
|
||||||
|
|
||||||
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
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)
|
if (shape_row == 0)
|
||||||
continue;
|
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
|
// 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++) {
|
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++) {
|
for (int8_t x = 0; x < COLUMNS; x++) {
|
||||||
if (row[x].packed != 0) {
|
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;
|
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
|
int success = 0; // if an error occurs, this value is <0
|
||||||
|
|||||||
@@ -5,14 +5,14 @@
|
|||||||
|
|
||||||
#include "../game/game.h"
|
#include "../game/game.h"
|
||||||
|
|
||||||
#define PX_DENS 25 // pixel density; pixels per block
|
#define PX_DENS 25 // pixel density; pixels per block
|
||||||
#define TET_PADDING 10 // padding around the tetris playing field
|
#define TET_PADDING 10 // padding around the tetris playing field
|
||||||
#define TET_WIDTH (COLUMNS * PX_DENS - TET_PADDING) // tetris playing field width
|
#define TET_WIDTH (COLUMNS * PX_DENS - TET_PADDING) // tetris playing field width
|
||||||
#define TET_HEIGHT (TET_WIDTH / COLUMNS * ROWS) // tetris playing field height
|
#define TET_HEIGHT (TET_WIDTH / COLUMNS * ROWS) // tetris playing field height
|
||||||
#define SCREEN_WIDTH ((COLUMNS + 6) * PX_DENS) // window width
|
#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_WIDTH (TET_WIDTH / COLUMNS) // width of a block
|
||||||
#define BLOCK_HEIGHT (TET_HEIGHT / ROWS) // height of a block
|
#define BLOCK_HEIGHT (TET_HEIGHT / ROWS) // height of a block
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SDL_Window* window;
|
SDL_Window* window;
|
||||||
@@ -21,4 +21,4 @@ typedef struct {
|
|||||||
} RenderData;
|
} RenderData;
|
||||||
|
|
||||||
int renderer_init(SDL_Window** window, SDL_Renderer** renderer);
|
int renderer_init(SDL_Window** window, SDL_Renderer** renderer);
|
||||||
void renderer_update(const RenderData* render_data);
|
void renderer_update(RenderData const* render_data);
|
||||||
|
|||||||
Reference in New Issue
Block a user