mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 05:55:46 +01:00
code cleanup + add font support
This commit is contained in:
4
makefile
4
makefile
@@ -4,8 +4,8 @@ NAME := tetris_clone
|
|||||||
CC := clang
|
CC := clang
|
||||||
STD := c17
|
STD := c17
|
||||||
LANG = c
|
LANG = c
|
||||||
CFLAGS := $(shell pkg-config --cflags sdl2) -Wall -Wextra -Wpedantic -Wno-pointer-arith
|
CFLAGS := $(shell pkg-config --cflags sdl2 SDL2_ttf) -Wall -Wextra -Wpedantic -Wno-pointer-arith
|
||||||
LDFLAGS := $(shell pkg-config --libs sdl2) -lm
|
LDFLAGS := $(shell pkg-config --libs sdl2 SDL2_ttf) -lm
|
||||||
|
|
||||||
ifeq ($(DEBUG),1)
|
ifeq ($(DEBUG),1)
|
||||||
CFLAGS += -DDEBUG -Og -g
|
CFLAGS += -DDEBUG -Og -g
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ void error(ErrorCode const error_code, char const* const format, ...) {
|
|||||||
vsnprintf(buffer, MAX_STR_LEN, format, args);
|
vsnprintf(buffer, MAX_STR_LEN, format, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
printf("\033[91mE\033[0m: %s\n", buffer);
|
fprintf(stderr, "\033[91mE\033[0m: %s\n", buffer);
|
||||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", buffer, NULL);
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", buffer, NULL);
|
||||||
|
|
||||||
exit(error_code);
|
exit(error_code);
|
||||||
@@ -29,5 +29,5 @@ void warn(char const* const format, ...) {
|
|||||||
vsnprintf(buffer, MAX_STR_LEN, format, args);
|
vsnprintf(buffer, MAX_STR_LEN, format, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
printf("\033[93mW\033[0m: %s\n", buffer);
|
fprintf(stderr, "\033[93mW\033[0m: %s\n", buffer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,14 @@ enum {
|
|||||||
ERROR_SDL_RENDERER = ERROR_SDL | 4,
|
ERROR_SDL_RENDERER = ERROR_SDL | 4,
|
||||||
ERROR_SDL_RENDERER_INIT = ERROR_SDL_RENDERER | ERROR_INIT,
|
ERROR_SDL_RENDERER_INIT = ERROR_SDL_RENDERER | ERROR_INIT,
|
||||||
|
|
||||||
|
|
||||||
// audio errors
|
// audio errors
|
||||||
ERROR_SDL_AUDIO = ERROR_SDL | 8,
|
ERROR_SDL_AUDIO = ERROR_SDL | 8,
|
||||||
ERROR_SDL_AUDIO_INIT = ERROR_SDL_AUDIO | ERROR_INIT,
|
ERROR_SDL_AUDIO_INIT = ERROR_SDL_AUDIO | ERROR_INIT,
|
||||||
|
|
||||||
|
// font errors
|
||||||
|
ERROR_SDL_FONT = ERROR_SDL | 16,
|
||||||
|
ERROR_SDL_FONT_INIT = ERROR_SDL_FONT | ERROR_INIT,
|
||||||
};
|
};
|
||||||
|
|
||||||
// call when a fatal error has occurred, the program will immediately terminate when called
|
// call when a fatal error has occurred, the program will immediately terminate when called
|
||||||
|
|||||||
24
src/main.c
24
src/main.c
@@ -4,8 +4,6 @@
|
|||||||
#include <SDL_error.h>
|
#include <SDL_error.h>
|
||||||
#include <SDL_events.h>
|
#include <SDL_events.h>
|
||||||
#include <SDL_keyboard.h>
|
#include <SDL_keyboard.h>
|
||||||
#include <SDL_render.h>
|
|
||||||
#include <SDL_video.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -13,6 +11,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "SDL_timer.h"
|
#include "SDL_timer.h"
|
||||||
|
#include "SDL_ttf.h"
|
||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
#include "game/game.h"
|
#include "game/game.h"
|
||||||
// #include "window/audio.h"
|
// #include "window/audio.h"
|
||||||
@@ -25,27 +24,21 @@
|
|||||||
|
|
||||||
bool playing = true;
|
bool playing = true;
|
||||||
|
|
||||||
SDL_Window* window = NULL;
|
RenderData render_data = {0};
|
||||||
SDL_Renderer* renderer = NULL;
|
|
||||||
GameData game_data = {0};
|
GameData game_data = {0};
|
||||||
|
|
||||||
|
|
||||||
// handles game application initialisation
|
// handles game application initialisation
|
||||||
static void init(void) {
|
static void init(void) {
|
||||||
// initialize SDL
|
// initialize SDL
|
||||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
|
||||||
error(ERROR_SDL_INIT, "SDL could not initialize! SDL Error: %s", SDL_GetError());
|
error(ERROR_SDL_INIT, "SDL could not initialize! SDL Error: %s", SDL_GetError());
|
||||||
return;
|
if (TTF_Init() != 0)
|
||||||
}
|
error(ERROR_SDL_FONT_INIT, "the TTF module of SDL could not initialize! TTF Error: %s", TTF_GetError());
|
||||||
|
|
||||||
// initialize the renderer
|
// initialize units
|
||||||
if (renderer_init(&window, &renderer) < 0) {
|
|
||||||
error(ERROR_SDL_RENDERER_INIT, SDL_GetError());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// initialize the game
|
|
||||||
game_init(&game_data);
|
game_init(&game_data);
|
||||||
|
renderer_init(&render_data, &game_data);
|
||||||
|
|
||||||
// initialize audio
|
// initialize audio
|
||||||
// AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
// AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
||||||
@@ -68,8 +61,7 @@ static void update(void) {
|
|||||||
|
|
||||||
// preform updates
|
// preform updates
|
||||||
game_update(&game_data, SDL_GetKeyboardState(NULL));
|
game_update(&game_data, SDL_GetKeyboardState(NULL));
|
||||||
renderer_update(&(RenderData){
|
renderer_update(&render_data);
|
||||||
window, renderer, &game_data});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handles game application quitting
|
// handles game application quitting
|
||||||
|
|||||||
@@ -11,26 +11,27 @@
|
|||||||
#include "../errors.h"
|
#include "../errors.h"
|
||||||
#include "../game/game.h"
|
#include "../game/game.h"
|
||||||
#include "../game/tetromino/shapes.h"
|
#include "../game/tetromino/shapes.h"
|
||||||
|
#include "SDL_ttf.h"
|
||||||
#include "colour8.h"
|
#include "colour8.h"
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
|
|
||||||
|
|
||||||
int renderer_init(SDL_Window** const window, SDL_Renderer** const renderer) {
|
void renderer_init(RenderData* const render_data, GameData const* const game_data) {
|
||||||
// create a new window
|
SDL_Window* const window = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||||
*window = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
if (window == NULL) error(ERROR_SDL_RENDERER_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError());
|
||||||
if (*window == NULL) {
|
|
||||||
error(ERROR_SDL_RENDERER_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// create a renderer
|
SDL_Renderer* const renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
|
||||||
*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());
|
||||||
if (*renderer == NULL) {
|
|
||||||
error(ERROR_SDL_RENDERER_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
TTF_Font* const font = TTF_OpenFont("/usr/share/fonts/TTF/JetBrainsMono-Regular.ttf", 5);
|
||||||
|
if (font == NULL) error(ERROR_SDL_FONT_INIT, "Failed to open font! TTF Error: %s", TTF_GetError());
|
||||||
|
|
||||||
|
// initialize the render data
|
||||||
|
*render_data = (RenderData){
|
||||||
|
game_data,
|
||||||
|
window,
|
||||||
|
renderer,
|
||||||
|
font};
|
||||||
}
|
}
|
||||||
|
|
||||||
// draws a block at the specified position
|
// draws a block at the specified position
|
||||||
@@ -74,7 +75,7 @@ static void render_level(SDL_Renderer* const renderer, GameData const* const dat
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void renderer_update(const RenderData* const render_data) {
|
void renderer_update(RenderData const* const render_data) {
|
||||||
SDL_Renderer* const renderer = render_data->renderer;
|
SDL_Renderer* const renderer = render_data->renderer;
|
||||||
GameData const* const game_data = render_data->game_data;
|
GameData const* const game_data = render_data->game_data;
|
||||||
|
|
||||||
@@ -99,3 +100,12 @@ void renderer_update(const RenderData* const render_data) {
|
|||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void renderer_free(RenderData* const render_data) {
|
||||||
|
SDL_DestroyRenderer(render_data->renderer);
|
||||||
|
SDL_DestroyWindow(render_data->window);
|
||||||
|
TTF_CloseFont(render_data->font);
|
||||||
|
render_data->renderer = NULL;
|
||||||
|
render_data->window = NULL;
|
||||||
|
render_data->font = NULL;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <SDL_render.h>
|
#include <SDL_render.h>
|
||||||
|
#include <SDL_ttf.h>
|
||||||
#include <SDL_video.h>
|
#include <SDL_video.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
@@ -15,10 +16,12 @@
|
|||||||
#define BLOCK_HEIGHT (TET_HEIGHT / ROWS) // height of a block
|
#define BLOCK_HEIGHT (TET_HEIGHT / ROWS) // height of a block
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
GameData const* game_data;
|
||||||
SDL_Window* window;
|
SDL_Window* window;
|
||||||
SDL_Renderer* renderer;
|
SDL_Renderer* renderer;
|
||||||
GameData* game_data;
|
TTF_Font* font;
|
||||||
} RenderData;
|
} RenderData;
|
||||||
|
|
||||||
int renderer_init(SDL_Window** window, SDL_Renderer** renderer);
|
void renderer_init(RenderData* render_data, GameData const* game_data);
|
||||||
void renderer_update(RenderData const* render_data);
|
void renderer_update(RenderData const* render_data);
|
||||||
|
void renderer_free(RenderData* render_data);
|
||||||
|
|||||||
Reference in New Issue
Block a user