gbasys

annotate src/font.c @ 1:c50064b181c2

stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 04 Mar 2012 07:06:41 +0200
parents 875ef6085efc
children 72c6429ae953
rev   line source
nuclear@0 1 /*
nuclear@0 2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
nuclear@0 3
nuclear@1 4 This file is part of gbasys, a library for GameBoy Advance development.
nuclear@0 5
nuclear@0 6 This program is free software; you can redistribute it and/or modify
nuclear@0 7 it under the terms of the GNU General Public License as published by
nuclear@0 8 the Free Software Foundation; either version 2 of the License, or
nuclear@0 9 (at your option) any later version.
nuclear@0 10
nuclear@0 11 This program is distributed in the hope that it will be useful,
nuclear@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@0 14 GNU General Public License for more details.
nuclear@0 15
nuclear@0 16 You should have received a copy of the GNU General Public License
nuclear@0 17 along with this program; if not, write to the Free Software
nuclear@0 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
nuclear@0 19 */
nuclear@0 20
nuclear@1 21 #include "config.h"
nuclear@0 22 #include <stdlib.h>
nuclear@0 23 #include "font.h"
nuclear@0 24 #include "gfx.h"
nuclear@0 25
nuclear@0 26 #if defined(CONFIG_FONT_8X16)
nuclear@0 27 #define DEFAULT_FONT &font_8x16
nuclear@0 28 #elif defined(CONFIG_FONT_8X8)
nuclear@0 29 #define DEFAULT_FONT &font_8x8
nuclear@0 30 #else /* no font compiled in */
nuclear@0 31 #define DEFAULT_FONT 0
nuclear@0 32 #endif /* CONFIG_FONT_8X16 */
nuclear@0 33
nuclear@0 34 /* active font */
nuclear@0 35 static struct font *font = DEFAULT_FONT;
nuclear@0 36
nuclear@0 37 /* active color */
nuclear@0 38 static unsigned short fg_color = 0xffff;
nuclear@0 39 static unsigned short bg_color = 0;
nuclear@0 40
nuclear@0 41 /* transparent background */
nuclear@0 42 static unsigned char bg_transparent = 1;
nuclear@0 43
nuclear@0 44 /* ---- state manipulation ---- */
nuclear@0 45 void set_font(struct font *fnt) {
nuclear@0 46 font = fnt;
nuclear@0 47 }
nuclear@0 48
nuclear@0 49 struct font *get_font(void) {
nuclear@0 50 return font;
nuclear@0 51 }
nuclear@0 52
nuclear@0 53 void set_text_color(unsigned short fg, unsigned short bg) {
nuclear@0 54 fg_color = fg;
nuclear@0 55 bg_color = bg;
nuclear@0 56 }
nuclear@0 57
nuclear@0 58 void set_text_writebg(int enable) {
nuclear@0 59 bg_transparent = !enable;
nuclear@0 60 }
nuclear@0 61
nuclear@0 62 struct pixel_buffer *get_glyph(unsigned char c, unsigned short fg, unsigned short bg, int bpp) {
nuclear@0 63 static struct pixel_buffer glyph;
nuclear@0 64
nuclear@0 65 if(glyph.x != font->x || glyph.y != font->y || glyph.bpp != bpp) {
nuclear@0 66 glyph.x = font->x;
nuclear@0 67 glyph.y = font->y;
nuclear@0 68 glyph.bpp = bpp;
nuclear@0 69 free(glyph.pixels);
nuclear@0 70 if(!(glyph.pixels = malloc(glyph.x * glyph.y * (bpp >> 3)))) {
nuclear@0 71 return 0;
nuclear@0 72 }
nuclear@0 73 }
nuclear@0 74
nuclear@0 75 if(bpp == 16) {
nuclear@0 76 int i, j;
nuclear@0 77 unsigned short *dptr = glyph.pixels;
nuclear@0 78 const unsigned char *bit_ptr = (unsigned char*)font->bitmap + font->y * (int)c;
nuclear@0 79 int sz = glyph.x * glyph.y;
nuclear@0 80
nuclear@0 81 for(i=0, j=0; i<sz; i++) {
nuclear@0 82 *dptr++ = (*bit_ptr & (0x80 >> j++)) ? fg : bg;
nuclear@0 83
nuclear@0 84 if(j > 7) {
nuclear@0 85 j = 0;
nuclear@0 86 bit_ptr++;
nuclear@0 87 }
nuclear@0 88 }
nuclear@0 89
nuclear@0 90 return &glyph;
nuclear@0 91 }
nuclear@0 92
nuclear@0 93 return 0;
nuclear@0 94 }
nuclear@0 95
nuclear@0 96 int draw_glyph(unsigned char c, int x, int y, struct pixel_buffer *pbuf) {
nuclear@0 97 struct pixel_buffer *glyph;
nuclear@0 98
nuclear@0 99 if(!(glyph = get_glyph(c, fg_color, bg_color, pbuf->bpp))) return -1;
nuclear@1 100
nuclear@0 101 if(pbuf->bpp == 16) {
nuclear@0 102 int i, j;
nuclear@0 103 unsigned short *dptr = (unsigned short*)pbuf->pixels + y * pbuf->x + x;
nuclear@0 104 unsigned short *sptr = glyph->pixels;
nuclear@0 105 int advance = pbuf->x - glyph->x;
nuclear@0 106
nuclear@0 107 for(j=0; j<glyph->y; j++) {
nuclear@0 108 for(i=0; i<glyph->x; i++) {
nuclear@0 109 if(bg_transparent && *sptr == bg_color) {
nuclear@0 110 dptr++;
nuclear@0 111 sptr++;
nuclear@0 112 } else {
nuclear@0 113 *dptr++ = *sptr++;
nuclear@0 114 }
nuclear@0 115 }
nuclear@0 116 dptr += advance;
nuclear@0 117 }
nuclear@0 118 }
nuclear@0 119
nuclear@0 120 return 0;
nuclear@0 121 }
nuclear@0 122
nuclear@0 123 /* low level string rendering */
nuclear@0 124 int draw_string(const char *str, int x, int y, struct pixel_buffer *pbuf) {
nuclear@0 125 while(*str) {
nuclear@0 126 if(draw_glyph(*str++, x, y, pbuf) == -1) return -1;
nuclear@0 127 x += font->x;
nuclear@0 128 }
nuclear@0 129 return 0;
nuclear@0 130 }