fix pedantic warnings

This commit is contained in:
2025-01-24 19:35:13 +01:00
parent ec77c6958f
commit 3190bb4732
12 changed files with 112 additions and 77 deletions

View File

@@ -4,7 +4,7 @@ NAME = sdl_template
CC := clang CC := clang
STD := c17 STD := c17
LANG = c LANG = c
CFLAGS := $(shell pkg-config --cflags sdl2) -Wall -g CFLAGS := $(shell pkg-config --cflags sdl2) -Wall -g -pedantic
LDFLAGS := $(shell pkg-config --libs sdl2) -lm LDFLAGS := $(shell pkg-config --libs sdl2) -lm
# file locations # file locations

View File

@@ -1,8 +1,9 @@
#include "errors.h" #include "errors.h"
#include <SDL2/SDL.h> #include <SDL_messagebox.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#define MAX_STR_LEN 128 #define MAX_STR_LEN 128

View File

@@ -1,10 +1,12 @@
#include "game.h" #include "game.h"
#include <SDL_scancode.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include "../main.h" #include "../main.h"
#include "../window/colour.h"
#include "shapes.h" #include "shapes.h"
@@ -38,7 +40,7 @@ static void clear_rows(Row* row) {
} }
// sets a shape to the screen // sets a shape to the screen
static void _set_shape(Row* row, const Shape shape, const Colour colour, const uint8_t pos_x) { static void set_shape_i(Row* row, const Shape shape, const Colour colour, const uint8_t pos_x) {
for (uint8_t y = 0; y < SHAPE_HEIGHT; y++) { for (uint8_t y = 0; y < SHAPE_HEIGHT; y++) {
ShapeRow shape_row = shape_get_row(shape, y); ShapeRow shape_row = shape_get_row(shape, y);
@@ -52,11 +54,11 @@ static void _set_shape(Row* row, const Shape shape, const Colour colour, const u
} }
static inline void set_shape(Row* row, const Shape shape, const Colour colour, const uint8_t pos_x, const uint8_t pos_y) { static inline void set_shape(Row* row, const Shape shape, const Colour colour, const uint8_t pos_x, const uint8_t pos_y) {
_set_shape(&row[pos_y], shape, colour, pos_x); // calls itself, but omitting the pos_y argument, instead opting for specifying the row set_shape_i(&row[pos_y], shape, colour, pos_x); // calls itself, but omitting the pos_y argument, instead opting for specifying the row
} }
// 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* keys) { void game_update(GameData* game_data, const uint8_t* keys) {
if (keys[SDL_SCANCODE_ESCAPE]) { if (keys[SDL_SCANCODE_ESCAPE]) {
stop(); stop();
} }

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <SDL2/SDL.h>
#include <stdint.h>
#include "../window/colour.h" #include "../window/colour.h"
@@ -18,4 +19,4 @@ typedef struct {
} GameData; } GameData;
// updates the game's state // updates the game's state
void game_update(GameData* game_data, const Uint8* keys); void game_update(GameData* game_data, const uint8_t* keys);

View File

@@ -1,36 +1,40 @@
#pragma once
#include <stdint.h> #include <stdint.h>
#include "../util/typed_enums.h"
#define SHAPE_WIDTH 4 #define SHAPE_WIDTH 4
#define SHAPE_HEIGHT 4 #define SHAPE_HEIGHT 4
typedef enum : uint16_t { typedef enum_t(uint16_t){
// clang-format off // clang-format off
/* 0 1 2 3 */ /* 0 1 2 3 */
TETROMINO_I = 0b1000100010001000, // 1000 1000 1000 1000 the I tetromino with no rotation TETROMINO_I = 0x8888, // 1000 1000 1000 1000 the I tetromino with no rotation
TETROMINO_I_90 = 0b0000000000001111, // 0000 0000 0000 1111 the I tetromino with a no rotation TETROMINO_I_90 = 0x000F, // 0000 0000 0000 1111 the I tetromino with a no rotation
TETROMINO_O = 0b1100110000000000, // 1100 1100 0000 0000 the O tetromino with no rotation TETROMINO_O = 0xCC00, // 1100 1100 0000 0000 the O tetromino with no rotation
TETROMINO_T = 0b1110010000000000, // 1110 0100 0000 0000 the T tetromino with no rotation TETROMINO_T = 0xE400, // 1110 0100 0000 0000 the T tetromino with no rotation
TETROMINO_T_90 = 0b1100110010000000, // 1100 1100 1000 0000 the T tetromino with a no rotation TETROMINO_T_90 = 0xCC80, // 1100 1100 1000 0000 the T tetromino with a no rotation
TETROMINO_T_180 = 0b0100111000000000, // 0100 1110 0000 0000 the T tetromino with a 180° rotation TETROMINO_T_180 = 0x4E00, // 0100 1110 0000 0000 the T tetromino with a 180° rotation
TETROMINO_T_270 = 0b0010011000100000, // 0010 0110 0010 0000 the T tetromino with a 270° rotation TETROMINO_T_270 = 0x2620, // 0010 0110 0010 0000 the T tetromino with a 270° rotation
TETROMINO_L = 0b1000100011000000, // 1000 1000 1100 0000 the L tetromino with no rotation TETROMINO_L = 0x88C0, // 1000 1000 1100 0000 the L tetromino with no rotation
TETROMINO_L_90 = 0b1110100000000000, // 1110 1000 0000 0000 the L tetromino with a no rotation TETROMINO_L_90 = 0xE800, // 1110 1000 0000 0000 the L tetromino with a no rotation
TETROMINO_L_180 = 0b1100010001000000, // 1100 0100 0100 0000 the L tetromino with a 180° rotation TETROMINO_L_180 = 0xC440, // 1100 0100 0100 0000 the L tetromino with a 180° rotation
TETROMINO_L_270 = 0b0000001011100000, // 0000 0010 1110 0000 the L tetromino with a 270° rotation TETROMINO_L_270 = 0x02E0, // 0000 0010 1110 0000 the L tetromino with a 270° rotation
TETROMINO_J = 0b0100010011000000, // 0100 0100 1100 0000 the J tetromino with no rotation TETROMINO_J = 0x44C0, // 0100 0100 1100 0000 the J tetromino with no rotation
TETROMINO_J_90 = 0b1000111000000000, // 1000 1110 0000 0000 the J tetromino with a no rotation TETROMINO_J_90 = 0x8E00, // 1000 1110 0000 0000 the J tetromino with a no rotation
TETROMINO_J_180 = 0b1100100010000000, // 1100 1000 1000 0000 the J tetromino with a 180° rotation TETROMINO_J_180 = 0xC880, // 1100 1000 1000 0000 the J tetromino with a 180° rotation
TETROMINO_J_270 = 0b1110001000000000, // 1110 0010 0000 0000 the J tetromino with a 270° rotation TETROMINO_J_270 = 0xC400, // 1110 0010 0000 0000 the J tetromino with a 270° rotation
TETROMINO_S = 0b0110110000000000, // 0110 1100 0000 0000 the S tetromino with no rotation TETROMINO_S = 0x6C00, // 0110 1100 0000 0000 the S tetromino with no rotation
TETROMINO_S_90 = 0b1000110001000000, // 1000 1100 0100 0000 the S tetromino with a no rotation TETROMINO_S_90 = 0x8C40, // 1000 1100 0100 0000 the S tetromino with a no rotation
TETROMINO_Z = 0b1100011000000000, // 1100 0110 0000 0000 the Z tetromino with no rotation TETROMINO_Z = 0xC600, // 1100 0110 0000 0000 the Z tetromino with no rotation
TETROMINO_Z_90 = 0b0100110010000000, // 0100 1100 1000 0000 the Z tetromino with a no rotation TETROMINO_Z_90 = 0x4C80, // 0100 1100 1000 0000 the Z tetromino with a no rotation
// clang-format on // clang-format on
} Shape; } Shape;
typedef uint8_t ShapeRow; typedef uint8_t ShapeRow;
static inline ShapeRow shape_get_row(Shape shape, uint8_t index) { static inline ShapeRow shape_get_row(Shape shape, uint8_t index) {
return shape >> (((SHAPE_HEIGHT - 1) - index) * 4) & 0b1111; return shape >> (((SHAPE_HEIGHT - 1) - index) * 4) & 0xF;
} }
static inline _Bool is_set(ShapeRow row, uint8_t index) { static inline _Bool is_set(ShapeRow row, uint8_t index) {

View File

@@ -1,8 +1,14 @@
#include "main.h" #include "main.h"
#include <SDL2/SDL.h> #include <SDL.h>
#include <SDL_error.h>
#include <SDL_events.h>
#include <SDL_keyboard.h>
#include <SDL_render.h>
#include <SDL_video.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "errors.h" #include "errors.h"
@@ -19,7 +25,7 @@ bool playing = true;
SDL_Window* window = NULL; SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL; SDL_Renderer* renderer = NULL;
GameData game_data = {}; GameData game_data = {0};
// handles game application initialisation // handles game application initialisation
@@ -68,6 +74,9 @@ void stop(void) {
// entry point of the application // entry point of the application
int main(int argc, char** argv) { int main(int argc, char** argv) {
(void)argc;
(void)argv;
init(); init();
while (playing) while (playing)

8
src/util/typed_enums.h Normal file
View File

@@ -0,0 +1,8 @@
#pragma once
#ifdef __clang__
#pragma clang diagnostic ignored "-Wfixed-enum-extension"
#define enum_t(T) enum : T
#else
#define enum_t(T) enum
#endif

View File

@@ -1,6 +1,11 @@
#include "audio.h" #include "audio.h"
#include <SDL2/SDL.h> #include <SDL_audio.h>
#include <SDL_error.h>
#include <SDL_stdinc.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "../errors.h" #include "../errors.h"
@@ -13,19 +18,19 @@ typedef struct {
} 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* stream, int len) { static void audio_mixer(void* userdata, uint8_t* stream, int32_t 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* device = userdata; // get the callback data
AudioData* audio = device->playing_audio; AudioData* audio = device->playing_audio;
for (int 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
if (audio[i].length <= 0) { if (audio[i].length <= 0) {
continue; continue;
} }
// get the length of which we shall be mixing // get the length of which we shall be mixing
Uint32 mix_length = SDL_min(audio[i].length, len); uint32_t mix_length = SDL_min(audio[i].length, 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);
@@ -35,13 +40,14 @@ static void audio_mixer(void* userdata, Uint8* stream, int 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** wav_buffer, Uint32* wav_length) { static void convert_audio(const AudioDevice* audio_device, const SDL_AudioSpec wav_spec, uint8_t** wav_buffer, uint32_t* 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);
// suddenly becomes signed
cvt.len = (*wav_length) * wav_spec.channels; // the buffer length cvt.len = (*wav_length) * wav_spec.channels; // the buffer length
cvt.buf = (Uint8*)SDL_malloc(cvt.len * cvt.len_mult); // allocate size for the new buffer cvt.buf = (uint8_t*)SDL_malloc(cvt.len * cvt.len_mult); // allocate size for the new buffer
memcpy(cvt.buf, *wav_buffer, *wav_length); // copy wav data to cvt buffer; memcpy(cvt.buf, *wav_buffer, *wav_length); // copy wav data to cvt buffer;
// convert // convert
@@ -67,7 +73,7 @@ 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 int freq, const SDL_AudioFormat format, const Uint8 channels, const Uint16 samples) { AudioDevice* audio_device_init(const int32_t freq, const SDL_AudioFormat format, const uint8_t channels, const Uint16 samples) {
// allocate memory for the audio device // allocate memory for the audio device
AudioDevice* audio_device = malloc(sizeof(AudioDevice)); AudioDevice* audio_device = malloc(sizeof(AudioDevice));
@@ -100,10 +106,9 @@ AudioDevice* audio_device_init(const int freq, const SDL_AudioFormat format, con
void audio_play(const AudioDevice* audio_device, const AudioData audio) { void audio_play(const AudioDevice* audio_device, const AudioData audio) {
AudioData* playing_audio = audio_device->playing_audio; AudioData* playing_audio = audio_device->playing_audio;
for (int 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
if (playing_audio[i].length <= 0) { if (playing_audio[i].length <= 0) {
// override the audio // override the audio
playing_audio[i] = audio; playing_audio[i] = audio;
break; // don't continue. :3 break; // don't continue. :3

View File

@@ -1,21 +1,23 @@
#pragma once #pragma once
#include <SDL2/SDL.h>
#include <SDL_audio.h>
#include <stdint.h>
typedef struct { typedef struct {
Uint32 length; uint32_t length;
Uint32 mixed_amount; uint32_t mixed_amount;
Uint8* buffer; uint8_t* buffer;
} AudioData; } AudioData;
typedef struct { typedef struct {
SDL_AudioDeviceID id; SDL_AudioDeviceID id;
int freq; int32_t freq;
SDL_AudioFormat format; SDL_AudioFormat format;
Uint8 channels; uint8_t channels;
AudioData* playing_audio; AudioData* playing_audio;
} AudioDevice; } AudioDevice;
AudioData audio_load_wav(const AudioDevice* audio_device, const char* file_path); 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 channels, const Uint16 samples); 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); void audio_play(const AudioDevice* audio_device, const AudioData audio);

View File

@@ -13,19 +13,19 @@ typedef union {
} Colour; } Colour;
#define RED ((uint8_t)0b11000011)
#define YELLOW ((uint8_t)0b11110011)
#define ORANGE ((uint8_t)0b11100011)
#define GREEN ((uint8_t)0b00110011)
#define CYAN ((uint8_t)0b00111111)
#define BLUE ((uint8_t)0b00001111)
#define MAGENTA ((uint8_t)0b11001111)
#define NONE ((uint8_t)0) #define NONE ((uint8_t)0)
#define BLACK ((uint8_t)3) #define BLACK ((uint8_t)3)
#define RED ((uint8_t)0xC0 | BLACK)
#define YELLOW ((uint8_t)0xF0 | BLACK)
#define ORANGE ((uint8_t)0xE0 | BLACK)
#define GREEN ((uint8_t)0x30 | BLACK)
#define CYAN ((uint8_t)0x3C | BLACK)
#define BLUE ((uint8_t)0x0C | BLACK)
#define MAGENTA ((uint8_t)0xCC | BLACK)
#define WHITE ((uint8_t)0xFF) #define WHITE ((uint8_t)0xFF)
#define COLOUR_NONE ((Colour){NONE})
#define COLOUR_BLACK ((Colour){BLACK})
#define COLOUR_RED ((Colour){RED}) #define COLOUR_RED ((Colour){RED})
#define COLOUR_YELLOW ((Colour){YELLOW}) #define COLOUR_YELLOW ((Colour){YELLOW})
#define COLOUR_ORANGE ((Colour){ORANGE}) #define COLOUR_ORANGE ((Colour){ORANGE})
@@ -33,7 +33,4 @@ typedef union {
#define COLOUR_CYAN ((Colour){CYAN}) #define COLOUR_CYAN ((Colour){CYAN})
#define COLOUR_BLUE ((Colour){BLUE}) #define COLOUR_BLUE ((Colour){BLUE})
#define COLOUR_MAGENTA ((Colour){MAGENTA}) #define COLOUR_MAGENTA ((Colour){MAGENTA})
#define COLOUR_NONE ((Colour){NONE})
#define COLOUR_BLACK ((Colour){BLACK})
#define COLOUR_WHITE ((Colour){WHITE}) #define COLOUR_WHITE ((Colour){WHITE})

View File

@@ -1,10 +1,15 @@
// initializes the window and renderer // initializes the window and renderer
#include "renderer.h" #include "renderer.h"
#include <SDL2/SDL.h> #include <SDL_error.h>
#include <SDL_rect.h>
#include <SDL_render.h>
#include <SDL_video.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include "../errors.h" #include "../errors.h"
#include "../game/game.h"
#include "colour.h" #include "colour.h"
#include "renderer.h" #include "renderer.h"

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL_render.h>
#include <SDL_video.h>
#include <stdint.h> #include <stdint.h>
#include "../game/game.h" #include "../game/game.h"