Added ability to open, close and render fonts

This commit is contained in:
abdelrahman committed 2022-09-25 15:58:54 +01:00
1 parent e3f4f9c94f
commit dbc8c88b89
6 files changed
+111 -9

No files matched your search

+29
View File
@@ -4,6 +4,7 @@
#include "SDL_image.h"
#include "SDL_mixer.h"
#include "SDL_render.h"
#include "SDL_ttf.h"
#include "constants.h"
#include "reusable_ids.h"
#include <stdbool.h>
@@ -20,6 +21,7 @@ typedef struct asset {
SDL_Texture *texture;
Mix_Chunk *chunk;
Mix_Music *music;
TTF_Font *font;
} data;
} Asset;
@@ -217,6 +219,14 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
loaded = false;
}
break;
case ASSET_TYPE_FONT:
asset.data.font = TTF_OpenFont(path, 32);
if (!(asset.data.font)) {
loaded = false;
}
break;
default:
loaded = false;
@@ -259,6 +269,9 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
case ASSET_TYPE_MUSIC:
asset.data.music = NULL;
break;
case ASSET_TYPE_FONT:
asset.data.font = NULL;
break;
default:
break;
}
@@ -300,6 +313,10 @@ void remove_asset_from_manager(AssetManager *am, AssetID *id_ptr) {
Mix_FreeMusic(am->assets[id].data.music);
am->assets[id].data.music = NULL;
break;
case ASSET_TYPE_FONT:
TTF_CloseFont(am->assets[id].data.font);
am->assets[id].data.font = NULL;
break;
default:
break;
}
@@ -317,6 +334,14 @@ SDL_Texture *get_texture(AssetManager *am, AssetID id) {
return am->assets[id].data.texture;
}
TTF_Font *get_font(AssetManager *am, AssetID id) {
if (id < 0 || am->assets[id].type != ASSET_TYPE_FONT) {
return NULL;
}
return am->assets[id].data.font;
}
Mix_Chunk *get_audio_chunk(AssetManager *am, AssetID id) {
if (id < 0 || am->assets[id].type != ASSET_TYPE_AUDIO_FX) {
return NULL;
@@ -360,6 +385,10 @@ void destroy_asset_manager(AssetManager **am) {
Mix_FreeMusic((*am)->assets[i].data.music);
(*am)->assets[i].data.music = NULL;
break;
case ASSET_TYPE_FONT:
TTF_CloseFont((*am)->assets[i].data.font);
(*am)->assets[i].data.font = NULL;
break;
default:
break;
}
+54 -5
View File
@@ -9,7 +9,9 @@
#include "SDL_mixer.h"
#include "SDL_pixels.h"
#include "SDL_render.h"
#include "SDL_surface.h"
#include "SDL_timer.h"
#include "SDL_ttf.h"
#include "Window.h"
#include "constants.h"
#include "engine_build_config.h"
@@ -33,9 +35,9 @@ INTERNAL struct game_engine {
AssetManager *asset_manager;
} GameEngine;
bool init_game(const char *game_name, uint32_t window_width,
uint32_t window_height, SDL_Color window_background_color,
bool full_screen, uint8_t audio_tracks) {
bool init_engine(const char *game_name, uint32_t window_width,
uint32_t window_height, SDL_Color window_background_color,
bool full_screen, uint8_t audio_tracks) {
bool initialisation_complete = true;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
@@ -54,6 +56,13 @@ bool init_game(const char *game_name, uint32_t window_width,
initialisation_complete = false;
}
if (TTF_Init() < 0) {
#ifdef DEBUG_ENGINE
printf("Failed to initialise SDL_ttf. Error: %s\n", TTF_GetError());
#endif /* DEBUG_ENGINE */
initialisation_complete = false;
}
// We're only interested in initialising SDL_mixer with WAVE files, so no
// need to pass any flags
if (Mix_Init(0) != 0) {
@@ -99,7 +108,7 @@ bool init_game(const char *game_name, uint32_t window_width,
}
if (!initialisation_complete) {
close_game();
close_engine();
return false;
}
@@ -157,6 +166,44 @@ void render_texture_asset(AssetID id, float x, float y, float w, float h) {
render_texture(GameEngine.window.renderer, texture, x, y, w, h);
}
void render_text_with_font_asset(AssetID id, const char *text, float x,
float y) {
if (id < 0) {
return;
}
TTF_Font *font = get_font(GameEngine.asset_manager, id);
if (!font) {
return;
}
SDL_Surface *surface =
TTF_RenderText_Solid(font, text, (SDL_Color){0, 0, 0, 255});
// Get the width and height, in pixels, of the text rendered using the font
int32_t w = 0;
int32_t h = 0;
TTF_SizeText(font, text, &w, &h);
render_surface(GameEngine.window.renderer, surface, x, y, w, h);
SDL_FreeSurface(surface);
surface = NULL;
}
void set_font_asset_size(AssetID id, uint32_t size) {
if (id < 0) {
return;
}
TTF_Font *font = get_font(GameEngine.asset_manager, id);
if (!font) {
return;
}
TTF_SetFontSize(font, size);
}
int8_t play_audio_asset(AssetID id, int16_t loops, bool fade_in,
uint32_t fade_length_in_ms) {
if (id < 0) {
@@ -530,7 +577,7 @@ void render() {
swap_render_buffers(&(GameEngine.window));
}
void close_game() {
void close_engine() {
destroy_asset_manager(&(GameEngine.asset_manager));
destroy_object_store(&(GameEngine.object_store));
@@ -547,6 +594,8 @@ void close_game() {
Mix_Quit();
TTF_Quit();
IMG_Quit();
SDL_Quit();
+15
View File
@@ -43,6 +43,21 @@ void render_clear(const Renderer *renderer, SDL_Color color) {
SDL_RenderClear(renderer->SDL_base_renderer);
}
void render_surface(const Renderer *renderer, SDL_Surface *surface, float x,
float y, float w, float h) {
SDL_Texture *texture =
SDL_CreateTextureFromSurface(renderer->SDL_base_renderer, surface);
if (!texture) {
return;
}
render_texture(renderer, texture, x, y, w, h);
SDL_DestroyTexture(texture);
texture = NULL;
}
void render_texture(const Renderer *renderer, SDL_Texture *texture, float x,
float y, float w, float h) {
if (!renderer || !texture) {