gbasys

view src/font.c @ 3:06726f0b8cd3

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