From 70413c38c8b565e9470eba1d4cc285c858cdb367 Mon Sep 17 00:00:00 2001 From: Quinn Date: Thu, 13 Feb 2025 13:07:20 +0100 Subject: [PATCH] setup base project (removing everything) --- assets/uwu | 1 - src/errors.c | 31 -------- src/errors.h | 28 ------- src/game/game.c | 17 ---- src/game/game.h | 11 --- src/main.c | 72 +---------------- src/main.h | 4 - src/window/audio.c | 148 ----------------------------------- src/window/audio.h | 32 -------- src/window/colour/colour32.h | 44 ----------- src/window/colour/colour8.h | 53 ------------- src/window/renderer.c | 47 ----------- src/window/renderer.h | 14 ---- 13 files changed, 2 insertions(+), 500 deletions(-) delete mode 100644 assets/uwu delete mode 100644 src/errors.c delete mode 100644 src/errors.h delete mode 100644 src/game/game.c delete mode 100644 src/game/game.h delete mode 100644 src/main.h delete mode 100644 src/window/audio.c delete mode 100644 src/window/audio.h delete mode 100644 src/window/colour/colour32.h delete mode 100644 src/window/colour/colour8.h delete mode 100644 src/window/renderer.c delete mode 100644 src/window/renderer.h diff --git a/assets/uwu b/assets/uwu deleted file mode 100644 index e108e50..0000000 --- a/assets/uwu +++ /dev/null @@ -1 +0,0 @@ -uwu diff --git a/src/errors.c b/src/errors.c deleted file mode 100644 index cccc373..0000000 --- a/src/errors.c +++ /dev/null @@ -1,31 +0,0 @@ -#include "errors.h" - -#include -#include -#include -#include - -void error(error_code const error_code, char const* const format, ...) { - char buffer[ERROR_MAX_STR_LEN] = {0}; // contains the buffer of the final string - - va_list args; - va_start(args, format); - vsnprintf(buffer, ERROR_MAX_STR_LEN, format, args); - va_end(args); - - printf("\033[91mE\033[0m: %s\n", buffer); - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", buffer, NULL); - - exit(error_code); -} - -void warn(char const* const format, ...) { - char buffer[ERROR_MAX_STR_LEN] = {0}; // contains the buffer of the final string - - va_list args; - va_start(args, format); - vsnprintf(buffer, ERROR_MAX_STR_LEN, format, args); - va_end(args); - - printf("\033[93mW\033[0m: %s\n", buffer); -} diff --git a/src/errors.h b/src/errors.h deleted file mode 100644 index c78b8e6..0000000 --- a/src/errors.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include - -#define ERROR_MAX_STR_LEN 128 - -typedef uint8_t error_code; -enum { - ERROR_MISC = -1, - - SUCCESS = 0, - ERROR_INIT = 1, - - // SDL errors - ERROR_SDL = 2, - ERROR_SDL_INIT = ERROR_SDL | ERROR_INIT, - - // renderer errors - ERROR_SDL_RENDERER = ERROR_SDL | 4, - ERROR_SDL_RENDERER_INIT = ERROR_SDL_RENDERER | ERROR_INIT, - - // audio errors - ERROR_SDL_AUDIO = ERROR_SDL | 8, - ERROR_SDL_AUDIO_INIT = ERROR_SDL_AUDIO | ERROR_INIT, -}; - -// call when a fatal error has occurred, the program will immediately terminate when called -void error(error_code const error_code, char const* format, ...); -void warn(char const* format, ...); diff --git a/src/game/game.c b/src/game/game.c deleted file mode 100644 index fd6eff7..0000000 --- a/src/game/game.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "game.h" - -#include -#include - -#include "../main.h" - - -void game_init(game_data* const dat) { - *dat = (game_data){0}; -} - -// called every time the game's state is updated -void game_update(game_data* const dat, uint8_t const* const keys) { - (void)dat; - if (keys[SDL_SCANCODE_ESCAPE]) stop(); -} diff --git a/src/game/game.h b/src/game/game.h deleted file mode 100644 index f5100b1..0000000 --- a/src/game/game.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include - -// stores the data used in the game -typedef struct { - uint8_t tmp; -} game_data; - -// updates the game's state -void game_init(game_data* game_dat); -void game_update(game_data* game_dat, const uint8_t* keys); diff --git a/src/main.c b/src/main.c index 2792ca5..3e4165a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,76 +1,8 @@ -#include "main.h" - -#include -#include -#include -#include -#include #include #include -#include -#include -#include "errors.h" -#include "game/game.h" -#include "window/renderer.h" - -bool playing = true; - -render_data render_dat = {0}; -game_data game_dat = {0}; - -// handles game application initialisation -static void init(void) { - // initialize SDL - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { - error(ERROR_SDL_INIT, "SDL could not initialize! SDL Error: %s", SDL_GetError()); - return; - } - - renderer_init(&render_dat, &game_dat); - - // initialize audio - // AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 255); - - // AudioData audio1 = audio_load_wav(audio_device, "FILE NAME"); - game_init(&game_dat); -} - -// handles game application updating -static void update(void) { - // update the input - { - SDL_Event e; - while (SDL_PollEvent(&e)) { - switch (e.type) { - case SDL_QUIT: - exit(SUCCESS); - break; - } - } - } - - // perform updates - game_update(&game_dat, SDL_GetKeyboardState(NULL)); - renderer_update(&render_dat); -} - -// handles game application quitting -void stop(void) { - playing = false; -} - -// entry point of the application int32_t main(int32_t argc, char** argv) { - (void)argc; - (void)argv; - init(); - - while (playing) - update(); - - // cleanup of resources - SDL_Quit(); - + (void)argc, (void)argv; + printf("%s\n", "Hello, World!"); return 0; } diff --git a/src/main.h b/src/main.h deleted file mode 100644 index 1369892..0000000 --- a/src/main.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -// stops execution of the game -void stop(void); diff --git a/src/window/audio.c b/src/window/audio.c deleted file mode 100644 index a014257..0000000 --- a/src/window/audio.c +++ /dev/null @@ -1,148 +0,0 @@ -#include "audio.h" - -#include -#include -#include -#include -#include -#include - -#include "../errors.h" - -// -// audio mixing -// -// audio callback from SDL_AudioSpec; called when the audio device needs more data -static void audio_mixer(void* const userdata, uint8_t* const stream, int32_t const len) { - memset(stream, 0, len); // clear the playing audio - audio_device* const dev = userdata; // get the callback data - - audio_player* prev = NULL; - audio_player* curr = dev->audio_dat; - while (curr != NULL) { - // if the current length, remove self from the list - if (curr->len == 0) { - audio_player* ncurr = curr->nxt; // store the next player as the new current player - - // set the current player to the new current player & free the current player - free(curr); - curr = ncurr; // ncurr can be NULL, this is fine - - // update the previous pointer accordingly - if (prev == NULL) - dev->audio_dat = ncurr; - else - prev->nxt = ncurr; - - // continue code execution - continue; - } - - // get the length of which we shall be mixing - uint32_t const mix_length = SDL_min(curr->len, (uint32_t)len); - - // mix the audio with the stream - SDL_MixAudioFormat(stream, curr->buf, dev->fmt, mix_length, SDL_MIX_MAXVOLUME); - curr->buf += mix_length; // move the pointer up a a bit - curr->len -= mix_length; // move up the mixed amount - - // increment across the list - prev = curr; - curr = curr->nxt; - } -} - -// converts the audio to the format of the audio device, reallocates wav_buf to a new size outputted to wav_len -static void convert_audio(audio_device const* const dev, SDL_AudioSpec const wav_spec, uint8_t** const wav_buf, uint32_t* const wav_len) { - // build the audio converter with the audio given - SDL_AudioCVT cvt = {0}; - SDL_BuildAudioCVT(&cvt, wav_spec.format, wav_spec.channels, wav_spec.freq, dev->fmt, dev->channels, dev->freq); - cvt.len = (*wav_len) * wav_spec.channels; // calculate the size of the source data in bytes by multiplying the length by the amount of channels (warn: uint32_t -> int32_t) - cvt.buf = realloc(*wav_buf, cvt.len * cvt.len_mult); // grow the inputted buffer for the conversion - - // performs the conversion - if (SDL_ConvertAudio(&cvt) != 0) - error(ERROR_SDL_AUDIO_INIT, "something went wrong when converting an audio buffer! SDL Error: %s", SDL_GetError()); - - // set the length to the new length - *wav_len = cvt.len_cvt; - - // reallocate the conversion buffer to match the new size - *wav_buf = realloc(cvt.buf, cvt.len_cvt); - if (*wav_buf == NULL) - error(ERROR_MISC, "null value when reallocating the audio buffer"); -} - - -// -// audio / audio device management -// -// loads a WAV file and returns the relevant information -audio_data audio_wav_load(audio_device const* const dev, char const* const fpath) { - SDL_AudioSpec wav_spec = {0}; - audio_data audio = {0}; - - SDL_LoadWAV(fpath, &wav_spec, &audio.buf, &audio.len); - convert_audio(dev, wav_spec, &audio.buf, &audio.len); - - // calculate the amount of seconds that the audio fragment has - audio.ms = 1000 * (((audio.len) / (SDL_AUDIO_BITSIZE(dev->fmt) / 8)) / wav_spec.channels / dev->freq); - - return audio; -} - -// initializes the audio device -audio_device* audio_device_init(int32_t const freq, SDL_AudioFormat const fmt, uint8_t const channels, uint16_t const samples) { - // allocate memory for the audio device - audio_device* const dev = malloc(sizeof(audio_device)); - - // define the audio specification - SDL_AudioSpec spec = {freq, fmt, channels, 0, samples, 0, 0, NULL, NULL}; - spec.callback = audio_mixer; - spec.userdata = dev; - - // create the audio device - *dev = (audio_device){ - NULL, // allocate memory on the heap for the playing audio array - SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0), - freq, - fmt, - channels, - }; - - // if the audio device isn't set, cause an error - if (dev->id < 1) { - error(ERROR_SDL_AUDIO_INIT, "AudioDivice failed to open! SDL Error: %s", SDL_GetError()); - return NULL; - } - - // default state of the device is paused, so we unpause it here - SDL_PauseAudioDevice(dev->id, 0); - return dev; -} - -// plays the audio -void audio_play(audio_device* const dev, audio_data const* audio) { - // create an audio player - audio_player* player = malloc(sizeof(audio_player)); - *player = (audio_player){ - dev->audio_dat, // set nxt to the first item in dev (can be NULL, this is fine) - audio->buf, - audio->len, - }; - - // assign ourselves to the first item - dev->audio_dat = player; -} - -// frees the audio device -void audio_device_free(audio_device* const dev) { - SDL_CloseAudioDevice(dev->id); - free(dev->audio_dat); - free(dev); -} - -// frees the buffer of the audio data -void audio_wav_unload(audio_data* dat) { - free(dat->buf); -} diff --git a/src/window/audio.h b/src/window/audio.h deleted file mode 100644 index 0d4b5e4..0000000 --- a/src/window/audio.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include -#include - -typedef struct { - uint8_t* buf; // the audio buffer - uint32_t len; // length in bytes of the audio buffer - uint32_t ms; // length in seconds of the audio buffer -} audio_data; - -typedef struct audio_player { - struct audio_player* nxt; // pointer to the next audio fragment (can be NULL) - uint8_t* buf; // pointer to the current - uint32_t len; // length remaining of the audio buffer -} audio_player; - -typedef struct { - audio_player* audio_dat; // linked list of audio players - SDL_AudioDeviceID id; // the audio device id - int32_t freq; - SDL_AudioFormat fmt; - uint8_t channels; -} audio_device; - - -audio_data audio_wav_load(audio_device const* audio_device, char const* file_path); -audio_device* audio_device_init(int freq, SDL_AudioFormat format, uint8_t channels, uint16_t samples); -void audio_play(audio_device* audio_device, audio_data const* audio); - -void audio_device_free(audio_device* dev); -void audio_wav_unload(audio_data* dat); diff --git a/src/window/colour/colour32.h b/src/window/colour/colour32.h deleted file mode 100644 index 8b46d31..0000000 --- a/src/window/colour/colour32.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include - -#include "SDL_render.h" - -// stores colour in a rgba format, each channel being a 8 bits wide. -typedef union { - uint32_t packed; - struct { - uint8_t a; - uint8_t b; - uint8_t g; - uint8_t r; - }; -} colour32; - -#define COLOUR32_BLACK ((colour32){0x000000FF}) -#define COLOUR32_RED ((colour32){0xFF0000FF}) -#define COLOUR32_YELLOW ((colour32){0xFFFF00FF}) -#define COLOUR32_ORANGE ((colour32){0xFF6D00FF}) -#define COLOUR32_GREEN ((colour32){0x00FF00FF}) -#define COLOUR32_CYAN ((colour32){0x00FFFFFF}) -#define COLOUR32_BLUE ((colour32){0x0000FFFF}) -#define COLOUR32_MAGENTA ((colour32){0xFF00FFFF}) -#define COLOUR32_WHITE ((colour32){0xFFFFFFFF}) - - -// sets the render colour to a colour32 value -static inline void set_colour32(SDL_Renderer* const renderer, colour32 const c) { - (void)SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, c.a); -} - - -// american macros: -#define color32 colour32 -#define COLOR32_BLACK COLOUR32_BLACK -#define COLOR32_RED COLOUR32_RED -#define COLOR32_YELLOW COLOUR32_YELLOW -#define COLOR32_ORANGE COLOUR32_ORANGE -#define COLOR32_GREEN COLOUR32_GREEN -#define COLOR32_CYAN COLOUR32_CYAN -#define COLOR32_BLUE COLOUR32_BLUE -#define COLOR32_MAGENTA COLOUR32_MAGENTA -#define COLOR32_WHITE COLOUR32_WHITE diff --git a/src/window/colour/colour8.h b/src/window/colour/colour8.h deleted file mode 100644 index b3a8678..0000000 --- a/src/window/colour/colour8.h +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once -#include - -#include "SDL_render.h" - -// stores colour in a rrrgggbb format, which maps exactly to 8 bits -typedef uint8_t colour8; - -/* rrrg ggbb */ -#define COLOUR8_BLACK ((colour8)0x00) // 0000 0000 -#define COLOUR8_RED ((colour8)0xE0) // 1110 0000 -#define COLOUR8_YELLOW ((colour8)0xFC) // 1111 1100 -#define COLOUR8_ORANGE ((colour8)0xEC) // 1111 1100 -#define COLOUR8_GREEN ((colour8)0x1C) // 0001 1100 -#define COLOUR8_CYAN ((colour8)0x1F) // 0001 1111 -#define COLOUR8_BLUE ((colour8)0x03) // 0000 0011 -#define COLOUR8_MAGENTA ((colour8)0xE3) // 1110 0011 -#define COLOUR8_WHITE ((colour8)0xFF) // 1111 1111 - -// gets the red channel in 32 bit colour space -static inline uint8_t colour8_red32(colour8 const colour) { - return (colour >> 5) * (255 / 7); -} - -// gets the green channel in 32 bit colour space -static inline uint8_t colour8_green32(colour8 const colour) { - return ((colour >> 2) & 7) * (255 / 7); -} - -// gets the blue channel in 32 bit colour space -static inline uint8_t colour8_blue32(colour8 const colour) { - return (colour & 3) * (255 / 3); -} - -// sets the render colour to a colour8 value -static inline void set_colour8(SDL_Renderer* const renderer, colour8 const c) { - (void)SDL_SetRenderDrawColor(renderer, colour8_red32(c), colour8_green32(c), colour8_blue32(c), 0xFF); -} - -// american macros: -#define color8 colour8 -#define color8_red32 colour8_red32 -#define color8_green32 colour8_green32 -#define color8_blue32 colour8_blue32 -#define COLOR8_BLACK COLOUR8_BLACK -#define COLOR8_RED COLOUR8_RED -#define COLOR8_YELLOW COLOUR8_YELLOW -#define COLOR8_ORANGE COLOUR8_ORANGE -#define COLOR8_GREEN COLOUR8_GREEN -#define COLOR8_CYAN COLOUR8_CYAN -#define COLOR8_BLUE COLOUR8_BLUE -#define COLOR8_MAGENTA COLOUR8_MAGENTA -#define COLOR8_WHITE COLOUR8_WHITE diff --git a/src/window/renderer.c b/src/window/renderer.c deleted file mode 100644 index 7cac6ba..0000000 --- a/src/window/renderer.c +++ /dev/null @@ -1,47 +0,0 @@ -#include "renderer.h" - -#include -#include -#include -#include - -#include "../errors.h" -#include "../game/game.h" -#include "colour/colour32.h" - - -// initializes the window and renderer -void renderer_init(render_data* const render_dat, game_data const* const game_dat) { - // create a new window - SDL_Window* const window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 100, 100, SDL_WINDOW_SHOWN); - if (window == NULL) - error(ERROR_SDL_RENDERER_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError()); - - // create a renderer - SDL_Renderer* const renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC); - if (renderer == NULL) - error(ERROR_SDL_RENDERER_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError()); - - *render_dat = (render_data){ - window, - renderer, - game_dat, - }; -} - -void renderer_update(const render_data* render_data) { - SDL_Renderer* renderer = render_data->renderer; - - int success = 0; // if an error occurs, this value is <0 - - // clear render - set_colour32(renderer, COLOUR32_BLACK); - success |= SDL_RenderClear(renderer); - - if (success < 0) { - warn("\033[93mW\033[0m: something went wrong whilst renderering! SDL Error: %s\n", SDL_GetError()); - return; - } - - SDL_RenderPresent(renderer); -} diff --git a/src/window/renderer.h b/src/window/renderer.h deleted file mode 100644 index 8ec760b..0000000 --- a/src/window/renderer.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include -#include - -#include "../game/game.h" - -typedef struct { - SDL_Window* window; - SDL_Renderer* renderer; - game_data const* game_dat; -} render_data; - -void renderer_init(render_data* render_dat, game_data const* game_dat); -void renderer_update(render_data const* render_data);