1 /*  TFont: On systems that support TTF FONTS, this creates font images
2     instead of using font images in SFont.c
3     Copyright (C) 2011 Dan Espen
4     Copyright (C) 2010,2011 Frank Zago
5     Copyright (C) 2003 Karl Bartel
6 
7     License: GPL or LGPL (at your choice)
8     WWW: http://www.linux-games.com/sfont/
9 
10     This program is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14 
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19 
20     You should have received a copy of the GNU General Public License
21     along with this program; if not, write to the Free Software
22     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 
24     Karl Bartel
25     Cecilienstr. 14
26     12307 Berlin
27     GERMANY
28     karlb@gmx.net
29 */
30 
31 /* If NOT using TTF Fonts, this file does nothing,
32    check Sfont.c. */
33 #ifdef HAVE_LIBSDL_TTF
34 #include <assert.h>
35 
36 #include "xgalaga.h"
37 
38 #include <sys/param.h>
39 #include <SDL/SDL_ttf.h>
40 
41 static const SDL_Color S_White  = {0xFF, 0xFF, 0xFF, 0};
42 static const SDL_Color S_Green  = {0x00, 0xFF, 0x00, 0};
43 static const SDL_Color S_Cyan   = {0x00, 0xFF, 0xFF, 0};
44 static const SDL_Color S_Red    = {0xFF, 0x00, 0x00, 0};
45 static const SDL_Color S_Yellow = {0xFF, 0xFF, 0x00, 0};
46 static const SDL_Color S_Black  = {0x00, 0x00, 0x00, 0};
47 static const SDL_Color S_Grey   = {0x88, 0x88, 0x88, 0};
48 
SFont_InitFont(int which)49 SFont_Font* SFont_InitFont(int which)
50 {
51     int i = 0;
52     SFont_Font* Font;
53     TTF_Font* tmpfont;
54 	static int once = 1;
55 	SDL_Color Color;
56 	int Size;
57 	char fontname[MAXPATHLEN];
58 
59 	/* First time in, initialize the TTF subsysten. */
60 	if (once) {
61 		once = 0;
62 
63 		if (TTF_Init() == -1) {
64 			fprintf(stderr,"Bad return code from TTF_Init (%s)\n", TTF_GetError());
65 			return NULL;
66 		}
67 		atexit(TTF_Quit);
68 	}
69 
70 	Size = 10;
71 
72 	switch(which) {
73 	case F_REG_GREEN:
74 		Color = S_Green;
75 		break;
76 	case F_REG_CYAN:
77 		Color = S_Cyan;
78 		break;
79 	case F_REG_YELLOW:
80 		Color = S_Yellow;
81 		break;
82 	case F_REG_RED:
83 		Color = S_Red;
84 		break;
85 	case F_REG_GREY:
86 		Color = S_Grey;
87 		break;
88 	case F_BIG_RED:
89 		Size = 46;
90 		Color = S_Red;
91 		break;
92 	default:
93 		fprintf(stderr, "Unknown font requested.\n");
94 		return NULL;
95 		break;
96 	}
97 
98 	snprintf(fontname, sizeof(fontname), "%s/fonts/%s", DATADIR, "LiberationMono-Bold.ttf");
99 	tmpfont = TTF_OpenFont(fontname, Size);
100 	if (tmpfont == NULL) {
101 		fprintf(stderr,"Bad return code from TTF_OpenFont (%s)\n",TTF_GetError());
102 		return NULL;
103 	}
104 
105 	Font = malloc(sizeof(SFont_Font));
106 	if (!Font)
107 		return NULL;
108 
109     for (i=32;i<127;i++) {
110 		int hi, wi;
111 		char ca[2];
112 		ca[0]=i;
113 		ca[1]=0;
114 		Font->CharSurf[i-32]=TTF_RenderText_Blended(tmpfont, ca, Color);
115 		hi = Font->CharSurf[i-32]->h;
116 		wi = Font->CharSurf[i-32]->w;
117     }
118 
119 	TTF_CloseFont(tmpfont);
120 
121     return Font;
122 }
123 
SFont_Write(const SFont_Font * Font,int x,int y,const char * text)124 void SFont_Write(const SFont_Font *Font, int x, int y, const char *text)
125 {
126     int charoffset;
127     SDL_Rect srcrect, dstrect;
128     int i;
129 
130     if(text == NULL)
131 		return;
132 
133     srcrect.y = 0;
134     dstrect.y = y+WINTOPOV;
135     srcrect.h = dstrect.h = Font->CharSurf[0]->h;
136     for(i=0; text[i] != '\0' && x <= screen->w ; i++) {
137 		charoffset = text[i] - 32;
138 		srcrect.w = dstrect.w = Font->CharSurf[charoffset]->w;
139 		srcrect.x = 0;
140 		dstrect.x = x;
141 		SDL_BlitSurface(Font->CharSurf[charoffset], &srcrect, screen, &dstrect);
142 		dstrect.h=srcrect.h;              /* changed by blit */
143 		x += dstrect.w;
144     }
145 }
146 
SFont_TextWidth(const SFont_Font * Font,const char * text)147 int SFont_TextWidth(const SFont_Font *Font, const char *text)
148 {
149     int charoffset=0;
150     int width = 0;
151     int i;
152 
153     if (text == NULL)
154 		return 0;
155 
156     for(i=0; text[i] != '\0'; i++) {
157 		charoffset = text[i] - 32;
158 		width += Font->CharSurf[charoffset]->w;
159     }
160 
161     return width;
162 }
163 
SFont_TextHeight(const SFont_Font * Font)164 int SFont_TextHeight(const SFont_Font* Font)
165 {
166     return Font->CharSurf[0]->h;
167 }
SFont_WriteCenter(const SFont_Font * Font,int y,const char * text)168 void SFont_WriteCenter(const SFont_Font *Font,
169 					   int y, const char *text)
170 {
171 	SFont_Write(Font, (screen->w - SFont_TextWidth(Font, text))/2, y, text);
172 }
173 
SFont_WriteRight(const SFont_Font * Font,int y,const char * text)174 void SFont_WriteRight(const SFont_Font *Font,
175 					  int y, const char *text)
176 {
177 	SFont_Write(Font, screen->w - SFont_TextWidth(Font, text), y, text);
178 }
179 #endif
180