make various changes regarding code safety and stylistic choices

This commit is contained in:
2025-01-12 20:12:43 +01:00
parent d915f0ede2
commit d04c1e879c
5 changed files with 22 additions and 22 deletions

View File

@@ -9,7 +9,7 @@
void error(const ErrorCode error_code, const char* format, ...) { void error(const ErrorCode error_code, const char* 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;
va_start(args, format); va_start(args, format);
vsnprintf(buffer, MAX_STR_LEN, format, args); vsnprintf(buffer, MAX_STR_LEN, format, args);
va_end(args); va_end(args);
@@ -23,7 +23,7 @@ void error(const ErrorCode error_code, const char* format, ...) {
void warn(const char* format, ...) { void warn(const char* 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;
va_start(args, format); va_start(args, format);
vsnprintf(buffer, MAX_STR_LEN, format, args); vsnprintf(buffer, MAX_STR_LEN, format, args);
va_end(args); va_end(args);

View File

@@ -35,8 +35,8 @@ static void init(void) {
} }
// initialize audio // initialize audio
AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096); AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 255);
//AudioData audio1 = audio_load_wav(audio_device, "FILE MANE"); AudioData audio1 = audio_load_wav(audio_device, "FILE NAME");
} }
// handles game application updating // handles game application updating
@@ -53,10 +53,12 @@ static void update(void) {
} }
} }
// preform updates // updates the game
game_update((GameData){}, SDL_GetKeyboardState(NULL)); 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 // handles game application quitting
@@ -65,7 +67,7 @@ void stop(void) {
} }
// entry point of the application // entry point of the application
int main(int argc, char** argv) { int main(void) {
init(); init();
while (playing) while (playing)

View File

@@ -13,7 +13,7 @@ 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;
@@ -25,7 +25,7 @@ static void audio_mixer(void* userdata, Uint8* stream, int len) {
} }
// 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);
@@ -37,7 +37,7 @@ 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** wav_buffer, Uint32* 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;
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);
cvt.len = (*wav_length) * wav_spec.channels; // the buffer length 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 // 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(const AudioDevice* audio_device, const char* file_path) {
SDL_AudioSpec wav_spec = {0}; SDL_AudioSpec wav_spec;
AudioData audio = {0}; AudioData audio;
SDL_LoadWAV(file_path, &wav_spec, &audio.buffer, &audio.length); SDL_LoadWAV(file_path, &wav_spec, &audio.buffer, &audio.length);
convert_audio(audio_device, 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 // 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 // allocate memory for the audio device
AudioDevice* audio_device = malloc(sizeof(AudioDevice)); 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++) { for (int 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

@@ -2,20 +2,20 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.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 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); void audio_play(const AudioDevice* audio_device, const AudioData audio);

View File

@@ -5,7 +5,6 @@
#include <stdio.h> #include <stdio.h>
#include "../errors.h" #include "../errors.h"
#include "../main.h"
int renderer_init(SDL_Window** window, SDL_Renderer** renderer) { int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {