-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdlhelp.c
267 lines (243 loc) · 7.01 KB
/
sdlhelp.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* help.c: Displaying online help.
*
* Copyright (C) 2010 Brian Raiter.
* This program is free software. See README for details.
*/
#include <ctype.h>
#include "SDL.h"
#include "SDL_ttf.h"
#include "yahtzee.h"
#include "gen.h"
#include "iosdlctl.h"
/* Colors for rendering the help text.
*/
static SDL_Color const textcolor = { 191, 191, 255, 0 };
static SDL_Color const bkgndcolor = { 0, 32, 80, 0 };
/* The current font.
*/
static TTF_Font *font;
/* Dimensions of the key graphics.
*/
static int keywidth, keyheight;
/* Wait for an input event. Returns false if the input event was an
* attempt to exit the program.
*/
static int getkey(void)
{
SDL_Event event;
for (;;) {
if (SDL_WaitEvent(&event) < 0)
exit(1);
if (event.type == SDL_QUIT)
return 0;
else if (event.type == SDL_MOUSEBUTTONDOWN)
return 1;
else if (event.type == SDL_KEYDOWN && event.key.keysym.unicode) {
return event.key.keysym.unicode != '\030';
}
}
}
/* Load the font and get some dimensions.
*/
static void initfont(char const *path, int fontheight)
{
font = TTF_OpenFont(path, fontheight);
if (!font)
croak("%s\nUnable to load font.", TTF_GetError());
keyheight = TTF_FontLineSkip(font) + 2;
keywidth = keyheight + 2;
}
/* Given a surface, draw a border around its edges, using the given
* color and width (in pixels).
*/
static void outlinesurface(SDL_Surface *surface, SDL_Color color, int width)
{
Uint32 colorvalue;
SDL_Rect rect;
colorvalue = SDL_MapRGB(surface->format, color.r, color.g, color.b);
rect.x = 0;
rect.y = 0;
rect.w = surface->w;
rect.h = width;
SDL_FillRect(surface, &rect, colorvalue);
rect.y = surface->h - width;
SDL_FillRect(surface, &rect, colorvalue);
rect.x = 0;
rect.y = 0;
rect.w = width;
rect.h = surface->h;
SDL_FillRect(surface, &rect, colorvalue);
rect.x = surface->w - width;
SDL_FillRect(surface, &rect, colorvalue);
}
/* Create a set of graphics containing the hot key characters for all
* of the controls.
*/
static void makekeys(SDL_Surface *keys[], struct sdlcontrol const *sdlcontrols)
{
static SDL_Color const keytextcolor = { 175, 175, 191, 0 };
static SDL_Color const keycolor = { 0, 0, 0, 0 };
SDL_Surface *image;
SDL_Rect rect;
int key, i;
for (i = 0 ; i < ctl_count ; ++i) {
key = toupper(sdlcontrols[i].control->key);
if (!key) {
keys[i] = NULL;
continue;
}
if (key == ' ') {
image = TTF_RenderUTF8_Shaded(font, "space",
keytextcolor, keycolor);
keys[i] = SDL_CreateRGBSurface(SDL_SWSURFACE,
image->w + keywidth, keyheight,
sdl_screen->format->BitsPerPixel,
sdl_screen->format->Rmask,
sdl_screen->format->Gmask,
sdl_screen->format->Bmask,
sdl_screen->format->Amask);
} else {
image = TTF_RenderGlyph_Shaded(font, key, keytextcolor, keycolor);
keys[i] = SDL_CreateRGBSurface(SDL_SWSURFACE, keywidth, keyheight,
sdl_screen->format->BitsPerPixel,
sdl_screen->format->Rmask,
sdl_screen->format->Gmask,
sdl_screen->format->Bmask,
sdl_screen->format->Amask);
}
SDL_FillRect(keys[i], NULL,
SDL_MapRGB(keys[i]->format,
keycolor.r, keycolor.g, keycolor.b));
outlinesurface(keys[i], keytextcolor, 1);
rect.x = (keys[i]->w - image->w) / 2;
rect.y = (keys[i]->h - image->h) / 2;
SDL_BlitSurface(image, NULL, keys[i], &rect);
SDL_FreeSurface(image);
}
}
/* Render a paragraph of text to the given surface, starting at the
* given y-coordinate and breaking on whitespace as necessary. Returns
* the y-coordinate just below the last line rendered.
*/
static int writetext(SDL_Surface *surface, char const *text, int y)
{
SDL_Surface *image;
SDL_Rect rect;
char *buf;
int textlen, pos, n, w;
rect.x = keywidth / 2;
rect.y = y;
textlen = strlen(text);
buf = allocate(textlen + 1);
pos = 0;
while (pos < textlen) {
n = textlen - pos;
memcpy(buf, text + pos, n + 1);
for (;;) {
TTF_SizeUTF8(font, buf, &w, NULL);
if (w <= surface->w - keywidth)
break;
while (--n && buf[n] != ' ') ;
if (n <= 0) {
n = strlen(buf);
break;
}
buf[n] = '\0';
}
image = TTF_RenderUTF8_Shaded(font, buf, textcolor, bkgndcolor);
SDL_BlitSurface(image, NULL, surface, &rect);
SDL_FreeSurface(image);
rect.y += TTF_FontLineSkip(font);
pos += n + 1;
}
free(buf);
return rect.y;
}
/* Display several lines of text (terminated with a null pointer) and
* wait for a keypress before returning.
*/
static int displaytext(char const *lines[])
{
int i, y;
initfont(FONT_MED_PATH, sdl_scalingunit * 3);
SDL_FillRect(sdl_screen, NULL,
SDL_MapRGB(sdl_screen->format,
bkgndcolor.r, bkgndcolor.g, bkgndcolor.b));
y = TTF_FontLineSkip(font) / 2;
for (i = 0 ; lines[i] ; ++i)
y = writetext(sdl_screen, *lines[i] ? lines[i] : " ", y);
SDL_UpdateRect(sdl_screen, 0, 0, 0, 0);
TTF_CloseFont(font);
font = NULL;
return getkey();
}
/* Temporarily display the online help text.
*/
int runhelp(void)
{
static char const *helptext[] = {
"Press Ctrl-+ and Ctrl-\xE2\x80\x93 to resize the window.",
"Press Ctrl-0 to restore the default window size.",
"Press ? or F1 to view this help text again.",
"Press Ctrl-K to view the keyboard shortcuts.",
"Press Ctrl-V to view the version and license information.",
"Press Ctrl-X to exit the program.",
NULL
};
int i, y;
initfont(FONT_MED_PATH, sdl_scalingunit * 3);
SDL_FillRect(sdl_screen, NULL,
SDL_MapRGB(sdl_screen->format,
bkgndcolor.r, bkgndcolor.g, bkgndcolor.b));
y = TTF_FontLineSkip(font) / 2;
for (i = 0 ; rulesinfo[i] ; ++i)
y = writetext(sdl_screen, rulesinfo[i], y);
y += TTF_FontLineSkip(font);
for (i = 0 ; helptext[i] ; ++i)
y = writetext(sdl_screen, helptext[i], y);
SDL_UpdateRect(sdl_screen, 0, 0, 0, 0);
TTF_CloseFont(font);
font = NULL;
return getkey();
return displaytext(helptext);
}
/* Temporarily display the license text.
*/
int showlicense(void)
{
int i, y;
initfont(FONT_MED_PATH, sdl_scalingunit * 3);
SDL_FillRect(sdl_screen, NULL,
SDL_MapRGB(sdl_screen->format,
bkgndcolor.r, bkgndcolor.g, bkgndcolor.b));
y = TTF_FontLineSkip(font) / 2;
for (i = 0 ; licenseinfo[i] ; ++i)
y = writetext(sdl_screen, licenseinfo[i], y);
SDL_UpdateRect(sdl_screen, 0, 0, 0, 0);
TTF_CloseFont(font);
font = NULL;
return getkey();
}
/* Temporarily render each of the key graphics next to its associated
* control.
*/
int showkeyhelp(struct sdlcontrol const *sdlcontrols)
{
SDL_Surface *keys[ctl_count];
SDL_Rect rect;
int i;
initfont(FONT_BOLD_PATH, sdl_scalingunit * 4);
makekeys(keys, sdlcontrols);
for (i = 0 ; i < ctl_count ; ++i) {
if (!keys[i])
continue;
rect.x = sdlcontrols[i].rect.x + 4;
rect.y = sdlcontrols[i].rect.y - 4;
SDL_BlitSurface(keys[i], NULL, sdl_screen, &rect);
SDL_FreeSurface(keys[i]);
}
SDL_UpdateRect(sdl_screen, 0, 0, 0, 0);
TTF_CloseFont(font);
font = NULL;
return getkey();
}