comparison dmtext_bm.c @ 75:e6535609c161

Initial implementation of loading and saving of bitmap fonts.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Oct 2012 05:51:08 +0300
parents 295d08376744
children 7d201aed1fd9
comparison
equal deleted inserted replaced
74:23ac82365a65 75:e6535609c161
101 dmFree(font); 101 dmFree(font);
102 return DMERR_OK; 102 return DMERR_OK;
103 } 103 }
104 104
105 105
106 int dmLoadBitmapFont(DMResource *res, DMBitmapFont **pfont)
107 {
108 return DMERR_OK;
109 }
110
111
112 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) 106 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont)
113 { 107 {
114 int glyph, xc, yc, xglyphs, yglyphs; 108 int glyph, xc, yc, xglyphs, yglyphs;
115 DMBitmapFont *font; 109 DMBitmapFont *font;
116 110
161 155
162 *pfont = font; 156 *pfont = font;
163 return DMERR_OK; 157 return DMERR_OK;
164 } 158 }
165 159
160
161 int dmLoadBitmapFont(DMResource *res, DMBitmapFont **pfont)
162 {
163 DMBitmapFont *font;
164 char magic[8];
165 Uint16 version;
166 Uint32 width, height, nglyphs;
167
168 // Check magic and version
169 dmf_read_str(res, (Uint8 *) &magic, 6);
170 dmf_read_le16(res, &version);
171
172 if (memcmp(magic, DMFONT_MAGIC, 6) != 0)
173 return DMERR_INVALID;
174
175 if (version != DMFONT_VERSION)
176 return DMERR_VERSION;
177
178 // Check other data
179 dmf_read_le32(res, &width);
180 dmf_read_le32(res, &height);
181 dmf_read_le32(res, &nglyphs);
182
183 if (width > 128 || height > 128 || nglyphs > 1024)
184 return DMERR_INVALID_DATA;
185
186 // Allocate font
187 if ((*pfont = font = dmNewBitmapFont(nglyphs)) == NULL)
188 return DMERR_MALLOC;
189
190 font->width = width;
191 font->height = height;
192 font->nglyphs = nglyphs;
193
194 // Read glyph data, if any
195 if (nglyphs > 0)
196 {
197 Uint32 i, BitsPerPixel, Rmask, Gmask, Bmask, Amask, pitch;
198
199 BitsPerPixel = dmfgetc(res);
200 dmf_read_le32(res, &Rmask);
201 dmf_read_le32(res, &Gmask);
202 dmf_read_le32(res, &Bmask);
203 dmf_read_le32(res, &Amask);
204
205 for (i = 0; i < nglyphs; i++)
206 {
207 int y;
208 Uint8 *pixels;
209 SDL_Surface *glyph;
210 dmf_read_le32(res, &width);
211 dmf_read_le32(res, &height);
212 dmf_read_le32(res, &pitch);
213
214 font->glyphs[i] = glyph = SDL_CreateRGBSurface(
215 SDL_SWSURFACE, width, height,
216 BitsPerPixel, Rmask, Gmask,
217 Bmask,
218 Amask);
219
220 if (glyph == NULL)
221 return DMERR_MALLOC;
222
223 pixels = glyph->pixels;
224 for (y = 0; y < glyph->h; y++)
225 {
226 if (dmfread(pixels, glyph->format->BytesPerPixel, glyph->w, res) != glyph->w)
227 return DMERR_FREAD;
228 pixels += glyph->pitch;
229 }
230 }
231 }
232
233 return DMERR_OK;
234 }
235
236
166 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font) 237 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font)
167 { 238 {
168 int i; 239 if (font == NULL)
169 240 return DMERR_NULLPTR;
170 dmf_write_str(res, (Uint8 *) "DMFONT", 6); 241
171 dmf_write_le16(res, 0x0100); 242 if (!dmf_write_str(res, (Uint8 *) DMFONT_MAGIC, 6))
243 return DMERR_FWRITE;
244
245 dmf_write_le16(res, DMFONT_VERSION);
172 dmf_write_le32(res, font->width); 246 dmf_write_le32(res, font->width);
173 dmf_write_le32(res, font->height); 247 dmf_write_le32(res, font->height);
174 dmf_write_le32(res, font->nglyphs); 248 dmf_write_le32(res, font->nglyphs);
175 249
176 for (i = 0; i < font->nglyphs; i++) 250 if (font->nglyphs > 0)
177 { 251 {
178 SDL_Surface *glyph = font->glyphs[i]; 252 int i;
179 dmf_write_le32(res, glyph->w); 253 SDL_Surface *glyph = font->glyphs[0];
180 dmf_write_le32(res, glyph->h); 254
181 dmf_write_le32(res, glyph->pitch); 255 dmfputc(glyph->format->BitsPerPixel, res);
182 dmf_write_str(res, glyph->pixels, glyph->h * glyph->pitch); 256 dmf_write_le32(res, glyph->format->Rmask);
183 } 257 dmf_write_le32(res, glyph->format->Gmask);
184 258 dmf_write_le32(res, glyph->format->Bmask);
185 return DMERR_OK; 259 dmf_write_le32(res, glyph->format->Amask);
186 } 260
261 for (i = 0; i < font->nglyphs; i++)
262 {
263 int y;
264 glyph = font->glyphs[i];
265 Uint8 *pixels = glyph->pixels;
266
267 dmf_write_le32(res, glyph->w);
268 dmf_write_le32(res, glyph->h);
269
270 for (y = 0; y < glyph->h; y++)
271 {
272 if (dmfwrite(pixels, glyph->format->BytesPerPixel, glyph->w, res) != glyph->w)
273 return DMERR_FWRITE;
274 pixels += glyph->pitch;
275 }
276 }
277 }
278
279 return DMERR_OK;
280 }