fix: pedantic warnings

This commit is contained in:
2025-01-24 19:50:31 +01:00
parent 591d09b610
commit 85f3a73409
8 changed files with 48 additions and 23 deletions

View File

@@ -1,8 +1,9 @@
#include "errors.h" #include "errors.h"
#include <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,14 @@
#include "game.h" #include "game.h"
#include <SDL_scancode.h>
#include <stdint.h>
#include "../main.h" #include "../main.h"
// 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) {
(void)game_data;
if (keys[SDL_SCANCODE_ESCAPE]) { if (keys[SDL_SCANCODE_ESCAPE]) {
stop(); stop();
} }

View File

@@ -1,9 +1,10 @@
#pragma once #pragma once
#include <SDL.h> #include <stdint.h>
// stores the data used in the game // stores the data used in the game
typedef struct { typedef struct {
void* val;
} 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,14 +1,20 @@
#include "main.h" #include "main.h"
#include <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 <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "errors.h" #include "errors.h"
#include "game/game.h" #include "game/game.h"
#include "window/audio.h" // #include "window/audio.h"
#include "window/renderer.h" #include "window/renderer.h"
#ifdef __EMSCRIPTEN__ // for web builds #ifdef __EMSCRIPTEN__ // for web builds
@@ -36,8 +42,8 @@ static void init(void) {
} }
// initialize audio // initialize audio
AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 255); // AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 255);
//AudioData audio1 = audio_load_wav(audio_device, "FILE NAME"); // AudioData audio1 = audio_load_wav(audio_device, "FILE NAME");
} }
// handles game application updating // handles game application updating
@@ -55,7 +61,7 @@ static void update(void) {
} }
// updates the game // updates the game
game_update((GameData){}, SDL_GetKeyboardState(NULL)); game_update((GameData){NULL}, SDL_GetKeyboardState(NULL));
// updates the render // updates the render
RenderData render_data = {window, renderer}; RenderData render_data = {window, renderer};
@@ -69,6 +75,8 @@ void stop(void) {
// entry point of the application // entry point of the application
int32_t main(int32_t argc, char** argv) { int32_t main(int32_t argc, char** argv) {
(void)argc;
(void)argv;
init(); init();
while (playing) while (playing)

View File

@@ -1,6 +1,11 @@
#include "audio.h" #include "audio.h"
#include <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"
@@ -18,7 +23,7 @@ static void audio_mixer(void* userdata, uint8_t* stream, int32_t len) {
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;
@@ -35,14 +40,15 @@ 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** 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; 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);
cvt.len = (*wav_length) * wav_spec.channels; // the buffer length // suddenly becomes signed
cvt.buf = (Uint8*)SDL_malloc(cvt.len * cvt.len_mult); // allocate size for the new buffer cvt.len = (*wav_length) * wav_spec.channels; // the buffer length
memcpy(cvt.buf, *wav_buffer, *wav_length); // copy wav data to cvt 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;
// convert // convert
SDL_ConvertAudio(&cvt); SDL_ConvertAudio(&cvt);
@@ -56,8 +62,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; SDL_AudioSpec wav_spec = {0};
AudioData audio; AudioData audio = {0};
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 +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 int32_t freq, const SDL_AudioFormat format, const uint8_t channels, const uint8_t 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,7 +106,7 @@ AudioDevice* audio_device_init(const int32_t freq, const SDL_AudioFormat format,
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

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include <SDL.h>
#include <SDL_audio.h>
#include <stdint.h>
typedef struct { typedef struct {
uint32_t length; uint32_t length;
@@ -17,5 +19,5 @@ typedef struct {
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 int32_t freq, const SDL_AudioFormat format, const uint8_t channels, const uint8_t 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

@@ -1,7 +1,9 @@
// initializes the window and renderer // initializes the window and renderer
#include "renderer.h" #include "renderer.h"
#include <SDL.h> #include <SDL_error.h>
#include <SDL_render.h>
#include <SDL_video.h>
#include <stdio.h> #include <stdio.h>
#include "../errors.h" #include "../errors.h"

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <SDL.h> #include <SDL_render.h>
#include <SDL_video.h>
typedef struct { typedef struct {
SDL_Window* window; SDL_Window* window;