-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl_ttf.c
168 lines (125 loc) · 3.49 KB
/
sdl_ttf.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gprolog.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include "sdllib_common.h"
#define RETURN_TTF_FAIL(F) \
fprintf(stderr, "%s failed -> %s\n", #F, TTF_GetError()); \
return PL_FALSE;
#define SHOW_TTF_FAIL(F) fprintf(stderr, "%s failed -> %s\n", #F, TTF_GetError());
// render a font texture into a renderer
void renderFont(SDL_Renderer* r, SDL_Surface* pText, int x, int y);
//--------------------------------------------------------------------
//
// Truetype (TFF) Functions
//
// "SDL_ttf"
//
// NB: --with-sdl-prefix !
//--------------------------------------------------------------------
PlBool gp_TTF_Init()
{
if (0 == TTF_Init()) {
return PL_TRUE;
}
RETURN_TTF_FAIL(TTF_Init);
}
PlBool gp_TTF_Quit()
{
TTF_Quit();
return PL_TRUE;
}
PlBool gp_TTF_OpenFont(char* fontName, PlLong size, PlTerm *font)
{
TTF_Font *pFont = TTF_OpenFont(fontName, (int)size);
if (!pFont) {
RETURN_TTF_FAIL(TTF_OpenFont);
}
*font = (PlLong)pFont;
return PL_TRUE;
}
PlBool gp_TTF_CloseFont(PlLong font)
{
TTF_CloseFont((TTF_Font*)font);
return PL_TRUE;
}
PlBool gp_TTF_RenderUTF8_Solid(PlTerm rndr, PlTerm font, PlLong x, PlLong y, char* text)
{
SDL_Color color = {255, 255, 255, 255};
SDL_GetRenderDrawColor(
(SDL_Renderer*)rndr,
&color.r, &color.g, &color.b, &color.a);
renderFont(
(SDL_Renderer*)rndr,
TTF_RenderUTF8_Solid((TTF_Font*)font, text, color),
(int)x, (int)y);
return PL_TRUE;
}
PlBool gp_TTF_RenderUTF8_Blended(PlTerm rndr, PlTerm font, PlLong x, PlLong y, char* text)
{
SDL_Color color = {255, 255, 255, 255};
SDL_GetRenderDrawColor(
(SDL_Renderer*)rndr,
&color.r, &color.g, &color.b, &color.a);
renderFont(
(SDL_Renderer*)rndr,
TTF_RenderUTF8_Blended((TTF_Font*)font, text, color),
(int)x, (int)y);
return PL_TRUE;
}
PlBool gp_TTF_RenderUTF8_Shaded(PlTerm rndr, PlTerm font, PlLong x, PlLong y, char* text)
{
SDL_Color color = {255, 255, 255, 255};
SDL_Color black = {0, 0, 0, 255};
SDL_GetRenderDrawColor(
(SDL_Renderer*)rndr,
&color.r, &color.g, &color.b, &color.a);
renderFont(
(SDL_Renderer*)rndr,
TTF_RenderUTF8_Shaded((TTF_Font*)font, text, color, black),
(int)x, (int)y);
return PL_TRUE;
}
PlBool gp_TTF_SizeUTF8(PlTerm font, char* text, PlLong *width, PlLong *height)
{
int w, h;
*width = 0;
*height = 0;
if (TTF_SizeUTF8((TTF_Font*)font, text, &w, &h)) {
RETURN_TTF_FAIL(TTF_SizeUTF8);
}
*width = (PlLong)w;
*height = (PlLong)h;
return PL_TRUE;
}
/**
* Renders a texture containing a rendered font incarnation to the
* specified (x,y) location in the output.
*
* @param SDL_Renderer* r contains the destination renderer
* @param SDK_Surface* s contains the rendered text source data
* @param int x the output x-coordinate in "r"
* @param int y the output y-coordinate in "r"
*/
void renderFont(SDL_Renderer* r, SDL_Surface* pText, int x, int y)
{
SDL_Rect dstRect = {(int)x, (int)y};
if (pText) {
SDL_Texture *tex = SDL_CreateTextureFromSurface(r, pText);
if (tex) {
SDL_QueryTexture(tex, NULL, NULL, &dstRect.w, &dstRect.h);
if(SDL_RenderCopy(r, tex, NULL, &dstRect)) {
SHOW_TTF_FAIL(TTF_RenderUTF8_Solid);
}
}
else {
SHOW_TTF_FAIL(SDL_CreateTextureFromSurface);
}
SDL_FreeSurface(pText);
}
else {
SHOW_TTF_FAIL(TTF_RenderUTF8_Solid);
}
}