Mercurial > sdl-ios-xcode
annotate test/testime.c @ 3134:f896821736fb gsoc2009_IME
Polish test code
author | Jiang Jiang <gzjjgod@gmail.com> |
---|---|
date | Thu, 06 Aug 2009 08:53:00 +0000 |
parents | 84119fe89d26 |
children | 962357f325e1 |
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 | |
11 #define DEFAULT_FONT "DroidSansFallback.ttf" | |
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 |
3134 | 21 void InitVideo(int argc, char *argv[]) |
3131 | 22 { |
3134 | 23 int width = 500, height = 250; |
3131 | 24 |
25 SDL_putenv("SDL_VIDEO_WINDOW_POS=center"); | |
26 if (SDL_Init(SDL_INIT_VIDEO) < 0) | |
27 { | |
28 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); | |
3134 | 29 exit(-1); |
3131 | 30 } |
31 | |
32 /* Initialize fonts */ | |
33 TTF_Init(); | |
34 | |
35 font = TTF_OpenFont(DEFAULT_FONT, DEFAULT_PTSIZE); | |
36 if (! font) | |
37 { | |
38 fprintf(stderr, "Failed to find font: %s\n", SDL_GetError()); | |
39 exit(-1); | |
40 } | |
41 | |
42 atexit(SDL_Quit); | |
43 | |
3134 | 44 int flags = SDL_HWSURFACE; |
45 if (argc > 1 && strcmp(argv[1], "--fullscreen") == 0) | |
46 { | |
47 SDL_DisplayMode mode; | |
48 SDL_GetDesktopDisplayMode(&mode); | |
49 | |
50 width = mode.w; | |
51 height = mode.h; | |
52 fprintf(stderr, "%dx%d\n", width, height); | |
53 flags |= SDL_FULLSCREEN; | |
54 } | |
55 | |
3131 | 56 /* Create window */ |
3134 | 57 screen = SDL_SetVideoMode(width, height, 32, flags); |
3131 | 58 if (screen == NULL) |
59 { | |
60 fprintf(stderr, "Unable to set %dx%d video: %s\n", | |
61 width, height, SDL_GetError()); | |
3134 | 62 exit(-1); |
3131 | 63 } |
3134 | 64 } |
65 | |
66 void CleanupVideo() | |
67 { | |
68 TTF_CloseFont(font); | |
69 TTF_Quit(); | |
70 } | |
71 | |
72 void InitInput() | |
73 { | |
74 backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF); | |
75 lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0); | |
3131 | 76 |
77 /* Prepare a rect for text input */ | |
3134 | 78 textRect.x = textRect.y = 100; |
79 textRect.w = screen->w - 2 * textRect.x; | |
80 textRect.h = 50; | |
81 | |
82 text[0] = 0; | |
83 markedRect = textRect; | |
84 markedText = NULL; | |
85 } | |
86 | |
87 static void RenderText(SDL_Surface *sur, | |
88 TTF_Font *font, | |
89 const char *text, | |
90 int x, int y, | |
91 SDL_Color color) | |
92 { | |
93 SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color); | |
94 SDL_Rect dest = { x, y, textSur->w, textSur->h }; | |
95 | |
96 SDL_BlitSurface(textSur, NULL, sur, &dest); | |
97 SDL_FreeSurface(textSur); | |
98 } | |
99 | |
100 void Redraw() | |
101 { | |
102 int w = 0, h = textRect.h; | |
103 SDL_Rect cursorRect, underlineRect; | |
104 | |
3131 | 105 SDL_FillRect(screen, &textRect, backColor); |
106 | |
3134 | 107 if (strlen(text)) |
108 { | |
109 RenderText(screen, font, text, textRect.x, textRect.y, textColor); | |
110 TTF_SizeUTF8(font, text, &w, &h); | |
111 } | |
112 | |
113 markedRect.x = textRect.x + w; | |
114 markedRect.w = textRect.w - w; | |
115 if (markedRect.w < 0) | |
116 { | |
117 SDL_Flip(screen); | |
118 return; | |
119 } | |
120 | |
121 SDL_FillRect(screen, &markedRect, backColor); | |
122 | |
123 if (markedText) | |
124 { | |
125 RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor); | |
126 TTF_SizeUTF8(font, markedText, &w, &h); | |
127 | |
128 underlineRect = markedRect; | |
129 underlineRect.y += (h - 2); | |
130 underlineRect.h = 2; | |
131 underlineRect.w = w; | |
132 SDL_FillRect(screen, &underlineRect, lineColor); | |
133 } | |
134 | |
135 cursorRect = markedRect; | |
136 cursorRect.w = 2; | |
137 cursorRect.h = h; | |
138 SDL_FillRect(screen, &cursorRect, lineColor); | |
3131 | 139 |
140 SDL_Flip(screen); | |
141 | |
3134 | 142 SDL_StartTextInput(&markedRect); |
143 } | |
144 | |
145 void | |
146 HotKey_ToggleFullScreen(void) | |
147 { | |
148 SDL_Surface *screen; | |
149 | |
150 screen = SDL_GetVideoSurface(); | |
151 if (SDL_WM_ToggleFullScreen(screen)) { | |
152 printf("Toggled fullscreen mode - now %s\n", | |
153 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); | |
154 } else { | |
155 printf("Unable to toggle fullscreen mode\n"); | |
156 } | |
157 } | |
158 | |
159 int main(int argc, char *argv[]) | |
160 { | |
161 InitVideo(argc, argv); | |
162 InitInput(); | |
163 Redraw(); | |
164 | |
3131 | 165 SDL_Event event; |
3134 | 166 int done = 0; |
3131 | 167 |
168 while (! done && SDL_WaitEvent(&event)) | |
169 { | |
170 switch (event.type) | |
171 { | |
172 case SDL_KEYDOWN: | |
3134 | 173 if (event.key.keysym.sym == SDLK_ESCAPE) { |
174 done = 1; | |
175 break; | |
176 } | |
177 | |
3131 | 178 fprintf(stderr, |
179 "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n", | |
180 event.key.which, event.key.keysym.scancode, | |
181 SDL_GetScancodeName(event.key.keysym.scancode), | |
182 event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym)); | |
183 break; | |
184 | |
185 case SDL_TEXTINPUT: | |
3134 | 186 if (strlen(event.text.text) == 0 || event.text.text[0] == '\n' || |
187 markedRect.w < 0) | |
188 break; | |
189 | |
3131 | 190 fprintf(stderr, "Keyboard %d: text input \"%s\"\n", |
191 event.text.which, event.text.text); | |
192 | |
3134 | 193 if (strlen(text) + strlen(event.text.text) < sizeof(text)) |
194 strcpy(text + strlen(text), event.text.text); | |
3131 | 195 |
196 fprintf(stderr, "text inputed: %s\n", text); | |
197 | |
3134 | 198 // After text inputed, we can clear up markedText because it |
199 // is committed | |
200 markedText = NULL; | |
201 Redraw(); | |
3131 | 202 break; |
203 | |
3132
88861448961f
Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3131
diff
changeset
|
204 case SDL_TEXTEDITING: |
88861448961f
Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3131
diff
changeset
|
205 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
|
206 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
|
207 |
3134 | 208 markedText = event.edit.text; |
209 Redraw(); | |
3132
88861448961f
Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3131
diff
changeset
|
210 break; |
88861448961f
Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3131
diff
changeset
|
211 |
3131 | 212 case SDL_QUIT: |
213 done = 1; | |
214 break; | |
215 | |
216 default: | |
217 break; | |
218 } | |
219 } | |
220 | |
3134 | 221 CleanupVideo(); |
3131 | 222 return 0; |
223 } | |
224 |