gba-trycatch

diff src/sdlsys/font.c @ 5:850be43b3135

sdl version
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 16 Jun 2014 22:01:45 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/sdlsys/font.c	Mon Jun 16 22:01:45 2014 +0300
     1.3 @@ -0,0 +1,124 @@
     1.4 +/*
     1.5 +Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
     1.6 +
     1.7 +This file is part of gbasys, 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 "config.h"
    1.25 +#include <stdlib.h>
    1.26 +#include "font.h"
    1.27 +#include "gbasys.h"
    1.28 +
    1.29 +#define DEFAULT_FONT	&font_8x8
    1.30 +
    1.31 +/* active font */
    1.32 +static struct font *font = DEFAULT_FONT;
    1.33 +
    1.34 +/* active color */
    1.35 +static unsigned short fg_color = 0xffff;
    1.36 +static unsigned short bg_color = 0;
    1.37 +
    1.38 +/* transparent background */
    1.39 +static unsigned char bg_transparent = 1;
    1.40 +
    1.41 +/* ---- state manipulation ---- */
    1.42 +void set_font(struct font *fnt) {
    1.43 +	font = fnt;
    1.44 +}
    1.45 +
    1.46 +struct font *get_font(void) {
    1.47 +	return font;
    1.48 +}
    1.49 +
    1.50 +void set_text_color(unsigned short fg, unsigned short bg) {
    1.51 +	fg_color = fg;
    1.52 +	bg_color = bg;
    1.53 +}
    1.54 +
    1.55 +void set_text_writebg(int enable) {
    1.56 +	bg_transparent = !enable;
    1.57 +}
    1.58 +
    1.59 +struct pixel_buffer *get_glyph(unsigned char c, unsigned short fg, unsigned short bg, int bpp) {
    1.60 +	static struct pixel_buffer glyph;
    1.61 +
    1.62 +	if(glyph.x != font->x || glyph.y != font->y || glyph.bpp != bpp) {
    1.63 +		glyph.x = font->x;
    1.64 +		glyph.y = font->y;
    1.65 +		glyph.bpp = bpp;
    1.66 +		free(glyph.pixels);
    1.67 +		if(!(glyph.pixels = malloc(glyph.x * glyph.y * (bpp >> 3)))) {
    1.68 +			return 0;
    1.69 +		}
    1.70 +	}
    1.71 +
    1.72 +	if(bpp == 16) {
    1.73 +		int i, j;
    1.74 +		unsigned short *dptr = glyph.pixels;
    1.75 +		const unsigned char *bit_ptr = (unsigned char*)font->bitmap + font->y * (int)c;
    1.76 +		int sz = glyph.x * glyph.y;
    1.77 +
    1.78 +		for(i=0, j=0; i<sz; i++) {
    1.79 +			*dptr++ = (*bit_ptr & (0x80 >> j++)) ? fg : bg;
    1.80 +
    1.81 +			if(j > 7) {
    1.82 +				j = 0;
    1.83 +				bit_ptr++;
    1.84 +			}
    1.85 +		}
    1.86 +
    1.87 +		return &glyph;
    1.88 +	}
    1.89 +
    1.90 +	return 0;
    1.91 +}
    1.92 +
    1.93 +int draw_glyph(unsigned char c, int x, int y, struct pixel_buffer *pbuf) {
    1.94 +	struct pixel_buffer *glyph;
    1.95 +
    1.96 +	if(!(glyph = get_glyph(c, fg_color, bg_color, pbuf->bpp))) return -1;
    1.97 +
    1.98 +	if(pbuf->bpp == 16) {
    1.99 +		int i, j;
   1.100 +		unsigned short *dptr = (unsigned short*)pbuf->pixels + y * pbuf->x + x;
   1.101 +		unsigned short *sptr = glyph->pixels;
   1.102 +		int advance = pbuf->x - glyph->x;
   1.103 +
   1.104 +		for(j=0; j<glyph->y; j++) {
   1.105 +			for(i=0; i<glyph->x; i++) {
   1.106 +				if(bg_transparent && *sptr == bg_color) {
   1.107 +					dptr++;
   1.108 +					sptr++;
   1.109 +				} else {
   1.110 +					*dptr++ = *sptr++;
   1.111 +				}
   1.112 +			}
   1.113 +			dptr += advance;
   1.114 +		}
   1.115 +	}
   1.116 +
   1.117 +	return 0;
   1.118 +}
   1.119 +
   1.120 +/* low level string rendering */
   1.121 +int draw_string(const char *str, int x, int y, struct pixel_buffer *pbuf) {
   1.122 +	while(*str) {
   1.123 +		if(draw_glyph(*str++, x, y, pbuf) == -1) return -1;
   1.124 +		x += font->x;
   1.125 +	}
   1.126 +	return 0;
   1.127 +}