nuclear@5: /* nuclear@5: Copyright 2004 John Tsiombikas nuclear@5: nuclear@5: This file is part of gbasys, a library for GameBoy Advance development. nuclear@5: nuclear@5: This program is free software; you can redistribute it and/or modify nuclear@5: it under the terms of the GNU General Public License as published by nuclear@5: the Free Software Foundation; either version 2 of the License, or nuclear@5: (at your option) any later version. nuclear@5: nuclear@5: This program is distributed in the hope that it will be useful, nuclear@5: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@5: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@5: GNU General Public License for more details. nuclear@5: nuclear@5: You should have received a copy of the GNU General Public License nuclear@5: along with this program; if not, write to the Free Software nuclear@5: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA nuclear@5: */ nuclear@5: nuclear@5: #include "config.h" nuclear@5: #include nuclear@5: #include "font.h" nuclear@5: #include "gbasys.h" nuclear@5: nuclear@5: #define DEFAULT_FONT &font_8x8 nuclear@5: nuclear@5: /* active font */ nuclear@5: static struct font *font = DEFAULT_FONT; nuclear@5: nuclear@5: /* active color */ nuclear@5: static unsigned short fg_color = 0xffff; nuclear@5: static unsigned short bg_color = 0; nuclear@5: nuclear@5: /* transparent background */ nuclear@5: static unsigned char bg_transparent = 1; nuclear@5: nuclear@5: /* ---- state manipulation ---- */ nuclear@5: void set_font(struct font *fnt) { nuclear@5: font = fnt; nuclear@5: } nuclear@5: nuclear@5: struct font *get_font(void) { nuclear@5: return font; nuclear@5: } nuclear@5: nuclear@5: void set_text_color(unsigned short fg, unsigned short bg) { nuclear@5: fg_color = fg; nuclear@5: bg_color = bg; nuclear@5: } nuclear@5: nuclear@5: void set_text_writebg(int enable) { nuclear@5: bg_transparent = !enable; nuclear@5: } nuclear@5: nuclear@5: struct pixel_buffer *get_glyph(unsigned char c, unsigned short fg, unsigned short bg, int bpp) { nuclear@5: static struct pixel_buffer glyph; nuclear@5: nuclear@5: if(glyph.x != font->x || glyph.y != font->y || glyph.bpp != bpp) { nuclear@5: glyph.x = font->x; nuclear@5: glyph.y = font->y; nuclear@5: glyph.bpp = bpp; nuclear@5: free(glyph.pixels); nuclear@5: if(!(glyph.pixels = malloc(glyph.x * glyph.y * (bpp >> 3)))) { nuclear@5: return 0; nuclear@5: } nuclear@5: } nuclear@5: nuclear@5: if(bpp == 16) { nuclear@5: int i, j; nuclear@5: unsigned short *dptr = glyph.pixels; nuclear@5: const unsigned char *bit_ptr = (unsigned char*)font->bitmap + font->y * (int)c; nuclear@5: int sz = glyph.x * glyph.y; nuclear@5: nuclear@5: for(i=0, j=0; i> j++)) ? fg : bg; nuclear@5: nuclear@5: if(j > 7) { nuclear@5: j = 0; nuclear@5: bit_ptr++; nuclear@5: } nuclear@5: } nuclear@5: nuclear@5: return &glyph; nuclear@5: } nuclear@5: nuclear@5: return 0; nuclear@5: } nuclear@5: nuclear@5: int draw_glyph(unsigned char c, int x, int y, struct pixel_buffer *pbuf) { nuclear@5: struct pixel_buffer *glyph; nuclear@5: nuclear@5: if(!(glyph = get_glyph(c, fg_color, bg_color, pbuf->bpp))) return -1; nuclear@5: nuclear@5: if(pbuf->bpp == 16) { nuclear@5: int i, j; nuclear@5: unsigned short *dptr = (unsigned short*)pbuf->pixels + y * pbuf->x + x; nuclear@5: unsigned short *sptr = glyph->pixels; nuclear@5: int advance = pbuf->x - glyph->x; nuclear@5: nuclear@5: for(j=0; jy; j++) { nuclear@5: for(i=0; ix; i++) { nuclear@5: if(bg_transparent && *sptr == bg_color) { nuclear@5: dptr++; nuclear@5: sptr++; nuclear@5: } else { nuclear@5: *dptr++ = *sptr++; nuclear@5: } nuclear@5: } nuclear@5: dptr += advance; nuclear@5: } nuclear@5: } nuclear@5: nuclear@5: return 0; nuclear@5: } nuclear@5: nuclear@5: /* low level string rendering */ nuclear@5: int draw_string(const char *str, int x, int y, struct pixel_buffer *pbuf) { nuclear@5: while(*str) { nuclear@5: if(draw_glyph(*str++, x, y, pbuf) == -1) return -1; nuclear@5: x += font->x; nuclear@5: } nuclear@5: return 0; nuclear@5: }