changeset 3137:311c678f3b2e gsoc2009_IME

Update testime program to accept font parameter.
author Jiang Jiang <gzjjgod@gmail.com>
date Thu, 06 Aug 2009 09:01:03 +0000
parents 962357f325e1
children 7100a1a20143
files test/testime.c
diffstat 1 files changed, 40 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/test/testime.c	Thu Aug 06 08:59:53 2009 +0000
+++ b/test/testime.c	Thu Aug 06 09:01:03 2009 +0000
@@ -8,7 +8,7 @@
 #include <SDL/SDL_ttf.h>
  
 #define DEFAULT_PTSIZE  30
-#define DEFAULT_FONT    "DroidSansFallback.ttf"
+#define DEFAULT_FONT    "/System/Library/Fonts/华文细黑.ttf"
 #define MAX_TEXT_LENGTH 256
 
 SDL_Surface *screen;
@@ -18,9 +18,38 @@
 SDL_Color textColor = { 0, 0, 0 };
 char text[MAX_TEXT_LENGTH], *markedText;
 
+void usage()
+{
+    printf("usage: testime [--font fontfile] [--fullscreen]\n");
+    exit(0);
+}
+
 void InitVideo(int argc, char *argv[])
 {
     int width = 500, height = 250;
+    int flags = SDL_HWSURFACE;
+    const char *fontname = DEFAULT_FONT;
+    int fullscreen = 0;
+
+    for (argc--, argv++; argc > 0; argc--, argv++)
+    {
+        if (strcmp(argv[0], "--help") == 0)
+            usage();
+
+        else if (strcmp(argv[0], "--fullscreen") == 0)
+            fullscreen = 1;
+
+        else if (strcmp(argv[0], "--font") == 0)
+        {
+            argc--;
+            argv++;
+
+            if (argc > 0)
+                fontname = argv[0];
+            else
+                usage();
+        }
+    }
 
     SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
     if (SDL_Init(SDL_INIT_VIDEO) < 0)
@@ -32,17 +61,17 @@
     /* Initialize fonts */
     TTF_Init();
 
-    font = TTF_OpenFont(DEFAULT_FONT, DEFAULT_PTSIZE);
+    font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
     if (! font)
     {
         fprintf(stderr, "Failed to find font: %s\n", SDL_GetError());
         exit(-1);
     }
 
+    printf("Using font: %s\n", fontname);
     atexit(SDL_Quit);
 
-    int flags = SDL_HWSURFACE;
-    if (argc > 1 && strcmp(argv[1], "--fullscreen") == 0)
+    if (fullscreen)
     {
         SDL_DisplayMode mode;
         SDL_GetDesktopDisplayMode(&mode);
@@ -123,8 +152,11 @@
         return;
     }
 
+    cursorRect = markedRect;
+    cursorRect.w = 2;
+    cursorRect.h = h;
+
     SDL_FillRect(screen, &markedRect, backColor);
-
     if (markedText)
     {
         RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
@@ -134,12 +166,12 @@
         underlineRect.y += (h - 2);
         underlineRect.h = 2;
         underlineRect.w = w;
+
+        cursorRect.x += w + 1;
+
         SDL_FillRect(screen, &underlineRect, lineColor);
     }
 
-    cursorRect = markedRect;
-    cursorRect.w = 2;
-    cursorRect.h = h;
     SDL_FillRect(screen, &cursorRect, lineColor);
 
     SDL_Flip(screen);