comparison dmtext_bm.c @ 65:03375aa0ef2b

Implement some new functions for bitmapped font handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 01 Oct 2012 11:20:56 +0300
parents ad1ef3f0d474
children f18ad054a695
comparison
equal deleted inserted replaced
64:ad1ef3f0d474 65:03375aa0ef2b
45 va_start(ap, fmt); 45 va_start(ap, fmt);
46 dmDrawBMTextVA(screen, font, mode, xc, yc, fmt, ap); 46 dmDrawBMTextVA(screen, font, mode, xc, yc, fmt, ap);
47 va_end(ap); 47 va_end(ap);
48 } 48 }
49 49
50
51 DMBitmapFont *dmNewBitmapFont(int nglyphs)
52 {
53 DMBitmapFont *font = dmMalloc0(sizeof(DMBitmapFont));
54 if (font == NULL)
55 return NULL;
56
57 font->nglyphs = nglyphs;
58 font->glyphs = dmCalloc(nglyphs, sizeof(SDL_Surface *));
59
60 return font;
61 }
62
63
64 int dmFreeBitmapFont(DMBitmapFont *font)
65 {
66 int i;
67
68 if (font == NULL)
69 return DMERR_NULLPTR;
70
71 for (i = 0; i < font->nglyphs; i++)
72 {
73 if (font->glyphs[i] != NULL)
74 {
75 SDL_FreeSurface(font->glyphs[i]);
76 font->glyphs[i] = NULL;
77 }
78 }
79
80 dmFree(font);
81 return DMERR_OK;
82 }
83
84
85 int dmLoadBitmapFont(DMResource *res, DMBitmapFont **pfont)
86 {
87 return DMERR_OK;
88 }
89
90
91 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont)
92 {
93 int glyph, xc, yc, xglyphs, yglyphs;
94 DMBitmapFont *font;
95
96 if (image->w < width || width < 4 || image->h < height || height < 4)
97 return DMERR_INVALID_ARGS;
98
99 xglyphs = image->w / width;
100 yglyphs = image->h / height;
101
102 if ((font = dmNewBitmapFont(xglyphs * yglyphs)) == NULL)
103 return DMERR_MALLOC;
104
105 font->width = width;
106 font->height = height;
107
108 glyph = 0;
109 for (yc = 0; yc < yglyphs; yc++)
110 for (xc = 0; xc < xglyphs; xc++)
111 {
112 SDL_Surface *bmp = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
113 image->format->BitsPerPixel,
114 image->format->Rmask,
115 image->format->Gmask,
116 image->format->Bmask,
117 image->format->Amask);
118
119 if (bmp == NULL)
120 {
121 dmFreeBitmapFont(font);
122 return DMERR_MALLOC;
123 }
124
125 font->glyphs[glyph++] = bmp;
126 }
127
128 *pfont = font;
129 return DMERR_OK;
130 }