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