This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
testime.c
150 lines (118 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* 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
static void render_text(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);
}
int main(int argc, char *argv[])
{
int width, height;
SDL_Surface *screen;
TTF_Font *font;
width = 500, height = 250;
SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
return -1;
}
/* 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);
/* Create window */
screen = SDL_SetVideoMode(width, height, 32,
SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL)
{
fprintf(stderr, "Unable to set %dx%d video: %s\n",
width, height, SDL_GetError());
return -1;
}
/* Prepare a rect for text input */
66
SDL_Rect textRect = { 100, 80, 300, 50 }, markedRect, underlineRect, cursorRect;
67
Uint32 backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
68
Uint32 lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
SDL_Color textColor = { 0, 0, 0 };
SDL_FillRect(screen, &textRect, backColor);
markedRect = textRect;
SDL_StartTextInput(&markedRect);
SDL_Flip(screen);
SDL_Event event;
int done = 0, inputed = 0;
int w, h;
char text[MAX_TEXT_LENGTH];
while (! done && SDL_WaitEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
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:
fprintf(stderr, "Keyboard %d: text input \"%s\"\n",
event.text.which, event.text.text);
if (inputed < sizeof(text))
{
strcpy(text + inputed, event.text.text);
inputed += strlen(event.text.text);
}
fprintf(stderr, "text inputed: %s\n", text);
SDL_FillRect(screen, &textRect, backColor);
render_text(screen, font, text, textRect.x, textRect.y, textColor);
TTF_SizeUTF8(font, text, &w, &h);
markedRect.x = textRect.x + w;
110
111
112
113
114
cursorRect = markedRect;
cursorRect.w = 2;
cursorRect.h = h;
SDL_FillRect(screen, &cursorRect, lineColor);
115
116
117
118
119
SDL_Flip(screen);
SDL_StartTextInput(&markedRect);
break;
120
121
122
123
124
125
case SDL_TEXTEDITING:
fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
event.edit.text, event.edit.start, event.edit.length);
SDL_FillRect(screen, &markedRect, backColor);
render_text(screen, font, event.edit.text, markedRect.x, markedRect.y, textColor);
126
127
128
129
130
131
132
TTF_SizeUTF8(font, event.edit.text, &w, &h);
underlineRect = markedRect;
underlineRect.y += (h - 2);
underlineRect.h = 2;
underlineRect.w = w;
SDL_FillRect(screen, &underlineRect, lineColor);
133
134
135
SDL_Flip(screen);
break;
136
137
138
139
140
141
142
143
144
145
146
147
148
149
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
TTF_CloseFont(font);
TTF_Quit();
return 0;
}