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