Restore the FreeType code

Now you can select either the FreeType fonts or the pre-rendered
fonts with CMake's 'FREETYPE_FONTS' option.
This commit is contained in:
Clownacy 2020-09-17 13:07:23 +01:00
parent a542a5c11d
commit 9973dddbbd
4 changed files with 92 additions and 79 deletions

View file

@ -22,6 +22,7 @@ option(FIX_MAJOR_BUGS "Fix bugs that invoke undefined behaviour or cause memory
option(DEBUG_SAVE "Re-enable the ability to drag-and-drop save files onto the window" OFF) option(DEBUG_SAVE "Re-enable the ability to drag-and-drop save files onto the window" OFF)
option(DOCONFIG "Compile a DoConfig clone tool - not useful for console ports" ON) option(DOCONFIG "Compile a DoConfig clone tool - not useful for console ports" ON)
option(LANCZOS_RESAMPLER "Use Lanczos filtering for audio resampling instead of linear-interpolation (Lanczos is more performance-intensive, but higher quality)" OFF) option(LANCZOS_RESAMPLER "Use Lanczos filtering for audio resampling instead of linear-interpolation (Lanczos is more performance-intensive, but higher quality)" OFF)
option(FREETYPE_FONTS "Use FreeType2 to render the DejaVu Mono (English) or Migu1M (Japanese) fonts, instead of using pre-rendered copies of Courier New (English) and MS Gothic (Japanese)" OFF)
set(BACKEND_RENDERER "SDLTexture" CACHE STRING "Which renderer the game should use: 'OpenGL3' for an OpenGL 3.2 renderer, 'OpenGLES2' for an OpenGL ES 2.0 renderer, 'SDLTexture' for SDL2's hardware-accelerated Texture API, 'SDLSurface' for SDL2's software-rendered Surface API, 'Wii U' for the Wii U's hardware-accelerated GX2 API, or 'Software' for a handwritten software renderer") set(BACKEND_RENDERER "SDLTexture" CACHE STRING "Which renderer the game should use: 'OpenGL3' for an OpenGL 3.2 renderer, 'OpenGLES2' for an OpenGL ES 2.0 renderer, 'SDLTexture' for SDL2's hardware-accelerated Texture API, 'SDLSurface' for SDL2's software-rendered Surface API, 'Wii U' for the Wii U's hardware-accelerated GX2 API, or 'Software' for a handwritten software renderer")
set(BACKEND_AUDIO "SDL2" CACHE STRING "Which audio backend the game should use: 'SDL2', 'miniaudio', 'WiiU-Hardware', 'WiiU-Software', or 'Null'") set(BACKEND_AUDIO "SDL2" CACHE STRING "Which audio backend the game should use: 'SDL2', 'miniaudio', 'WiiU-Hardware', 'WiiU-Software', or 'Null'")
@ -278,6 +279,10 @@ if(LANCZOS_RESAMPLER)
target_compile_definitions(CSE2 PRIVATE LANCZOS_RESAMPLER) target_compile_definitions(CSE2 PRIVATE LANCZOS_RESAMPLER)
endif() endif()
if(FREETYPE_FONTS)
target_compile_definitions(CSE2 PRIVATE FREETYPE_FONTS)
endif()
if(PKG_CONFIG_STATIC_LIBS) if(PKG_CONFIG_STATIC_LIBS)
target_link_options(CSE2 PRIVATE "-static") target_link_options(CSE2 PRIVATE "-static")
endif() endif()
@ -570,6 +575,7 @@ if(BACKEND_PLATFORM MATCHES "SDL2" OR BACKEND_AUDIO MATCHES "SDL2")
endif() endif()
endif() endif()
if(FREETYPE_FONTS)
if(NOT FORCE_LOCAL_LIBS) if(NOT FORCE_LOCAL_LIBS)
find_package(Freetype) find_package(Freetype)
@ -606,6 +612,7 @@ else()
add_subdirectory("external/freetype" EXCLUDE_FROM_ALL) add_subdirectory("external/freetype" EXCLUDE_FROM_ALL)
target_link_libraries(CSE2 PRIVATE freetype) target_link_libraries(CSE2 PRIVATE freetype)
endif() endif()
endif()
if(BACKEND_RENDERER MATCHES "OpenGL3") if(BACKEND_RENDERER MATCHES "OpenGL3")
add_subdirectory("external/glad" EXCLUDE_FROM_ALL) add_subdirectory("external/glad" EXCLUDE_FROM_ALL)

View file

@ -657,7 +657,8 @@ int RestoreSurfaces(void)
void InitTextObject(const char *name) void InitTextObject(const char *name)
{ {
(void)name; // Unused in this branch (void)name; // Unused in this branch
/*
#ifdef FREETYPE_FONTS
std::string path = gDataPath + "/Font/font"; std::string path = gDataPath + "/Font/font";
// Get font size // Get font size
@ -706,7 +707,7 @@ void InitTextObject(const char *name)
} }
font = LoadFont(path.c_str(), width, height); font = LoadFont(path.c_str(), width, height);
*/ #else
std::string bitmap_path; std::string bitmap_path;
std::string metadata_path; std::string metadata_path;
@ -724,6 +725,7 @@ void InitTextObject(const char *name)
} }
font = LoadBitmapFont(bitmap_path.c_str(), metadata_path.c_str()); font = LoadBitmapFont(bitmap_path.c_str(), metadata_path.c_str());
#endif
} }
void PutText(int x, int y, const char *text, unsigned long color) void PutText(int x, int y, const char *text, unsigned long color)

View file

@ -5,9 +5,11 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#ifdef FREETYPE_FONTS
#include <ft2build.h> #include <ft2build.h>
#include FT_FREETYPE_H #include FT_FREETYPE_H
#include FT_BITMAP_H #include FT_BITMAP_H
#endif
#include "Bitmap.h" #include "Bitmap.h"
#include "File.h" #include "File.h"
@ -46,6 +48,11 @@ typedef struct Glyph
typedef struct Font typedef struct Font
{ {
#ifdef FREETYPE_FONTS
FT_Library library;
FT_Face face;
unsigned char *data;
#else
unsigned char *image_buffer; unsigned char *image_buffer;
size_t image_buffer_width; size_t image_buffer_width;
size_t image_buffer_height; size_t image_buffer_height;
@ -53,11 +60,7 @@ typedef struct Font
size_t glyph_slot_height; size_t glyph_slot_height;
size_t total_local_glyphs; size_t total_local_glyphs;
Glyph *local_glyphs; Glyph *local_glyphs;
/* #endif
FT_Library library;
FT_Face face;
unsigned char *data;
*/
Glyph glyphs[TOTAL_GLYPH_SLOTS]; Glyph glyphs[TOTAL_GLYPH_SLOTS];
Glyph *glyph_list_head; Glyph *glyph_list_head;
RenderBackend_GlyphAtlas *atlas; RenderBackend_GlyphAtlas *atlas;
@ -999,34 +1002,7 @@ static Glyph* GetGlyph(Font *font, unsigned long unicode_value)
// Couldn't find glyph - overwrite the old at the end. // Couldn't find glyph - overwrite the old at the end.
// The one at the end hasn't been used in a while anyway. // The one at the end hasn't been used in a while anyway.
for (size_t i = 0; i < font->total_local_glyphs; ++i) #ifdef FREETYPE_FONTS
{
if (font->local_glyphs[i].unicode_value == unicode_value)
{
glyph->unicode_value = font->local_glyphs[i].unicode_value;
glyph->width = font->local_glyphs[i].width;
glyph->height = font->local_glyphs[i].height;
glyph->x_offset = font->local_glyphs[i].x_offset;
glyph->y_offset = font->local_glyphs[i].y_offset;
glyph->x_advance = font->local_glyphs[i].x_advance;
RenderBackend_UploadGlyph(font->atlas, glyph->x, glyph->y, &font->image_buffer[font->local_glyphs[i].y * font->image_buffer_width + font->local_glyphs[i].x], glyph->width, glyph->height, font->image_buffer_width);
*glyph_pointer = glyph->next;
glyph->next = font->glyph_list_head;
font->glyph_list_head = glyph;
return glyph;
}
}
/*
unsigned int glyph_index = FT_Get_Char_Index(font->face, unicode_value); unsigned int glyph_index = FT_Get_Char_Index(font->face, unicode_value);
#ifdef ENABLE_FONT_ANTIALIASING #ifdef ENABLE_FONT_ANTIALIASING
@ -1078,7 +1054,7 @@ static Glyph* GetGlyph(Font *font, unsigned long unicode_value)
glyph->y_offset = (font->face->size->metrics.ascender + (64 / 2)) / 64 - font->face->glyph->bitmap_top; glyph->y_offset = (font->face->size->metrics.ascender + (64 / 2)) / 64 - font->face->glyph->bitmap_top;
glyph->x_advance = font->face->glyph->advance.x / 64; glyph->x_advance = font->face->glyph->advance.x / 64;
RenderBackend_UploadGlyph(font->atlas, glyph->x, glyph->y, bitmap.buffer, glyph->width, glyph->height); RenderBackend_UploadGlyph(font->atlas, glyph->x, glyph->y, bitmap.buffer, glyph->width, glyph->height, glyph->width);
FT_Bitmap_Done(font->library, &bitmap); FT_Bitmap_Done(font->library, &bitmap);
@ -1091,12 +1067,33 @@ static Glyph* GetGlyph(Font *font, unsigned long unicode_value)
FT_Bitmap_Done(font->library, &bitmap); FT_Bitmap_Done(font->library, &bitmap);
} }
*/ #else
for (size_t i = 0; i < font->total_local_glyphs; ++i)
{
if (font->local_glyphs[i].unicode_value == unicode_value)
{
glyph->unicode_value = font->local_glyphs[i].unicode_value;
glyph->width = font->local_glyphs[i].width;
glyph->height = font->local_glyphs[i].height;
glyph->x_offset = font->local_glyphs[i].x_offset;
glyph->y_offset = font->local_glyphs[i].y_offset;
glyph->x_advance = font->local_glyphs[i].x_advance;
RenderBackend_UploadGlyph(font->atlas, glyph->x, glyph->y, &font->image_buffer[font->local_glyphs[i].y * font->image_buffer_width + font->local_glyphs[i].x], glyph->width, glyph->height, font->image_buffer_width);
*glyph_pointer = glyph->next;
glyph->next = font->glyph_list_head;
font->glyph_list_head = glyph;
return glyph;
}
}
#endif
return NULL; return NULL;
} }
/* #ifdef FREETYPE_FONTS
Font* LoadFontFromData(const unsigned char *data, size_t data_size, size_t cell_width, size_t cell_height) Font* LoadFontFromData(const unsigned char *data, size_t data_size, size_t cell_width, size_t cell_height)
{ {
Font *font = (Font*)malloc(sizeof(Font)); Font *font = (Font*)malloc(sizeof(Font));
@ -1173,8 +1170,7 @@ Font* LoadFont(const char *font_filename, size_t cell_width, size_t cell_height)
return font; return font;
} }
*/ #else
Font* LoadBitmapFont(const char *bitmap_path, const char *metadata_path) Font* LoadBitmapFont(const char *bitmap_path, const char *metadata_path)
{ {
size_t bitmap_width, bitmap_height; size_t bitmap_width, bitmap_height;
@ -1261,6 +1257,7 @@ Font* LoadBitmapFont(const char *bitmap_path, const char *metadata_path)
return NULL; return NULL;
} }
#endif
void DrawText(Font *font, RenderBackend_Surface *surface, int x, int y, unsigned long colour, const char *string) void DrawText(Font *font, RenderBackend_Surface *surface, int x, int y, unsigned long colour, const char *string)
{ {
@ -1303,12 +1300,15 @@ void UnloadFont(Font *font)
if (font != NULL) if (font != NULL)
{ {
RenderBackend_DestroyGlyphAtlas(font->atlas); RenderBackend_DestroyGlyphAtlas(font->atlas);
#ifdef FREETYPE_FONTS
FT_Done_Face(font->face);
free(font->data);
FT_Done_FreeType(font->library);
#else
free(font->local_glyphs); free(font->local_glyphs);
FreeBitmap(font->image_buffer); FreeBitmap(font->image_buffer);
#endif
// FT_Done_Face(font->face);
// free(font->data);
// FT_Done_FreeType(font->library);
free(font); free(font);
} }

View file

@ -6,8 +6,12 @@
typedef struct Font Font; typedef struct Font Font;
//Font* LoadFontFromData(const unsigned char *data, size_t data_size, size_t cell_width, size_t cell_height); #ifdef FREETYPE_FONTS
//Font* LoadFont(const char *font_filename, size_t cell_width, size_t cell_height); Font* LoadFontFromData(const unsigned char *data, size_t data_size, size_t cell_width, size_t cell_height);
Font* LoadFont(const char *font_filename, size_t cell_width, size_t cell_height);
#else
Font* LoadBitmapFont(const char *bitmap_path, const char *metadata_path); Font* LoadBitmapFont(const char *bitmap_path, const char *metadata_path);
#endif
void DrawText(Font *font, RenderBackend_Surface *surface, int x, int y, unsigned long colour, const char *string); void DrawText(Font *font, RenderBackend_Surface *surface, int x, int y, unsigned long colour, const char *string);
void UnloadFont(Font *font); void UnloadFont(Font *font);