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