comparison test/testime.c @ 3280:00cace2d9080

Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
author Sam Lantinga <slouken@libsdl.org>
date Sat, 19 Sep 2009 13:29:40 +0000
parents
children 2d3a9f229ba1
comparison
equal deleted inserted replaced
3279:fd207dce9f94 3280:00cace2d9080
1 /* A simple program to test the Input Method support in the SDL library (1.3+) */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 #include "SDL.h"
8 #ifdef HAVE_SDL_TTF
9 #include "SDL_ttf.h"
10 #endif
11
12 #define DEFAULT_PTSIZE 30
13 #define DEFAULT_FONT "/System/Library/Fonts/华文细黑.ttf"
14 #define MAX_TEXT_LENGTH 256
15
16 SDL_Surface *screen;
17
18 #ifdef HAVE_SDL_TTF
19 TTF_Font *font;
20 #endif
21 SDL_Rect textRect, markedRect;
22 Uint32 lineColor, backColor;
23 SDL_Color textColor = { 0, 0, 0 };
24 char text[MAX_TEXT_LENGTH], *markedText;
25
26 void usage()
27 {
28 printf("usage: testime [--font fontfile] [--fullscreen]\n");
29 exit(0);
30 }
31
32 void InitVideo(int argc, char *argv[])
33 {
34 int width = 500, height = 250;
35 int flags = SDL_HWSURFACE;
36 const char *fontname = DEFAULT_FONT;
37 int fullscreen = 0;
38
39 for (argc--, argv++; argc > 0; argc--, argv++)
40 {
41 if (strcmp(argv[0], "--help") == 0)
42 usage();
43
44 else if (strcmp(argv[0], "--fullscreen") == 0)
45 fullscreen = 1;
46
47 else if (strcmp(argv[0], "--font") == 0)
48 {
49 argc--;
50 argv++;
51
52 if (argc > 0)
53 fontname = argv[0];
54 else
55 usage();
56 }
57 }
58
59 SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
60 if (SDL_Init(SDL_INIT_VIDEO) < 0)
61 {
62 fprintf(stderr, "Unable to init SDL: %s\n", TTF_GetError());
63 exit(-1);
64 }
65
66 #ifdef HAVE_SDL_TTF
67 /* Initialize fonts */
68 TTF_Init();
69
70 font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
71 if (! font)
72 {
73 fprintf(stderr, "Failed to find font: %s\n", SDL_GetError());
74 exit(-1);
75 }
76 #endif
77
78 printf("Using font: %s\n", fontname);
79 atexit(SDL_Quit);
80
81 if (fullscreen)
82 {
83 SDL_DisplayMode mode;
84 SDL_GetDesktopDisplayMode(&mode);
85
86 width = mode.w;
87 height = mode.h;
88 fprintf(stderr, "%dx%d\n", width, height);
89 flags |= SDL_FULLSCREEN;
90 }
91
92 /* Create window */
93 screen = SDL_SetVideoMode(width, height, 32, flags);
94 if (screen == NULL)
95 {
96 fprintf(stderr, "Unable to set %dx%d video: %s\n",
97 width, height, SDL_GetError());
98 exit(-1);
99 }
100 }
101
102 void CleanupVideo()
103 {
104 SDL_StopTextInput();
105 #ifdef HAVE_SDL_TTF
106 TTF_CloseFont(font);
107 TTF_Quit();
108 #endif
109 }
110
111 void InitInput()
112 {
113 backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
114 lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0);
115
116 /* Prepare a rect for text input */
117 textRect.x = textRect.y = 100;
118 textRect.w = screen->w - 2 * textRect.x;
119 textRect.h = 50;
120
121 text[0] = 0;
122 markedRect = textRect;
123 markedText = NULL;
124
125 SDL_StartTextInput();
126 }
127
128 #ifdef HAVE_SDL_TTF
129 static void RenderText(SDL_Surface *sur,
130 TTF_Font *font,
131 const char *text,
132 int x, int y,
133 SDL_Color color)
134 {
135 SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color);
136 SDL_Rect dest = { x, y, textSur->w, textSur->h };
137
138 SDL_BlitSurface(textSur, NULL, sur, &dest);
139 SDL_FreeSurface(textSur);
140 }
141 #endif
142
143 void Redraw()
144 {
145 int w = 0, h = textRect.h;
146 SDL_Rect cursorRect, underlineRect;
147
148 SDL_FillRect(screen, &textRect, backColor);
149
150 #ifdef HAVE_SDL_TTF
151 if (strlen(text))
152 {
153 RenderText(screen, font, text, textRect.x, textRect.y, textColor);
154 TTF_SizeUTF8(font, text, &w, &h);
155 }
156 #endif
157
158 markedRect.x = textRect.x + w;
159 markedRect.w = textRect.w - w;
160 if (markedRect.w < 0)
161 {
162 SDL_Flip(screen);
163 // Stop text input because we cannot hold any more characters
164 SDL_StopTextInput();
165 return;
166 }
167
168 cursorRect = markedRect;
169 cursorRect.w = 2;
170 cursorRect.h = h;
171
172 SDL_FillRect(screen, &markedRect, backColor);
173 if (markedText)
174 {
175 #ifdef HAVE_SDL_TTF
176 RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
177 TTF_SizeUTF8(font, markedText, &w, &h);
178 #endif
179
180 underlineRect = markedRect;
181 underlineRect.y += (h - 2);
182 underlineRect.h = 2;
183 underlineRect.w = w;
184
185 cursorRect.x += w + 1;
186
187 SDL_FillRect(screen, &underlineRect, lineColor);
188 }
189
190 SDL_FillRect(screen, &cursorRect, lineColor);
191
192 SDL_Flip(screen);
193
194 SDL_SetTextInputRect(&markedRect);
195 }
196
197 void
198 HotKey_ToggleFullScreen(void)
199 {
200 SDL_Surface *screen;
201
202 screen = SDL_GetVideoSurface();
203 if (SDL_WM_ToggleFullScreen(screen)) {
204 printf("Toggled fullscreen mode - now %s\n",
205 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
206 } else {
207 printf("Unable to toggle fullscreen mode\n");
208 }
209 }
210
211 int main(int argc, char *argv[])
212 {
213 InitVideo(argc, argv);
214 InitInput();
215 Redraw();
216
217 SDL_Event event;
218 int done = 0;
219
220 while (! done && SDL_WaitEvent(&event))
221 {
222 switch (event.type)
223 {
224 case SDL_KEYDOWN:
225 if (event.key.keysym.sym == SDLK_ESCAPE) {
226 done = 1;
227 break;
228 }
229
230 fprintf(stderr,
231 "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
232 event.key.which, event.key.keysym.scancode,
233 SDL_GetScancodeName(event.key.keysym.scancode),
234 event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
235 break;
236
237 case SDL_TEXTINPUT:
238 if (strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
239 markedRect.w < 0)
240 break;
241
242 fprintf(stderr, "Keyboard %d: text input \"%s\"\n",
243 event.text.which, event.text.text);
244
245 if (strlen(text) + strlen(event.text.text) < sizeof(text))
246 strcpy(text + strlen(text), event.text.text);
247
248 fprintf(stderr, "text inputed: %s\n", text);
249
250 // After text inputed, we can clear up markedText because it
251 // is committed
252 markedText = NULL;
253 Redraw();
254 break;
255
256 case SDL_TEXTEDITING:
257 fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
258 event.edit.text, event.edit.start, event.edit.length);
259
260 markedText = event.edit.text;
261 Redraw();
262 break;
263
264 case SDL_QUIT:
265 done = 1;
266 break;
267
268 default:
269 break;
270 }
271 }
272
273 CleanupVideo();
274 return 0;
275 }