Added ability to open, close and render fonts
This commit is contained in:
1 parent
e3f4f9c94f
commit
dbc8c88b89
6 files changed
+107
-5
No files matched your search
@@ -4,6 +4,7 @@
|
|||||||
#include "Renderer.h"
|
#include "Renderer.h"
|
||||||
#include "SDL_mixer.h"
|
#include "SDL_mixer.h"
|
||||||
#include "SDL_render.h"
|
#include "SDL_render.h"
|
||||||
|
#include "SDL_ttf.h"
|
||||||
#include "reusable_ids.h"
|
#include "reusable_ids.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
@@ -15,6 +16,7 @@ typedef enum {
|
|||||||
ASSET_TYPE_IMAGE,
|
ASSET_TYPE_IMAGE,
|
||||||
ASSET_TYPE_AUDIO_FX,
|
ASSET_TYPE_AUDIO_FX,
|
||||||
ASSET_TYPE_MUSIC,
|
ASSET_TYPE_MUSIC,
|
||||||
|
ASSET_TYPE_FONT,
|
||||||
|
|
||||||
ASSET_TYPE_COUNT
|
ASSET_TYPE_COUNT
|
||||||
} AssetType;
|
} AssetType;
|
||||||
@@ -25,6 +27,7 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
|
|||||||
AssetType type, Renderer *renderer);
|
AssetType type, Renderer *renderer);
|
||||||
void remove_asset_from_manager(AssetManager *am, AssetID *id_ptr);
|
void remove_asset_from_manager(AssetManager *am, AssetID *id_ptr);
|
||||||
SDL_Texture *get_texture(AssetManager *am, AssetID id);
|
SDL_Texture *get_texture(AssetManager *am, AssetID id);
|
||||||
|
TTF_Font *get_font(AssetManager *am, AssetID id);
|
||||||
Mix_Chunk *get_audio_chunk(AssetManager *am, AssetID id);
|
Mix_Chunk *get_audio_chunk(AssetManager *am, AssetID id);
|
||||||
Mix_Music *get_music(AssetManager *am, AssetID id);
|
Mix_Music *get_music(AssetManager *am, AssetID id);
|
||||||
void destroy_asset_manager(AssetManager **am);
|
void destroy_asset_manager(AssetManager **am);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif // !__cplusplus
|
#endif // !__cplusplus
|
||||||
|
|
||||||
bool init_game(const char *game_name, uint32_t window_width,
|
bool init_engine(const char *game_name, uint32_t window_width,
|
||||||
uint32_t window_height, SDL_Color window_background_color,
|
uint32_t window_height, SDL_Color window_background_color,
|
||||||
bool full_screen, uint8_t audio_tracks);
|
bool full_screen, uint8_t audio_tracks);
|
||||||
bool game_running();
|
bool game_running();
|
||||||
@@ -21,6 +21,9 @@ ObjectID add_object(void *object, EventHandlerPtr eh, UpdateFuncPtr uf,
|
|||||||
void remove_object(ObjectID *id);
|
void remove_object(ObjectID *id);
|
||||||
AssetID load_asset(const char *path, AssetType type);
|
AssetID load_asset(const char *path, AssetType type);
|
||||||
void render_texture_asset(AssetID id, float x, float y, float w, float h);
|
void render_texture_asset(AssetID id, float x, float y, float w, float h);
|
||||||
|
void render_text_with_font_asset(AssetID id, const char *text, float x,
|
||||||
|
float y);
|
||||||
|
void set_font_asset_size(AssetID id, uint32_t size);
|
||||||
int8_t play_audio_asset(AssetID id, int16_t loops, bool fade_in,
|
int8_t play_audio_asset(AssetID id, int16_t loops, bool fade_in,
|
||||||
uint32_t fade_length_in_ms);
|
uint32_t fade_length_in_ms);
|
||||||
void pause_audio_asset_on_track(AssetID id, uint8_t track);
|
void pause_audio_asset_on_track(AssetID id, uint8_t track);
|
||||||
@@ -54,7 +57,7 @@ void close_asset(AssetID *id);
|
|||||||
void handle_events();
|
void handle_events();
|
||||||
void update();
|
void update();
|
||||||
void render();
|
void render();
|
||||||
void close_game();
|
void close_engine();
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
#include "SDL_render.h"
|
#include "SDL_render.h"
|
||||||
|
#include "SDL_surface.h"
|
||||||
#include "SDL_video.h"
|
#include "SDL_video.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
@@ -16,6 +17,8 @@ Renderer *create_renderer();
|
|||||||
bool init_renderer(Renderer *renderer, SDL_Window *window);
|
bool init_renderer(Renderer *renderer, SDL_Window *window);
|
||||||
SDL_Texture *load_texture(const Renderer *renderer, const char *path);
|
SDL_Texture *load_texture(const Renderer *renderer, const char *path);
|
||||||
void render_clear(const Renderer *renderer, SDL_Color color);
|
void render_clear(const Renderer *renderer, SDL_Color color);
|
||||||
|
void render_surface(const Renderer *renderer, SDL_Surface *surface, float x,
|
||||||
|
float y, float w, float h);
|
||||||
void render_texture(const Renderer *renderer, SDL_Texture *texture, float x,
|
void render_texture(const Renderer *renderer, SDL_Texture *texture, float x,
|
||||||
float y, float w, float h);
|
float y, float w, float h);
|
||||||
void swap_buffers(const Renderer *renderer);
|
void swap_buffers(const Renderer *renderer);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "SDL_image.h"
|
#include "SDL_image.h"
|
||||||
#include "SDL_mixer.h"
|
#include "SDL_mixer.h"
|
||||||
#include "SDL_render.h"
|
#include "SDL_render.h"
|
||||||
|
#include "SDL_ttf.h"
|
||||||
#include "constants.h"
|
#include "constants.h"
|
||||||
#include "reusable_ids.h"
|
#include "reusable_ids.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@@ -20,6 +21,7 @@ typedef struct asset {
|
|||||||
SDL_Texture *texture;
|
SDL_Texture *texture;
|
||||||
Mix_Chunk *chunk;
|
Mix_Chunk *chunk;
|
||||||
Mix_Music *music;
|
Mix_Music *music;
|
||||||
|
TTF_Font *font;
|
||||||
} data;
|
} data;
|
||||||
} Asset;
|
} Asset;
|
||||||
|
|
||||||
@@ -217,6 +219,14 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
|
|||||||
loaded = false;
|
loaded = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case ASSET_TYPE_FONT:
|
||||||
|
asset.data.font = TTF_OpenFont(path, 32);
|
||||||
|
|
||||||
|
if (!(asset.data.font)) {
|
||||||
|
loaded = false;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
loaded = false;
|
loaded = false;
|
||||||
@@ -259,6 +269,9 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
|
|||||||
case ASSET_TYPE_MUSIC:
|
case ASSET_TYPE_MUSIC:
|
||||||
asset.data.music = NULL;
|
asset.data.music = NULL;
|
||||||
break;
|
break;
|
||||||
|
case ASSET_TYPE_FONT:
|
||||||
|
asset.data.font = NULL;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -300,6 +313,10 @@ void remove_asset_from_manager(AssetManager *am, AssetID *id_ptr) {
|
|||||||
Mix_FreeMusic(am->assets[id].data.music);
|
Mix_FreeMusic(am->assets[id].data.music);
|
||||||
am->assets[id].data.music = NULL;
|
am->assets[id].data.music = NULL;
|
||||||
break;
|
break;
|
||||||
|
case ASSET_TYPE_FONT:
|
||||||
|
TTF_CloseFont(am->assets[id].data.font);
|
||||||
|
am->assets[id].data.font = NULL;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -317,6 +334,14 @@ SDL_Texture *get_texture(AssetManager *am, AssetID id) {
|
|||||||
return am->assets[id].data.texture;
|
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) {
|
Mix_Chunk *get_audio_chunk(AssetManager *am, AssetID id) {
|
||||||
if (id < 0 || am->assets[id].type != ASSET_TYPE_AUDIO_FX) {
|
if (id < 0 || am->assets[id].type != ASSET_TYPE_AUDIO_FX) {
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -360,6 +385,10 @@ void destroy_asset_manager(AssetManager **am) {
|
|||||||
Mix_FreeMusic((*am)->assets[i].data.music);
|
Mix_FreeMusic((*am)->assets[i].data.music);
|
||||||
(*am)->assets[i].data.music = NULL;
|
(*am)->assets[i].data.music = NULL;
|
||||||
break;
|
break;
|
||||||
|
case ASSET_TYPE_FONT:
|
||||||
|
TTF_CloseFont((*am)->assets[i].data.font);
|
||||||
|
(*am)->assets[i].data.font = NULL;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
+52
-3
@@ -9,7 +9,9 @@
|
|||||||
#include "SDL_mixer.h"
|
#include "SDL_mixer.h"
|
||||||
#include "SDL_pixels.h"
|
#include "SDL_pixels.h"
|
||||||
#include "SDL_render.h"
|
#include "SDL_render.h"
|
||||||
|
#include "SDL_surface.h"
|
||||||
#include "SDL_timer.h"
|
#include "SDL_timer.h"
|
||||||
|
#include "SDL_ttf.h"
|
||||||
#include "Window.h"
|
#include "Window.h"
|
||||||
#include "constants.h"
|
#include "constants.h"
|
||||||
#include "engine_build_config.h"
|
#include "engine_build_config.h"
|
||||||
@@ -33,7 +35,7 @@ INTERNAL struct game_engine {
|
|||||||
AssetManager *asset_manager;
|
AssetManager *asset_manager;
|
||||||
} GameEngine;
|
} GameEngine;
|
||||||
|
|
||||||
bool init_game(const char *game_name, uint32_t window_width,
|
bool init_engine(const char *game_name, uint32_t window_width,
|
||||||
uint32_t window_height, SDL_Color window_background_color,
|
uint32_t window_height, SDL_Color window_background_color,
|
||||||
bool full_screen, uint8_t audio_tracks) {
|
bool full_screen, uint8_t audio_tracks) {
|
||||||
bool initialisation_complete = true;
|
bool initialisation_complete = true;
|
||||||
@@ -54,6 +56,13 @@ bool init_game(const char *game_name, uint32_t window_width,
|
|||||||
initialisation_complete = false;
|
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
|
// We're only interested in initialising SDL_mixer with WAVE files, so no
|
||||||
// need to pass any flags
|
// need to pass any flags
|
||||||
if (Mix_Init(0) != 0) {
|
if (Mix_Init(0) != 0) {
|
||||||
@@ -99,7 +108,7 @@ bool init_game(const char *game_name, uint32_t window_width,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!initialisation_complete) {
|
if (!initialisation_complete) {
|
||||||
close_game();
|
close_engine();
|
||||||
|
|
||||||
return false;
|
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);
|
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,
|
int8_t play_audio_asset(AssetID id, int16_t loops, bool fade_in,
|
||||||
uint32_t fade_length_in_ms) {
|
uint32_t fade_length_in_ms) {
|
||||||
if (id < 0) {
|
if (id < 0) {
|
||||||
@@ -530,7 +577,7 @@ void render() {
|
|||||||
swap_render_buffers(&(GameEngine.window));
|
swap_render_buffers(&(GameEngine.window));
|
||||||
}
|
}
|
||||||
|
|
||||||
void close_game() {
|
void close_engine() {
|
||||||
destroy_asset_manager(&(GameEngine.asset_manager));
|
destroy_asset_manager(&(GameEngine.asset_manager));
|
||||||
|
|
||||||
destroy_object_store(&(GameEngine.object_store));
|
destroy_object_store(&(GameEngine.object_store));
|
||||||
@@ -547,6 +594,8 @@ void close_game() {
|
|||||||
|
|
||||||
Mix_Quit();
|
Mix_Quit();
|
||||||
|
|
||||||
|
TTF_Quit();
|
||||||
|
|
||||||
IMG_Quit();
|
IMG_Quit();
|
||||||
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
|||||||
@@ -43,6 +43,21 @@ void render_clear(const Renderer *renderer, SDL_Color color) {
|
|||||||
SDL_RenderClear(renderer->SDL_base_renderer);
|
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,
|
void render_texture(const Renderer *renderer, SDL_Texture *texture, float x,
|
||||||
float y, float w, float h) {
|
float y, float w, float h) {
|
||||||
if (!renderer || !texture) {
|
if (!renderer || !texture) {
|
||||||
|
|||||||
Reference in new issue
Block a user