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