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,20 +1,20 @@
#pragma once #pragma once
#define SCREEN_WIDTH 960 #define SCREEN_WIDTH 960
#define SCREEN_HEIGHT 640 #define SCREEN_HEIGHT 640
#define BOUNCER_HEIGHT 5 #define BOUNCER_HEIGHT 5
#define BOUNCER_WIDTH_DEFAULT 75 #define BOUNCER_WIDTH_DEFAULT 75
#define BOUNCER_SPEED 10.0F #define BOUNCER_SPEED 10.0F
#define BALL_SIZE_DEFAULT 10 #define BALL_SIZE_DEFAULT 10
#define BALL_SPEED 5.0F #define BALL_SPEED 5.0F
#define BRICK_WIDTH 30 #define BRICK_WIDTH 30
#define BRICK_HEIGHT 15 #define BRICK_HEIGHT 15
#define BRICK_PADDING 2 #define BRICK_PADDING 2
#define BRICK_PADDING_TOP 30 #define BRICK_PADDING_TOP 30
#define BRICK_ROWS 5 #define BRICK_ROWS 5
#define BRICK_COLUMNS (int)(SCREEN_WIDTH / (BRICK_WIDTH + BRICK_PADDING)) #define BRICK_COLUMNS (int)(SCREEN_WIDTH / (BRICK_WIDTH + BRICK_PADDING))
#define PI 3.14159265358979323846F // pi was being fucky! :D #define PI 3.14159265358979323846F // pi was being fucky! :D

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

View File

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

View File

@@ -1,22 +1,22 @@
#pragma once #pragma once
#include <SDL2/SDL.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include "../constants.h" #include "../constants.h"
#include "../window/colour.h"
#include "../window/audio.h" #include "../window/audio.h"
#include "../window/colour.h"
#include "vector2.h" #include "vector2.h"
typedef struct { typedef struct {
Vector2 pos; Vector2 pos;
Vector2 direction; Vector2 direction;
unsigned size; uint32_t size;
bool moving; bool moving;
} Ball; } Ball;
typedef struct { typedef struct {
Vector2 pos; Vector2 pos;
unsigned width; uint32_t width;
} Bouncer; } Bouncer;
typedef struct { typedef struct {
@@ -35,4 +35,4 @@ typedef struct {
void level_init(Level* level); 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 "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 <stdbool.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"
@@ -74,6 +82,8 @@ 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();
#if __EMSCRIPTEN__ #if __EMSCRIPTEN__

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,14 +40,15 @@ 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);
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);
@@ -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); 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);
audio.mixed_amount = audio.length;
return audio; return audio;
} }
// 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_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));
@@ -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) { 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,20 +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;
Uint8* buffer; uint32_t mixed_amount;
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

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

View File

@@ -1,12 +1,18 @@
#include "renderer.h" #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 <stdio.h>
#include "../constants.h" #include "../constants.h"
#include "../errors.h" #include "../errors.h"
#include "../game/level.h" #include "../game/level.h"
// initializes the window and renderer // initializes the window and renderer
int renderer_init(SDL_Window** window, SDL_Renderer** renderer) { int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
// init the window // init the window

View File

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