annotate dmtext_bm.c @ 73:295d08376744

Add function for saving a bitmap font.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 01 Oct 2012 14:18:35 +0300
parents a791146e3094
children e6535609c161
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
70
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
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 {
70
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
24 xc += 15;
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
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
72 DMBitmapFont *dmNewBitmapFont(int nglyphs)
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
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
78 font->nglyphs = nglyphs;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
79 font->glyphs = dmCalloc(nglyphs, sizeof(SDL_Surface *));
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
80
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
81 return font;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
82 }
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
83
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
84
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
85 int dmFreeBitmapFont(DMBitmapFont *font)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
86 {
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
87 int i;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
88
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
89 if (font == NULL)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
90 return DMERR_NULLPTR;
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 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
93 {
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
94 if (font->glyphs[i] != NULL)
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 SDL_FreeSurface(font->glyphs[i]);
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
97 font->glyphs[i] = NULL;
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 }
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 dmFree(font);
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
102 return DMERR_OK;
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
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
106 int dmLoadBitmapFont(DMResource *res, DMBitmapFont **pfont)
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 return DMERR_OK;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
109 }
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 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
113 {
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
114 int glyph, xc, yc, xglyphs, yglyphs;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
115 DMBitmapFont *font;
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 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
118 return DMERR_INVALID_ARGS;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
119
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
120 xglyphs = image->w / width;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
121 yglyphs = image->h / height;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
122
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
123 if ((font = dmNewBitmapFont(xglyphs * yglyphs)) == NULL)
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
124 return DMERR_MALLOC;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
125
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
126 font->width = width;
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
127 font->height = height;
70
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
128
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
129 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
130 image->w, image->h,
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
131 width, height,
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
132 xglyphs, yglyphs, xglyphs * yglyphs);
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
133
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
134 glyph = 0;
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 {
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
138 SDL_Surface *bmp = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
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
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
145 if (bmp == NULL)
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
a791146e3094 Make BM text work, a bit at least.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
157 SDL_BlitSurface(image, &r, bmp, NULL);
65
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
158
03375aa0ef2b Implement some new functions for bitmapped font handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
159 font->glyphs[glyph++] = bmp;
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
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
166 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font)
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
167 {
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
168 int i;
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
169
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
170 dmf_write_str(res, (Uint8 *) "DMFONT", 6);
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
171 dmf_write_le16(res, 0x0100);
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
172 dmf_write_le32(res, font->width);
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
173 dmf_write_le32(res, font->height);
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
174 dmf_write_le32(res, font->nglyphs);
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
175
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
176 for (i = 0; i < font->nglyphs; i++)
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
177 {
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
178 SDL_Surface *glyph = font->glyphs[i];
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
179 dmf_write_le32(res, glyph->w);
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
180 dmf_write_le32(res, glyph->h);
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
181 dmf_write_le32(res, glyph->pitch);
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
182 dmf_write_str(res, glyph->pixels, glyph->h * glyph->pitch);
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
183 }
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
184
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
185 return DMERR_OK;
295d08376744 Add function for saving a bitmap font.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
186 }