replace spaces with tabs

This commit is contained in:
2025-03-21 17:35:54 +01:00
parent 19a223dd29
commit afdc7de496
17 changed files with 426 additions and 426 deletions

View File

@@ -11,61 +11,61 @@
// writes the arguments to the specified buffer
// using a macro instead of an inline function because fmt otherwise gets horribly messed up
#define write_args(buf, fmt) \
va_list args; \
va_start(args, fmt); \
(void)vsnprintf(buf, PRINT_BUFFER_SIZE, fmt, args); \
va_end(args);
va_list args; \
va_start(args, fmt); \
(void)vsnprintf(buf, PRINT_BUFFER_SIZE, fmt, args); \
va_end(args);
static gamestatus status = STATUS_RUNNING;
void set_gamestatus(gamestatus nstatus) {
status = nstatus;
status = nstatus;
}
gamestatus get_gamestatus(void) {
return status;
return status;
}
void debug(char const* fmt, ...) {
char const* env = getenv("DEBUG");
if (env == NULL || *env != '1')
return;
char const* env = getenv("DEBUG");
if (env == NULL || *env != '1')
return;
char buf[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt);
char buf[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt);
(void)fprintf(stdout, "\033[95m%s\033[0m\n", buf);
(void)fprintf(stdout, "\033[95m%s\033[0m\n", buf);
}
void info(char const* fmt, ...) {
char buf[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt);
(void)fprintf(stdout, "\033[0m%s\033[0m\n", buf); // write colour here for consistency
char buf[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt);
(void)fprintf(stdout, "\033[0m%s\033[0m\n", buf); // write colour here for consistency
}
void warn(char const* fmt, ...) {
char buf[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt);
(void)fprintf(stderr, "\033[93mW: %s\033[0m\n", buf);
char buf[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt);
(void)fprintf(stderr, "\033[93mW: %s\033[0m\n", buf);
}
void error(char const* fmt, ...) {
char buf[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt);
(void)fprintf(stderr, "\033[mW: %s\033[0m", buf);
char buf[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt);
(void)fprintf(stderr, "\033[mW: %s\033[0m", buf);
}
noreturn void fatal(gamestatus error_code, char const* fname, uint32_t ln, char const* fmt, ...) {
char buf1[PRINT_BUFFER_SIZE] = {0};
write_args(buf1, fmt);
char buf1[PRINT_BUFFER_SIZE] = {0};
write_args(buf1, fmt);
char buf2[PRINT_BUFFER_SIZE * 2] = {0};
sprintf(buf2, "%s\n at %s:%u (exitcode: %u)", buf1, fname, ln, error_code);
char buf2[PRINT_BUFFER_SIZE * 2] = {0};
sprintf(buf2, "%s\n at %s:%u (exitcode: %u)", buf1, fname, ln, error_code);
(void)fprintf(stderr, "\033[91mE: %s\033[0m\n", buf2);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", buf2, NULL);
(void)fprintf(stderr, "\033[91mE: %s\033[0m\n", buf2);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", buf2, NULL);
// set status, but exit immediately, as code is not allowed to execute beyond this point
set_gamestatus(error_code);
exit(status);
// set status, but exit immediately, as code is not allowed to execute beyond this point
set_gamestatus(error_code);
exit(status);
}

View File

@@ -6,7 +6,7 @@
/* defines statuses in the 0..127, any higher/negative values are POSIX-reserved.
* The max value (or -1) shall mean the application is running, anything else shall mean an exit code of some kind */
enum {
// clang-format off
// clang-format off
STATUS_SUCCESS = 0, // 0; successful exit
STATUS_ERROR = 1, // miscellaneous error
ERROR_INIT = STATUS_ERROR | 2, // initialisation error
@@ -24,7 +24,7 @@ enum {
ERROR_SDL_AUDIO_INIT = ERROR_SDL_INIT | 8, // audio initialization error
STATUS_RUNNING = -1,
// clang-format on
// clang-format on
};
typedef int8_t gamestatus;

View File

@@ -10,19 +10,19 @@
#include "gametime.h"
void game_init(gamedata* dat) {
*dat = (gamedata){
gametime_new(),
};
*dat = (gamedata){
gametime_new(),
};
}
void game_update(gamedata* dat) {
gametime_update(&dat->time);
uint8_t const* keys = SDL_GetKeyboardState(NULL);
gametime_update(&dat->time);
uint8_t const* keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_ESCAPE])
set_gamestatus(STATUS_SUCCESS);
if (keys[SDL_SCANCODE_ESCAPE])
set_gamestatus(STATUS_SUCCESS);
}
void game_free(gamedata* dat) {
*dat = (gamedata){0};
*dat = (gamedata){0};
}

View File

@@ -11,7 +11,7 @@
#define TAUf (M_PIf * 2.0F) // τ constant as a 32-bit floating point
typedef struct {
gametime time;
gametime time;
} gamedata;
void game_init(gamedata*); // initializes everything needed to start the game; outputs to game_data

View File

@@ -3,35 +3,35 @@
#include <time.h>
typedef struct {
struct timespec ts; // stores the time at the current update
double sec; // stores the current time in seconds
float scale; // multiplier for the time calculation, default value is 1.0
float delta; // the time that it took between updates
struct timespec ts; // stores the time at the current update
double sec; // stores the current time in seconds
float scale; // multiplier for the time calculation, default value is 1.0
float delta; // the time that it took between updates
} gametime;
// initializes the gametime struct
static inline gametime gametime_new(void) {
struct timespec ts;
timespec_get(&ts, TIME_UTC);
struct timespec ts;
timespec_get(&ts, TIME_UTC);
return (gametime){
ts,
0.0,
1.0F,
0.0F,
};
return (gametime){
ts,
0.0,
1.0F,
0.0F,
};
}
// updates the internal variables
static inline void gametime_update(gametime* gt) {
struct timespec ts;
timespec_get(&ts, TIME_UTC);
gt->sec = (double)ts.tv_nsec * 1e-9; // calculate the current time in seconds
gt->delta = ((double)(ts.tv_nsec - gt->ts.tv_nsec) * 1e-9) * gt->scale; // calculate how much time has passed between this and last frame
gt->ts = ts; // update the game's timespec
struct timespec ts;
timespec_get(&ts, TIME_UTC);
gt->sec = (double)ts.tv_nsec * 1e-9; // calculate the current time in seconds
gt->delta = ((double)(ts.tv_nsec - gt->ts.tv_nsec) * 1e-9) * gt->scale; // calculate how much time has passed between this and last frame
gt->ts = ts; // update the game's timespec
}
// gets how many times the game updates per second
static inline float gametime_get_ups(gametime* gt) {
return 1.0F / gt->delta;
return 1.0F / gt->delta;
}

View File

@@ -15,52 +15,52 @@ static renderdata rdat;
// initialize the game
static void init(void) {
// initialize SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
fatal(ERROR_SDL_INIT, __FILE_NAME__, __LINE__, "SDL could not initialize! SDL Error: %s", SDL_GetError());
// initialize SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
fatal(ERROR_SDL_INIT, __FILE_NAME__, __LINE__, "SDL could not initialize! SDL Error: %s", SDL_GetError());
// initialize other game components
gt = gametime_new();
game_init(&gdat);
render_init(&rdat, &gdat);
// initialize other game components
gt = gametime_new();
game_init(&gdat);
render_init(&rdat, &gdat);
}
// perform the updates to the game
static void update(void) {
// update the input
{
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
set_gamestatus(STATUS_SUCCESS);
break;
}
}
}
// update the input
{
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
set_gamestatus(STATUS_SUCCESS);
break;
}
}
}
// perform updates
gametime_update(&gt);
game_update(&gdat);
render_update(&rdat);
// perform updates
gametime_update(&gt);
game_update(&gdat);
render_update(&rdat);
}
// entry-point of the application
int32_t main(int32_t argc, char** argv) {
(void)argc, (void)argv;
(void)argc, (void)argv;
init();
debug("successfully initialized!");
init();
debug("successfully initialized!");
while (get_gamestatus() == STATUS_RUNNING)
update();
while (get_gamestatus() == STATUS_RUNNING)
update();
debug("done! starting to free resources...");
game_free(&gdat);
render_free(&rdat);
SDL_Quit();
debug("done! starting to free resources...");
game_free(&gdat);
render_free(&rdat);
SDL_Quit();
gamestatus exit_code = get_gamestatus();
debug("quitting with an exit code of %u", exit_code);
return exit_code;
gamestatus exit_code = get_gamestatus();
debug("quitting with an exit code of %u", exit_code);
return exit_code;
}

View File

@@ -26,143 +26,143 @@
// define the dynamic array structure
typedef struct {
DYNARR_TYPE* dat; // contains the data of the dynamic array
size_t count; // contains the amount of elements of the dynamic array
size_t cap; // contains the capacity of the dynamic array
DYNARR_TYPE* dat; // contains the data of the dynamic array
size_t count; // contains the amount of elements of the dynamic array
size_t cap; // contains the capacity of the dynamic array
} DYNARR_NAME;
// cleans up the resources associated with the array, do not use after this step. This is undefined behaviour
DYNARR_LINKAGE void DYNARR_FUNC(free)(DYNARR_NAME* arr) {
free(arr->dat); // free(NULL) is allowed
*arr = (DYNARR_NAME){0}; // zero out all fields to re-initialize
free(arr->dat); // free(NULL) is allowed
*arr = (DYNARR_NAME){0}; // zero out all fields to re-initialize
}
// sets the capacity exactly, does not respect capacity scaling use `resize` if capacity scaling must be respected
// returns 0 upon success, 1 upon failure
DYNARR_LINKAGE uint8_t DYNARR_FUNC(resize_exact)(DYNARR_NAME* arr, size_t ncap) {
if (ncap < arr->count) return 1; // the new capacity is smaller than the count, this is very likely unintentional
if (ncap == arr->cap) return 0; // the capacity is already the new capacity; no work needs to be done
if (ncap == 0) {
DYNARR_FUNC(free)(arr);
return 0;
}
if (ncap < arr->count) return 1; // the new capacity is smaller than the count, this is very likely unintentional
if (ncap == arr->cap) return 0; // the capacity is already the new capacity; no work needs to be done
if (ncap == 0) {
DYNARR_FUNC(free)(arr);
return 0;
}
// (re)allocate the memory for the array
DYNARR_TYPE* nptr = realloc(arr->dat, ncap * sizeof(DYNARR_TYPE)); // if dat is NULL, behaviour is equivalent to "malloc"
// (re)allocate the memory for the array
DYNARR_TYPE* nptr = realloc(arr->dat, ncap * sizeof(DYNARR_TYPE)); // if dat is NULL, behaviour is equivalent to "malloc"
// if memory (re)allocation failed; return
if (nptr == NULL)
return 1;
// if memory (re)allocation failed; return
if (nptr == NULL)
return 1;
arr->dat = nptr;
arr->cap = ncap;
return 0;
arr->dat = nptr;
arr->cap = ncap;
return 0;
}
DYNARR_LINKAGE DYNARR_NAME DYNARR_FUNC(init)(void) {
return (DYNARR_NAME){0};
return (DYNARR_NAME){0};
}
// resizes the capacity, respects capacity scaling, use `resize_exact` if this behaviour isn't desirable (often it is)
// returns 0 upon success, 1 upon failure
DYNARR_LINKAGE uint8_t DYNARR_FUNC(resize)(DYNARR_NAME* arr, size_t ncap) {
if (ncap < arr->count) return 1; // the new count is less than the current count, this is very likely unintentional
if (ncap == arr->cap) return 0; // the current capacity has already been set to this
if (ncap == 0) {
DYNARR_FUNC(free)(arr);
return 0;
}
if (ncap < arr->count) return 1; // the new count is less than the current count, this is very likely unintentional
if (ncap == arr->cap) return 0; // the current capacity has already been set to this
if (ncap == 0) {
DYNARR_FUNC(free)(arr);
return 0;
}
// convert the capacity into a power of 2 by selecting the most significan bit
ncap--; // first remove 1, to decrease the most significant bit
for (uint16_t i = 1; i < SIZE_WIDTH; i <<= 1) // loop through each bit in size_t
ncap |= ncap >> i; // OR together the shifted result
ncap++; // finally, add one so 0111 -> 1000
// convert the capacity into a power of 2 by selecting the most significan bit
ncap--; // first remove 1, to decrease the most significant bit
for (uint16_t i = 1; i < SIZE_WIDTH; i <<= 1) // loop through each bit in size_t
ncap |= ncap >> i; // OR together the shifted result
ncap++; // finally, add one so 0111 -> 1000
// calculates what the new size should be by adding the amount of items to the count
// assumes scaling factor is 2
return DYNARR_FUNC(resize_exact)(arr, ncap);
// calculates what the new size should be by adding the amount of items to the count
// assumes scaling factor is 2
return DYNARR_FUNC(resize_exact)(arr, ncap);
}
// adds an item to the dynamic array, doubles the capacity if the new count exceeds the maximum
// `dat` is not allowed to overlap with the selected range in the array
// returns non-zero upon failure
DYNARR_LINKAGE uint8_t DYNARR_FUNC(add_bulk)(DYNARR_NAME* arr, DYNARR_TYPE* dat, size_t datcount, size_t idx) {
if (idx > arr->count) return 1; // the index is greater than the count
if (DYNARR_COUNT_MAX - datcount < arr->count) return 1; // the count will overflow
if (datcount == 0) return 0; // the count is zero, nothing needs to be done
if (idx > arr->count) return 1; // the index is greater than the count
if (DYNARR_COUNT_MAX - datcount < arr->count) return 1; // the count will overflow
if (datcount == 0) return 0; // the count is zero, nothing needs to be done
size_t orgcount = arr->count;
arr->count += datcount;
size_t orgcount = arr->count;
arr->count += datcount;
// resize the array if the new count has hit the capacity
if (arr->cap <= arr->count) {
// resize the array to the new count using resize (count cannot be a zero-value at this point)
if (DYNARR_FUNC(resize)(arr, arr->count))
return 1;
}
// resize the array if the new count has hit the capacity
if (arr->cap <= arr->count) {
// resize the array to the new count using resize (count cannot be a zero-value at this point)
if (DYNARR_FUNC(resize)(arr, arr->count))
return 1;
}
// move the data stored at the current position if we must insert
if (idx < orgcount)
memmove(&arr->dat[datcount + idx], &arr->dat[idx], (orgcount - idx) * sizeof(DYNARR_TYPE));
memcpy(&arr->dat[idx], dat, datcount * sizeof(DYNARR_TYPE)); // copy the original data to the index (do not overlap)
return 0;
// move the data stored at the current position if we must insert
if (idx < orgcount)
memmove(&arr->dat[datcount + idx], &arr->dat[idx], (orgcount - idx) * sizeof(DYNARR_TYPE));
memcpy(&arr->dat[idx], dat, datcount * sizeof(DYNARR_TYPE)); // copy the original data to the index (do not overlap)
return 0;
}
// adds an item to the dynamic array, doubles the capacity if the new count exceeds the maximum
// returns 0 upon success, 1 upon failure
DYNARR_LINKAGE uint8_t DYNARR_FUNC(add)(DYNARR_NAME* arr, DYNARR_TYPE item) {
return DYNARR_FUNC(add_bulk)(arr, &item, 1, arr->count);
return DYNARR_FUNC(add_bulk)(arr, &item, 1, arr->count);
}
// trims the parts of the dynamic array that isn't in use (does not respect scaling)
DYNARR_LINKAGE uint8_t DYNARR_FUNC(shrink)(DYNARR_NAME* arr) {
if (arr->cap == arr->count) return 0; // return success if no work needs to be done
return DYNARR_FUNC(resize_exact)(arr, arr->count);
if (arr->cap == arr->count) return 0; // return success if no work needs to be done
return DYNARR_FUNC(resize_exact)(arr, arr->count);
}
// removes a block of indices from sidx..eidx (inclusive) does not shrink the array afterwards, use `remove_bulk` instead if this is undesirable behaviour
// returns non-zero value upon failure
DYNARR_LINKAGE uint8_t DYNARR_FUNC(remove_bulk_noshrink)(DYNARR_NAME* arr, size_t sidx, size_t eidx) {
if (arr->count == 0) return 0; // no work needs to be done
if (arr->count <= sidx) return 1; // start index is out of bounds
if (arr->count <= eidx) return 1; // end index is out of bounds
if (eidx < sidx) return 1; // end index must be greater than or equal to start index
if (arr->count == 0) return 0; // no work needs to be done
if (arr->count <= sidx) return 1; // start index is out of bounds
if (arr->count <= eidx) return 1; // end index is out of bounds
if (eidx < sidx) return 1; // end index must be greater than or equal to start index
_Bool move = eidx < arr->count - 1; // calculate if we should move the memory after what has been removed
arr->count -= eidx - sidx + 1; // should always be less than or equal to count
_Bool move = eidx < arr->count - 1; // calculate if we should move the memory after what has been removed
arr->count -= eidx - sidx + 1; // should always be less than or equal to count
if (move)
memmove(&arr->dat[sidx], &arr->dat[eidx + 1], arr->count - sidx);
if (move)
memmove(&arr->dat[sidx], &arr->dat[eidx + 1], arr->count - sidx);
return 0;
return 0;
}
// removes a block of indices from sidx..eidx (inclusive)
// resizes the array if the new size is a quarter of the original size if this is undesirable, use `remove_bulk_noshrink` instead
// returns non-zero value upon failure
DYNARR_LINKAGE uint8_t DYNARR_FUNC(remove_bulk)(DYNARR_NAME* arr, size_t sidx, size_t eidx) {
if (DYNARR_FUNC(remove_bulk_noshrink)(arr, sidx, eidx))
return 1;
if (DYNARR_FUNC(remove_bulk_noshrink)(arr, sidx, eidx))
return 1;
// shrink the array when the new size is a quarter of the original size
if (arr->count < arr->cap / 4)
return DYNARR_FUNC(shrink)(arr);
// shrink the array when the new size is a quarter of the original size
if (arr->count < arr->cap / 4)
return DYNARR_FUNC(shrink)(arr);
return 0;
return 0;
}
// removes an item from the dynamic array from a certain index, does not shrink the array afterwards, if this is undesirable, use `remove` instead
// returns non-zero value upon failure
DYNARR_LINKAGE uint8_t DYNARR_FUNC(remove_noshrink)(DYNARR_NAME* arr, size_t idx) {
return DYNARR_FUNC(remove_bulk_noshrink)(arr, idx, idx);
return DYNARR_FUNC(remove_bulk_noshrink)(arr, idx, idx);
}
// removes an item from the dynamic array from a certain index
// resizes the array if the new size is a quarter of the original size if this is undesirable, use `remove_noshrink` instead
// returns non-zero value upon failure
DYNARR_LINKAGE uint8_t DYNARR_FUNC(remove)(DYNARR_NAME* arr, size_t idx) {
return DYNARR_FUNC(remove_bulk)(arr, idx, idx);
return DYNARR_FUNC(remove_bulk)(arr, idx, idx);
}
// clean up all defined definitions so they can be used again later

View File

@@ -4,62 +4,62 @@
// stores a 2D point using a floating-point number
typedef struct {
float x;
float y;
float x;
float y;
} float2;
// adds one float2 to another
static inline float2 float2_add(float2 v1, float2 v2) {
return (float2){v1.x + v2.x, v1.y + v2.y};
return (float2){v1.x + v2.x, v1.y + v2.y};
}
// subtracts one float2 from another
static inline float2 float2_sub(float2 v1, float2 v2) {
return (float2){v1.x - v2.x, v1.y - v2.y};
return (float2){v1.x - v2.x, v1.y - v2.y};
}
// multiplies one float2 by another
static inline float2 float2_mul(float2 v1, float2 v2) {
return (float2){v1.x * v2.x, v1.y * v2.y};
return (float2){v1.x * v2.x, v1.y * v2.y};
}
// divides one float2 by another
static inline float2 float2_div(float2 v1, float2 v2) {
return (float2){v1.x / v2.x, v1.y / v2.y};
return (float2){v1.x / v2.x, v1.y / v2.y};
}
// preforms a scalar multiplication upon the float2 (multiplies the float2 by some value)
static inline float2 float2_mul_s(float2 v, float n) {
return (float2){v.x * n, v.y * n};
return (float2){v.x * n, v.y * n};
}
// preforms a scalar division upon the float2 (divides the float2 by some value)
static inline float2 float2_div_s(float2 v, float n) {
return (float2){v.x / n, v.y / n};
return (float2){v.x / n, v.y / n};
}
// negates the float2 (-v)
static inline float2 float2_neg(float2 v) {
return (float2){-v.x, -v.y};
return (float2){-v.x, -v.y};
}
// gets the squared magnitude/length of float2
static inline float float2_mag2(float2 v) {
return (v.x * v.x) + (v.y * v.y);
return (v.x * v.x) + (v.y * v.y);
}
// gets the length of float2 (length)
static inline float float2_mag(float2 v) {
return sqrtf(float2_mag2(v));
return sqrtf(float2_mag2(v));
}
// normalizes the float2
static inline float2 float2_norm(float2 v) {
float s = 1.0F / float2_mag(v); // get the scaling factor
return float2_mul_s(v, s); // scale the vector by the scaling factor (slightly more efficient than dividing)
float s = 1.0F / float2_mag(v); // get the scaling factor
return float2_mul_s(v, s); // scale the vector by the scaling factor (slightly more efficient than dividing)
}
// gets the dot product of two float2s
static inline float float2_dot(float2 v1, float2 v2) {
return v1.x * v2.x + v1.y * v2.y;
return v1.x * v2.x + v1.y * v2.y;
}

View File

@@ -4,62 +4,62 @@
// stores a 3D point using a floating-point number
typedef struct {
float x;
float y;
float z;
float x;
float y;
float z;
} float3;
// adds one float3 to another
static inline float3 float3_add(float3 v1, float3 v2) {
return (float3){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z};
return (float3){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z};
}
// subtracts one float3 from another
static inline float3 float3_sub(float3 v1, float3 v2) {
return (float3){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z};
return (float3){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z};
}
// multiplies one float3 by another
static inline float3 float3_mul(float3 v1, float3 v2) {
return (float3){v1.x * v2.x, v1.y * v2.y, v1.z * v2.z};
return (float3){v1.x * v2.x, v1.y * v2.y, v1.z * v2.z};
}
// divides one float3 by another
static inline float3 float3_div(float3 v1, float3 v2) {
return (float3){v1.x / v2.x, v1.y / v2.y, v1.z / v2.z};
return (float3){v1.x / v2.x, v1.y / v2.y, v1.z / v2.z};
}
// preforms a scalar multiplication upon the float3 (multiplies the float3 by some value)
static inline float3 float3_mul_s(float3 v, float n) {
return (float3){v.x * n, v.y * n, v.z * n};
return (float3){v.x * n, v.y * n, v.z * n};
}
// preforms a scalar division upon the float3 (divides the float3 by some value)
static inline float3 float3_div_s(float3 v, float n) {
return (float3){v.x / n, v.y / n, v.z / n};
return (float3){v.x / n, v.y / n, v.z / n};
}
static inline float3 float3_neg(float3 v) {
return (float3){-v.x, -v.y, -v.z};
return (float3){-v.x, -v.y, -v.z};
}
// gets the squared magnitude/length of float3
static inline float float3_mag2(float3 v) {
return (v.x * v.x) + (v.y * v.y) + (v.z * v.z);
return (v.x * v.x) + (v.y * v.y) + (v.z * v.z);
}
// gets the length of float3 (length)
static inline float float3_mag(float3 v) {
return sqrtf(float3_mag2(v));
return sqrtf(float3_mag2(v));
}
// normalizes the float3
static inline float3 float3_norm(float3 v) {
float s = 1.0F / float3_mag(v); // get the scaling factor
return float3_mul_s(v, s); // scale the vector by the scaling factor (slightly more efficient than dividing)
float s = 1.0F / float3_mag(v); // get the scaling factor
return float3_mul_s(v, s); // scale the vector by the scaling factor (slightly more efficient than dividing)
}
// gets the dot product of two float3s
static inline float float3_dot(float3 v1, float3 v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}

View File

@@ -4,63 +4,63 @@
// stores a 3D point using a floating-point number
typedef struct {
float x;
float y;
float z;
float w;
float x;
float y;
float z;
float w;
} float4;
// adds one float4 to another
static inline float4 float4_add(float4 v1, float4 v2) {
return (float4){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w};
return (float4){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w};
}
// subtracts one float4 from another
static inline float4 float4_sub(float4 v1, float4 v2) {
return (float4){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w};
return (float4){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w};
}
// multiplies one float4 by another
static inline float4 float4_mul(float4 v1, float4 v2) {
return (float4){v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w};
return (float4){v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w};
}
// divides one float4 by another
static inline float4 float4_div(float4 v1, float4 v2) {
return (float4){v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w};
return (float4){v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w};
}
// preforms a scalar multiplication upon the float4 (multiplies the float4 by some value)
static inline float4 float4_mul_s(float4 v, float n) {
return (float4){v.x * n, v.y * n, v.z * n, v.w * n};
return (float4){v.x * n, v.y * n, v.z * n, v.w * n};
}
// preforms a scalar division upon the float4 (divides the float4 by some value)
static inline float4 float4_div_s(float4 v, float n) {
return (float4){v.x / n, v.y / n, v.z / n, v.w / n};
return (float4){v.x / n, v.y / n, v.z / n, v.w / n};
}
static inline float4 float4_neg(float4 v) {
return (float4){-v.x, -v.y, -v.z, -v.w};
return (float4){-v.x, -v.y, -v.z, -v.w};
}
// gets the squared magnitude/length of float4
static inline float float4_mag2(float4 v) {
return (v.x * v.x) + (v.y * v.y) + (v.z * v.z) + (v.w * v.w);
return (v.x * v.x) + (v.y * v.y) + (v.z * v.z) + (v.w * v.w);
}
// gets the length of float4 (length)
static inline float float4_mag(float4 v) {
return sqrtf(float4_mag2(v));
return sqrtf(float4_mag2(v));
}
// normalizes the float4
static inline float4 float4_norm(float4 v) {
float s = 1.0F / float4_mag(v); // get the scaling factor
return float4_mul_s(v, s); // scale the vector by the scaling factor (slightly more efficient than dividing)
float s = 1.0F / float4_mag(v); // get the scaling factor
return float4_mul_s(v, s); // scale the vector by the scaling factor (slightly more efficient than dividing)
}
// gets the dot product of two float4s
static inline float float4_dot(float4 v1, float4 v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
}

View File

@@ -5,89 +5,89 @@
// converts a float3 to a quaternion
static inline float4 quat_from_float3(float3 v) {
return (float4){
.w = 0.0F,
.x = v.x,
.y = v.y,
.z = v.z,
};
return (float4){
.w = 0.0F,
.x = v.x,
.y = v.y,
.z = v.z,
};
}
// converts euler angles into quaternion (ordered roll, pitch, yaw) (in radians)
static inline float4 quat_from_euler(float3 euler) {
euler = float3_mul_s(euler, 0.5F); // half the angles due to quaternions using θ/2 in the formula
float cx = cosf(euler.x), sx = sinf(euler.x);
float cy = cosf(euler.y), sy = sinf(euler.y);
float cz = cosf(euler.z), sz = sinf(euler.z);
euler = float3_mul_s(euler, 0.5F); // half the angles due to quaternions using θ/2 in the formula
float cx = cosf(euler.x), sx = sinf(euler.x);
float cy = cosf(euler.y), sy = sinf(euler.y);
float cz = cosf(euler.z), sz = sinf(euler.z);
return (float4){
.w = cx * cy * cz - sx * sy * sz,
.x = sx * cy * cz + cx * sy * sz,
.y = cx * sy * cz - sx * cy * sz,
.z = cx * cy * sz + sx * sy * cz,
};
return (float4){
.w = cx * cy * cz - sx * sy * sz,
.x = sx * cy * cz + cx * sy * sz,
.y = cx * sy * cz - sx * cy * sz,
.z = cx * cy * sz + sx * sy * cz,
};
}
// converts quaternion into euler angles (ordered as roll, pitch, yaw)
static inline float3 quat_to_euler(float4 q) {
// warn: do not read from these variables until set
float3 euler;
float a, b;
// warn: do not read from these variables until set
float3 euler;
float a, b;
// compute the roll (Φ)
a = 2 * (q.w * q.x + q.y * q.z); // sin(r)•cos(p)
b = 1 - 2 * (q.x * q.x + q.y * q.y); // cos(r)•cos(p)
euler.x = atan2f(a, b);
// compute the roll (Φ)
a = 2 * (q.w * q.x + q.y * q.z); // sin(r)•cos(p)
b = 1 - 2 * (q.x * q.x + q.y * q.y); // cos(r)•cos(p)
euler.x = atan2f(a, b);
// compute the pitch (θ)
a = 2 * (q.w * q.y - q.z * q.x);
euler.y = fabsf(a) >= 1 ? copysignf(M_PI_2, a) : asinf(a); // if |a| >=1, sgn(a)•(π/2), else asin(a)
// compute the pitch (θ)
a = 2 * (q.w * q.y - q.z * q.x);
euler.y = fabsf(a) >= 1 ? copysignf(M_PI_2, a) : asinf(a); // if |a| >=1, sgn(a)•(π/2), else asin(a)
// compute the yaw (ψ)
a = 2 * (q.w * q.z + q.x * q.y); // sin(y)•cos(y)
b = 1 - 2 * (q.y * q.y + q.z * q.z); // cos(y)•cos(y)
euler.z = atan2f(a, b);
// compute the yaw (ψ)
a = 2 * (q.w * q.z + q.x * q.y); // sin(y)•cos(y)
b = 1 - 2 * (q.y * q.y + q.z * q.z); // cos(y)•cos(y)
euler.z = atan2f(a, b);
// return the final angles
return euler;
// return the final angles
return euler;
}
// multiplies two quaternions
static inline float4 quat_mul(float4 q1, float4 q2) {
return (float4){
.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z,
.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y,
.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z,
.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x,
};
return (float4){
.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z,
.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y,
.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z,
.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x,
};
}
// get the conjugate of the quaternion (negates the vector portion)
static inline float4 quat_conj(float4 q) {
return (float4){
.w = q.w,
.x = -q.x,
.y = -q.y,
.z = -q.z,
};
return (float4){
.w = q.w,
.x = -q.x,
.y = -q.y,
.z = -q.z,
};
}
// get the multiplicative inverse of the quaternion (conj / mag²)
static inline float4 quat_inv(float4 q) {
float mag2 = float4_mag2(q);
if (mag2 == 0.0F) return (float4){0};
mag2 = 1.0F / mag2;
return float4_mul_s(quat_conj(q), mag2);
float mag2 = float4_mag2(q);
if (mag2 == 0.0F) return (float4){0};
mag2 = 1.0F / mag2;
return float4_mul_s(quat_conj(q), mag2);
}
// rotates a vector by the quaternion (q must be a unit quaternion (normalized))
static inline float3 quat_rot(float4 q, float3 v) {
q = quat_mul(quat_mul(q, quat_from_float3(v)), quat_conj(q)); // q•v•q¯¹ (using conjugate for q⁻¹, as for unit quaternions this is the same as the multiplicative inverse)
return (float3){q.x, q.y, q.z};
q = quat_mul(quat_mul(q, quat_from_float3(v)), quat_conj(q)); // q•v•q¯¹ (using conjugate for q⁻¹, as for unit quaternions this is the same as the multiplicative inverse)
return (float3){q.x, q.y, q.z};
}
// rotates a vector by the quaternion, q may be non-normalized
static inline float3 quat_rot_s(float4 q, float3 v) {
q = quat_mul(quat_mul(q, quat_from_float3(v)), quat_inv(q)); // q•v•q¯¹
return (float3){q.x, q.y, q.z};
q = quat_mul(quat_mul(q, quat_from_float3(v)), quat_inv(q)); // q•v•q¯¹
return (float3){q.x, q.y, q.z};
}

View File

@@ -20,171 +20,171 @@
#include "../error.h"
static void audiomixer(void* const userdata, uint8_t* const stream, int32_t const len) {
memset(stream, 0, len); // clear the playing audio
audiodevice* const dev = userdata; // retreive the callback data
memset(stream, 0, len); // clear the playing audio
audiodevice* const dev = userdata; // retreive the callback data
// return if dev is null, since it can fail to initialize
if (dev == NULL) return;
// return if dev is null, since it can fail to initialize
if (dev == NULL) return;
struct audioplayer* prev = NULL;
struct audioplayer* curr = dev->audio_players;
while (curr != NULL) {
// if the current audio fragment has reached the end of their data
if (curr->len == 0) {
struct audioplayer* ncurr = curr->nxt;
struct audioplayer* prev = NULL;
struct audioplayer* curr = dev->audio_players;
while (curr != NULL) {
// if the current audio fragment has reached the end of their data
if (curr->len == 0) {
struct audioplayer* ncurr = curr->nxt;
// free the memory allocated to it and assign the next to to the currently playing
free(curr);
curr = ncurr;
// free the memory allocated to it and assign the next to to the currently playing
free(curr);
curr = ncurr;
// write to the audio device if prev hasn't been set yet
if (prev == NULL)
dev->audio_players = curr;
else
prev->nxt = curr;
// write to the audio device if prev hasn't been set yet
if (prev == NULL)
dev->audio_players = curr;
else
prev->nxt = curr;
// continue so if curr is now NULL, the loop stops
continue;
}
// continue so if curr is now NULL, the loop stops
continue;
}
// calculate how much of the current audio player we should mix into the stream
uint32_t const mixlen = SDL_min(curr->len, (uint32_t)len);
// calculate how much of the current audio player we should mix into the stream
uint32_t const mixlen = SDL_min(curr->len, (uint32_t)len);
// mix the current buffer into the stream, and update the audio player values accordingly
SDL_MixAudioFormat(stream, curr->buf, dev->fmt, mixlen, SDL_MIX_MAXVOLUME);
curr->buf += mixlen;
curr->len -= mixlen;
// mix the current buffer into the stream, and update the audio player values accordingly
SDL_MixAudioFormat(stream, curr->buf, dev->fmt, mixlen, SDL_MIX_MAXVOLUME);
curr->buf += mixlen;
curr->len -= mixlen;
// increment the current node
prev = curr;
curr = curr->nxt;
}
// increment the current node
prev = curr;
curr = curr->nxt;
}
}
static int8_t audio_cvt(audiodevice const* dev, SDL_AudioSpec const* spec, uint8_t** bufptr, uint32_t* len) {
// init the converter
SDL_AudioCVT cvt;
SDL_BuildAudioCVT(&cvt, spec->format, spec->channels, spec->freq, dev->fmt, dev->channels, dev->freq);
cvt.len = (*len) * 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(*bufptr, cvt.len * cvt.len_mult); // grow the inputted buffer for the conversion
// init the converter
SDL_AudioCVT cvt;
SDL_BuildAudioCVT(&cvt, spec->format, spec->channels, spec->freq, dev->fmt, dev->channels, dev->freq);
cvt.len = (*len) * 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(*bufptr, cvt.len * cvt.len_mult); // grow the inputted buffer for the conversion
// ensure the conversion buffer reallocation goes correctly
if (cvt.buf == NULL) {
error("%s:%u something went wrong whilst growing the audio buffer whilst converting!", __FILE_NAME__, __LINE__);
free(*bufptr);
return 1;
}
// ensure the conversion buffer reallocation goes correctly
if (cvt.buf == NULL) {
error("%s:%u something went wrong whilst growing the audio buffer whilst converting!", __FILE_NAME__, __LINE__);
free(*bufptr);
return 1;
}
// converts the audio to the new format
if (!SDL_ConvertAudio(&cvt)) {
error("something went wrong when loading/converting an audio buffer! SDL Error: %s", SDL_GetError());
free(cvt.buf);
return 1;
}
// converts the audio to the new format
if (!SDL_ConvertAudio(&cvt)) {
error("something went wrong when loading/converting an audio buffer! SDL Error: %s", SDL_GetError());
free(cvt.buf);
return 1;
}
*len = cvt.len;
*len = cvt.len;
*bufptr = realloc(cvt.buf, cvt.len_cvt);
if (*bufptr == NULL) {
warn("%s:%u something went wrong whilst shrinking the audio buffer whilst converting!", __FILE_NAME__, __LINE__);
*bufptr = cvt.buf; // use the conversion buffer, as this one will be valid if realloc fails
}
*bufptr = realloc(cvt.buf, cvt.len_cvt);
if (*bufptr == NULL) {
warn("%s:%u something went wrong whilst shrinking the audio buffer whilst converting!", __FILE_NAME__, __LINE__);
*bufptr = cvt.buf; // use the conversion buffer, as this one will be valid if realloc fails
}
return 0;
return 0;
}
audiodevice* audio_device_init(int32_t freq, SDL_AudioFormat fmt, uint8_t channels, uint16_t samples) {
audiodevice* dev = malloc(sizeof(audiodevice));
audiodevice* dev = malloc(sizeof(audiodevice));
if (dev == NULL) {
error("%s:%u null pointer when allocating memory for the audio device!", __FILE_NAME__, __LINE__);
return NULL;
}
if (dev == NULL) {
error("%s:%u null pointer when allocating memory for the audio device!", __FILE_NAME__, __LINE__);
return NULL;
}
// define the audio specification
SDL_AudioSpec spec = {freq, fmt, channels, 0, samples, 0, 0, NULL, NULL};
spec.callback = audiomixer;
spec.userdata = dev;
// define the audio specification
SDL_AudioSpec spec = {freq, fmt, channels, 0, samples, 0, 0, NULL, NULL};
spec.callback = audiomixer;
spec.userdata = dev;
// create the audio device
*dev = (audiodevice){
NULL,
SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0),
freq,
fmt,
channels,
};
// create the audio device
*dev = (audiodevice){
NULL,
SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0),
freq,
fmt,
channels,
};
if (dev->id < 1) {
error("%s:%u audio device failed to open! SDL Error: %s", __FILE_NAME__, __LINE__, SDL_GetError());
free(dev);
return NULL;
}
if (dev->id < 1) {
error("%s:%u audio device failed to open! SDL Error: %s", __FILE_NAME__, __LINE__, SDL_GetError());
free(dev);
return NULL;
}
// default state of the device is paused, so we unpause it here
SDL_PauseAudioDevice(dev->id, 0);
return dev;
// default state of the device is paused, so we unpause it here
SDL_PauseAudioDevice(dev->id, 0);
return dev;
}
void audio_play(audiodevice* dev, audiodata const* audio) {
if (dev == NULL) return; // dev might fail to initialize
if (audio->len == 0) return; // audio might fail to initialize
if (dev == NULL) return; // dev might fail to initialize
if (audio->len == 0) return; // audio might fail to initialize
// create an audio player
struct audioplayer* player = malloc(sizeof(struct audioplayer));
*player = (struct audioplayer){
dev->audio_players, // set nxt to the first item in dev (can be NULL, this is fine)
audio->buf,
audio->len,
};
// create an audio player
struct audioplayer* player = malloc(sizeof(struct audioplayer));
*player = (struct audioplayer){
dev->audio_players, // 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_players = player;
// assign ourselves to the first item
dev->audio_players = player;
}
void audio_device_free(audiodevice* dev) {
if (dev == NULL) return;
SDL_CloseAudioDevice(dev->id);
if (dev == NULL) return;
SDL_CloseAudioDevice(dev->id);
struct audioplayer* curr = dev->audio_players;
struct audioplayer* curr = dev->audio_players;
// free all audio players
while (curr != NULL) {
dev->audio_players = curr->nxt; // use audio_players in dev as a cache
free(curr);
curr = dev->audio_players;
}
// free all audio players
while (curr != NULL) {
dev->audio_players = curr->nxt; // use audio_players in dev as a cache
free(curr);
curr = dev->audio_players;
}
// free the audio device itself
free(dev);
// free the audio device itself
free(dev);
}
audiodata audio_wav_load(audiodevice const* dev, char const* fpath) {
if (dev == NULL) return (audiodata){0};
SDL_AudioSpec spec;
audiodata audio;
if (dev == NULL) return (audiodata){0};
SDL_AudioSpec spec;
audiodata audio;
debug("loading audio file '%s'...", fpath);
debug("loading audio file '%s'...", fpath);
if (!fexists(fpath)) {
error("%s:%u couldn't find audio file '%s'!", __FILE_NAME__, __LINE__, fpath);
return (audiodata){0};
}
if (!fexists(fpath)) {
error("%s:%u couldn't find audio file '%s'!", __FILE_NAME__, __LINE__, fpath);
return (audiodata){0};
}
// load and parse the audio to the correct format
SDL_LoadWAV(fpath, &spec, &audio.buf, &audio.len);
if (!!audio_cvt(dev, &spec, &audio.buf, &audio.len)) {
free(audio.buf);
return (audiodata){0};
}
// load and parse the audio to the correct format
SDL_LoadWAV(fpath, &spec, &audio.buf, &audio.len);
if (!!audio_cvt(dev, &spec, &audio.buf, &audio.len)) {
free(audio.buf);
return (audiodata){0};
}
// calculate the time in miliseconds of the audio fragment
audio.ms = 1000 * (((audio.len) / (SDL_AUDIO_BITSIZE(dev->fmt) / 8)) / spec.channels / dev->freq);
// calculate the time in miliseconds of the audio fragment
audio.ms = 1000 * (((audio.len) / (SDL_AUDIO_BITSIZE(dev->fmt) / 8)) / spec.channels / dev->freq);
return audio;
return audio;
}
void audio_wav_unload(audiodata* audio) {
free(audio->buf);
*audio = (audiodata){0}; // zero out all audio data
free(audio->buf);
*audio = (audiodata){0}; // zero out all audio data
}

View File

@@ -4,24 +4,24 @@
#include <stdint.h>
struct audiodata {
uint8_t* buf; // pointer to the audio buffer
uint32_t len; // length in bytes of the audio buffer
uint32_t ms; // length in miliseconds of the audio buffer
uint8_t* buf; // pointer to the audio buffer
uint32_t len; // length in bytes of the audio buffer
uint32_t ms; // length in miliseconds of the audio buffer
};
// contains the data of the audio fragments to be played
struct audioplayer {
struct audioplayer* nxt; // pointer to the next audioplayer (may be null)
uint8_t* buf; // pointer to the current item in the buffer to be played
uint32_t len; // the length in bytes that the buffer has remaining
struct audioplayer* nxt; // pointer to the next audioplayer (may be null)
uint8_t* buf; // pointer to the current item in the buffer to be played
uint32_t len; // the length in bytes that the buffer has remaining
};
struct audiodevice {
struct audioplayer* audio_players;
SDL_AudioDeviceID id;
int32_t freq;
SDL_AudioFormat fmt;
uint8_t channels;
struct audioplayer* audio_players;
SDL_AudioDeviceID id;
int32_t freq;
SDL_AudioFormat fmt;
uint8_t channels;
};
typedef struct audiodata audiodata;

View File

@@ -5,13 +5,13 @@
// 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;
};
uint32_t packed;
struct {
uint8_t a;
uint8_t b;
uint8_t g;
uint8_t r;
};
} colour32;
#define COLOUR32_BLACK ((colour32){0x000000FF})
@@ -26,7 +26,7 @@ typedef union {
// 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);
(void)SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, c.a);
}
// american macros:

View File

@@ -19,22 +19,22 @@ typedef uint8_t colour8;
// gets the red channel in 32 bit colour space
static inline uint8_t colour8_red32(colour8 const colour) {
return (colour >> 5) * (255 / 7);
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);
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);
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);
(void)SDL_SetRenderDrawColor(renderer, colour8_red32(c), colour8_green32(c), colour8_blue32(c), 0xFF);
}
// american macros:

View File

@@ -12,32 +12,32 @@
#include "colour/colour32.h"
void render_init(renderdata* const rdat, gamedata const* const gdat) {
SDL_Window* const window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_SHOWN);
if (window == NULL)
fatal(ERROR_SDL_RENDERING_INIT, __FILE_NAME__, __LINE__, "failed to create a window. SDL Error: %s", SDL_GetError());
SDL_Window* const window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_SHOWN);
if (window == NULL)
fatal(ERROR_SDL_RENDERING_INIT, __FILE_NAME__, __LINE__, "failed to create a window. SDL Error: %s", SDL_GetError());
// render using vsync to limit updates to the refresh rate of the monitor
SDL_Renderer* const renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
fatal(ERROR_SDL_RENDERING_INIT, __FILE_NAME__, __LINE__, "failed to create a renderer. SDL Error: %s", SDL_GetError());
// render using vsync to limit updates to the refresh rate of the monitor
SDL_Renderer* const renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
fatal(ERROR_SDL_RENDERING_INIT, __FILE_NAME__, __LINE__, "failed to create a renderer. SDL Error: %s", SDL_GetError());
*rdat = (renderdata){
window,
renderer,
gdat,
};
*rdat = (renderdata){
window,
renderer,
gdat,
};
}
void render_update(renderdata const* const rdat) {
set_colour32(rdat->renderer, COLOUR32_BLACK);
SDL_RenderClear(rdat->renderer);
set_colour32(rdat->renderer, COLOUR32_BLACK);
SDL_RenderClear(rdat->renderer);
// present the renderer
SDL_RenderPresent(rdat->renderer);
// present the renderer
SDL_RenderPresent(rdat->renderer);
}
void render_free(renderdata* const rdat) {
SDL_DestroyRenderer(rdat->renderer);
SDL_DestroyWindow(rdat->window);
*rdat = (renderdata){0};
SDL_DestroyRenderer(rdat->renderer);
SDL_DestroyWindow(rdat->window);
*rdat = (renderdata){0};
}

View File

@@ -7,9 +7,9 @@
// contains the data necessary for rendering
typedef struct {
SDL_Window* window;
SDL_Renderer* renderer;
gamedata const* gdat;
SDL_Window* window;
SDL_Renderer* renderer;
gamedata const* gdat;
} renderdata;
void render_init(renderdata*, gamedata const*); // initializes the renderer, outputs to render_data