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