annotate src/dmtext_bm.c @ 2294:7f6ba3b32f54

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 03 Jul 2019 10:28:43 +0300
parents 934cc71c97eb
children 92b93a12c014
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1 /*
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
2 * DMLib
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
3 * -- Bitmap and TTF text & font support
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
4 * Programmed and designed by Matti 'ccr' Hamalainen
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
5 * (C) Copyright 2012 Tecnic Software productions (TNSP)
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
6 */
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
7 #include "dmtext.h"
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
8
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
9
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
10 void dmDrawBMTextConst(SDL_Surface *screen, const DMBitmapFont *font,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
11 const BOOL condensed, const int mode, int xc, int yc, const char *fmt)
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
12 {
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
13 const char *ptr = fmt;
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
14 DMUnscaledBlitFunc blit = dmGetUnscaledBlitFunc(font->glyphs->format, screen->format, mode);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
15 SDL_Surface surf;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
16 Uint8 *orig = font->glyphs->pixels;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
17
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
18 memcpy(&surf, font->glyphs, sizeof(SDL_Surface));
70
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
19
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
20 while (*ptr)
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
21 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
22 unsigned char ch = *ptr++;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
23 if (ch < font->maxglyph)
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
24 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
25 DMBitmapGlyph *glyph = &font->glyphMap[ch];
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
26 if (glyph->index >= 0)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
27 {
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
28 surf.pixels = orig + glyph->index * font->gsize;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
29 surf.w = glyph->width;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
30 surf.h = glyph->height;
982
21aeff49d974 Oops. 100L fix to bitmapped text rendering.
Matti Hamalainen <ccr@tnsp.org>
parents: 875
diff changeset
31
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
32 blit(&surf, xc, yc, screen);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
33 xc += condensed ? glyph->width : font->width;
875
8aa0d640af74 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
34 }
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
35 else
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
36 xc += font->width;
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
37 }
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
38 else
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
39 xc += font->width;
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
40 }
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
41 }
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
42
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
43
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
44 void dmDrawBMTextVA(SDL_Surface *screen, const DMBitmapFont *font,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
45 const BOOL condensed, const int mode, const int xc, const int yc,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
46 const char *fmt, va_list ap)
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
47 {
200
b842cc92c787 No need for dynamic allocation here, I think.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
48 char tmp[512];
b842cc92c787 No need for dynamic allocation here, I think.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
49 vsnprintf(tmp, sizeof(tmp), fmt, ap);
634
656332eec724 Add condensed boolean flag to bitmap text rendering functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 200
diff changeset
50 dmDrawBMTextConst(screen, font, condensed, mode, xc, yc, tmp);
200
b842cc92c787 No need for dynamic allocation here, I think.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
51 dmFree(tmp);
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
52 }
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
53
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
54
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
55 void dmDrawBMText(SDL_Surface *screen, const DMBitmapFont *font,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
56 const BOOL condensed, const int mode, const int xc, const int yc,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
57 const char *fmt, ...)
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
58 {
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
59 va_list ap;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 982
diff changeset
60
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
61 va_start(ap, fmt);
634
656332eec724 Add condensed boolean flag to bitmap text rendering functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 200
diff changeset
62 dmDrawBMTextVA(screen, font, condensed, mode, xc, yc, fmt, ap);
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
63 va_end(ap);
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
64 }
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
65
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
66
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
67 DMBitmapFont *dmNewBitmapFont(const int nglyphs, const int maxglyph,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
68 const int width, const int height, const int bpp)
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
69 {
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
70 DMBitmapFont *font = dmMalloc0(sizeof(DMBitmapFont));
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
71 if (font == NULL || (bpp != 8 && bpp != 32))
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
72 return NULL;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 982
diff changeset
73
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
74 font->width = width;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
75 font->height = height;
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
76 font->nglyphs = nglyphs;
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
77 font->maxglyph = maxglyph;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
78
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
79 if ((font->glyphMap = dmCalloc(font->maxglyph, sizeof(DMBitmapGlyph))) == NULL)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
80 goto error;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
81
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
82 if ((font->glyphs = SDL_CreateRGBSurfaceWithFormat(
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
83 0, width, height * (nglyphs + 1), bpp,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
84 bpp == 8 ? SDL_PIXELFORMAT_INDEX8 : SDL_PIXELFORMAT_RGBA32)) == NULL)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
85 goto error;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
86
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
87 font->gsize = font->height * font->glyphs->pitch;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
88
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
89 for (int i = 0; i < font->maxglyph; i++)
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
90 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
91 DMBitmapGlyph *glyph = &font->glyphMap[i];
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
92 glyph->width = width;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
93 glyph->height = height;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
94 glyph->index = -1; // means that this index is empty/unused
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
95 }
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
96
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
97 return font;
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
98
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
99 error:
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
100 dmFreeBitmapFont(font);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
101 return NULL;
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
102 }
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
103
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
104
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
105 int dmFreeBitmapFont(DMBitmapFont *font)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
106 {
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
107 if (font == NULL)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
108 return DMERR_NULLPTR;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 982
diff changeset
109
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
110 if (font->glyphs != NULL)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
111 SDL_FreeSurface(font->glyphs);
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
112
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
113 dmFree(font->glyphMap);
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
114 dmFree(font);
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
115 return DMERR_OK;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
116 }
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
117
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
118
100
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
119 /* Set the palette for each glyph. While the function allows you to
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
120 * specify 'start' and 'end' indices and palette array freely, you should
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
121 * typically use DMFONT_NPALETTE size palette starting at index 0.
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
122 */
1950
a3983da9b8b9 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 1557
diff changeset
123 int dmSetBitmapFontPalette(DMBitmapFont *font, const SDL_Color *pal, const int start, const int size)
100
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
124 {
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
125 if (font == NULL)
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
126 return DMERR_NULLPTR;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 982
diff changeset
127
102
751af3f0e3f2 Oops, 100L :(
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
128 if (start < 0 || size < 1)
100
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
129 return DMERR_INVALID_ARGS;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 982
diff changeset
130
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
131 SDL_SetPaletteColors(font->glyphs->format->palette, pal, start, size);
100
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
132
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
133 return DMERR_OK;
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
134 }
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
135
1950
a3983da9b8b9 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 1557
diff changeset
136
162
040e3bbce5ba Add some extra debugging stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
137 //#define FN_DEBUG
100
f16d102dbbac Add a function for setting bitmapped font palette.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
138
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
139
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
140 int dmLoadBitmapFont(DMResource *fp, DMBitmapFont **pfont)
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
141 {
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
142 DMBitmapFont *font;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
143 char magic[8];
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
144 Uint16 version, nglyphs, maxglyph;
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
145 Uint8 width, height, bpp;
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
146 BOOL tsfont = FALSE;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 982
diff changeset
147
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
148 // Check magic and version
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
149 if (!dmf_read_str(fp, (Uint8 *) &magic, 6) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
150 !dmf_read_le16(fp, &version))
1951
ebcb7713bb6a Add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 1950
diff changeset
151 return DMERR_FREAD;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 982
diff changeset
152
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
153 // Check if it is a legacy TSFONT file
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
154 if (memcmp(magic, TSFONT_MAGIC, 6) == 0)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
155 {
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
156 // Yep, we handle these a bit differently
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
157 int encoding = dmfgetc(fp);
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
158 tsfont = TRUE;
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
159
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
160 #ifdef FN_DEBUG
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
161 fprintf(stderr, "TSFONT v%d.%d (0x%04x), encoding=%d\n",
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
162 version >> 8, version & 0xff, version, encoding);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
163 #endif
132
b5569c84f00a Add support for loading ancient version of TSFONTs.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
164
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
165 if (version > TSFONT_VERSION || version < 0x0200)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
166 return DMERR_VERSION;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 982
diff changeset
167
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
168 // There were only two encodings, 0 = none and 1 = RLE
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
169 // of which RLE was never actually used ... derp.
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
170 if (encoding != 0)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
171 return DMERR_NOT_SUPPORTED;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
172 }
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
173 else
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
174 if (memcmp(magic, DMFONT_MAGIC, 6) == 0)
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
175 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
176 #ifdef FN_DEBUG
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
177 fprintf(stderr, "DMFONT v%d.%d (0x%04x)\n",
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
178 version >> 8, version & 0xff, version);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
179 #endif
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
180
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
181 if (version > DMFONT_VERSION)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
182 return DMERR_VERSION;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
183 }
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
184 else
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
185 return DMERR_INVALID;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 982
diff changeset
186
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
187 // Read other header data
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
188 if (tsfont)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
189 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
190 // TSFONT has number of glyphs and dimensions
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
191 Uint8 tmp, unused;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
192 if (!dmf_read_byte(fp, &tmp) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
193 !dmf_read_byte(fp, &width) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
194 !dmf_read_byte(fp, &height) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
195 !dmf_read_byte(fp, &unused))
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
196 return DMERR_FREAD;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 982
diff changeset
197
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
198 nglyphs = tmp;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
199 maxglyph = 256;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
200 bpp = 8;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 982
diff changeset
201
132
b5569c84f00a Add support for loading ancient version of TSFONTs.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
202 // Very old TSFONTs have some extra data that is not used
b5569c84f00a Add support for loading ancient version of TSFONTs.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
203 // .. can't actually even remember what it was for.
b5569c84f00a Add support for loading ancient version of TSFONTs.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
204 if (version == 0x0200)
b5569c84f00a Add support for loading ancient version of TSFONTs.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
205 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
206 for (int i = 0; i < 32; i++)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
207 {
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
208 if (!dmf_read_byte(fp, &unused))
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
209 return DMERR_FREAD;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
210 }
132
b5569c84f00a Add support for loading ancient version of TSFONTs.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
211 }
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
212 }
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
213 else
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
214 {
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
215 // DMFONT has Uint16 values for nglyphs and maxglyph, plus BPP
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
216 if (!dmf_read_le16(fp, &nglyphs) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
217 !dmf_read_le16(fp, &maxglyph) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
218 !dmf_read_byte(fp, &width) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
219 !dmf_read_byte(fp, &height) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
220 !dmf_read_byte(fp, &bpp))
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
221 return DMERR_FREAD;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
222 }
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
223
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
224 #ifdef FN_DEBUG
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
225 fprintf(stderr, "nglyphs=%d, maxglyph=%d, width=%d, height=%d, bpp=%d\n",
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
226 nglyphs, maxglyph, width, height, bpp);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
227 #endif
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
228
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
229 if (width < DMFONT_MIN_WIDTH ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
230 height < DMFONT_MIN_HEIGHT ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
231 width > DMFONT_MAX_WIDTH ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
232 height > DMFONT_MAX_HEIGHT ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
233 nglyphs > DMFONT_MAX_GLYPHS ||
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
234 nglyphs > maxglyph ||
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
235 maxglyph > DMFONT_MAX_GLYPHS ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
236 maxglyph < 1)
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
237 return DMERR_INVALID_DATA;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
238
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
239 if (bpp != 8 && bpp != 32)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
240 return DMERR_NOT_SUPPORTED;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
241
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
242 // Allocate font
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
243 if ((*pfont = font = dmNewBitmapFont(nglyphs, maxglyph, width, height, bpp)) == NULL)
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
244 return DMERR_MALLOC;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
245
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
246 // Setup palette for 8bpp fonts
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
247 if (bpp == 8)
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
248 {
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
249 SDL_Color pal[DMFONT_NPALETTE];
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
250 for (int n = 0; n < DMFONT_NPALETTE; n++)
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
251 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
252 pal[n].r = pal[n].g = pal[n].b = n > 0 ? 255 : 0;
1557
5e5f75b45f8d Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
253 pal[n].a = n > 0 ? 255 : 0;
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
254 }
1966
934cc71c97eb Use dmSetBitmapFontPalette().
Matti Hamalainen <ccr@tnsp.org>
parents: 1957
diff changeset
255 dmSetBitmapFontPalette(font, pal, 0, DMFONT_NPALETTE);
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
256 }
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
257
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
258 // Read glyph data, if any
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
259 for (int i = 0; i < nglyphs; i++)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
260 {
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
261 DMBitmapGlyph *glyph;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
262 Uint8 gwidth, gheight;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
263 Uint16 gindex;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
264 Uint8 *pixels;
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
265
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
266 // TSFONT format has only byte sized index
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
267 if (tsfont)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
268 gindex = dmfgetc(fp);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
269 else
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
270 dmf_read_le16(fp, &gindex);
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
271
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
272 // Read dimensions
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
273 if (!dmf_read_byte(fp, &gwidth) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
274 !dmf_read_byte(fp, &gheight))
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
275 return DMERR_FREAD;
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
276
132
b5569c84f00a Add support for loading ancient version of TSFONTs.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
277 #ifdef FN_DEBUG
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
278 fprintf(stderr, "#%d @ %d - %d x %d\n", i, gindex, gwidth, gheight);
132
b5569c84f00a Add support for loading ancient version of TSFONTs.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
279 #endif
b5569c84f00a Add support for loading ancient version of TSFONTs.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
280
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
281 // Check the glyph data
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
282 if (gwidth < DMFONT_MIN_WIDTH ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
283 gheight < DMFONT_MIN_HEIGHT ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
284 gwidth > DMFONT_MAX_WIDTH ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
285 gheight > DMFONT_MAX_HEIGHT ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
286 gwidth > width ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
287 gheight > height ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
288 gindex >= maxglyph)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
289 return DMERR_INVALID_DATA;
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
290
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
291 // Set glyph data
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
292 glyph = &font->glyphMap[gindex];
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
293 glyph->width = gwidth;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
294 glyph->height = gheight;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
295 glyph->index = i;
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
296
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
297 // Read pixel data
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
298 pixels = font->glyphs->pixels + (i * font->gsize);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
299 for (int y = 0; y < glyph->height; y++)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
300 {
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
301 if (dmfread(pixels, font->glyphs->format->BytesPerPixel,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
302 glyph->width, fp) != (size_t) glyph->width)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
303 return DMERR_FREAD;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
304
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1951
diff changeset
305 pixels += font->glyphs->pitch;
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
306 }
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
307 }
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
308
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
309 return DMERR_OK;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
310 }