SDL  2.0
testime.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#include "SDL_test_common.h"
+ Include dependency graph for testime.c:

Go to the source code of this file.

Macros

#define DEFAULT_PTSIZE   30
 
#define DEFAULT_FONT   "/System/Library/Fonts/华文细黑.ttf"
 
#define MAX_TEXT_LENGTH   256
 

Functions

size_t utf8_length (unsigned char c)
 
char * utf8_next (char *p)
 
char * utf8_advance (char *p, size_t distance)
 
void usage ()
 
void InitInput ()
 
void CleanupVideo ()
 
void _Redraw (SDL_Renderer *renderer)
 
void Redraw ()
 
int main (int argc, char *argv[])
 

Variables

static SDLTest_CommonStatestate
 
static SDL_Rect textRect
 
static SDL_Rect markedRect
 
static SDL_Color lineColor = {0,0,0,0}
 
static SDL_Color backColor = {255,255,255,0}
 
static SDL_Color textColor = {0,0,0,0}
 
static char text [MAX_TEXT_LENGTH]
 
static char markedText [SDL_TEXTEDITINGEVENT_TEXT_SIZE]
 
static int cursor = 0
 

Macro Definition Documentation

#define DEFAULT_FONT   "/System/Library/Fonts/华文细黑.ttf"

Definition at line 26 of file testime.c.

Referenced by main().

#define DEFAULT_PTSIZE   30

Definition at line 25 of file testime.c.

Referenced by main().

#define MAX_TEXT_LENGTH   256

Definition at line 27 of file testime.c.

Function Documentation

void _Redraw ( SDL_Renderer renderer)

Definition at line 112 of file testime.c.

References cursor, SDL_Rect::h, SDL_Surface::h, markedRect, markedText, NULL, SDL_CreateTextureFromSurface, SDL_DestroyTexture, SDL_FreeSurface, SDL_RenderCopy, SDL_RenderFillRect, SDL_SetRenderDrawColor, SDL_SetTextInputRect, SDL_StartTextInput, SDL_StopTextInput, text, utf8_advance(), SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by Redraw().

112  {
113  int w = 0, h = textRect.h;
114  SDL_Rect cursorRect, underlineRect;
115 
116  SDL_SetRenderDrawColor(renderer, 255,255,255,255);
117  SDL_RenderFillRect(renderer,&textRect);
118 
119 #ifdef HAVE_SDL_TTF
120  if (*text)
121  {
122  SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
123  SDL_Rect dest = {textRect.x, textRect.y, textSur->w, textSur->h };
124 
125  SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
126  SDL_FreeSurface(textSur);
127 
128  SDL_RenderCopy(renderer,texture,NULL,&dest);
129  SDL_DestroyTexture(texture);
130  TTF_SizeUTF8(font, text, &w, &h);
131  }
132 #endif
133 
134  markedRect.x = textRect.x + w;
135  markedRect.w = textRect.w - w;
136  if (markedRect.w < 0)
137  {
138  /* Stop text input because we cannot hold any more characters */
140  return;
141  }
142  else
143  {
145  }
146 
147  cursorRect = markedRect;
148  cursorRect.w = 2;
149  cursorRect.h = h;
150 
151  SDL_SetRenderDrawColor(renderer, 255,255,255,255);
152  SDL_RenderFillRect(renderer,&markedRect);
153 
154  if (markedText[0])
155  {
156 #ifdef HAVE_SDL_TTF
157  if (cursor)
158  {
159  char *p = utf8_advance(markedText, cursor);
160  char c = 0;
161  if (!p)
162  p = &markedText[strlen(markedText)];
163 
164  c = *p;
165  *p = 0;
166  TTF_SizeUTF8(font, markedText, &w, 0);
167  cursorRect.x += w;
168  *p = c;
169  }
170  SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, markedText, textColor);
171  SDL_Rect dest = {markedRect.x, markedRect.y, textSur->w, textSur->h };
172  TTF_SizeUTF8(font, markedText, &w, &h);
173  SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
174  SDL_FreeSurface(textSur);
175 
176  SDL_RenderCopy(renderer,texture,NULL,&dest);
177  SDL_DestroyTexture(texture);
178 #endif
179 
180  underlineRect = markedRect;
181  underlineRect.y += (h - 2);
182  underlineRect.h = 2;
183  underlineRect.w = w;
184 
185  SDL_SetRenderDrawColor(renderer, 0,0,0,0);
186  SDL_RenderFillRect(renderer,&markedRect);
187  }
188 
189  SDL_SetRenderDrawColor(renderer, 0,0,0,0);
190  SDL_RenderFillRect(renderer,&cursorRect);
191 
193 }
#define SDL_RenderFillRect
GLenum GLenum GLuint texture
GLfloat GLfloat p
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
static int cursor
Definition: testime.c:35
#define SDL_RenderCopy
char * utf8_advance(char *p, size_t distance)
Definition: testime.c:71
#define SDL_StopTextInput
#define SDL_CreateTextureFromSurface
#define SDL_FreeSurface
const GLubyte * c
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
#define SDL_StartTextInput
#define NULL
Definition: begin_code.h:143
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:34
#define SDL_SetTextInputRect
static char markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
Definition: testime.c:34
#define SDL_DestroyTexture
int h
Definition: SDL_rect.h:67
static SDL_Rect textRect
Definition: testime.c:30
#define SDL_SetRenderDrawColor
GLubyte GLubyte GLubyte GLubyte w
int y
Definition: SDL_rect.h:66
static SDL_Rect markedRect
Definition: testime.c:30
GLfloat GLfloat GLfloat GLfloat h
static SDL_Color textColor
Definition: testime.c:33
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
void CleanupVideo ( )

Definition at line 102 of file testime.c.

References SDL_StopTextInput.

Referenced by main().

103 {
105 #ifdef HAVE_SDL_TTF
106  TTF_CloseFont(font);
107  TTF_Quit();
108 #endif
109 }
#define SDL_StopTextInput
void InitInput ( )

Definition at line 87 of file testime.c.

References DEFAULT_WINDOW_WIDTH, SDL_Rect::h, markedText, SDL_StartTextInput, text, textRect, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by main().

88 {
89 
90  /* Prepare a rect for text input */
91  textRect.x = textRect.y = 100;
93  textRect.h = 50;
94 
95  text[0] = 0;
97  markedText[0] = 0;
98 
100 }
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
#define SDL_StartTextInput
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:34
static char markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
Definition: testime.c:34
int h
Definition: SDL_rect.h:67
static SDL_Rect textRect
Definition: testime.c:30
#define DEFAULT_WINDOW_WIDTH
int y
Definition: SDL_rect.h:66
static SDL_Rect markedRect
Definition: testime.c:30
int main ( int  argc,
char *  argv[] 
)

Definition at line 210 of file testime.c.

References CleanupVideo(), cursor, DEFAULT_FONT, DEFAULT_PTSIZE, done, SDL_Event::edit, i, InitInput(), SDL_Event::key, SDL_KeyboardEvent::keysym, SDL_TextEditingEvent::length, markedText, SDLTest_CommonState::num_windows, Redraw(), renderer, SDLTest_CommonState::renderers, SDL_Keysym::scancode, SDL_BLENDMODE_NONE, SDL_GetKeyName, SDL_GetScancodeName, SDL_INIT_VIDEO, SDL_KEYDOWN, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_PollEvent, SDL_Quit, SDL_RenderClear, SDL_SetRenderDrawBlendMode, SDL_SetRenderDrawColor, SDL_strlcat, SDL_strlen, SDL_TEXTEDITING, SDL_TEXTINPUT, SDLK_BACKSPACE, SDLK_RETURN, SDLTest_CommonArg(), SDLTest_CommonCreateState(), SDLTest_CommonEvent(), SDLTest_CommonInit(), SDLTest_CommonQuit(), SDL_TextEditingEvent::start, SDL_Keysym::sym, text, SDL_TextEditingEvent::text, SDL_TextInputEvent::text, SDL_Event::text, SDL_Event::type, usage(), and SDL_Rect::w.

210  {
211  int i, done;
213  const char *fontname = DEFAULT_FONT;
214 
215  /* Enable standard application logging */
217 
218  /* Initialize test framework */
220  if (!state) {
221  return 1;
222  }
223  for (i = 1; i < argc;i++) {
225  }
226  for (argc--, argv++; argc > 0; argc--, argv++)
227  {
228  if (strcmp(argv[0], "--help") == 0) {
229  usage();
230  return 0;
231  }
232 
233  else if (strcmp(argv[0], "--font") == 0)
234  {
235  argc--;
236  argv++;
237 
238  if (argc > 0)
239  fontname = argv[0];
240  else {
241  usage();
242  return 0;
243  }
244  }
245  }
246 
247  if (!SDLTest_CommonInit(state)) {
248  return 2;
249  }
250 
251 
252 #ifdef HAVE_SDL_TTF
253  /* Initialize fonts */
254  TTF_Init();
255 
256  font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
257  if (! font)
258  {
259  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find font: %s\n", TTF_GetError());
260  exit(-1);
261  }
262 #endif
263 
264  SDL_Log("Using font: %s\n", fontname);
265  atexit(SDL_Quit);
266 
267  InitInput();
268  /* Create the windows and initialize the renderers */
269  for (i = 0; i < state->num_windows; ++i) {
272  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
273  SDL_RenderClear(renderer);
274  }
275  Redraw();
276  /* Main render loop */
277  done = 0;
278  while (!done) {
279  /* Check for events */
280  while (SDL_PollEvent(&event)) {
281  SDLTest_CommonEvent(state, &event, &done);
282  switch(event.type) {
283  case SDL_KEYDOWN: {
284  switch (event.key.keysym.sym)
285  {
286  case SDLK_RETURN:
287  text[0]=0x00;
288  Redraw();
289  break;
290  case SDLK_BACKSPACE:
291  {
292  size_t textlen = SDL_strlen(text);
293 
294  do {
295  if (textlen==0)
296  {
297  break;
298  }
299  if ((text[textlen-1] & 0x80) == 0x00)
300  {
301  /* One byte */
302  text[textlen-1]=0x00;
303  break;
304  }
305  if ((text[textlen-1] & 0xC0) == 0x80)
306  {
307  /* Byte from the multibyte sequence */
308  text[textlen-1]=0x00;
309  textlen--;
310  }
311  if ((text[textlen-1] & 0xC0) == 0xC0)
312  {
313  /* First byte of multibyte sequence */
314  text[textlen-1]=0x00;
315  break;
316  }
317  } while(1);
318 
319  Redraw();
320  }
321  break;
322  }
323 
324  if (done)
325  {
326  break;
327  }
328 
329  SDL_Log("Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
330  event.key.keysym.scancode,
332  event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
333  break;
334 
335  case SDL_TEXTINPUT:
336  if (event.text.text[0] == '\0' || event.text.text[0] == '\n' ||
337  markedRect.w < 0)
338  break;
339 
340  SDL_Log("Keyboard: text input \"%s\"\n", event.text.text);
341 
342  if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
343  SDL_strlcat(text, event.text.text, sizeof(text));
344 
345  SDL_Log("text inputed: %s\n", text);
346 
347  /* After text inputed, we can clear up markedText because it */
348  /* is committed */
349  markedText[0] = 0;
350  Redraw();
351  break;
352 
353  case SDL_TEXTEDITING:
354  SDL_Log("text editing \"%s\", selected range (%d, %d)\n",
355  event.edit.text, event.edit.start, event.edit.length);
356 
357  strcpy(markedText, event.edit.text);
358  cursor = event.edit.start;
359  Redraw();
360  break;
361  }
362  break;
363 
364  }
365  }
366  }
367  CleanupVideo();
369  return 0;
370 }
void CleanupVideo()
Definition: testime.c:102
#define SDL_PollEvent
#define SDL_strlcat
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
#define SDL_SetRenderDrawBlendMode
SDL_Scancode scancode
Definition: SDL_keyboard.h:49
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
static int cursor
Definition: testime.c:35
#define SDL_LogError
SDL_Renderer * renderer
#define SDL_Log
SDL_TextInputEvent text
Definition: SDL_events.h:528
char text[SDL_TEXTINPUTEVENT_TEXT_SIZE]
Definition: SDL_events.h:223
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
void InitInput()
Definition: testime.c:87
struct _cl_event * event
#define SDL_Quit
int done
Definition: checkkeys.c:28
char text[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
Definition: SDL_events.h:208
void Redraw()
Definition: testime.c:195
static SDLTest_CommonState * state
Definition: testime.c:29
SDL_Keysym keysym
Definition: SDL_events.h:196
int w
Definition: SDL_rect.h:67
SDL_Renderer ** renderers
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
#define SDL_LogSetPriority
SDL_TextEditingEvent edit
Definition: SDL_events.h:527
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:34
#define SDL_RenderClear
#define SDL_GetScancodeName
static char markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
Definition: testime.c:34
SDL_KeyboardEvent key
Definition: SDL_events.h:526
#define SDL_strlen
#define SDL_GetKeyName
SDL_Keycode sym
Definition: SDL_keyboard.h:50
#define DEFAULT_FONT
Definition: testime.c:26
General event structure.
Definition: SDL_events.h:521
#define SDL_SetRenderDrawColor
void usage()
Definition: testime.c:81
#define DEFAULT_PTSIZE
Definition: testime.c:25
static SDL_Rect markedRect
Definition: testime.c:30
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
#define SDL_INIT_VIDEO
Definition: SDL.h:77
Uint32 type
Definition: SDL_events.h:523
void Redraw ( )

Definition at line 195 of file testime.c.

References _Redraw(), i, NULL, SDLTest_CommonState::num_windows, renderer, SDLTest_CommonState::renderers, SDL_RenderClear, SDL_RenderPresent, SDL_SetRenderDrawColor, and SDLTest_CommonState::windows.

Referenced by main().

195  {
196  int i;
197  for (i = 0; i < state->num_windows; ++i) {
199  if (state->windows[i] == NULL)
200  continue;
201  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
202  SDL_RenderClear(renderer);
203 
204  _Redraw(renderer);
205 
206  SDL_RenderPresent(renderer);
207  }
208 }
SDL_Window ** windows
SDL_Renderer * renderer
static SDLTest_CommonState * state
Definition: testime.c:29
SDL_Renderer ** renderers
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
#define NULL
Definition: begin_code.h:143
#define SDL_RenderClear
#define SDL_SetRenderDrawColor
void _Redraw(SDL_Renderer *renderer)
Definition: testime.c:112
#define SDL_RenderPresent
void usage ( )

Definition at line 81 of file testime.c.

References SDL_Log.

Referenced by main().

82 {
83  SDL_Log("usage: testime [--font fontfile]\n");
84  exit(0);
85 }
#define SDL_Log
char* utf8_advance ( char *  p,
size_t  distance 
)

Definition at line 71 of file testime.c.

References i, and utf8_next().

Referenced by _Redraw().

72 {
73  size_t i = 0;
74  for (; i < distance && p; ++i)
75  {
76  p = utf8_next(p);
77  }
78  return p;
79 }
GLfloat GLfloat p
char * utf8_next(char *p)
Definition: testime.c:55
GLsizei GLsizei GLfloat distance
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
size_t utf8_length ( unsigned char  c)

Definition at line 40 of file testime.c.

Referenced by utf8_next().

41 {
42  c = (unsigned char)(0xff & c);
43  if (c < 0x80)
44  return 1;
45  else if ((c >> 5) ==0x6)
46  return 2;
47  else if ((c >> 4) == 0xe)
48  return 3;
49  else if ((c >> 3) == 0x1e)
50  return 4;
51  else
52  return 0;
53 }
const GLubyte * c
char* utf8_next ( char *  p)

Definition at line 55 of file testime.c.

References i, and utf8_length().

Referenced by utf8_advance().

56 {
57  size_t len = utf8_length(*p);
58  size_t i = 0;
59  if (!len)
60  return 0;
61 
62  for (; i < len; ++i)
63  {
64  ++p;
65  if (!*p)
66  return 0;
67  }
68  return p;
69 }
GLfloat GLfloat p
GLenum GLsizei len
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
size_t utf8_length(unsigned char c)
Definition: testime.c:40

Variable Documentation

SDL_Color backColor = {255,255,255,0}
static

Definition at line 32 of file testime.c.

int cursor = 0
static

Definition at line 35 of file testime.c.

Referenced by _Redraw(), and main().

SDL_Color lineColor = {0,0,0,0}
static

Definition at line 31 of file testime.c.

SDL_Rect markedRect
static

Definition at line 30 of file testime.c.

Referenced by _Redraw().

char markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
static

Definition at line 34 of file testime.c.

Referenced by _Redraw(), InitInput(), and main().

SDLTest_CommonState* state
static

Definition at line 29 of file testime.c.

SDL_Color textColor = {0,0,0,0}
static

Definition at line 33 of file testime.c.

SDL_Rect textRect
static

Definition at line 30 of file testime.c.

Referenced by InitInput().