gbasys

diff src/font.c @ 0:875ef6085efc

gbasys mercurial repository
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 04 Mar 2012 04:04:25 +0200
parents
children c50064b181c2
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/font.c	Sun Mar 04 04:04:25 2012 +0200
     1.3 @@ -0,0 +1,130 @@
     1.4 +/*
     1.5 +Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
     1.6 +
     1.7 +This file is part of libgba, a library for GameBoy Advance development.
     1.8 +
     1.9 +This program is free software; you can redistribute it and/or modify
    1.10 +it under the terms of the GNU General Public License as published by
    1.11 +the Free Software Foundation; either version 2 of the License, or
    1.12 +(at your option) any later version.
    1.13 +
    1.14 +This program is distributed in the hope that it will be useful,
    1.15 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 +GNU General Public License for more details.
    1.18 +
    1.19 +You should have received a copy of the GNU General Public License
    1.20 +along with this program; if not, write to the Free Software
    1.21 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.22 +*/
    1.23 +
    1.24 +#include "libgba_config.h"
    1.25 +#include <stdlib.h>
    1.26 +#include "font.h"
    1.27 +#include "gfx.h"
    1.28 +
    1.29 +#if defined(CONFIG_FONT_8X16)
    1.30 +#define DEFAULT_FONT	&font_8x16
    1.31 +#elif defined(CONFIG_FONT_8X8)
    1.32 +#define DEFAULT_FONT	&font_8x8
    1.33 +#else	/* no font compiled in */
    1.34 +#define DEFAULT_FONT	0
    1.35 +#endif	/* CONFIG_FONT_8X16 */
    1.36 +
    1.37 +/* active font */
    1.38 +static struct font *font = DEFAULT_FONT;
    1.39 +
    1.40 +/* active color */
    1.41 +static unsigned short fg_color = 0xffff;
    1.42 +static unsigned short bg_color = 0;
    1.43 +
    1.44 +/* transparent background */
    1.45 +static unsigned char bg_transparent = 1;
    1.46 +
    1.47 +/* ---- state manipulation ---- */
    1.48 +void set_font(struct font *fnt) {
    1.49 +	font = fnt;
    1.50 +}
    1.51 +
    1.52 +struct font *get_font(void) {
    1.53 +	return font;
    1.54 +}
    1.55 +
    1.56 +void set_text_color(unsigned short fg, unsigned short bg) {
    1.57 +	fg_color = fg;
    1.58 +	bg_color = bg;
    1.59 +}
    1.60 +
    1.61 +void set_text_writebg(int enable) {
    1.62 +	bg_transparent = !enable;
    1.63 +}
    1.64 +
    1.65 +struct pixel_buffer *get_glyph(unsigned char c, unsigned short fg, unsigned short bg, int bpp) {
    1.66 +	static struct pixel_buffer glyph;
    1.67 +
    1.68 +	if(glyph.x != font->x || glyph.y != font->y || glyph.bpp != bpp) {
    1.69 +		glyph.x = font->x;
    1.70 +		glyph.y = font->y;
    1.71 +		glyph.bpp = bpp;
    1.72 +		free(glyph.pixels);
    1.73 +		if(!(glyph.pixels = malloc(glyph.x * glyph.y * (bpp >> 3)))) {
    1.74 +			return 0;
    1.75 +		}
    1.76 +	}
    1.77 +
    1.78 +	if(bpp == 16) {
    1.79 +		int i, j;
    1.80 +		unsigned short *dptr = glyph.pixels;
    1.81 +		const unsigned char *bit_ptr = (unsigned char*)font->bitmap + font->y * (int)c;
    1.82 +		int sz = glyph.x * glyph.y;
    1.83 +
    1.84 +		for(i=0, j=0; i<sz; i++) {
    1.85 +			*dptr++ = (*bit_ptr & (0x80 >> j++)) ? fg : bg;
    1.86 +
    1.87 +			if(j > 7) {
    1.88 +				j = 0;
    1.89 +				bit_ptr++;
    1.90 +			}
    1.91 +		}
    1.92 +
    1.93 +		return &glyph;
    1.94 +	}
    1.95 +
    1.96 +	return 0;
    1.97 +}
    1.98 +
    1.99 +int draw_glyph(unsigned char c, int x, int y, struct pixel_buffer *pbuf) {
   1.100 +	struct pixel_buffer *glyph;
   1.101 +
   1.102 +	if(!(glyph = get_glyph(c, fg_color, bg_color, pbuf->bpp))) return -1;
   1.103 +	
   1.104 +	if(pbuf->bpp == 16) {
   1.105 +		int i, j;
   1.106 +		unsigned short *dptr = (unsigned short*)pbuf->pixels + y * pbuf->x + x;
   1.107 +		unsigned short *sptr = glyph->pixels;
   1.108 +		int advance = pbuf->x - glyph->x;
   1.109 +
   1.110 +		for(j=0; j<glyph->y; j++) {
   1.111 +			for(i=0; i<glyph->x; i++) {
   1.112 +				if(bg_transparent && *sptr == bg_color) {
   1.113 +					dptr++;
   1.114 +					sptr++;
   1.115 +				} else {
   1.116 +					*dptr++ = *sptr++;
   1.117 +				}
   1.118 +			}
   1.119 +			dptr += advance;
   1.120 +		}
   1.121 +	}
   1.122 +
   1.123 +	return 0;
   1.124 +}
   1.125 +
   1.126 +/* low level string rendering */
   1.127 +int draw_string(const char *str, int x, int y, struct pixel_buffer *pbuf) {
   1.128 +	while(*str) {
   1.129 +		if(draw_glyph(*str++, x, y, pbuf) == -1) return -1;
   1.130 +		x += font->x;
   1.131 +	}
   1.132 +	return 0;
   1.133 +}