use tabs over spaces

This commit is contained in:
2025-06-18 15:48:56 +02:00
parent 3b306288e9
commit b446a43738
19 changed files with 599 additions and 605 deletions

View File

@@ -11,179 +11,179 @@
#include "../util/compat.h"
static void audiomixer(void* const userdata, uint8_t* const stream, int 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
int const mixlen = SDL_min(curr->len, (unsigned)len);
// calculate how much of the current audio player we should mix into the stream
int const mixlen = SDL_min(curr->len, (unsigned)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;
}
}
// converts the inputted audio to the format of dev
// returns 1 upon failure, 0 upon success. When 1 is returned *bufptr will be freed. Otherwise *bufptr is reallocated
static int8_t audio_cvt(audiodevice const* dev, SDL_AudioSpec const* spec, uint8_t** bufptr, unsigned* len) {
// init the converter
SDL_AudioCVT cvt;
if (SDL_BuildAudioCVT(&cvt, spec->format, spec->channels, spec->freq, dev->fmt, dev->channels, dev->freq) < 0) {
error("%s:%u could not build the audio converter! SDL Error: %s", __FILE_NAME__, __LINE__, SDL_GetError());
free(*bufptr); // free the buffer upon an error, as we won't be using this
return 1;
} else if (!cvt.needed) { // ensure the conversion is necessary
return 0;
}
cvt.len = (*len); // specify the length of the source data buffer in bytes (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;
if (SDL_BuildAudioCVT(&cvt, spec->format, spec->channels, spec->freq, dev->fmt, dev->channels, dev->freq) < 0) {
error("%s:%u could not build the audio converter! SDL Error: %s", __FILE_NAME__, __LINE__, SDL_GetError());
free(*bufptr); // free the buffer upon an error, as we won't be using this
return 1;
} else if (!cvt.needed) { // ensure the conversion is necessary
return 0;
}
cvt.len = (*len); // specify the length of the source data buffer in bytes (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 failed to reallocate the audio buffer to the new size of %u bytes!", __FILE_NAME__, __LINE__, cvt.len);
free(*bufptr); // free the inputted pointer, as realloc doesn't clear this address if it fails
return 1;
}
// ensure the conversion buffer reallocation goes correctly
if (cvt.buf == NULL) {
error("%s:%u failed to reallocate the audio buffer to the new size of %u bytes!", __FILE_NAME__, __LINE__, cvt.len);
free(*bufptr); // free the inputted pointer, as realloc doesn't clear this address if it fails
return 1;
}
// converts the audio to the new format
if (SDL_ConvertAudio(&cvt)) {
error("%s:%u something went wrong when loading/converting an audio buffer! SDL Error: %s", __FILE_NAME__, __LINE__, SDL_GetError());
free(cvt.buf); // free the conversion buffer if it fails, as realloc moved the data to this adress; old adress is no longer valid
return 1;
}
// converts the audio to the new format
if (SDL_ConvertAudio(&cvt)) {
error("%s:%u something went wrong when loading/converting an audio buffer! SDL Error: %s", __FILE_NAME__, __LINE__, SDL_GetError());
free(cvt.buf); // free the conversion buffer if it fails, as realloc moved the data to this adress; old adress is no longer valid
return 1;
}
// update output
*len = cvt.len_cvt; // set the length to the new length after the conversion
*bufptr = realloc(cvt.buf, cvt.len_cvt); // reallocate the buffer to the new size
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
}
// update output
*len = cvt.len_cvt; // set the length to the new length after the conversion
*bufptr = realloc(cvt.buf, cvt.len_cvt); // reallocate the buffer to the new size
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(int 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 (faccess(fpath, FA_R)) {
error("%s:%u audio file either isn't readable or doesn't exist. path: '%s'!", __FILE_NAME__, __LINE__, fpath);
return (audiodata){0};
}
if (faccess(fpath, FA_R)) {
error("%s:%u audio file either isn't readable or doesn't exist. path: '%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)) {
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)) {
return (audiodata){0};
}
// calculate the time in milliseconds of the audio fragment
// by dividing the audio bytelength by the format's bitsize, by the audio device's channels and the audio device's frequency
audio.ms = (((1000 * audio.len) / (SDL_AUDIO_BITSIZE(dev->fmt) / 8)) / dev->channels / dev->freq);
// calculate the time in milliseconds of the audio fragment
// by dividing the audio bytelength by the format's bitsize, by the audio device's channels and the audio device's frequency
audio.ms = (((1000 * audio.len) / (SDL_AUDIO_BITSIZE(dev->fmt) / 8)) / dev->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;
int freq;
SDL_AudioFormat fmt;
uint8_t channels;
struct audioplayer* audio_players;
SDL_AudioDeviceID id;
int 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

@@ -21,141 +21,141 @@
#define COLOUR_SCORE COLOUR32_YELLOW
void render_init(renderdata* const render_dat, gamedata const* const game_dat) {
SDL_Window* const window = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) fatal(ERROR_SDL_RENDERING_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError());
SDL_Window* const window = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) fatal(ERROR_SDL_RENDERING_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError());
SDL_Renderer* const renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (renderer == NULL) fatal(ERROR_SDL_RENDERING_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError());
SDL_Renderer* const renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (renderer == NULL) fatal(ERROR_SDL_RENDERING_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError());
TTF_Font* const font = TTF_OpenFont("pixeldroid_botic-regular.ttf", PX_DENS);
if (font == NULL) error("Failed to open font! TTF Error: %s", TTF_GetError());
TTF_Font* const font = TTF_OpenFont("pixeldroid_botic-regular.ttf", PX_DENS);
if (font == NULL) error("Failed to open font! TTF Error: %s", TTF_GetError());
// initialize the render data
*render_dat = (renderdata){
game_dat,
window,
renderer,
font,
calloc(1, sizeof(struct render_cache)), // zero-initialize the memory as we read from it
};
// initialize the render data
*render_dat = (renderdata){
game_dat,
window,
renderer,
font,
calloc(1, sizeof(struct render_cache)), // zero-initialize the memory as we read from it
};
}
static inline int32_t get_column_pos(uint8_t column) {
return column * BLOCK_WIDTH + 1 + TET_PADDING;
return column * BLOCK_WIDTH + 1 + TET_PADDING;
}
static inline int32_t get_row_pos(uint8_t row) {
return row * BLOCK_HEIGHT + 1 + TET_PADDING;
return row * BLOCK_HEIGHT + 1 + TET_PADDING;
}
static void draw_score_text(renderdata const* dat) {
struct render_cache* const cache = dat->cache;
uint16_t const score = dat->game_dat->score;
struct render_cache* const cache = dat->cache;
uint16_t const score = dat->game_dat->score;
SDL_Renderer* const renderer = dat->renderer;
TTF_Font* const font = dat->font;
SDL_Renderer* const renderer = dat->renderer;
TTF_Font* const font = dat->font;
if (cache->prevscore != score || cache->score_texture == NULL) {
char score_text[6]; // max digits of a uint16 + \0 terminator
if (!score) sprintf(score_text, "0");
else sprintf(score_text, "%hu0", score);
if (cache->prevscore != score || cache->score_texture == NULL) {
char score_text[6]; // max digits of a uint16 + \0 terminator
if (!score) sprintf(score_text, "0");
else sprintf(score_text, "%hu0", score);
SDL_Surface* const txt_surface = TTF_RenderText_Solid(font, score_text, (SDL_Colour){COLOUR_SCORE.r, COLOUR_SCORE.g, COLOUR_SCORE.b, COLOUR_SCORE.a});
SDL_Texture* const txt_texture = SDL_CreateTextureFromSurface(renderer, txt_surface);
SDL_Surface* const txt_surface = TTF_RenderText_Solid(font, score_text, (SDL_Colour){COLOUR_SCORE.r, COLOUR_SCORE.g, COLOUR_SCORE.b, COLOUR_SCORE.a});
SDL_Texture* const txt_texture = SDL_CreateTextureFromSurface(renderer, txt_surface);
if (cache->score_texture != NULL || cache->score_surface != NULL) {
// free old data
SDL_FreeSurface(cache->score_surface);
SDL_DestroyTexture(cache->score_texture);
}
if (cache->score_texture != NULL || cache->score_surface != NULL) {
// free old data
SDL_FreeSurface(cache->score_surface);
SDL_DestroyTexture(cache->score_texture);
}
// write data to cache
cache->score_surface = txt_surface;
cache->score_texture = txt_texture;
}
// write data to cache
cache->score_surface = txt_surface;
cache->score_texture = txt_texture;
}
if (cache->score_surface == NULL || cache->score_texture == NULL) {
error("the score texture was unavailable!",);
return;
}
if (cache->score_surface == NULL || cache->score_texture == NULL) {
error("the score texture was unavailable!", );
return;
}
SDL_Rect text_rect = {get_column_pos(COLUMNS + 1), get_row_pos(0), cache->score_surface->w, cache->score_surface->h};
SDL_RenderCopy(renderer, cache->score_texture, NULL, &text_rect);
SDL_Rect text_rect = {get_column_pos(COLUMNS + 1), get_row_pos(0), cache->score_surface->w, cache->score_surface->h};
SDL_RenderCopy(renderer, cache->score_texture, NULL, &text_rect);
}
// draws a block at the specified position
static inline int draw_block(SDL_Renderer* const renderer, int8_t const x, int8_t const y) {
SDL_Rect const block = {get_column_pos(x), get_row_pos(y), BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1};
return SDL_RenderFillRect(renderer, &block);
SDL_Rect const block = {get_column_pos(x), get_row_pos(y), BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1};
return SDL_RenderFillRect(renderer, &block);
}
// draws a shape at the specified position
static void draw_shape(SDL_Renderer* const renderer, shape_id const id, int8_t const pos_x, int8_t const pos_y) {
shape const shape = shape_from_id(id);
set_colour8(renderer, colour_from_id(id));
shape const shape = shape_from_id(id);
set_colour8(renderer, colour_from_id(id));
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
shape_row const shape_row = shape_get_row(shape, y);
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
shape_row const shape_row = shape_get_row(shape, y);
if (shape_row == 0)
continue;
if (shape_row == 0)
continue;
for (int8_t x = 0; x < SHAPE_WIDTH; x++)
if (shape_is_set(shape_row, x))
draw_block(renderer, pos_x + x, pos_y + y);
}
for (int8_t x = 0; x < SHAPE_WIDTH; x++)
if (shape_is_set(shape_row, x))
draw_block(renderer, pos_x + x, pos_y + y);
}
}
// draw the block data in the level
static void render_level(SDL_Renderer* const renderer, gamedata const* const data) {
for (int8_t y = 0; y < ROWS; y++) {
row_const const row = data->rows[y];
for (int8_t y = 0; y < ROWS; y++) {
row_const const row = data->rows[y];
for (int8_t x = 0; x < COLUMNS; x++) {
if (row[x] != 0) {
set_colour8(renderer, row[x]);
draw_block(renderer, x, y);
}
}
}
for (int8_t x = 0; x < COLUMNS; x++) {
if (row[x] != 0) {
set_colour8(renderer, row[x]);
draw_block(renderer, x, y);
}
}
}
}
void render_update(renderdata const* const dat) {
SDL_Renderer* const renderer = dat->renderer;
gamedata const* const game_data = dat->game_dat;
SDL_Renderer* const renderer = dat->renderer;
gamedata const* const game_data = dat->game_dat;
int success = 0; // if an error occurs, this value is <0
int success = 0; // if an error occurs, this value is <0
// clear render
set_colour32(renderer, COLOUR32_BLACK); // using colour32 is more efficient, as it sets the colours directly
success |= SDL_RenderClear(renderer);
// clear render
set_colour32(renderer, COLOUR32_BLACK); // using colour32 is more efficient, as it sets the colours directly
success |= SDL_RenderClear(renderer);
set_colour32(renderer, COLOUR32_WHITE);
set_colour32(renderer, COLOUR32_WHITE);
static SDL_Rect const field_size = {TET_PADDING, TET_PADDING, TET_WIDTH + 1, TET_HEIGHT + 1};
SDL_RenderDrawRect(renderer, &field_size);
draw_shape(renderer, game_data->nxt[game_data->curr_idx + 1], COLUMNS + 1, 3); // draw the next shape
static SDL_Rect const field_size = {TET_PADDING, TET_PADDING, TET_WIDTH + 1, TET_HEIGHT + 1};
SDL_RenderDrawRect(renderer, &field_size);
draw_shape(renderer, game_data->nxt[game_data->curr_idx + 1], COLUMNS + 1, 3); // draw the next shape
if (dat->font)
draw_score_text(dat);
if (dat->font)
draw_score_text(dat);
render_level(renderer, dat->game_dat);
draw_shape(renderer, game_data->nxt[game_data->curr_idx], game_data->sel_x, game_data->sel_y); // draw the current shape
render_level(renderer, dat->game_dat);
draw_shape(renderer, game_data->nxt[game_data->curr_idx], game_data->sel_x, game_data->sel_y); // draw the current shape
if (success < 0) {
warn("something went wrong whilst renderering! SDL Error: %s\n", SDL_GetError());
return;
}
if (success < 0) {
warn("something went wrong whilst renderering! SDL Error: %s\n", SDL_GetError());
return;
}
SDL_RenderPresent(renderer);
SDL_RenderPresent(renderer);
}
void render_free(renderdata* const render_data) {
SDL_DestroyRenderer(render_data->renderer);
SDL_DestroyWindow(render_data->window);
TTF_CloseFont(render_data->font);
SDL_FreeSurface(render_data->cache->score_surface);
SDL_DestroyTexture(render_data->cache->score_texture);
free(render_data->cache);
*render_data = (renderdata){0};
SDL_DestroyRenderer(render_data->renderer);
SDL_DestroyWindow(render_data->window);
TTF_CloseFont(render_data->font);
SDL_FreeSurface(render_data->cache->score_surface);
SDL_DestroyTexture(render_data->cache->score_texture);
free(render_data->cache);
*render_data = (renderdata){0};
}

View File

@@ -19,18 +19,18 @@
// contains the data that's cached between renders
struct render_cache {
SDL_Texture* score_texture;
SDL_Surface* score_surface;
uint16_t prevscore;
SDL_Texture* score_texture;
SDL_Surface* score_surface;
uint16_t prevscore;
};
// contains the data necessary for rendering
typedef struct {
gamedata const* game_dat;
SDL_Window* window;
SDL_Renderer* renderer;
TTF_Font* font;
struct render_cache* cache;
gamedata const* game_dat;
SDL_Window* window;
SDL_Renderer* renderer;
TTF_Font* font;
struct render_cache* cache;
} renderdata;
void render_init(renderdata*, gamedata const*); // initializes the renderer, outputs to render_data