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

+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();