Add basic implementation for bitmap font
This commit is contained in:
1 parent
918de7bce0
commit
7ec35a0b27
3 files changed
+120
-10
No files matched your search
+12
-10
@@ -27,6 +27,7 @@ add_library(${PROJECT_NAME} SHARED
|
|||||||
src/Renderer.c
|
src/Renderer.c
|
||||||
src/ObjectStore.c
|
src/ObjectStore.c
|
||||||
src/AssetManager.c
|
src/AssetManager.c
|
||||||
|
src/BitmapFont.c
|
||||||
src/reusable_ids.c
|
src/reusable_ids.c
|
||||||
src/math_utils.c
|
src/math_utils.c
|
||||||
)
|
)
|
||||||
@@ -47,16 +48,17 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
|
|||||||
)
|
)
|
||||||
|
|
||||||
install(FILES
|
install(FILES
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/engine_build_config.h
|
${CMAKE_CURRENT_BINARY_DIR}/engine_build_config.h
|
||||||
include/aliases.h
|
include/aliases.h
|
||||||
include/constants.h
|
include/constants.h
|
||||||
include/GameEngine.h
|
include/GameEngine.h
|
||||||
include/Window.h
|
include/Window.h
|
||||||
include/Renderer.h
|
include/Renderer.h
|
||||||
include/ObjectStore.h
|
include/ObjectStore.h
|
||||||
include/AssetManager.h
|
include/AssetManager.h
|
||||||
include/reusable_ids.h
|
include/BitmapFont.h
|
||||||
include/math_utils.h
|
include/reusable_ids.h
|
||||||
|
include/math_utils.h
|
||||||
CONFIGURATIONS Release
|
CONFIGURATIONS Release
|
||||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef BITMAP_FONT_H
|
||||||
|
#define BITMAP_FONT_H
|
||||||
|
|
||||||
|
#include "AssetManager.h"
|
||||||
|
#include "Renderer.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // !__cplusplus
|
||||||
|
|
||||||
|
typedef struct bitmap_font BitmapFont;
|
||||||
|
|
||||||
|
BitmapFont *load_bitmap_font(AssetManager *am, const Renderer *renderer,
|
||||||
|
const char *filepath, u64 tile_width,
|
||||||
|
u64 tile_height);
|
||||||
|
|
||||||
|
RenderRect get_character_sprite(BitmapFont *font, u64 index);
|
||||||
|
|
||||||
|
void unload_bitmap_font(AssetManager *am, BitmapFont **font);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif // !__cplusplus
|
||||||
|
|
||||||
|
#endif // !BITMAP_FONT_H
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
#include "BitmapFont.h"
|
||||||
|
#include "AssetManager.h"
|
||||||
|
#include "Renderer.h"
|
||||||
|
#include "SDL_render.h"
|
||||||
|
#include "aliases.h"
|
||||||
|
#include "reusable_ids.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct bitmap_font {
|
||||||
|
AssetID font_atlas;
|
||||||
|
u64 width;
|
||||||
|
u64 height;
|
||||||
|
u64 tile_width;
|
||||||
|
u64 tile_height;
|
||||||
|
u64 rows;
|
||||||
|
u64 cols;
|
||||||
|
};
|
||||||
|
|
||||||
|
BitmapFont *load_bitmap_font(AssetManager *am, const Renderer *renderer,
|
||||||
|
const char *filepath, u64 tile_width,
|
||||||
|
u64 tile_height) {
|
||||||
|
BitmapFont *font = (BitmapFont *)malloc(sizeof(BitmapFont));
|
||||||
|
if (!font) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
font->font_atlas =
|
||||||
|
load_asset_to_manager(am, filepath, ASSET_TYPE_IMAGE, renderer);
|
||||||
|
|
||||||
|
if (font->font_atlas == INVALID_ID) {
|
||||||
|
unload_bitmap_font(am, &font);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Texture *texture = get_texture(am, font->font_atlas);
|
||||||
|
|
||||||
|
i32 w;
|
||||||
|
i32 h;
|
||||||
|
|
||||||
|
if (SDL_QueryTexture(texture, NULL, NULL, &w, &h) != 0) {
|
||||||
|
unload_bitmap_font(am, &font);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
font->width = w;
|
||||||
|
font->height = h;
|
||||||
|
|
||||||
|
font->tile_width = tile_width;
|
||||||
|
font->tile_height = tile_height;
|
||||||
|
|
||||||
|
font->rows = font->height / font->tile_height;
|
||||||
|
font->cols = font->width / font->tile_width;
|
||||||
|
|
||||||
|
return font;
|
||||||
|
}
|
||||||
|
|
||||||
|
RenderRect get_character_sprite(BitmapFont *font, u64 index) {
|
||||||
|
u64 col = index % font->cols;
|
||||||
|
u64 row = index / font->cols;
|
||||||
|
|
||||||
|
RenderRect rect = {
|
||||||
|
.x = col * font->tile_width,
|
||||||
|
.y = row * font->tile_height,
|
||||||
|
.width = font->tile_width,
|
||||||
|
.height = font->tile_height,
|
||||||
|
};
|
||||||
|
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
void unload_bitmap_font(AssetManager *am, BitmapFont **font) {
|
||||||
|
if (!am || !(*font)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
remove_asset_from_manager(am, &((*font)->font_atlas));
|
||||||
|
|
||||||
|
free(*font);
|
||||||
|
|
||||||
|
*font = NULL;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user