From d04c1e879c73569b42c8bac67211b44aad43d4b4 Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 12 Jan 2025 20:12:43 +0100 Subject: [PATCH] make various changes regarding code safety and stylistic choices --- src/errors.c | 4 ++-- src/main.c | 14 ++++++++------ src/window/audio.c | 13 ++++++------- src/window/audio.h | 12 ++++++------ src/window/renderer.c | 1 - 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/errors.c b/src/errors.c index fa7d860..3b4ef09 100644 --- a/src/errors.c +++ b/src/errors.c @@ -9,7 +9,7 @@ void error(const ErrorCode error_code, const char* format, ...) { char buffer[MAX_STR_LEN] = {0}; // contains the buffer of the final string - va_list args = {0}; + va_list args; va_start(args, format); vsnprintf(buffer, MAX_STR_LEN, format, args); va_end(args); @@ -23,7 +23,7 @@ void error(const ErrorCode error_code, const char* format, ...) { void warn(const char* format, ...) { char buffer[MAX_STR_LEN] = {0}; // contains the buffer of the final string - va_list args = {0}; + va_list args; va_start(args, format); vsnprintf(buffer, MAX_STR_LEN, format, args); va_end(args); diff --git a/src/main.c b/src/main.c index 6262a5e..5610baf 100644 --- a/src/main.c +++ b/src/main.c @@ -35,8 +35,8 @@ static void init(void) { } // initialize audio - AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096); - //AudioData audio1 = audio_load_wav(audio_device, "FILE MANE"); + AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 255); + AudioData audio1 = audio_load_wav(audio_device, "FILE NAME"); } // handles game application updating @@ -53,10 +53,12 @@ static void update(void) { } } - // preform updates + // updates the game game_update((GameData){}, SDL_GetKeyboardState(NULL)); - renderer_update(&(RenderData){ - window, renderer}); + + // updates the render + RenderData render_data = {window, renderer}; + renderer_update(&render_data); } // handles game application quitting @@ -65,7 +67,7 @@ void stop(void) { } // entry point of the application -int main(int argc, char** argv) { +int main(void) { init(); while (playing) diff --git a/src/window/audio.c b/src/window/audio.c index 9791118..26f4771 100644 --- a/src/window/audio.c +++ b/src/window/audio.c @@ -13,7 +13,7 @@ typedef struct { } AudioCallbackData; // 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 AudioDevice* device = userdata; // get the callback data AudioData* audio = device->playing_audio; @@ -25,7 +25,7 @@ static void audio_mixer(void* userdata, Uint8* stream, int len) { } // 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 SDL_MixAudioFormat(stream, audio[i].buffer, device->format, mix_length, SDL_MIX_MAXVOLUME); @@ -37,7 +37,7 @@ static void audio_mixer(void* userdata, Uint8* stream, int 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** wav_buffer, Uint32* wav_length) { // build the audio converter with the audio given - SDL_AudioCVT cvt = {0}; + SDL_AudioCVT cvt; SDL_BuildAudioCVT(&cvt, wav_spec.format, wav_spec.channels, wav_spec.freq, audio_device->format, audio_device->channels, audio_device->freq); cvt.len = (*wav_length) * wav_spec.channels; // the buffer length @@ -56,8 +56,8 @@ 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) { - SDL_AudioSpec wav_spec = {0}; - AudioData audio = {0}; + SDL_AudioSpec wav_spec; + AudioData audio; SDL_LoadWAV(file_path, &wav_spec, &audio.buffer, &audio.length); convert_audio(audio_device, wav_spec, &audio.buffer, &audio.length); @@ -67,7 +67,7 @@ AudioData audio_load_wav(const AudioDevice* audio_device, const char* file_path) } // 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 uint8_t samples) { // allocate memory for the audio device AudioDevice* audio_device = malloc(sizeof(AudioDevice)); @@ -103,7 +103,6 @@ void audio_play(const AudioDevice* audio_device, const AudioData audio) { for (int i = 0; i < MAX_SOUNDS; i++) { // overrite audio that has been deallocated if (playing_audio[i].length <= 0) { - // override the audio playing_audio[i] = audio; break; // don't continue. :3 diff --git a/src/window/audio.h b/src/window/audio.h index bd54743..3340544 100644 --- a/src/window/audio.h +++ b/src/window/audio.h @@ -2,20 +2,20 @@ #include typedef struct { - Uint32 length; - Uint32 mixed_amount; - Uint8* buffer; + uint32_t length; + uint32_t mixed_amount; + uint8_t* buffer; } AudioData; typedef struct { SDL_AudioDeviceID id; - int freq; + int32_t freq; SDL_AudioFormat format; - Uint8 channels; + uint8_t channels; AudioData* playing_audio; } 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 channels, const Uint16 samples); +AudioDevice* audio_device_init(const int32_t freq, const SDL_AudioFormat format, const uint8_t channels, const uint8_t samples); void audio_play(const AudioDevice* audio_device, const AudioData audio); diff --git a/src/window/renderer.c b/src/window/renderer.c index 8ba1333..9d48702 100644 --- a/src/window/renderer.c +++ b/src/window/renderer.c @@ -5,7 +5,6 @@ #include #include "../errors.h" -#include "../main.h" int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {