Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
229 lines (183 loc) · 5.53 KB

testime.c

File metadata and controls

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