Mercurial > hg > dmlib
changeset 60:f28cd66356f6
Initial work for bitmapped fonts and text drawing. Also moved TTF header
stuff to dmtext.h.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 01 Oct 2012 09:46:56 +0300 |
parents | c560703e85ed |
children | a33e47232161 |
files | Makefile.gen dmlib.h dmtext.c dmtext.h |
diffstat | 4 files changed, 111 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile.gen Mon Oct 01 07:51:08 2012 +0300 +++ b/Makefile.gen Mon Oct 01 09:46:56 2012 +0300 @@ -66,6 +66,11 @@ DMLIB_OBJS += dmblit.o endif +ifeq ($(DM_GFX_BM_TEXT),yes) +DM_CFLAGS += -DDM_GFX_BM_TEXT +DMLIB_OBJS += dmtext.o +endif + ifeq ($(DM_GFX_TTF_TEXT),yes) DM_CFLAGS += -DDM_GFX_TTF_TEXT DMLIB_OBJS += dmtext.o
--- a/dmlib.h Mon Oct 01 07:51:08 2012 +0300 +++ b/dmlib.h Mon Oct 01 09:46:56 2012 +0300 @@ -8,9 +8,6 @@ #define DMLIB_H #include <SDL.h> -#ifdef DM_GFX_TTF_TEXT -#include <SDL_ttf.h> -#endif #include <stdarg.h> #ifdef DM_USE_ASSERTS @@ -220,14 +217,6 @@ DMFloat dmPerlinNoise2D(DMFloat x, DMFloat y, DMFloat alpha, DMFloat beta, int n); -/* TTF text drawing - */ -#ifdef DM_GFX_TTF_TEXT -void dmDrawTTFTextConst(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt); -void dmDrawTTFTextVA(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt, va_list ap); -void dmDrawTTFText(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt, ...); -#endif - /* Arbitrary line drawing */ #ifdef DM_GFX_LINES
--- a/dmtext.c Mon Oct 01 07:51:08 2012 +0300 +++ b/dmtext.c Mon Oct 01 09:46:56 2012 +0300 @@ -1,4 +1,12 @@ -#include "dmlib.h" +/* + * DMLib + * -- Bitmap and TTF text & font support + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2012 Tecnic Software productions (TNSP) + */ +#include "dmtext.h" + +#ifdef DM_GFX_TTF_TEXT void dmDrawTTFTextConst(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt) { @@ -34,3 +42,48 @@ va_end(ap); } +#endif + +#ifdef DM_GFX_BM_TEXT + +void dmDrawBMTextConst(SDL_Surface *screen, DMBitmapFont *font, int mode, int xc, int yc, const char *fmt) +{ + const char *ptr = fmt; + DMScaledBlitFunc *blit = dmGetScaledBlitFunc(screen, font->glyphs[0], mode); + while (*ptr) + { + char ch = *ptr++; + + if (isprint(ch) && ch != ' ' && ch != '\t') + { + SDL_Surface *glyph = font->glyphs[(unsigned char) ch]; + blit(glyph, xc, yc, glyph->width, glyph->height, screen); + xc += glyph->width; + } + else + xc += font->width; + } +} + + +void dmDrawBMTextVA(SDL_Surface *screen, DMBitmapFont *font, int mode, int xc, int yc, const char *fmt, va_list ap) +{ + char *tmp = dm_strdup_vprintf(fmt, ap); + if (tmp != NULL) + { + dmDrawBMTextConst(screen, font, col, x, y, tmp); + dmFree(tmp); + } +} + + +void dmDrawBMText(SDL_Surface *screen, DMBitmapFont *font, int mode, int xc, int yc, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + dmDrawBMTextVA(screen, font, col, x, y, fmt, ap); + va_end(ap); +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dmtext.h Mon Oct 01 09:46:56 2012 +0300 @@ -0,0 +1,52 @@ +/* + * DMLib + * -- Bitmap font support + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2012 Tecnic Software productions (TNSP) + */ +#ifndef DMFONT_H +#define DMFONT_H + +#include "dmlib.h" +#include "dmres.h" + +#ifdef DM_GFX_TTF_TEXT +#include <SDL_ttf.h> +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* Bitmapped fonts and text + */ +typedef struct +{ + int width, height; + + int nglyphs; + SDL_Surface **glyphs; +} DMBitmapFont; + + +int dmLoadBitmapFont(DMResource *res, DMBitmapFont **font); + +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); +void dmDrawBMText(SDL_Surface *screen, DMBitmapFont *font, int mode, int xc, int yc, const char *fmt, ...); + + +/* TTF text drawing + */ +#ifdef DM_GFX_TTF_TEXT +void dmDrawTTFTextConst(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt); +void dmDrawTTFTextVA(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt, va_list ap); +void dmDrawTTFText(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt, ...); +#endif + + +#ifdef __cplusplus +} +#endif + +#endif // DMFONT_H