view dmtext.c @ 63:3d9da937db69

More work on the text support.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 01 Oct 2012 10:42:56 +0300
parents f28cd66356f6
children
line wrap: on
line source

/*
 * 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 xc, int yc, const char *fmt)
{
    SDL_Surface *text = TTF_RenderText_Blended(font, fmt, col);
    if (text)
    {
        SDL_Rect rect;
        rect.x = xc;
        rect.y = yc;
        rect.w = text->w;
        rect.h = text->h;
        SDL_BlitSurface(text, NULL, screen, &rect);
        SDL_FreeSurface(text);
    }
}

void dmDrawTTFTextVA(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int xc, int yc, const char *fmt, va_list ap)
{
    char *tmp = dm_strdup_vprintf(fmt, ap);
    if (tmp != NULL)
    {
        dmDrawTTFTextConst(screen, font, col, xc, yc, tmp);
        dmFree(tmp);
    }
}

void dmDrawTTFText(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int xc, int yc, const char *fmt, ...)
{
    va_list ap;
    
    va_start(ap, fmt);
    dmDrawTTFTextVA(screen, font, col, xc, yc, fmt, ap);
    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->format, font->glyphs[0]->format, 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->w, glyph->h, screen);
            xc += glyph->w;
        }
        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, mode, xc, yc, 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, mode, xc, yc, fmt, ap);
    va_end(ap);
}

#endif