annotate dmtext_bm.c @ 91:e1e308167991

Various improvements in the bitmapped font loading and saving functions.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Oct 2012 17:50:28 +0300
parents b10884934aca
children f16d102dbbac
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"
73
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
8 #include "dmresw.h"
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
9
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
10
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
11 void dmDrawBMTextConst(SDL_Surface *screen, DMBitmapFont *font, int mode, int xc, int yc, const char *fmt)
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;
66
f18ad054a695 Oops, some function arguments were reversed.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
14 DMScaledBlitFunc blit = dmGetScaledBlitFunc(font->glyphs[0]->format, screen->format, mode);
70
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
15
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
16 while (*ptr)
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
17 {
70
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
18 SDL_Surface *glyph;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
19 int pos;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
20 int ch = *ptr++;
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
21
76
7d201aed1fd9 Cleanups, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
22 if (ch == '_')
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
23 {
76
7d201aed1fd9 Cleanups, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
24 xc += 4;
70
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
25 continue;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
26 }
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
27 else
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
28 if (ch >= 'A' && ch <= 'Z')
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
29 pos = ch - 'A' + 256 + 1;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
30 else
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
31 if (ch >= 'a' && ch <= 'z')
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
32 pos = ch - 'a' + 1;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
33 else
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
34 if (ch >= '0' && ch <= '9')
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
35 pos = ch - '0' + 48;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
36 else
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
37 pos = ch;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
38
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
39 if (pos >= 0 && pos < font->nglyphs)
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
40 {
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
41 glyph = font->glyphs[pos];
63
3d9da937db69 More work on the text support.
Matti Hamalainen <ccr@tnsp.org>
parents: 60
diff changeset
42 blit(glyph, xc, yc, glyph->w, glyph->h, screen);
3d9da937db69 More work on the text support.
Matti Hamalainen <ccr@tnsp.org>
parents: 60
diff changeset
43 xc += glyph->w;
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
44 }
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
45 else
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
46 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
47 }
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
48 }
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
49
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
50
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
51 void dmDrawBMTextVA(SDL_Surface *screen, DMBitmapFont *font, int mode, int xc, int yc, const char *fmt, va_list ap)
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 char *tmp = dm_strdup_vprintf(fmt, ap);
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
54 if (tmp != NULL)
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
55 {
63
3d9da937db69 More work on the text support.
Matti Hamalainen <ccr@tnsp.org>
parents: 60
diff changeset
56 dmDrawBMTextConst(screen, font, mode, xc, yc, tmp);
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
57 dmFree(tmp);
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 }
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
60
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
61
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
62 void dmDrawBMText(SDL_Surface *screen, DMBitmapFont *font, int mode, int xc, int yc, const char *fmt, ...)
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
63 {
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
64 va_list ap;
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
65
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
66 va_start(ap, fmt);
63
3d9da937db69 More work on the text support.
Matti Hamalainen <ccr@tnsp.org>
parents: 60
diff changeset
67 dmDrawBMTextVA(screen, font, 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
68 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
69 }
f28cd66356f6 Initial work for bitmapped fonts and text drawing. Also moved TTF header
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
70
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
71
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
72 DMBitmapFont *dmNewBitmapFont(int nglyphs, int width, int height)
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
73 {
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
74 DMBitmapFont *font = dmMalloc0(sizeof(DMBitmapFont));
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
75 if (font == NULL)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
76 return NULL;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
77
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
78 font->width = width;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
79 font->height = height;
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
80 font->nglyphs = nglyphs;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
81 font->glyphs = dmCalloc(nglyphs, sizeof(SDL_Surface *));
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
82 if (font->glyphs == NULL)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
83 {
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
84 dmFree(font);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
85 return NULL;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
86 }
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
87
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
88 return font;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
89 }
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
90
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
91
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
92 int dmFreeBitmapFont(DMBitmapFont *font)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
93 {
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
94 int i;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
95
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
96 if (font == NULL)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
97 return DMERR_NULLPTR;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
98
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
99 for (i = 0; i < font->nglyphs; i++)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
100 {
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
101 if (font->glyphs[i] != NULL)
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 SDL_FreeSurface(font->glyphs[i]);
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
104 font->glyphs[i] = NULL;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
105 }
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
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
108 dmFree(font);
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
109 return DMERR_OK;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
110 }
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
111
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
112
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
113 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
114 {
76
7d201aed1fd9 Cleanups, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
115 int nglyph, xc, yc, xglyphs, yglyphs;
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
116 DMBitmapFont *font;
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 if (image->w < width || width < 4 || image->h < height || height < 4)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
119 return DMERR_INVALID_ARGS;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
120
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
121 xglyphs = image->w / width;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
122 yglyphs = image->h / height;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
123
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
124 if ((font = dmNewBitmapFont(xglyphs * yglyphs, width, height)) == NULL)
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
125 return DMERR_MALLOC;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
126
76
7d201aed1fd9 Cleanups, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
127 /*
70
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
128 fprintf(stderr, "%d x %d split as %d x %d blocks => %d x %d = %d glyphs\n",
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
129 image->w, image->h,
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
130 width, height,
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
131 xglyphs, yglyphs, xglyphs * yglyphs);
76
7d201aed1fd9 Cleanups, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
132 */
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
133
76
7d201aed1fd9 Cleanups, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
134 nglyph = 0;
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
135 for (yc = 0; yc < yglyphs; yc++)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
136 for (xc = 0; xc < xglyphs; xc++)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
137 {
76
7d201aed1fd9 Cleanups, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
138 SDL_Surface *glyph = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
139 image->format->BitsPerPixel,
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
140 image->format->Rmask,
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
141 image->format->Gmask,
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
142 image->format->Bmask,
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
143 image->format->Amask);
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
144
76
7d201aed1fd9 Cleanups, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
145 if (glyph == NULL)
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
146 {
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
147 dmFreeBitmapFont(font);
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
148 return DMERR_MALLOC;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
149 }
70
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
150
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
151 SDL_Rect r;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
152 r.x = xc * width;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
153 r.y = yc * height;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
154 r.w = width;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
155 r.h = height;
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
156
76
7d201aed1fd9 Cleanups, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
157 SDL_BlitSurface(image, &r, glyph, NULL);
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
158
76
7d201aed1fd9 Cleanups, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
159 font->glyphs[nglyph++] = glyph;
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
160 }
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
161
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
162 *pfont = font;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
163 return DMERR_OK;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
164 }
73
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
165
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
166
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
167 int dmLoadBitmapFont(DMResource *res, DMBitmapFont **pfont)
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
168 {
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
169 DMBitmapFont *font;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
170 char magic[8];
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
171 Uint16 version, nglyphs, maxglyph;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
172 int width, height;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
173 BOOL tsfont = FALSE;
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
174
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
175 // Check magic and version
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
176 dmf_read_str(res, (Uint8 *) &magic, 6);
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
177 dmf_read_le16(res, &version);
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
178
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
179 // 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
180 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
181 {
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
182 // Yep, we handle these a bit differently
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
183 int encoding = dmfgetc(res);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
184 tsfont = TRUE;
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
185
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
186 if (version > TSFONT_VERSION)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
187 return DMERR_VERSION;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
188
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
189 // 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
190 // 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
191 if (encoding != 0)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
192 return DMERR_NOT_SUPPORTED;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
193 }
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
194 else
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
195 {
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
196 if (memcmp(magic, DMFONT_MAGIC, 6) != 0)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
197 return DMERR_INVALID;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
198
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
199 if (version > DMFONT_VERSION)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
200 return DMERR_VERSION;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
201 }
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
202
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
203 // 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
204 if (tsfont)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
205 {
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
206 // TSFONT only has number of glyphs stored in the file
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
207 nglyphs = dmfgetc(res);
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
208
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
209 // Maximum glyph number
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
210 maxglyph = 255;
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
211 }
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
212 else
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
213 {
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
214 dmf_read_le16(res, &nglyphs);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
215 dmf_read_le16(res, &maxglyph);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
216 }
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
217
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
218 width = dmfgetc(res);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
219 height = dmfgetc(res);
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
220
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
221 if (tsfont)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
222 {
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
223 // TSFONT color assignments (boolean) .. we discard this.
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
224 dmfgetc(res);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
225 }
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
226
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
227 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
228 height < DMFONT_MIN_HEIGHT ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
229 width > DMFONT_MAX_WIDTH ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
230 height > DMFONT_MAX_HEIGHT ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
231 nglyphs > DMFONT_MAX_GLYPHS ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
232 maxglyph > DMFONT_MAX_GLYPHS ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
233 maxglyph < 1)
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
234 return DMERR_INVALID_DATA;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
235
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
236 // Allocate font
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
237 if ((*pfont = font = dmNewBitmapFont(maxglyph, width, height)) == NULL)
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
238 return DMERR_MALLOC;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
239
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
240 // Read glyph data, if any
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
241 if (nglyphs > 0)
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
242 {
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
243 int n, i;
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
244 Uint32 BitsPerPixel, Rmask, Gmask, Bmask, Amask;
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
245 SDL_Color pal[DMFONT_NPALETTE];
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
246
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
247 // Setup palette for 8bpp fonts
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
248 for (n = 0; n < DMFONT_NPALETTE; n++)
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
249 {
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
250 pal[n].r = n * 16;
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
251 pal[n].g = n * 16;
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
252 pal[n].b = n * 16;
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
253 pal[n].unused = n > 0 ? n * 16 : 0;
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
254 }
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
255
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
256 if (tsfont)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
257 {
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
258 BitsPerPixel = 8;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
259 Rmask = Gmask = Bmask = Amask = 0;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
260 }
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
261 else
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
262 {
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
263 BitsPerPixel = dmfgetc(res);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
264 dmf_read_le32(res, &Rmask);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
265 dmf_read_le32(res, &Gmask);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
266 dmf_read_le32(res, &Bmask);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
267 dmf_read_le32(res, &Amask);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
268 }
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
269
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
270 for (i = 0; i < nglyphs; i++)
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
271 {
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
272 int y;
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
273 Uint16 index;
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
274 Uint8 *pixels;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
275 SDL_Surface *glyph;
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
276
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
277 // TSFONT format has only byte sized index
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
278 if (tsfont)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
279 index = dmfgetc(res);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
280 else
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
281 dmf_read_le16(res, &index);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
282
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
283 // Read dimensions
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
284 width = dmfgetc(res);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
285 height = dmfgetc(res);
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
286
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
287 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
288 height < DMFONT_MIN_HEIGHT ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
289 width > DMFONT_MAX_WIDTH ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
290 height > DMFONT_MAX_HEIGHT ||
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
291 index > maxglyph)
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
292 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
293
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
294 // Allocate bitmap
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
295 font->glyphs[index] = glyph = SDL_CreateRGBSurface(
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
296 SDL_SWSURFACE, width, height,
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
297 BitsPerPixel, Rmask, Gmask,
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
298 Bmask,
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
299 Amask);
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
300
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
301 if (glyph == NULL)
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
302 return DMERR_MALLOC;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
303
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
304 if (BitsPerPixel == 8)
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
305 SDL_SetColors(glyph, pal, 0, DMFONT_NPALETTE);
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
306
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
307 // Read pixel data
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
308 pixels = glyph->pixels;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
309 for (y = 0; y < glyph->h; y++)
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
310 {
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
311 if (dmfread(pixels, glyph->format->BytesPerPixel, glyph->w, res) != (size_t) glyph->w)
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
312 return DMERR_FREAD;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
313 pixels += glyph->pitch;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
314 }
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
315 }
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
316 }
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
317
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
318 return DMERR_OK;
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
319 }
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
320
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
321
73
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
322 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font)
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
323 {
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
324 int maxglyph, nglyphs, n;
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
325 if (font == NULL)
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
326 return DMERR_NULLPTR;
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
327
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
328 if (font->nglyphs > DMFONT_MAX_GLYPHS ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
329 font->width > DMFONT_MAX_WIDTH ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
330 font->height > DMFONT_MAX_HEIGHT ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
331 font->width < DMFONT_MIN_WIDTH ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
332 font->height < DMFONT_MIN_HEIGHT)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
333 return DMERR_INVALID_DATA;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
334
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
335 // Count number of actually existing glyphs
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
336 for (maxglyph = nglyphs = n = 0; n < font->nglyphs; n++)
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
337 {
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
338 if (font->glyphs[n] != NULL)
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
339 {
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
340 nglyphs++;
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
341 maxglyph = n;
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
342 }
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
343 }
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
344
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
345 // Write the DMFONT header
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
346 if (!dmf_write_str(res, (Uint8 *) DMFONT_MAGIC, 6))
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
347 return DMERR_FWRITE;
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
348
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
349 dmf_write_le16(res, DMFONT_VERSION);
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
350 dmf_write_le16(res, nglyphs);
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
351 dmf_write_le16(res, maxglyph);
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
352 dmfputc(font->width, res);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
353 dmfputc(font->height, res);
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
354
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
355 if (nglyphs > 0)
73
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
356 {
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
357 int i;
91
e1e308167991 Various improvements in the bitmapped font loading and saving functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
358 SDL_Surface *glyph = font->glyphs[maxglyph];
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
359
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
360 // If there are actual glyphs stored, save thi
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
361 dmfputc(glyph->format->BitsPerPixel, res);
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
362 dmf_write_le32(res, glyph->format->Rmask);
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
363 dmf_write_le32(res, glyph->format->Gmask);
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
364 dmf_write_le32(res, glyph->format->Bmask);
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
365 dmf_write_le32(res, glyph->format->Amask);
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
366
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
367 for (i = 0; i < font->nglyphs; i++)
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
368 {
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
369 glyph = font->glyphs[i];
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
370 if (glyph != NULL)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
371 {
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
372 int y;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
373 Uint8 *pixels = glyph->pixels;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
374
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
375 if (glyph->w < DMFONT_MIN_WIDTH ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
376 glyph->h < DMFONT_MIN_HEIGHT ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
377 glyph->w > DMFONT_MAX_WIDTH ||
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
378 glyph->h > DMFONT_MAX_HEIGHT)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
379 return DMERR_INVALID_DATA;
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
380
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
381 // Each glyph has its table index and w/h stored
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
382 dmf_write_le16(res, i);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
383 dmf_write_le16(res, glyph->w);
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
384 dmf_write_le16(res, glyph->h);
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
385
89
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
386 // Write the pixel data
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
387 for (y = 0; y < glyph->h; y++)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
388 {
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
389 if (dmfwrite(pixels, glyph->format->BytesPerPixel, glyph->w, res) != (size_t) glyph->w)
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
390 return DMERR_FWRITE;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
391 pixels += glyph->pitch;
b10884934aca Various improvements in bitmapped font support, and addition of legacy
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
392 }
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
393 }
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
394 }
73
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
395 }
75
e6535609c161 Initial implementation of loading and saving of bitmap fonts.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
396
73
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
397 return DMERR_OK;
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
398 }