2 showfont: An example of using the SDL_ttf library with 2D graphics.
3 Copyright (C) 1997-2004 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /* A simple program to test the text rendering feature of the TTF library */
38 #define DEFAULT_PTSIZE 18
39 #define DEFAULT_TEXT "The quick brown fox jumped over the lazy dog"
40 #define NUM_COLORS 256
43 "Usage: %s [-solid] [-utf8|-unicode] [-b] [-i] [-u] [-fgcol r,g,b] [-bgcol r,g,b] <font>.ttf [ptsize] [text]\n";
45 static void cleanup(int exitcode
)
52 int main(int argc
, char *argv
[])
54 char *argv0
= argv
[0];
57 SDL_Surface
*text
, *temp
;
60 int rdiff
, gdiff
, bdiff
;
61 SDL_Color colors
[NUM_COLORS
];
62 SDL_Color white
= { 0xFF, 0xFF, 0xFF, 0 };
63 SDL_Color black
= { 0x00, 0x00, 0x00, 0 };
76 char *message
, string
[128];
78 /* Look for special execution mode */
80 /* Look for special rendering types */
82 renderstyle
= TTF_STYLE_NORMAL
;
83 rendertype
= RENDER_LATIN1
;
84 /* Default is black and white */
87 for ( i
=1; argv
[i
] && argv
[i
][0] == '-'; ++i
) {
88 if ( strcmp(argv
[i
], "-solid") == 0 ) {
91 if ( strcmp(argv
[i
], "-utf8") == 0 ) {
92 rendertype
= RENDER_UTF8
;
94 if ( strcmp(argv
[i
], "-unicode") == 0 ) {
95 rendertype
= RENDER_UNICODE
;
97 if ( strcmp(argv
[i
], "-b") == 0 ) {
98 renderstyle
|= TTF_STYLE_BOLD
;
100 if ( strcmp(argv
[i
], "-i") == 0 ) {
101 renderstyle
|= TTF_STYLE_ITALIC
;
103 if ( strcmp(argv
[i
], "-u") == 0 ) {
104 renderstyle
|= TTF_STYLE_UNDERLINE
;
106 if ( strcmp(argv
[i
], "-dump") == 0 ) {
109 if ( strcmp(argv
[i
], "-fgcol") == 0 ) {
111 if ( sscanf (argv
[++i
], "%d,%d,%d", &r
, &g
, &b
) != 3 ) {
112 fprintf(stderr
, Usage
, argv0
);
115 forecol
->r
= (Uint8
)r
;
116 forecol
->g
= (Uint8
)g
;
117 forecol
->b
= (Uint8
)b
;
119 if ( strcmp(argv
[i
], "-bgcol") == 0 ) {
121 if ( sscanf (argv
[++i
], "%d,%d,%d", &r
, &g
, &b
) != 3 ) {
122 fprintf(stderr
, Usage
, argv0
);
125 backcol
->r
= (Uint8
)r
;
126 backcol
->g
= (Uint8
)g
;
127 backcol
->b
= (Uint8
)b
;
129 fprintf(stderr
, Usage
, argv0
);
138 fprintf(stderr
, Usage
, argv0
);
143 if ( SDL_Init(SDL_INIT_VIDEO
) < 0 ) {
144 fprintf(stderr
, "Couldn't initialize SDL: %s\n",SDL_GetError());
148 /* Initialize the TTF library */
149 if ( TTF_Init() < 0 ) {
150 fprintf(stderr
, "Couldn't initialize TTF: %s\n",SDL_GetError());
155 /* Open the font file with the requested point size */
158 ptsize
= atoi(argv
[1]);
162 ptsize
= DEFAULT_PTSIZE
;
166 font
= TTF_OpenFont(argv
[0], ptsize
);
167 if ( font
== NULL
) {
168 fprintf(stderr
, "Couldn't load %d pt font from %s: %s\n",
169 ptsize
, argv
[0], SDL_GetError());
172 TTF_SetFontStyle(font
, renderstyle
);
175 for( i
= 48; i
< 123; i
++ ) {
176 SDL_Surface
* glyph
= NULL
;
178 glyph
= TTF_RenderGlyph_Shaded( font
, i
, *forecol
, *backcol
);
182 sprintf( outname
, "glyph-%d.bmp", i
);
183 SDL_SaveBMP( glyph
, outname
);
190 /* Set a 640x480x8 video mode */
191 screen
= SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE
);
192 if ( screen
== NULL
) {
193 fprintf(stderr
, "Couldn't set 640x480x8 video mode: %s\n",
198 /* Set a palette that is good for the foreground colored text */
199 rdiff
= backcol
->r
- forecol
->r
;
200 gdiff
= backcol
->g
- forecol
->g
;
201 bdiff
= backcol
->b
- forecol
->b
;
202 for ( i
=0; i
<NUM_COLORS
; ++i
) {
203 colors
[i
].r
= forecol
->r
+ (i
*rdiff
)/4;
204 colors
[i
].g
= forecol
->g
+ (i
*gdiff
)/4;
205 colors
[i
].b
= forecol
->b
+ (i
*bdiff
)/4;
207 SDL_SetColors(screen
, colors
, 0, NUM_COLORS
);
209 /* Clear the background to background color */
210 SDL_FillRect(screen
, NULL
,
211 SDL_MapRGB(screen
->format
, backcol
->r
, backcol
->g
, backcol
->b
));
212 SDL_UpdateRect(screen
, 0, 0, 0, 0);
214 /* Show which font file we're looking at */
215 sprintf(string
, "Font file: %s", argv
[0]); /* possible overflow */
217 text
= TTF_RenderText_Solid(font
, string
, *forecol
);
219 text
= TTF_RenderText_Shaded(font
, string
, *forecol
, *backcol
);
221 if ( text
!= NULL
) {
226 SDL_BlitSurface(text
, NULL
, screen
, &dstrect
);
227 SDL_FreeSurface(text
);
230 /* Render and center the message */
234 message
= DEFAULT_TEXT
;
236 switch (rendertype
) {
239 text
= TTF_RenderText_Solid(font
,message
,*forecol
);
241 text
= TTF_RenderText_Shaded(font
,message
,*forecol
,*backcol
);
247 text
= TTF_RenderUTF8_Solid(font
,message
,*forecol
);
249 text
= TTF_RenderUTF8_Shaded(font
,message
,*forecol
,*backcol
);
255 Uint16 unicode_text
[BUFSIZ
];
257 /* Use iconv to convert the message into utf-16.
258 * "char" and "" are aliases for the local 8-bit encoding */
260 /*ICONV_CONST*/ char *from_str
= message
;
261 char *to_str
= (char*)unicode_text
;
262 size_t from_sz
= strlen(message
) + 1;
263 size_t to_sz
= sizeof(unicode_text
);
266 if ((cd
= iconv_open("UTF-16", "char")) == (iconv_t
)-1
267 && (cd
= iconv_open("UTF-16", "")) == (iconv_t
)-1) {
268 perror("Couldn't open iconv");
272 res
= iconv(cd
, &from_str
, &from_sz
, &to_str
, &to_sz
);
274 perror("Couldn't use iconv");
282 /* Convert the message from ascii into utf-16.
283 * This is unreliable as a test because it always
284 * gives the local ordering. */
285 for (index
= 0; message
[index
]; index
++) {
286 unicode_text
[index
] = message
[index
];
288 unicode_text
[index
] = 0;
292 text
= TTF_RenderUNICODE_Solid(font
,
293 unicode_text
, *forecol
);
295 text
= TTF_RenderUNICODE_Shaded(font
,
296 unicode_text
, *forecol
, *backcol
);
301 text
= NULL
; /* This shouldn't happen */
304 if ( text
== NULL
) {
305 fprintf(stderr
, "Couldn't render text: %s\n", SDL_GetError());
309 dstrect
.x
= (screen
->w
- text
->w
)/2;
310 dstrect
.y
= (screen
->h
- text
->h
)/2;
313 printf("Font is generally %d big, and string is %hd big\n",
314 TTF_FontHeight(font
), text
->h
);
316 /* Blit the text surface */
317 if ( SDL_BlitSurface(text
, NULL
, screen
, &dstrect
) < 0 ) {
318 fprintf(stderr
, "Couldn't blit text to display: %s\n",
323 SDL_UpdateRect(screen
, 0, 0, 0, 0);
325 /* Set the text colorkey and convert to display format */
326 if ( SDL_SetColorKey(text
, SDL_SRCCOLORKEY
|SDL_RLEACCEL
, 0) < 0 ) {
327 fprintf(stderr
, "Warning: Couldn't set text colorkey: %s\n",
330 temp
= SDL_DisplayFormat(text
);
331 if ( temp
!= NULL
) {
332 SDL_FreeSurface(text
);
336 /* Wait for a keystroke, and blit text on mouse press */
339 if ( SDL_WaitEvent(&event
) < 0 ) {
340 fprintf(stderr
, "SDL_PullEvent() error: %s\n",
345 switch (event
.type
) {
346 case SDL_MOUSEBUTTONDOWN
:
347 dstrect
.x
= event
.button
.x
- text
->w
/2;
348 dstrect
.y
= event
.button
.y
- text
->h
/2;
351 if ( SDL_BlitSurface(text
, NULL
, screen
,
353 SDL_UpdateRects(screen
, 1, &dstrect
);
356 "Couldn't blit text to display: %s\n",
369 SDL_FreeSurface(text
);
373 /* Not reached, but fixes compiler warnings */