shithub: cstory

Download patch

ref: aa6174b3f21eadb32eb227bb595e74bc3efc8983
parent: 5da3b72fca6a541fdb67153558b5a2ff5d8c83bd
author: Clownacy <Clownacy@users.noreply.github.com>
date: Tue Aug 13 14:32:44 EDT 2019

Glyphs now regenerate with the rest of the game's textures

God-damn this code is ugly

--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -530,6 +530,8 @@
 			}
 		}
 	}
+
+	RestoreGlyphs(gFont);
 }
 
 #ifdef WINDOWS
--- a/src/Font.cpp
+++ b/src/Font.cpp
@@ -955,6 +955,51 @@
 	return lookup[value];
 }
 
+static void SendGlyphToBackend(FontObject *font_object, Backend_Glyph *backend)
+{
+	if (backend == NULL)
+		return;
+
+	FT_Bitmap bitmap;
+	FT_Bitmap_New(&bitmap);
+	FT_Bitmap_Convert(font_object->library, &font_object->face->glyph->bitmap, &bitmap, 1);
+
+	switch (font_object->face->glyph->bitmap.pixel_mode)
+	{
+		case FT_PIXEL_MODE_LCD:
+			for (unsigned int y = 0; y < bitmap.rows; ++y)
+			{
+				unsigned char *pixel_pointer = bitmap.buffer + y * bitmap.pitch;
+
+				for (unsigned int x = 0; x < bitmap.width; ++x)
+				{
+					*pixel_pointer = GammaCorrect(*pixel_pointer);
+					++pixel_pointer;
+				}
+			}
+
+			break;
+
+		case FT_PIXEL_MODE_GRAY:
+			for (unsigned int y = 0; y < bitmap.rows; ++y)
+			{
+				unsigned char *pixel_pointer = bitmap.buffer + y * bitmap.pitch;
+
+				for (unsigned int x = 0; x < bitmap.width; ++x)
+				{
+					*pixel_pointer = GammaCorrect((*pixel_pointer * 0xFF) / (bitmap.num_grays - 1));
+					++pixel_pointer;
+				}
+			}
+
+			break;
+	}
+
+	Backend_LoadGlyphPixels(backend, bitmap.buffer, bitmap.pitch);
+
+	FT_Bitmap_Done(font_object->library, &bitmap);
+}
+
 static CachedGlyph* GetGlyphCached(FontObject *font_object, unsigned long unicode_value)
 {
 	CachedGlyph *glyph;
@@ -978,48 +1023,15 @@
 		FT_Load_Glyph(font_object->face, glyph_index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
 #endif
 
-		glyph->unicode_value = unicode_value;
-		glyph->x = font_object->face->glyph->bitmap_left;
-		glyph->y = (FT_MulFix(font_object->face->ascender, font_object->face->size->metrics.y_scale) - font_object->face->glyph->metrics.horiBearingY + (64 / 2)) / 64;
-		glyph->x_advance = font_object->face->glyph->advance.x / 64;
-
-		FT_Bitmap bitmap;
-		FT_Bitmap_New(&bitmap);
-		FT_Bitmap_Convert(font_object->library, &font_object->face->glyph->bitmap, &bitmap, 1);
-
 		unsigned char pixel_mode;
 		switch (font_object->face->glyph->bitmap.pixel_mode)
 		{
 			case FT_PIXEL_MODE_LCD:
 				pixel_mode = FONT_PIXEL_MODE_LCD;
-
-				for (unsigned int y = 0; y < bitmap.rows; ++y)
-				{
-					unsigned char *pixel_pointer = bitmap.buffer + y * bitmap.pitch;
-
-					for (unsigned int x = 0; x < bitmap.width; ++x)
-					{
-						*pixel_pointer = GammaCorrect(*pixel_pointer);
-						++pixel_pointer;
-					}
-				}
-
 				break;
 
 			case FT_PIXEL_MODE_GRAY:
 				pixel_mode = FONT_PIXEL_MODE_GRAY;
-
-				for (unsigned int y = 0; y < bitmap.rows; ++y)
-				{
-					unsigned char *pixel_pointer = bitmap.buffer + y * bitmap.pitch;
-
-					for (unsigned int x = 0; x < bitmap.width; ++x)
-					{
-						*pixel_pointer = GammaCorrect((*pixel_pointer * 0xFF) / (bitmap.num_grays - 1));
-						++pixel_pointer;
-					}
-				}
-
 				break;
 
 			case FT_PIXEL_MODE_MONO:
@@ -1027,10 +1039,13 @@
 				break;
 		}
 
-		glyph->backend = Backend_CreateGlyph((pixel_mode == FT_PIXEL_MODE_LCD ? bitmap.width / 3 : bitmap.width), bitmap.rows, pixel_mode);
-		Backend_LoadGlyphPixels(glyph->backend, bitmap.buffer, bitmap.pitch);
+		glyph->backend = Backend_CreateGlyph((pixel_mode == FONT_PIXEL_MODE_LCD ? font_object->face->glyph->bitmap.width / 3 : font_object->face->glyph->bitmap.width), font_object->face->glyph->bitmap.rows, pixel_mode);
+		glyph->unicode_value = unicode_value;
+		glyph->x = font_object->face->glyph->bitmap_left;
+		glyph->y = (FT_MulFix(font_object->face->ascender, font_object->face->size->metrics.y_scale) - font_object->face->glyph->metrics.horiBearingY + (64 / 2)) / 64;
+		glyph->x_advance = font_object->face->glyph->advance.x / 64;
 
-		FT_Bitmap_Done(font_object->library, &bitmap);
+		SendGlyphToBackend(font_object, glyph->backend);
 	}
 
 	return glyph;
@@ -1173,5 +1188,21 @@
 		free(font_object->data);
 		FT_Done_FreeType(font_object->library);
 		free(font_object);
+	}
+}
+
+void RestoreGlyphs(FontObject *font_object)
+{
+	for (CachedGlyph *glyph = font_object->glyph_list_head; glyph != NULL; glyph = glyph->next)
+	{
+		unsigned int glyph_index = FT_Get_Char_Index(font_object->face, glyph->unicode_value);
+
+#ifndef DISABLE_FONT_ANTIALIASING
+		FT_Load_Glyph(font_object->face, glyph_index, FT_LOAD_RENDER | (font_object->lcd_mode ? FT_LOAD_TARGET_LCD : 0));
+#else
+		FT_Load_Glyph(font_object->face, glyph_index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
+#endif
+
+		SendGlyphToBackend(font_object, glyph->backend);
 	}
 }
--- a/src/Font.h
+++ b/src/Font.h
@@ -10,3 +10,4 @@
 FontObject* LoadFont(const char *font_filename, unsigned int cell_width, unsigned int cell_height);
 void DrawText(FontObject *font_object, Backend_Surface *surface, int x, int y, unsigned long colour, const char *string, size_t string_length);
 void UnloadFont(FontObject *font_object);
+void RestoreGlyphs(FontObject *font_object);