mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
rename window io
This commit is contained in:
189
src/io/audio.c
Normal file
189
src/io/audio.c
Normal file
@@ -0,0 +1,189 @@
|
||||
#include "audio.h"
|
||||
|
||||
#include <SDL_audio.h>
|
||||
#include <SDL_error.h>
|
||||
#include <SDL_stdinc.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../error.h"
|
||||
#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
|
||||
|
||||
// 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;
|
||||
|
||||
// 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;
|
||||
|
||||
// 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);
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
audiodevice* audio_device_init(int freq, SDL_AudioFormat fmt, uint8_t channels, uint16_t samples) {
|
||||
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;
|
||||
}
|
||||
|
||||
// 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,
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
void audio_device_free(audiodevice* dev) {
|
||||
if (dev == NULL) return;
|
||||
SDL_CloseAudioDevice(dev->id);
|
||||
|
||||
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 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;
|
||||
|
||||
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};
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
return audio;
|
||||
}
|
||||
|
||||
void audio_wav_unload(audiodata* audio) {
|
||||
free(audio->buf);
|
||||
*audio = (audiodata){0}; // zero out all audio data
|
||||
}
|
||||
35
src/io/audio.h
Normal file
35
src/io/audio.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL_audio.h>
|
||||
#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
|
||||
};
|
||||
|
||||
// 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 audiodevice {
|
||||
struct audioplayer* audio_players;
|
||||
SDL_AudioDeviceID id;
|
||||
int freq;
|
||||
SDL_AudioFormat fmt;
|
||||
uint8_t channels;
|
||||
};
|
||||
|
||||
typedef struct audiodata audiodata;
|
||||
typedef struct audiodevice audiodevice;
|
||||
|
||||
audiodevice* audio_device_init(int, SDL_AudioFormat, uint8_t, uint16_t);
|
||||
void audio_play(audiodevice*, audiodata const*);
|
||||
void audio_device_free(audiodevice*);
|
||||
|
||||
audiodata audio_wav_load(audiodevice const*, char const*);
|
||||
void audio_wav_unload(audiodata*);
|
||||
42
src/io/colour/colour32.h
Normal file
42
src/io/colour/colour32.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
53
src/io/colour/colour8.h
Normal file
53
src/io/colour/colour8.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
161
src/io/renderer.c
Normal file
161
src/io/renderer.c
Normal file
@@ -0,0 +1,161 @@
|
||||
#include "renderer.h"
|
||||
|
||||
#include <SDL_error.h>
|
||||
#include <SDL_pixels.h>
|
||||
#include <SDL_rect.h>
|
||||
#include <SDL_render.h>
|
||||
#include <SDL_surface.h>
|
||||
#include <SDL_ttf.h>
|
||||
#include <SDL_video.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "../error.h"
|
||||
#include "../game/game.h"
|
||||
#include "../game/tetromino/shapes.h"
|
||||
#include "colour/colour32.h"
|
||||
#include "colour/colour8.h"
|
||||
|
||||
#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_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());
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
static inline int32_t get_row_pos(uint8_t row) {
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 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));
|
||||
|
||||
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
||||
shape_row const shape_row = shape_get_row(shape, y);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// 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 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;
|
||||
|
||||
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);
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
if (success < 0) {
|
||||
warn("something went wrong whilst renderering! SDL Error: %s\n", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
|
||||
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};
|
||||
}
|
||||
38
src/io/renderer.h
Normal file
38
src/io/renderer.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL_render.h>
|
||||
#include <SDL_surface.h>
|
||||
#include <SDL_ttf.h>
|
||||
#include <SDL_video.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../game/game.h"
|
||||
|
||||
#define PX_DENS 25 // pixel density; pixels per block
|
||||
#define TET_PADDING 10 // padding around the tetris playing field
|
||||
#define TET_WIDTH (COLUMNS * PX_DENS - TET_PADDING) // tetris playing field width
|
||||
#define TET_HEIGHT (TET_WIDTH / COLUMNS * ROWS) // tetris playing field height
|
||||
#define SCREEN_WIDTH ((COLUMNS + 6) * PX_DENS) // window width
|
||||
#define SCREEN_HEIGHT ((COLUMNS) * PX_DENS / COLUMNS * ROWS) // window height
|
||||
#define BLOCK_WIDTH (TET_WIDTH / COLUMNS) // width of a block
|
||||
#define BLOCK_HEIGHT (TET_HEIGHT / ROWS) // height of a block
|
||||
|
||||
// contains the data that's cached between renders
|
||||
struct render_cache {
|
||||
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;
|
||||
} renderdata;
|
||||
|
||||
void render_init(renderdata*, gamedata const*); // initializes the renderer, outputs to render_data
|
||||
void render_update(renderdata const*); // causes a draw to occur, will also determine update rate
|
||||
void render_free(renderdata*); // frees the memory allocated to the renderer in render_data
|
||||
Reference in New Issue
Block a user