Mercurial > hg > dmlib
comparison 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 |
comparison
equal
deleted
inserted
replaced
1956:6147dd3fa5d2 | 1957:ef08af6887b7 |
---|---|
22 */ | 22 */ |
23 #ifdef DM_GFX_BM_TEXT | 23 #ifdef DM_GFX_BM_TEXT |
24 | 24 |
25 // DMFONT format constants | 25 // DMFONT format constants |
26 #define DMFONT_MAGIC "DMFONT" | 26 #define DMFONT_MAGIC "DMFONT" |
27 #define DMFONT_VERSION 0x0100 | 27 #define DMFONT_VERSION 0x0200 |
28 | 28 |
29 #define DMFONT_MIN_WIDTH 3 | 29 #define DMFONT_MIN_WIDTH 3 |
30 #define DMFONT_MIN_HEIGHT 3 | 30 #define DMFONT_MIN_HEIGHT 3 |
31 #define DMFONT_MAX_WIDTH 128 | 31 #define DMFONT_MAX_WIDTH 128 |
32 #define DMFONT_MAX_HEIGHT 128 | 32 #define DMFONT_MAX_HEIGHT 128 |
33 #define DMFONT_MAX_GLYPHS 1024 | 33 #define DMFONT_MAX_GLYPHS 1024 |
34 | |
35 #define DMFONT_NPALETTE 256 | 34 #define DMFONT_NPALETTE 256 |
36 | 35 |
37 // Legacy TSFONT loading support | 36 // Legacy TSFONT loading support |
38 #define TSFONT_MAGIC "TSFONT" | 37 #define TSFONT_MAGIC "TSFONT" |
39 #define TSFONT_VERSION 0x0205 | 38 #define TSFONT_VERSION 0x0205 |
40 | 39 |
41 | 40 |
42 typedef struct | 41 typedef struct |
43 { | 42 { |
44 int width, height; // Dimensions | 43 int width, height, // Dimensions of this glyph |
45 int nglyphs; // Size of glyphs array | 44 index; // Index to surface, value of "maxglyph" means unused |
46 SDL_Surface **glyphs; // NOTE! Not all glyphs may be allocated | 45 } DMBitmapGlyph; |
46 | |
47 | |
48 typedef struct | |
49 { | |
50 int width, height; // Dimensions | |
51 int nglyphs, // How many glyphs worth of data is allocated for the surface | |
52 maxglyph; // Number of character indices, e.g. typically 256 | |
53 size_t gsize; // Size of one glyph in bytes, e.g. glyphs->pitch * font->height | |
54 | |
55 DMBitmapGlyph *glyphMap; | |
56 SDL_Surface *glyphs; // Surface containing glyphs | |
47 } DMBitmapFont; | 57 } DMBitmapFont; |
48 | 58 |
49 | 59 |
50 DMBitmapFont *dmNewBitmapFont(const int nglyphs, const int width, const int height); | 60 DMBitmapFont *dmNewBitmapFont(const int nglyphs, const int maxglyph, const int width, const int height, const int bpp); |
51 int dmFreeBitmapFont(DMBitmapFont *font); | 61 int dmFreeBitmapFont(DMBitmapFont *font); |
52 int dmLoadBitmapFont(DMResource *res, DMBitmapFont **pfont); | 62 int dmLoadBitmapFont(DMResource *res, DMBitmapFont **pfont); |
53 int dmSetBitmapFontPalette(DMBitmapFont *font, const SDL_Color *pal, const int start, const int size); | 63 int dmSetBitmapFontPalette(DMBitmapFont *font, const SDL_Color *pal, const int start, const int size); |
54 | 64 |
55 | 65 |
56 void dmDrawBMTextConst(SDL_Surface *screen, DMBitmapFont *font, BOOL condensed, int mode, int xc, int yc, const char *str); | 66 void dmDrawBMTextConst(SDL_Surface *screen, const DMBitmapFont *font, |
57 void dmDrawBMTextVA(SDL_Surface *screen, DMBitmapFont *font, BOOL condensed, int mode, int xc, int yc, const char *fmt, va_list ap); | 67 const BOOL condensed, const int mode, int xc, int yc, const char *str); |
58 void dmDrawBMText(SDL_Surface *screen, DMBitmapFont *font, BOOL condensed, int mode, int xc, int yc, const char *fmt, ...); | 68 void dmDrawBMTextVA(SDL_Surface *screen, const DMBitmapFont *font, |
69 const BOOL condensed, const int mode, const int xc, const int yc, const char *fmt, va_list ap); | |
70 void dmDrawBMText(SDL_Surface *screen, const DMBitmapFont *font, | |
71 const BOOL condensed, const int mode, const int xc, const int yc, const char *fmt, ...); | |
59 | 72 |
60 | 73 |
61 static inline SDL_Surface *dmGetBMGlyph(DMBitmapFont *font, int ch) | 74 static inline void dmInitializeGetBMGlyphSurface(SDL_Surface *surf, DMBitmapFont *font) |
62 { | 75 { |
76 memcpy(surf, font->glyphs, sizeof(SDL_Surface)); | |
77 } | |
78 | |
79 static inline void dmGetBMGlyph(SDL_Surface *surf, DMBitmapFont *font, int ch) | |
80 { | |
81 DMBitmapGlyph *glyph; | |
63 if (ch < 0 || ch >= font->nglyphs || ch == '\n' || ch == '\r') | 82 if (ch < 0 || ch >= font->nglyphs || ch == '\n' || ch == '\r') |
64 ch = 32; | 83 ch = 32; |
65 | 84 |
66 return font->glyphs[ch]; | 85 glyph = &font->glyphMap[ch]; |
86 surf->w = glyph->width; | |
87 surf->h = glyph->height; | |
88 surf->pixels = font->glyphs->pixels + font->gsize * glyph->index; | |
67 } | 89 } |
68 #endif | 90 #endif |
69 | 91 |
70 | 92 |
71 /* TTF text drawing | 93 /* TTF text drawing |