gbasys
view src/font.c @ 10:9d05431f4a16
fixed GET_R/GET_B macros which where reversed
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 30 Jan 2016 05:23:48 +0200 |
parents | c50064b181c2 |
children |
line source
1 /*
2 gbasys - a gameboy advance hardware abstraction library
3 Copyright (C) 2004-2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "config.h"
19 #include <stdlib.h>
20 #include "font.h"
21 #include "gfx.h"
23 #if defined(CONFIG_FONT_8X16)
24 #define DEFAULT_FONT &font_8x16
25 #elif defined(CONFIG_FONT_8X8)
26 #define DEFAULT_FONT &font_8x8
27 #else /* no font compiled in */
28 #define DEFAULT_FONT 0
29 #endif /* CONFIG_FONT_8X16 */
31 /* active font */
32 static struct font *font = DEFAULT_FONT;
34 /* active color */
35 static unsigned short fg_color = 0xffff;
36 static unsigned short bg_color = 0;
38 /* transparent background */
39 static unsigned char bg_transparent = 1;
41 /* ---- state manipulation ---- */
42 void set_font(struct font *fnt) {
43 font = fnt;
44 }
46 struct font *get_font(void) {
47 return font;
48 }
50 void set_text_color(unsigned short fg, unsigned short bg) {
51 fg_color = fg;
52 bg_color = bg;
53 }
55 void set_text_writebg(int enable) {
56 bg_transparent = !enable;
57 }
59 struct pixel_buffer *get_glyph(unsigned char c, unsigned short fg, unsigned short bg, int bpp) {
60 static struct pixel_buffer glyph;
62 if(glyph.x != font->x || glyph.y != font->y || glyph.bpp != bpp) {
63 glyph.x = font->x;
64 glyph.y = font->y;
65 glyph.bpp = bpp;
66 free(glyph.pixels);
67 if(!(glyph.pixels = malloc(glyph.x * glyph.y * (bpp >> 3)))) {
68 return 0;
69 }
70 }
72 if(bpp == 16) {
73 int i, j;
74 unsigned short *dptr = glyph.pixels;
75 const unsigned char *bit_ptr = (unsigned char*)font->bitmap + font->y * (int)c;
76 int sz = glyph.x * glyph.y;
78 for(i=0, j=0; i<sz; i++) {
79 *dptr++ = (*bit_ptr & (0x80 >> j++)) ? fg : bg;
81 if(j > 7) {
82 j = 0;
83 bit_ptr++;
84 }
85 }
87 return &glyph;
88 }
90 return 0;
91 }
93 int draw_glyph(unsigned char c, int x, int y, struct pixel_buffer *pbuf) {
94 struct pixel_buffer *glyph;
96 if(!(glyph = get_glyph(c, fg_color, bg_color, pbuf->bpp))) return -1;
98 if(pbuf->bpp == 16) {
99 int i, j;
100 unsigned short *dptr = (unsigned short*)pbuf->pixels + y * pbuf->x + x;
101 unsigned short *sptr = glyph->pixels;
102 int advance = pbuf->x - glyph->x;
104 for(j=0; j<glyph->y; j++) {
105 for(i=0; i<glyph->x; i++) {
106 if(bg_transparent && *sptr == bg_color) {
107 dptr++;
108 sptr++;
109 } else {
110 *dptr++ = *sptr++;
111 }
112 }
113 dptr += advance;
114 }
115 }
117 return 0;
118 }
120 /* low level string rendering */
121 int draw_string(const char *str, int x, int y, struct pixel_buffer *pbuf) {
122 while(*str) {
123 if(draw_glyph(*str++, x, y, pbuf) == -1) return -1;
124 x += font->x;
125 }
126 return 0;
127 }