Mercurial > sdl-ios-xcode
annotate test/testime.c @ 3136:962357f325e1 gsoc2009_IME
Further polish API, fix crash in test program.
author | Jiang Jiang <gzjjgod@gmail.com> |
---|---|
date | Thu, 06 Aug 2009 08:59:53 +0000 |
parents | f896821736fb |
children | 311c678f3b2e |
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 { | |
3136
962357f325e1
Further polish API, fix crash in test program.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3134
diff
changeset
|
68 SDL_StopTextInput(); |
3134 | 69 TTF_CloseFont(font); |
70 TTF_Quit(); | |
71 } | |
72 | |
73 void InitInput() | |
74 { | |
75 backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF); | |
76 lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0); | |
3131 | 77 |
78 /* Prepare a rect for text input */ | |
3134 | 79 textRect.x = textRect.y = 100; |
80 textRect.w = screen->w - 2 * textRect.x; | |
81 textRect.h = 50; | |
82 | |
83 text[0] = 0; | |
84 markedRect = textRect; | |
85 markedText = NULL; | |
3136
962357f325e1
Further polish API, fix crash in test program.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3134
diff
changeset
|
86 |
962357f325e1
Further polish API, fix crash in test program.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3134
diff
changeset
|
87 SDL_StartTextInput(); |
3134 | 88 } |
89 | |
90 static void RenderText(SDL_Surface *sur, | |
91 TTF_Font *font, | |
92 const char *text, | |
93 int x, int y, | |
94 SDL_Color color) | |
95 { | |
96 SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color); | |
97 SDL_Rect dest = { x, y, textSur->w, textSur->h }; | |
98 | |
99 SDL_BlitSurface(textSur, NULL, sur, &dest); | |
100 SDL_FreeSurface(textSur); | |
101 } | |
102 | |
103 void Redraw() | |
104 { | |
105 int w = 0, h = textRect.h; | |
106 SDL_Rect cursorRect, underlineRect; | |
107 | |
3131 | 108 SDL_FillRect(screen, &textRect, backColor); |
109 | |
3134 | 110 if (strlen(text)) |
111 { | |
112 RenderText(screen, font, text, textRect.x, textRect.y, textColor); | |
113 TTF_SizeUTF8(font, text, &w, &h); | |
114 } | |
115 | |
116 markedRect.x = textRect.x + w; | |
117 markedRect.w = textRect.w - w; | |
118 if (markedRect.w < 0) | |
119 { | |
120 SDL_Flip(screen); | |
3136
962357f325e1
Further polish API, fix crash in test program.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3134
diff
changeset
|
121 // 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
|
122 SDL_StopTextInput(); |
3134 | 123 return; |
124 } | |
125 | |
126 SDL_FillRect(screen, &markedRect, backColor); | |
127 | |
128 if (markedText) | |
129 { | |
130 RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor); | |
131 TTF_SizeUTF8(font, markedText, &w, &h); | |
132 | |
133 underlineRect = markedRect; | |
134 underlineRect.y += (h - 2); | |
135 underlineRect.h = 2; | |
136 underlineRect.w = w; | |
137 SDL_FillRect(screen, &underlineRect, lineColor); | |
138 } | |
139 | |
140 cursorRect = markedRect; | |
141 cursorRect.w = 2; | |
142 cursorRect.h = h; | |
143 SDL_FillRect(screen, &cursorRect, lineColor); | |
3131 | 144 |
145 SDL_Flip(screen); | |
146 | |
3136
962357f325e1
Further polish API, fix crash in test program.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3134
diff
changeset
|
147 SDL_SetTextInputRect(&markedRect); |
3134 | 148 } |
149 | |
150 void | |
151 HotKey_ToggleFullScreen(void) | |
152 { | |
153 SDL_Surface *screen; | |
154 | |
155 screen = SDL_GetVideoSurface(); | |
156 if (SDL_WM_ToggleFullScreen(screen)) { | |
157 printf("Toggled fullscreen mode - now %s\n", | |
158 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); | |
159 } else { | |
160 printf("Unable to toggle fullscreen mode\n"); | |
161 } | |
162 } | |
163 | |
164 int main(int argc, char *argv[]) | |
165 { | |
166 InitVideo(argc, argv); | |
167 InitInput(); | |
168 Redraw(); | |
169 | |
3131 | 170 SDL_Event event; |
3134 | 171 int done = 0; |
3131 | 172 |
173 while (! done && SDL_WaitEvent(&event)) | |
174 { | |
175 switch (event.type) | |
176 { | |
177 case SDL_KEYDOWN: | |
3134 | 178 if (event.key.keysym.sym == SDLK_ESCAPE) { |
179 done = 1; | |
180 break; | |
181 } | |
182 | |
3131 | 183 fprintf(stderr, |
184 "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n", | |
185 event.key.which, event.key.keysym.scancode, | |
186 SDL_GetScancodeName(event.key.keysym.scancode), | |
187 event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym)); | |
188 break; | |
189 | |
190 case SDL_TEXTINPUT: | |
3134 | 191 if (strlen(event.text.text) == 0 || event.text.text[0] == '\n' || |
192 markedRect.w < 0) | |
193 break; | |
194 | |
3131 | 195 fprintf(stderr, "Keyboard %d: text input \"%s\"\n", |
196 event.text.which, event.text.text); | |
197 | |
3134 | 198 if (strlen(text) + strlen(event.text.text) < sizeof(text)) |
199 strcpy(text + strlen(text), event.text.text); | |
3131 | 200 |
201 fprintf(stderr, "text inputed: %s\n", text); | |
202 | |
3134 | 203 // After text inputed, we can clear up markedText because it |
204 // is committed | |
205 markedText = NULL; | |
206 Redraw(); | |
3131 | 207 break; |
208 | |
3132
88861448961f
Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3131
diff
changeset
|
209 case SDL_TEXTEDITING: |
88861448961f
Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3131
diff
changeset
|
210 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
|
211 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
|
212 |
3134 | 213 markedText = event.edit.text; |
214 Redraw(); | |
3132
88861448961f
Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3131
diff
changeset
|
215 break; |
88861448961f
Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents:
3131
diff
changeset
|
216 |
3131 | 217 case SDL_QUIT: |
218 done = 1; | |
219 break; | |
220 | |
221 default: | |
222 break; | |
223 } | |
224 } | |
225 | |
3134 | 226 CleanupVideo(); |
3131 | 227 return 0; |
228 } | |
229 |