Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
377 lines (317 loc) · 8.91 KB

testime.c

File metadata and controls

377 lines (317 loc) · 8.91 KB
 
1
/* A simple program to test the Input Method support in the SDL library (1.3+) */
Oct 4, 2009
Oct 4, 2009
2
3
4
5
6
7
8
9
10
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#ifdef HAVE_SDL_TTF
#include "SDL_ttf.h"
#endif
Oct 4, 2009
Oct 4, 2009
11
12
#define DEFAULT_PTSIZE 30
Feb 2, 2011
Feb 2, 2011
13
#define DEFAULT_FONT "/System/Library/Fonts/华文细黑.ttf"
14
15
16
17
18
19
20
21
22
23
#define MAX_TEXT_LENGTH 256
SDL_Surface *screen;
#ifdef HAVE_SDL_TTF
TTF_Font *font;
#endif
SDL_Rect textRect, markedRect;
Uint32 lineColor, backColor;
SDL_Color textColor = { 0, 0, 0 };
Jul 25, 2010
Jul 25, 2010
24
char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
Jul 25, 2010
Jul 25, 2010
25
26
int cursor = 0;
Feb 2, 2011
Feb 2, 2011
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
size_t utf8_length(unsigned char c)
{
c = (unsigned char)(0xff & c);
if (c < 0x80)
return 1;
else if ((c >> 5) ==0x6)
return 2;
else if ((c >> 4) == 0xe)
return 3;
else if ((c >> 3) == 0x1e)
return 4;
else
return 0;
}
char *utf8_next(char *p)
{
size_t len = utf8_length(*p);
size_t i = 0;
if (!len)
return 0;
for (; i < len; ++i)
{
++p;
if (!*p)
return 0;
}
return p;
}
Jul 25, 2010
Jul 25, 2010
57
58
59
60
61
62
63
64
65
66
char *utf8_advance(char *p, size_t distance)
{
size_t i = 0;
for (; i < distance && p; ++i)
{
p = utf8_next(p);
}
return p;
}
67
68
69
70
71
72
73
74
75
void usage()
{
printf("usage: testime [--font fontfile] [--fullscreen]\n");
exit(0);
}
void InitVideo(int argc, char *argv[])
{
Nov 21, 2009
Nov 21, 2009
76
int width = 640, height = 480;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
int flags = SDL_HWSURFACE;
const char *fontname = DEFAULT_FONT;
int fullscreen = 0;
for (argc--, argv++; argc > 0; argc--, argv++)
{
if (strcmp(argv[0], "--help") == 0)
usage();
else if (strcmp(argv[0], "--fullscreen") == 0)
fullscreen = 1;
else if (strcmp(argv[0], "--font") == 0)
{
argc--;
argv++;
if (argc > 0)
fontname = argv[0];
else
usage();
}
}
Dec 16, 2009
Dec 16, 2009
101
SDL_setenv("SDL_VIDEO_WINDOW_POS", "center", 1);
102
103
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
Sep 19, 2009
Sep 19, 2009
104
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
105
106
107
108
109
110
111
112
113
114
exit(-1);
}
#ifdef HAVE_SDL_TTF
/* Initialize fonts */
TTF_Init();
font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
if (! font)
{
Sep 19, 2009
Sep 19, 2009
115
fprintf(stderr, "Failed to find font: %s\n", TTF_GetError());
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
exit(-1);
}
#endif
printf("Using font: %s\n", fontname);
atexit(SDL_Quit);
if (fullscreen)
{
SDL_DisplayMode mode;
SDL_GetDesktopDisplayMode(&mode);
width = mode.w;
height = mode.h;
fprintf(stderr, "%dx%d\n", width, height);
flags |= SDL_FULLSCREEN;
}
/* Create window */
screen = SDL_SetVideoMode(width, height, 32, flags);
if (screen == NULL)
{
fprintf(stderr, "Unable to set %dx%d video: %s\n",
width, height, SDL_GetError());
exit(-1);
}
}
void CleanupVideo()
{
SDL_StopTextInput();
#ifdef HAVE_SDL_TTF
TTF_CloseFont(font);
TTF_Quit();
#endif
}
void InitInput()
{
backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0);
/* Prepare a rect for text input */
textRect.x = textRect.y = 100;
textRect.w = screen->w - 2 * textRect.x;
textRect.h = 50;
text[0] = 0;
markedRect = textRect;
Jul 25, 2010
Jul 25, 2010
165
markedText[0] = 0;
166
167
168
169
170
171
172
173
174
175
176
SDL_StartTextInput();
}
#ifdef HAVE_SDL_TTF
static void RenderText(SDL_Surface *sur,
TTF_Font *font,
const char *text,
int x, int y,
SDL_Color color)
{
Aug 22, 2010
Aug 22, 2010
177
if (text && *text) {
Aug 12, 2010
Aug 12, 2010
178
179
SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color);
SDL_Rect dest = { x, y, textSur->w, textSur->h };
Aug 12, 2010
Aug 12, 2010
181
182
183
SDL_BlitSurface(textSur, NULL, sur, &dest);
SDL_FreeSurface(textSur);
}
184
185
186
187
188
189
190
191
192
193
194
}
#endif
void Redraw()
{
int w = 0, h = textRect.h;
SDL_Rect cursorRect, underlineRect;
SDL_FillRect(screen, &textRect, backColor);
#ifdef HAVE_SDL_TTF
Aug 22, 2010
Aug 22, 2010
195
if (*text)
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
{
RenderText(screen, font, text, textRect.x, textRect.y, textColor);
TTF_SizeUTF8(font, text, &w, &h);
}
#endif
markedRect.x = textRect.x + w;
markedRect.w = textRect.w - w;
if (markedRect.w < 0)
{
SDL_Flip(screen);
// Stop text input because we cannot hold any more characters
SDL_StopTextInput();
return;
}
Nov 21, 2009
Nov 21, 2009
211
212
213
214
else
{
SDL_StartTextInput();
}
215
216
217
218
219
220
cursorRect = markedRect;
cursorRect.w = 2;
cursorRect.h = h;
SDL_FillRect(screen, &markedRect, backColor);
Jul 25, 2010
Jul 25, 2010
221
if (markedText[0])
222
223
{
#ifdef HAVE_SDL_TTF
Jul 25, 2010
Jul 25, 2010
224
225
226
227
228
229
230
231
232
233
234
235
236
if (cursor)
{
char *p = utf8_advance(markedText, cursor);
char c = 0;
if (!p)
p = &markedText[strlen(markedText)];
c = *p;
*p = 0;
TTF_SizeUTF8(font, markedText, &w, 0);
cursorRect.x += w;
*p = c;
}
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
268
269
270
271
RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
TTF_SizeUTF8(font, markedText, &w, &h);
#endif
underlineRect = markedRect;
underlineRect.y += (h - 2);
underlineRect.h = 2;
underlineRect.w = w;
SDL_FillRect(screen, &underlineRect, lineColor);
}
SDL_FillRect(screen, &cursorRect, lineColor);
SDL_Flip(screen);
SDL_SetTextInputRect(&markedRect);
}
void
HotKey_ToggleFullScreen(void)
{
SDL_Surface *screen;
screen = SDL_GetVideoSurface();
if (SDL_WM_ToggleFullScreen(screen)) {
printf("Toggled fullscreen mode - now %s\n",
(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
} else {
printf("Unable to toggle fullscreen mode\n");
}
}
int main(int argc, char *argv[])
{
Oct 4, 2009
Oct 4, 2009
272
273
274
SDL_Event event;
int done = 0;
275
276
277
278
279
280
281
282
283
InitVideo(argc, argv);
InitInput();
Redraw();
while (! done && SDL_WaitEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
Nov 21, 2009
Nov 21, 2009
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
done = 1;
break;
case SDLK_RETURN:
text[0]=0x00;
Redraw();
break;
case SDLK_BACKSPACE:
{
int textlen=SDL_strlen(text);
do {
if (textlen==0)
{
break;
}
if ((text[textlen-1] & 0x80) == 0x00)
{
/* One byte */
text[textlen-1]=0x00;
break;
}
if ((text[textlen-1] & 0xC0) == 0x80)
{
/* Byte from the multibyte sequence */
text[textlen-1]=0x00;
textlen--;
}
if ((text[textlen-1] & 0xC0) == 0xC0)
{
/* First byte of multibyte sequence */
text[textlen-1]=0x00;
break;
}
} while(1);
Redraw();
}
break;
}
if (done)
{
329
330
331
332
break;
}
fprintf(stderr,
May 10, 2010
May 10, 2010
333
334
"Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
event.key.keysym.scancode,
335
336
337
338
339
SDL_GetScancodeName(event.key.keysym.scancode),
event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
break;
case SDL_TEXTINPUT:
Nov 21, 2009
Nov 21, 2009
340
if (SDL_strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
341
342
343
markedRect.w < 0)
break;
May 10, 2010
May 10, 2010
344
fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text);
Nov 21, 2009
Nov 21, 2009
346
if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
Aug 22, 2010
Aug 22, 2010
347
SDL_strlcat(text, event.text.text, sizeof(text));
348
349
350
351
352
fprintf(stderr, "text inputed: %s\n", text);
// After text inputed, we can clear up markedText because it
// is committed
Jul 25, 2010
Jul 25, 2010
353
markedText[0] = 0;
354
355
356
357
358
359
360
Redraw();
break;
case SDL_TEXTEDITING:
fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
event.edit.text, event.edit.start, event.edit.length);
Jul 25, 2010
Jul 25, 2010
361
strcpy(markedText, event.edit.text);
Jul 25, 2010
Jul 25, 2010
362
cursor = event.edit.start;
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
Redraw();
break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
CleanupVideo();
return 0;
}