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