# HG changeset patch # User Matti Hamalainen # Date 1349079656 -10800 # Node ID 03375aa0ef2be73735700c5186011f455c1899b4 # Parent ad1ef3f0d474a49f4be89dc49a451fee891f8c1a Implement some new functions for bitmapped font handling. diff -r ad1ef3f0d474 -r 03375aa0ef2b dmtext.h --- a/dmtext.h Mon Oct 01 11:03:10 2012 +0300 +++ b/dmtext.h Mon Oct 01 11:20:56 2012 +0300 @@ -30,7 +30,10 @@ } DMBitmapFont; -int dmLoadBitmapFont(DMResource *res, DMBitmapFont **font); +DMBitmapFont *dmNewBitmapFont(int nglyphs); +int dmFreeBitmapFont(DMBitmapFont *font); +int dmLoadBitmapFont(DMResource *res, DMBitmapFont **pfont); +int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont); void dmDrawBMTextConst(SDL_Surface *screen, DMBitmapFont *font, int mode, int xc, int yc, const char *str); void dmDrawBMTextVA(SDL_Surface *screen, DMBitmapFont *font, int mode, int xc, int yc, const char *fmt, va_list ap); diff -r ad1ef3f0d474 -r 03375aa0ef2b dmtext_bm.c --- a/dmtext_bm.c Mon Oct 01 11:03:10 2012 +0300 +++ b/dmtext_bm.c Mon Oct 01 11:20:56 2012 +0300 @@ -47,3 +47,84 @@ va_end(ap); } + +DMBitmapFont *dmNewBitmapFont(int nglyphs) +{ + DMBitmapFont *font = dmMalloc0(sizeof(DMBitmapFont)); + if (font == NULL) + return NULL; + + font->nglyphs = nglyphs; + font->glyphs = dmCalloc(nglyphs, sizeof(SDL_Surface *)); + + return font; +} + + +int dmFreeBitmapFont(DMBitmapFont *font) +{ + int i; + + if (font == NULL) + return DMERR_NULLPTR; + + for (i = 0; i < font->nglyphs; i++) + { + if (font->glyphs[i] != NULL) + { + SDL_FreeSurface(font->glyphs[i]); + font->glyphs[i] = NULL; + } + } + + dmFree(font); + return DMERR_OK; +} + + +int dmLoadBitmapFont(DMResource *res, DMBitmapFont **pfont) +{ + return DMERR_OK; +} + + +int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) +{ + int glyph, xc, yc, xglyphs, yglyphs; + DMBitmapFont *font; + + if (image->w < width || width < 4 || image->h < height || height < 4) + return DMERR_INVALID_ARGS; + + xglyphs = image->w / width; + yglyphs = image->h / height; + + if ((font = dmNewBitmapFont(xglyphs * yglyphs)) == NULL) + return DMERR_MALLOC; + + font->width = width; + font->height = height; + + glyph = 0; + for (yc = 0; yc < yglyphs; yc++) + for (xc = 0; xc < xglyphs; xc++) + { + SDL_Surface *bmp = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, + image->format->BitsPerPixel, + image->format->Rmask, + image->format->Gmask, + image->format->Bmask, + image->format->Amask); + + if (bmp == NULL) + { + dmFreeBitmapFont(font); + return DMERR_MALLOC; + } + + font->glyphs[glyph++] = bmp; + } + + *pfont = font; + return DMERR_OK; +}