test/testime.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 22 Aug 2010 11:56:07 -0700
changeset 4638 08d22c54a21f
parent 4637 8c229370ddba
child 4763 518d1679d2d0
permissions -rw-r--r--
Don't need to use strlen() to determine if there's text.
Use the SDL safe strcpy() function
     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 #ifdef HAVE_SDL_TTF
     9 #include "SDL_ttf.h"
    10 #endif
    11 
    12 #define DEFAULT_PTSIZE  30
    13 #ifdef __QNXNTO__
    14     #define DEFAULT_FONT    "/usr/photon/font_repository/tt0003m_.ttf"
    15 #else
    16     #define DEFAULT_FONT    "/System/Library/Fonts/华文细黑.ttf"
    17 #endif
    18 #define MAX_TEXT_LENGTH 256
    19 
    20 SDL_Surface *screen;
    21 
    22 #ifdef HAVE_SDL_TTF
    23 TTF_Font *font;
    24 #endif
    25 SDL_Rect textRect, markedRect;
    26 Uint32 lineColor, backColor;
    27 SDL_Color textColor = { 0, 0, 0 };
    28 char text[MAX_TEXT_LENGTH], *markedText;
    29 
    30 void usage()
    31 {
    32     printf("usage: testime [--font fontfile] [--fullscreen]\n");
    33     exit(0);
    34 }
    35 
    36 void InitVideo(int argc, char *argv[])
    37 {
    38     int width = 640, height = 480;
    39     int flags = SDL_HWSURFACE;
    40     const char *fontname = DEFAULT_FONT;
    41     int fullscreen = 0;
    42 
    43     for (argc--, argv++; argc > 0; argc--, argv++)
    44     {
    45         if (strcmp(argv[0], "--help") == 0)
    46             usage();
    47 
    48         else if (strcmp(argv[0], "--fullscreen") == 0)
    49             fullscreen = 1;
    50 
    51         else if (strcmp(argv[0], "--font") == 0)
    52         {
    53             argc--;
    54             argv++;
    55 
    56             if (argc > 0)
    57                 fontname = argv[0];
    58             else
    59                 usage();
    60         }
    61     }
    62 
    63     SDL_setenv("SDL_VIDEO_WINDOW_POS", "center", 1);
    64     if (SDL_Init(SDL_INIT_VIDEO) < 0)
    65     {
    66         fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
    67         exit(-1);
    68     }
    69 
    70 #ifdef HAVE_SDL_TTF
    71     /* Initialize fonts */
    72     TTF_Init();
    73 
    74     font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
    75     if (! font)
    76     {
    77         fprintf(stderr, "Failed to find font: %s\n", TTF_GetError());
    78         exit(-1);
    79     }
    80 #endif
    81 
    82     printf("Using font: %s\n", fontname);
    83     atexit(SDL_Quit);
    84 
    85     if (fullscreen)
    86     {
    87         SDL_DisplayMode mode;
    88         SDL_GetDesktopDisplayMode(&mode);
    89 
    90         width = mode.w;
    91         height = mode.h;
    92         fprintf(stderr, "%dx%d\n", width, height);
    93         flags |= SDL_FULLSCREEN;
    94     }
    95 
    96     /* Create window */
    97     screen = SDL_SetVideoMode(width, height, 32, flags);
    98     if (screen == NULL)
    99     {
   100         fprintf(stderr, "Unable to set %dx%d video: %s\n",
   101                 width, height, SDL_GetError());
   102         exit(-1);
   103     }
   104 }
   105 
   106 void CleanupVideo()
   107 {
   108     SDL_StopTextInput();
   109 #ifdef HAVE_SDL_TTF
   110     TTF_CloseFont(font);
   111     TTF_Quit();
   112 #endif
   113 }
   114 
   115 void InitInput()
   116 {
   117     backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
   118     lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0);
   119 
   120     /* Prepare a rect for text input */
   121     textRect.x = textRect.y = 100;
   122     textRect.w = screen->w - 2 * textRect.x;
   123     textRect.h = 50;
   124 
   125     text[0] = 0;
   126     markedRect = textRect;
   127     markedText = NULL;
   128 
   129     SDL_StartTextInput();
   130 }
   131 
   132 #ifdef HAVE_SDL_TTF
   133 static void RenderText(SDL_Surface *sur,
   134                         TTF_Font *font,
   135                         const char *text,
   136                         int x, int y,
   137                         SDL_Color color)
   138 {
   139     if (text && *text) {
   140         SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color);
   141         SDL_Rect dest = { x, y, textSur->w, textSur->h };
   142 
   143         SDL_BlitSurface(textSur, NULL, sur, &dest);
   144         SDL_FreeSurface(textSur);
   145     }
   146 }
   147 #endif
   148 
   149 void Redraw()
   150 {
   151     int w = 0, h = textRect.h;
   152     SDL_Rect cursorRect, underlineRect;
   153 
   154     SDL_FillRect(screen, &textRect, backColor);
   155 
   156 #ifdef HAVE_SDL_TTF
   157     if (*text)
   158     {
   159         RenderText(screen, font, text, textRect.x, textRect.y, textColor);
   160         TTF_SizeUTF8(font, text, &w, &h);
   161     }
   162 #endif
   163 
   164     markedRect.x = textRect.x + w;
   165     markedRect.w = textRect.w - w;
   166     if (markedRect.w < 0)
   167     {
   168         SDL_Flip(screen);
   169         // Stop text input because we cannot hold any more characters
   170         SDL_StopTextInput();
   171         return;
   172     }
   173     else
   174     {
   175         SDL_StartTextInput();
   176     }
   177 
   178     cursorRect = markedRect;
   179     cursorRect.w = 2;
   180     cursorRect.h = h;
   181 
   182     SDL_FillRect(screen, &markedRect, backColor);
   183     if (markedText)
   184     {
   185 #ifdef HAVE_SDL_TTF
   186         RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
   187         TTF_SizeUTF8(font, markedText, &w, &h);
   188 #endif
   189 
   190         underlineRect = markedRect;
   191         underlineRect.y += (h - 2);
   192         underlineRect.h = 2;
   193         underlineRect.w = w;
   194 
   195         cursorRect.x += w + 1;
   196 
   197         SDL_FillRect(screen, &underlineRect, lineColor);
   198     }
   199 
   200     SDL_FillRect(screen, &cursorRect, lineColor);
   201 
   202     SDL_Flip(screen);
   203 
   204     SDL_SetTextInputRect(&markedRect);
   205 }
   206 
   207 void
   208 HotKey_ToggleFullScreen(void)
   209 {
   210     SDL_Surface *screen;
   211 
   212     screen = SDL_GetVideoSurface();
   213     if (SDL_WM_ToggleFullScreen(screen)) {
   214         printf("Toggled fullscreen mode - now %s\n",
   215                (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
   216     } else {
   217         printf("Unable to toggle fullscreen mode\n");
   218     }
   219 }
   220 
   221 int main(int argc, char *argv[])
   222 {
   223     SDL_Event event;
   224     int done = 0;
   225 
   226     InitVideo(argc, argv);
   227     InitInput();
   228     Redraw();
   229 
   230     while (! done && SDL_WaitEvent(&event))
   231     {
   232         switch (event.type)
   233         {
   234         case SDL_KEYDOWN:
   235             switch (event.key.keysym.sym)
   236             {
   237                 case SDLK_ESCAPE:
   238                      done = 1;
   239                      break;
   240                 case SDLK_RETURN:
   241                      text[0]=0x00;
   242                      Redraw();
   243                      break;
   244                 case SDLK_BACKSPACE:
   245                      {
   246                          int textlen=SDL_strlen(text);
   247 
   248                          do {
   249                              if (textlen==0)
   250                              {
   251                                  break;
   252                              }
   253                              if ((text[textlen-1] & 0x80) == 0x00)
   254                              {
   255                                  /* One byte */
   256                                  text[textlen-1]=0x00;
   257                                  break;
   258                              }
   259                              if ((text[textlen-1] & 0xC0) == 0x80)
   260                              {
   261                                  /* Byte from the multibyte sequence */
   262                                  text[textlen-1]=0x00;
   263                                  textlen--;
   264                              }
   265                              if ((text[textlen-1] & 0xC0) == 0xC0)
   266                              {
   267                                  /* First byte of multibyte sequence */
   268                                  text[textlen-1]=0x00;
   269                                  break;
   270                              }
   271                          } while(1);
   272 
   273                          Redraw();
   274                      }
   275                      break;
   276             }
   277 
   278             if (done)
   279             {
   280                 break;
   281             }
   282 
   283             fprintf(stderr,
   284                     "Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
   285                     event.key.keysym.scancode,
   286                     SDL_GetScancodeName(event.key.keysym.scancode),
   287                     event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
   288             break;
   289 
   290         case SDL_TEXTINPUT:
   291             if (SDL_strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
   292                 markedRect.w < 0)
   293                 break;
   294 
   295             fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text);
   296 
   297             if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
   298                 SDL_strlcpy(text + SDL_strlen(text), event.text.text, sizeof(text));
   299 
   300             fprintf(stderr, "text inputed: %s\n", text);
   301 
   302             // After text inputed, we can clear up markedText because it
   303             // is committed
   304             markedText = NULL;
   305             Redraw();
   306             break;
   307 
   308         case SDL_TEXTEDITING:
   309             fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
   310                     event.edit.text, event.edit.start, event.edit.length);
   311 
   312             markedText = event.edit.text;
   313             Redraw();
   314             break;
   315 
   316         case SDL_QUIT:
   317             done = 1;
   318             break;
   319 
   320         default:
   321             break;
   322         }
   323     }
   324 
   325     CleanupVideo();
   326     return 0;
   327 }