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