changeset 100:f16d102dbbac

Add a function for setting bitmapped font palette.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Oct 2012 20:33:01 +0300
parents 7a59611f9d4f
children 8bba97252d85
files dmtext.h dmtext_bm.c
diffstat 2 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/dmtext.h	Tue Oct 02 20:26:08 2012 +0300
+++ b/dmtext.h	Tue Oct 02 20:33:01 2012 +0300
@@ -52,6 +52,7 @@
 int dmLoadBitmapFont(DMResource *res, DMBitmapFont **pfont);
 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font);
 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont);
+int dmSetBitmapFontPalette(DMBitmapFont *font, SDL_Color *pal, int start, int size);
 
 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);
--- a/dmtext_bm.c	Tue Oct 02 20:26:08 2012 +0300
+++ b/dmtext_bm.c	Tue Oct 02 20:33:01 2012 +0300
@@ -110,6 +110,34 @@
 }
 
 
+/* Set the palette for each glyph. While the function allows you to
+ * specify 'start' and 'end' indices and palette array freely, you should
+ * typically use DMFONT_NPALETTE size palette starting at index 0.
+ */
+int dmSetBitmapFontPalette(DMBitmapFont *font, SDL_Color *pal, int start, int size)
+{
+    int i;
+
+    if (font == NULL)
+        return DMERR_NULLPTR;
+    
+    if (start < 0 || end < start)
+        return DMERR_INVALID_ARGS;
+    
+    for (i = 0; i < font->nglyphs; i++)
+    {
+        SDL_Surface *glyph = font->glyphs[i];
+        if (glyph != NULL)
+        {
+            if (glyph->format->BitsPerPixel == 8)
+                SDL_SetColors(glyph, pal, start, end);
+        }
+    }
+
+    return DMERR_OK;
+}
+
+
 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont)
 {
     int nglyph, xc, yc, xglyphs, yglyphs;