gba-x3dtest
view src/sdlsys/font.c @ 19:62390f9cc93e
texture cache optimization failed
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 30 Jun 2014 09:07:41 +0300 |
parents | |
children |
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 "gbasys.h"
26 #define DEFAULT_FONT &font_8x8
28 /* active font */
29 static struct font *font = DEFAULT_FONT;
31 /* active color */
32 static unsigned short fg_color = 0xffff;
33 static unsigned short bg_color = 0;
35 /* transparent background */
36 static unsigned char bg_transparent = 1;
38 /* ---- state manipulation ---- */
39 void set_font(struct font *fnt) {
40 font = fnt;
41 }
43 struct font *get_font(void) {
44 return font;
45 }
47 void set_text_color(unsigned short fg, unsigned short bg) {
48 fg_color = fg;
49 bg_color = bg;
50 }
52 void set_text_writebg(int enable) {
53 bg_transparent = !enable;
54 }
56 struct pixel_buffer *get_glyph(unsigned char c, unsigned short fg, unsigned short bg, int bpp) {
57 static struct pixel_buffer glyph;
59 if(glyph.x != font->x || glyph.y != font->y || glyph.bpp != bpp) {
60 glyph.x = font->x;
61 glyph.y = font->y;
62 glyph.bpp = bpp;
63 free(glyph.pixels);
64 if(!(glyph.pixels = malloc(glyph.x * glyph.y * (bpp >> 3)))) {
65 return 0;
66 }
67 }
69 if(bpp == 16) {
70 int i, j;
71 unsigned short *dptr = glyph.pixels;
72 const unsigned char *bit_ptr = (unsigned char*)font->bitmap + font->y * (int)c;
73 int sz = glyph.x * glyph.y;
75 for(i=0, j=0; i<sz; i++) {
76 *dptr++ = (*bit_ptr & (0x80 >> j++)) ? fg : bg;
78 if(j > 7) {
79 j = 0;
80 bit_ptr++;
81 }
82 }
84 return &glyph;
85 }
87 return 0;
88 }
90 int draw_glyph(unsigned char c, int x, int y, struct pixel_buffer *pbuf) {
91 struct pixel_buffer *glyph;
93 if(!(glyph = get_glyph(c, fg_color, bg_color, pbuf->bpp))) return -1;
95 if(pbuf->bpp == 16) {
96 int i, j;
97 unsigned short *dptr = (unsigned short*)pbuf->pixels + y * pbuf->x + x;
98 unsigned short *sptr = glyph->pixels;
99 int advance = pbuf->x - glyph->x;
101 for(j=0; j<glyph->y; j++) {
102 for(i=0; i<glyph->x; i++) {
103 if(bg_transparent && *sptr == bg_color) {
104 dptr++;
105 sptr++;
106 } else {
107 *dptr++ = *sptr++;
108 }
109 }
110 dptr += advance;
111 }
112 }
114 return 0;
115 }
117 /* low level string rendering */
118 int draw_string(const char *str, int x, int y, struct pixel_buffer *pbuf) {
119 while(*str) {
120 if(draw_glyph(*str++, x, y, pbuf) == -1) return -1;
121 x += font->x;
122 }
123 return 0;
124 }