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

Latest commit

 

History

History
369 lines (309 loc) · 9.84 KB

testime.c

File metadata and controls

369 lines (309 loc) · 9.84 KB
 
Jun 22, 2011
Jun 22, 2011
1
2
3
4
5
6
7
8
9
10
11
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
Jan 22, 2012
Jan 22, 2012
12
/* A simple program to test the Input Method support in the SDL library (2.0+) */
Jun 22, 2011
Jun 22, 2011
13
14
15
16
17
18
19
20
21
22
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#ifdef HAVE_SDL_TTF
#include "SDL_ttf.h"
#endif
May 31, 2012
May 31, 2012
23
24
#include "common.h"
Jun 22, 2011
Jun 22, 2011
25
26
27
28
#define DEFAULT_PTSIZE 30
#define DEFAULT_FONT "/System/Library/Fonts/华文细黑.ttf"
#define MAX_TEXT_LENGTH 256
May 31, 2012
May 31, 2012
29
30
31
32
33
34
35
static CommonState *state;
static SDL_Rect textRect, markedRect;
static SDL_Color lineColor = {0,0,0,0};
static SDL_Color backColor = {255,255,255,0};
static SDL_Color textColor = {0,0,0,0};
static char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
static int cursor = 0;
Jun 22, 2011
Jun 22, 2011
36
#ifdef HAVE_SDL_TTF
May 31, 2012
May 31, 2012
37
static TTF_Font *font;
Jun 22, 2011
Jun 22, 2011
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
#endif
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;
}
char *utf8_advance(char *p, size_t distance)
{
size_t i = 0;
for (; i < distance && p; ++i)
{
p = utf8_next(p);
}
return p;
}
void usage()
{
May 31, 2012
May 31, 2012
83
printf("usage: testime [--font fontfile]\n");
Jun 22, 2011
Jun 22, 2011
84
85
86
87
88
89
90
91
exit(0);
}
void InitInput()
{
/* Prepare a rect for text input */
textRect.x = textRect.y = 100;
May 31, 2012
May 31, 2012
92
textRect.w = DEFAULT_WINDOW_WIDTH - 2 * textRect.x;
Jun 22, 2011
Jun 22, 2011
93
94
95
96
97
98
99
100
101
textRect.h = 50;
text[0] = 0;
markedRect = textRect;
markedText[0] = 0;
SDL_StartTextInput();
}
May 31, 2012
May 31, 2012
102
void CleanupVideo()
Jun 22, 2011
Jun 22, 2011
103
{
May 31, 2012
May 31, 2012
104
105
106
107
SDL_StopTextInput();
#ifdef HAVE_SDL_TTF
TTF_CloseFont(font);
TTF_Quit();
Jun 22, 2011
Jun 22, 2011
108
#endif
May 31, 2012
May 31, 2012
109
}
Jun 22, 2011
Jun 22, 2011
110
May 31, 2012
May 31, 2012
111
112
void _Redraw(SDL_Renderer * renderer) {
Jun 22, 2011
Jun 22, 2011
113
114
115
int w = 0, h = textRect.h;
SDL_Rect cursorRect, underlineRect;
May 31, 2012
May 31, 2012
116
117
SDL_SetRenderDrawColor(renderer, 255,255,255,255);
SDL_RenderFillRect(renderer,&textRect);
Jun 22, 2011
Jun 22, 2011
118
119
120
121
#ifdef HAVE_SDL_TTF
if (*text)
{
May 31, 2012
May 31, 2012
122
123
124
125
126
127
128
129
SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
SDL_Rect dest = {textRect.x, textRect.y, textSur->w, textSur->h };
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
SDL_FreeSurface(textSur);
SDL_RenderCopy(renderer,texture,NULL,&dest);
SDL_DestroyTexture(texture);
Jun 22, 2011
Jun 22, 2011
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
TTF_SizeUTF8(font, text, &w, &h);
}
#endif
markedRect.x = textRect.x + w;
markedRect.w = textRect.w - w;
if (markedRect.w < 0)
{
// Stop text input because we cannot hold any more characters
SDL_StopTextInput();
return;
}
else
{
SDL_StartTextInput();
}
cursorRect = markedRect;
cursorRect.w = 2;
cursorRect.h = h;
May 31, 2012
May 31, 2012
151
152
153
SDL_SetRenderDrawColor(renderer, 255,255,255,255);
SDL_RenderFillRect(renderer,&markedRect);
Jun 22, 2011
Jun 22, 2011
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
if (markedText[0])
{
#ifdef HAVE_SDL_TTF
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;
}
May 31, 2012
May 31, 2012
170
171
SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, markedText, textColor);
SDL_Rect dest = {markedRect.x, markedRect.y, textSur->w, textSur->h };
Jun 22, 2011
Jun 22, 2011
172
TTF_SizeUTF8(font, markedText, &w, &h);
May 31, 2012
May 31, 2012
173
174
175
176
177
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
SDL_FreeSurface(textSur);
SDL_RenderCopy(renderer,texture,NULL,&dest);
SDL_DestroyTexture(texture);
Jun 22, 2011
Jun 22, 2011
178
179
180
181
182
183
184
#endif
underlineRect = markedRect;
underlineRect.y += (h - 2);
underlineRect.h = 2;
underlineRect.w = w;
May 31, 2012
May 31, 2012
185
186
SDL_SetRenderDrawColor(renderer, 0,0,0,0);
SDL_RenderFillRect(renderer,&markedRect);
Jun 22, 2011
Jun 22, 2011
187
188
}
May 31, 2012
May 31, 2012
189
190
SDL_SetRenderDrawColor(renderer, 0,0,0,0);
SDL_RenderFillRect(renderer,&cursorRect);
Jun 22, 2011
Jun 22, 2011
191
192
193
194
SDL_SetTextInputRect(&markedRect);
}
May 31, 2012
May 31, 2012
195
196
197
198
199
200
201
202
203
204
void Redraw() {
int i;
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
_Redraw(renderer);
SDL_RenderPresent(renderer);
Jun 22, 2011
Jun 22, 2011
205
206
207
}
}
May 31, 2012
May 31, 2012
208
209
int main(int argc, char *argv[]) {
int i, done;
Jun 22, 2011
Jun 22, 2011
210
SDL_Event event;
May 31, 2012
May 31, 2012
211
const char *fontname = DEFAULT_FONT;
Jun 22, 2011
Jun 22, 2011
212
May 31, 2012
May 31, 2012
213
214
215
216
217
218
219
220
221
/* Initialize test framework */
state = CommonCreateState(argv, SDL_INIT_VIDEO);
if (!state) {
return 1;
}
for (i = 1; i < argc;i++) {
CommonArg(state, i);
}
for (argc--, argv++; argc > 0; argc--, argv++)
Jun 22, 2011
Jun 22, 2011
222
{
May 31, 2012
May 31, 2012
223
224
225
226
if (strcmp(argv[0], "--help") == 0) {
usage();
return 0;
}
Jun 22, 2011
Jun 22, 2011
227
May 31, 2012
May 31, 2012
228
229
230
231
else if (strcmp(argv[0], "--font") == 0)
{
argc--;
argv++;
Jun 22, 2011
Jun 22, 2011
232
May 31, 2012
May 31, 2012
233
234
235
236
237
if (argc > 0)
fontname = argv[0];
else {
usage();
return 0;
Jun 22, 2011
Jun 22, 2011
238
}
May 31, 2012
May 31, 2012
239
240
241
242
243
244
}
}
if (!CommonInit(state)) {
return 2;
}
Jun 22, 2011
Jun 22, 2011
245
246
May 31, 2012
May 31, 2012
247
248
249
#ifdef HAVE_SDL_TTF
/* Initialize fonts */
TTF_Init();
Jun 22, 2011
Jun 22, 2011
250
May 31, 2012
May 31, 2012
251
252
253
254
255
256
257
font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
if (! font)
{
fprintf(stderr, "Failed to find font: %s\n", TTF_GetError());
exit(-1);
}
#endif
Jun 22, 2011
Jun 22, 2011
258
May 31, 2012
May 31, 2012
259
260
printf("Using font: %s\n", fontname);
atexit(SDL_Quit);
Jun 22, 2011
Jun 22, 2011
261
May 31, 2012
May 31, 2012
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
InitInput();
/* Create the windows and initialize the renderers */
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
SDL_RenderClear(renderer);
}
Redraw();
/* Main render loop */
done = 0;
while (!done) {
/* Check for events */
while (SDL_PollEvent(&event)) {
CommonEvent(state, &event, &done);
switch(event.type) {
case SDL_KEYDOWN: {
switch (event.key.keysym.sym)
{
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)
{
break;
}
fprintf(stderr,
"Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
event.key.keysym.scancode,
SDL_GetScancodeName(event.key.keysym.scancode),
event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
break;
case SDL_TEXTINPUT:
if (SDL_strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
markedRect.w < 0)
break;
fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text);
if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
SDL_strlcat(text, event.text.text, sizeof(text));
fprintf(stderr, "text inputed: %s\n", text);
// After text inputed, we can clear up markedText because it
// is committed
markedText[0] = 0;
Redraw();
break;
case SDL_TEXTEDITING:
fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
event.edit.text, event.edit.start, event.edit.length);
strcpy(markedText, event.edit.text);
cursor = event.edit.start;
Redraw();
break;
}
break;
}
Jun 22, 2011
Jun 22, 2011
361
362
363
}
}
CleanupVideo();
May 31, 2012
May 31, 2012
364
CommonQuit(state);
Jun 22, 2011
Jun 22, 2011
365
366
return 0;
}
May 31, 2012
May 31, 2012
367
Jun 22, 2011
Jun 22, 2011
368
369
/* vi: set ts=4 sw=4 expandtab: */