pedantic fixes and stylistic changes

This commit is contained in:
unset
2025-01-25 16:21:23 +01:00
parent a25e65795a
commit f25594e060
11 changed files with 78 additions and 43 deletions

View File

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

View File

@@ -1,6 +1,7 @@
#pragma once
#include <stdint.h>
typedef unsigned char ErrorCode;
typedef uint8_t ErrorCode;
enum {
SUCCESS = 0,
FAILURE = -1,

View File

@@ -1,11 +1,14 @@
#include "level.h"
#include <SDL2/SDL.h>
#include <SDL_scancode.h>
#include <SDL_stdinc.h>
#include <stdbool.h>
#include <stdint.h>
#include "../constants.h"
#include "../main.h"
#include "../window/renderer.h"
#include "../window/audio.h"
#include "../window/colour.h"
#include "vector2.h"
@@ -49,7 +52,7 @@ void level_init(Level* level) {
}
// updates the player's "bouncer"
static void update_player(Bouncer* bouncer, Ball* ball, const Uint8* keys) {
static void update_player(Bouncer* bouncer, Ball* ball, const uint8_t* keys) {
// if move bouncer LEFT
if (keys[SDL_SCANCODE_A] || keys[SDL_SCANCODE_LEFT]) {
if (((bouncer->pos.x) < 0) == false) {
@@ -150,7 +153,7 @@ static void update_ball(Level* level, Ball* ball, Bouncer* bouncer) {
}
// updates the level
void level_update(Level* level, const Uint8* keys) {
void level_update(Level* level, const uint8_t* keys) {
Bouncer* bouncer = &level->bouncer;
Ball* ball = &level->ball;

View File

@@ -1,22 +1,22 @@
#pragma once
#include <SDL2/SDL.h>
#include <stdbool.h>
#include <stdint.h>
#include "../constants.h"
#include "../window/colour.h"
#include "../window/audio.h"
#include "../window/colour.h"
#include "vector2.h"
typedef struct {
Vector2 pos;
Vector2 direction;
unsigned size;
uint32_t size;
bool moving;
} Ball;
typedef struct {
Vector2 pos;
unsigned width;
uint32_t width;
} Bouncer;
typedef struct {
@@ -35,4 +35,4 @@ typedef struct {
void level_init(Level* level);
void level_update(Level* level, const Uint8* keys);
void level_update(Level* level, const uint8_t* keys);

View File

@@ -1,8 +1,16 @@
#include "main.h"
#include <SDL2/SDL.h>
#include <SDL.h>
#include <SDL_audio.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 <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "errors.h"
@@ -74,6 +82,8 @@ void stop(void) {
// entry point of the application
int main(int argc, char** argv) {
(void)argc;
(void)argv;
init();
#if __EMSCRIPTEN__

View File

@@ -1,6 +1,11 @@
#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"
@@ -13,19 +18,19 @@ 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;
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
if (audio[i].length <= 0) {
continue;
}
// 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);
@@ -35,13 +40,14 @@ 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) {
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
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);
// suddenly becomes signed
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;
// convert
@@ -61,12 +67,13 @@ AudioData audio_load_wav(const AudioDevice* audio_device, const char* file_path)
SDL_LoadWAV(file_path, &wav_spec, &audio.buffer, &audio.length);
convert_audio(audio_device, wav_spec, &audio.buffer, &audio.length);
audio.mixed_amount = audio.length;
return audio;
}
// 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_t samples) {
// allocate memory for the audio device
AudioDevice* audio_device = malloc(sizeof(AudioDevice));
@@ -99,7 +106,7 @@ AudioDevice* audio_device_init(const int freq, const SDL_AudioFormat format, con
void audio_play(const AudioDevice* audio_device, const AudioData 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
if (playing_audio[i].length <= 0) {
// override the audio

View File

@@ -1,20 +1,23 @@
#pragma once
#include <SDL2/SDL.h>
#include <SDL_audio.h>
#include <stdint.h>
typedef struct {
Uint32 length;
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 int freq, const SDL_AudioFormat format, const uint8_t channels, const uint16_t samples);
void audio_play(const AudioDevice* audio_device, const AudioData audio);

View File

@@ -1,9 +1,12 @@
#pragma once
#include <stdint.h>
typedef union {
unsigned packed;
struct {
unsigned char a;
unsigned char b;
unsigned char g;
unsigned char r;
uint8_t a;
uint8_t b;
uint8_t g;
uint8_t r;
};
} Colour;

View File

@@ -1,12 +1,18 @@
#include "renderer.h"
#include <SDL2/SDL.h>
#include <SDL.h>
#include <SDL_error.h>
#include <SDL_rect.h>
#include <SDL_render.h>
#include <SDL_video.h>
#include <stdio.h>
#include "../constants.h"
#include "../errors.h"
#include "../game/level.h"
// initializes the window and renderer
int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
// init the window

View File

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