gba-trycatch

annotate src/sdlsys/font.c @ 11:c47e54796b82

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