From cde17f778f20114f10ad52e93050d4ea0c522864 Mon Sep 17 00:00:00 2001 From: Crayon2000 Date: Tue, 6 Jul 2010 05:07:55 +0000 Subject: [PATCH] [CHG] BMF function are a bit faster, character definition is taken inside an array --- GRRLIB/GRRLIB/GRRLIB_bmf.c | 25 ++++++++++++------------- GRRLIB/GRRLIB/GRRLIB_print.c | 28 ++++++++++++---------------- GRRLIB/GRRLIB/grrlib.h | 3 +-- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/GRRLIB/GRRLIB/GRRLIB_bmf.c b/GRRLIB/GRRLIB/GRRLIB_bmf.c index 23ce8a9..4f85edf 100644 --- a/GRRLIB/GRRLIB/GRRLIB_bmf.c +++ b/GRRLIB/GRRLIB/GRRLIB_bmf.c @@ -27,6 +27,7 @@ THE SOFTWARE. /** * Load a ByteMap font structure from a buffer. + * File format version 1.1 is used, more information could be found at http://bmf.wz.cz/bmf-format.htm * @param my_bmf The ByteMap font buffer to load. * @return A GRRLIB_bytemapFont structure filled with BMF information. * @see GRRLIB_FreeBMF @@ -34,7 +35,7 @@ THE SOFTWARE. GRRLIB_bytemapFont* GRRLIB_LoadBMF (const u8 my_bmf[] ) { GRRLIB_bytemapFont *fontArray = (struct GRRLIB_bytemapFont *)malloc(sizeof(GRRLIB_bytemapFont)); u32 i, j = 1; - u8 lineheight, usedcolors, highestcolor, nbPalette; + u8 lineheight, usedcolors, highestcolor, nbPalette, c; short int sizeover, sizeunder, sizeinner, numcolpal; u16 nbPixels; @@ -58,19 +59,18 @@ GRRLIB_bytemapFont* GRRLIB_LoadBMF (const u8 my_bmf[] ) { memcpy(fontArray->name, &my_bmf[18 + numcolpal], j); j = 18 + numcolpal + j; fontArray->nbChar = (my_bmf[j] | my_bmf[j+1]<<8); - fontArray->charDef = (GRRLIB_bytemapChar *)calloc(fontArray->nbChar, sizeof(GRRLIB_bytemapChar)); j++; for (i=0; i < fontArray->nbChar; i++) { - fontArray->charDef[i].character = my_bmf[++j]; - fontArray->charDef[i].width = my_bmf[++j]; - fontArray->charDef[i].height = my_bmf[++j]; - fontArray->charDef[i].relx = my_bmf[++j]; - fontArray->charDef[i].rely = my_bmf[++j]; - fontArray->charDef[i].kerning = my_bmf[++j]; - nbPixels = fontArray->charDef[i].width * fontArray->charDef[i].height; - fontArray->charDef[i].data = (u8 *)malloc(nbPixels); - if (nbPixels && fontArray->charDef[i].data) { - memcpy(fontArray->charDef[i].data, &my_bmf[++j], nbPixels); + c = my_bmf[++j]; + fontArray->charDef[c].width = my_bmf[++j]; + fontArray->charDef[c].height = my_bmf[++j]; + fontArray->charDef[c].relx = my_bmf[++j]; + fontArray->charDef[c].rely = my_bmf[++j]; + fontArray->charDef[c].kerning = my_bmf[++j]; + nbPixels = fontArray->charDef[c].width * fontArray->charDef[c].height; + fontArray->charDef[c].data = (u8 *)malloc(nbPixels); + if (nbPixels && fontArray->charDef[c].data) { + memcpy(fontArray->charDef[c].data, &my_bmf[++j], nbPixels); j += (nbPixels - 1); } } @@ -88,7 +88,6 @@ void GRRLIB_FreeBMF (const GRRLIB_bytemapFont *bmf) { for (i=0; inbChar; i++) { free(bmf->charDef[i].data); } - free(bmf->charDef); free(bmf->palette); free(bmf->name); } diff --git a/GRRLIB/GRRLIB/GRRLIB_print.c b/GRRLIB/GRRLIB/GRRLIB_print.c index 6fcb3a2..b152a7c 100644 --- a/GRRLIB/GRRLIB/GRRLIB_print.c +++ b/GRRLIB/GRRLIB/GRRLIB_print.c @@ -69,11 +69,12 @@ void GRRLIB_Printf (const f32 xpos, const f32 ypos, void GRRLIB_PrintBMF (const f32 xpos, const f32 ypos, const GRRLIB_bytemapFont *bmf, const char *text, ...) { - uint i, n, size; - u16 j; + uint i, size; + u8 *pdata; u8 x, y; char tmp[1024]; f32 xoff = xpos; + const GRRLIB_bytemapChar *pchar; va_list argp; va_start(argp, text); @@ -81,22 +82,17 @@ void GRRLIB_PrintBMF (const f32 xpos, const f32 ypos, va_end(argp); for (i=0; inbChar; j++) { - if (tmp[i] == bmf->charDef[j].character) { - n=0; - for (y=0; ycharDef[j].height; y++) { - for (x=0; xcharDef[j].width; x++) { - if (bmf->charDef[j].data[n]) { - GRRLIB_Plot(xoff + x + bmf->charDef[j].relx, - ypos + y + bmf->charDef[j].rely, - bmf->palette[bmf->charDef[j].data[n]]); - } - n++; - } + pchar = &bmf->charDef[(u8)tmp[i]]; + pdata = pchar->data; + for (y=0; yheight; y++) { + for (x=0; xwidth; x++) { + if (*pdata++) { + GRRLIB_Plot(xoff + x + pchar->relx, + ypos + y + pchar->rely, + bmf->palette[*pdata]); } - xoff += bmf->charDef[j].kerning + bmf->tracking; - break; } } + xoff += pchar->kerning + bmf->tracking; } } diff --git a/GRRLIB/GRRLIB/grrlib.h b/GRRLIB/GRRLIB/grrlib.h index 6616149..a4d513d 100644 --- a/GRRLIB/GRRLIB/grrlib.h +++ b/GRRLIB/GRRLIB/grrlib.h @@ -130,7 +130,6 @@ typedef struct GRRLIB_texImg { * Structure to hold the bytemap character information. */ typedef struct GRRLIB_bytemapChar { - u8 character; /**< Character identity. */ u8 width; /**< Character width. */ u8 height; /**< Character height. */ s8 relx; /**< Horizontal offset relative to cursor (-128 to 127). */ @@ -150,7 +149,7 @@ typedef struct GRRLIB_bytemapFont { u8 version; /**< Version. */ s8 tracking; /**< Tracking (Add-space after each char) (-128 to 127). */ - GRRLIB_bytemapChar *charDef; /**< Array of bitmap characters. */ + GRRLIB_bytemapChar charDef[256]; /**< Array of bitmap characters. */ } GRRLIB_bytemapFont; //------------------------------------------------------------------------------