-
Notifications
You must be signed in to change notification settings - Fork 19
/
font.c
71 lines (57 loc) · 1.53 KB
/
font.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <SDL/SDL_ttf.h>
#include <stdio.h>
#include "config.h"
#include "font.h"
#include "sdl_ogl.h"
static const int DEFAULT_FONT_SIZE = 50;
static TTF_Font *font = NULL;
static SDL_Color col = { 255, 255, 255 };
int font_init( void ) {
const struct config *config = config_get();
int size;
if( config->iface.theme.font_size > 0 )
size = config->iface.theme.font_size;
else
size = DEFAULT_FONT_SIZE;
col.r = config->iface.theme.font_rgb.red;
col.g = config->iface.theme.font_rgb.green;
col.b = config->iface.theme.font_rgb.blue;
if( TTF_Init() != 0 ) {
fprintf( stderr, "Error: Couldn't initialise font library: %s\n", TTF_GetError() );
return -1;
}
if( config->iface.theme.font_file && *config->iface.theme.font_file ) {
font = TTF_OpenFont( config->iface.theme.font_file, size );
if( font == NULL ) {
fprintf( stderr, "Error: Couldn't load font '%s': %s\n", config->iface.theme.font_file, TTF_GetError() );
return -1;
}
}
return 0;
}
void font_free( void ) {
if( font )
TTF_CloseFont( font );
TTF_Quit();
}
void font_pause( void ) {
font_free();
}
int font_resume( void ) {
return font_init();
}
SDL_Surface *font_render( const char *text ) {
return TTF_RenderText_Blended( font, text, col );
}
struct texture *font_create_texture( const char *text ) {
struct texture *t = NULL;
SDL_Surface *s = font_render( text );
if( s ) {
t = ogl_create_texture( s );
if( t == NULL ) {
fprintf( stderr, "Error: Couldn't create texture for text '%s'\n", text );
}
SDL_FreeSurface( s );
}
return t;
}