diff src/dmtext.h @ 1957:ef08af6887b7

Revamp the bitmap font system to use single SDL_Surface for the font graphics instead of N surfaces for each separate glyph.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 30 Jun 2018 05:24:12 +0300
parents a3983da9b8b9
children 1edf494226c6
line wrap: on
line diff
--- a/src/dmtext.h	Sat Jun 30 05:22:15 2018 +0300
+++ b/src/dmtext.h	Sat Jun 30 05:24:12 2018 +0300
@@ -24,14 +24,13 @@
 
 // DMFONT format constants
 #define DMFONT_MAGIC         "DMFONT"
-#define DMFONT_VERSION       0x0100
+#define DMFONT_VERSION       0x0200
 
 #define DMFONT_MIN_WIDTH     3
 #define DMFONT_MIN_HEIGHT    3
 #define DMFONT_MAX_WIDTH     128
 #define DMFONT_MAX_HEIGHT    128
 #define DMFONT_MAX_GLYPHS    1024
-
 #define DMFONT_NPALETTE      256
 
 // Legacy TSFONT loading support
@@ -41,29 +40,52 @@
 
 typedef struct
 {
-    int width, height;     // Dimensions
-    int nglyphs;           // Size of glyphs array
-    SDL_Surface **glyphs;  // NOTE! Not all glyphs may be allocated
+    int width, height,       // Dimensions of this glyph
+        index;               // Index to surface, value of "maxglyph" means unused
+} DMBitmapGlyph;
+
+
+typedef struct
+{
+    int width, height;       // Dimensions
+    int nglyphs,             // How many glyphs worth of data is allocated for the surface
+        maxglyph;            // Number of character indices, e.g. typically 256
+    size_t gsize;            // Size of one glyph in bytes, e.g. glyphs->pitch * font->height
+
+    DMBitmapGlyph *glyphMap;
+    SDL_Surface *glyphs;     // Surface containing glyphs
 } DMBitmapFont;
 
 
-DMBitmapFont *dmNewBitmapFont(const int nglyphs, const int width, const int height);
+DMBitmapFont *dmNewBitmapFont(const int nglyphs, const int maxglyph, const int width, const int height, const int bpp);
 int dmFreeBitmapFont(DMBitmapFont *font);
 int dmLoadBitmapFont(DMResource *res, DMBitmapFont **pfont);
 int dmSetBitmapFontPalette(DMBitmapFont *font, const SDL_Color *pal, const int start, const int size);
 
 
-void dmDrawBMTextConst(SDL_Surface *screen, DMBitmapFont *font, BOOL condensed, int mode, int xc, int yc, const char *str);
-void dmDrawBMTextVA(SDL_Surface *screen, DMBitmapFont *font, BOOL condensed, int mode, int xc, int yc, const char *fmt, va_list ap);
-void dmDrawBMText(SDL_Surface *screen, DMBitmapFont *font, BOOL condensed, int mode, int xc, int yc, const char *fmt, ...);
+void dmDrawBMTextConst(SDL_Surface *screen, const DMBitmapFont *font,
+    const BOOL condensed, const int mode, int xc, int yc, const char *str);
+void dmDrawBMTextVA(SDL_Surface *screen, const DMBitmapFont *font,
+    const BOOL condensed, const int mode, const int xc, const int yc, const char *fmt, va_list ap);
+void dmDrawBMText(SDL_Surface *screen, const DMBitmapFont *font,
+    const BOOL condensed, const int mode, const int xc, const int yc, const char *fmt, ...);
 
 
-static inline SDL_Surface *dmGetBMGlyph(DMBitmapFont *font, int ch)
+static inline void dmInitializeGetBMGlyphSurface(SDL_Surface *surf, DMBitmapFont *font)
 {
+    memcpy(surf, font->glyphs, sizeof(SDL_Surface));
+}
+
+static inline void dmGetBMGlyph(SDL_Surface *surf, DMBitmapFont *font, int ch)
+{
+    DMBitmapGlyph *glyph;
     if (ch < 0 || ch >= font->nglyphs || ch == '\n' || ch == '\r')
         ch = 32;
 
-    return font->glyphs[ch];
+    glyph = &font->glyphMap[ch];
+    surf->w = glyph->width;
+    surf->h = glyph->height;
+    surf->pixels = font->glyphs->pixels + font->gsize * glyph->index;
 }
 #endif