comparison test/testime.c @ 3131:009bd8f81947 gsoc2009_IME

Add IME test program
author Jiang Jiang <gzjjgod@gmail.com>
date Wed, 01 Jul 2009 05:52:17 +0000
parents
children 88861448961f
comparison
equal deleted inserted replaced
3130:fef1a835af43 3131:009bd8f81947
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
14 static void render_text(SDL_Surface *sur,
15 TTF_Font *font,
16 const char *text,
17 int x, int y,
18 SDL_Color color)
19 {
20 SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color);
21 SDL_Rect dest = { x, y, textSur->w, textSur->h };
22
23 SDL_BlitSurface(textSur, NULL, sur, &dest);
24 SDL_FreeSurface(textSur);
25 }
26
27 int main(int argc, char *argv[])
28 {
29 int width, height;
30 SDL_Surface *screen;
31 TTF_Font *font;
32
33 width = 500, height = 250;
34
35 SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
36
37 if (SDL_Init(SDL_INIT_VIDEO) < 0)
38 {
39 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
40 return -1;
41 }
42
43 /* Initialize fonts */
44 TTF_Init();
45
46 font = TTF_OpenFont(DEFAULT_FONT, DEFAULT_PTSIZE);
47 if (! font)
48 {
49 fprintf(stderr, "Failed to find font: %s\n", SDL_GetError());
50 exit(-1);
51 }
52
53 atexit(SDL_Quit);
54
55 /* Create window */
56 screen = SDL_SetVideoMode(width, height, 32,
57 SDL_HWSURFACE | SDL_DOUBLEBUF);
58 if (screen == NULL)
59 {
60 fprintf(stderr, "Unable to set %dx%d video: %s\n",
61 width, height, SDL_GetError());
62 return -1;
63 }
64
65 /* Prepare a rect for text input */
66 SDL_Rect textRect = { 100, 80, 300, 50 }, markedRect;
67 Uint32 backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
68 SDL_Color textColor = { 0, 0, 0 };
69 SDL_FillRect(screen, &textRect, backColor);
70
71 markedRect = textRect;
72 SDL_StartTextInput(&markedRect);
73
74 SDL_Flip(screen);
75
76 SDL_Event event;
77 int done = 0, inputed = 0;
78 int w, h;
79 char text[MAX_TEXT_LENGTH];
80
81 while (! done && SDL_WaitEvent(&event))
82 {
83 switch (event.type)
84 {
85 case SDL_KEYDOWN:
86 fprintf(stderr,
87 "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
88 event.key.which, event.key.keysym.scancode,
89 SDL_GetScancodeName(event.key.keysym.scancode),
90 event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
91 break;
92
93 case SDL_TEXTINPUT:
94 fprintf(stderr, "Keyboard %d: text input \"%s\"\n",
95 event.text.which, event.text.text);
96
97 if (inputed < sizeof(text))
98 {
99 strcpy(text + inputed, event.text.text);
100 inputed += strlen(event.text.text);
101 }
102
103 fprintf(stderr, "text inputed: %s\n", text);
104 SDL_FillRect(screen, &textRect, backColor);
105
106 render_text(screen, font, text, textRect.x, textRect.y, textColor);
107 TTF_SizeUTF8(font, text, &w, &h);
108 markedRect.x = textRect.x + w;
109 SDL_Flip(screen);
110
111 SDL_StartTextInput(&markedRect);
112 break;
113
114 case SDL_QUIT:
115 done = 1;
116 break;
117
118 default:
119 break;
120 }
121 }
122
123 TTF_CloseFont(font);
124 TTF_Quit();
125
126 return 0;
127 }
128