Mercurial > sdl-ios-xcode
changeset 3131:009bd8f81947 gsoc2009_IME
Add IME test program
author | Jiang Jiang <gzjjgod@gmail.com> |
---|---|
date | Wed, 01 Jul 2009 05:52:17 +0000 |
parents | fef1a835af43 |
children | 88861448961f |
files | test/Makefile.in test/testime.c |
diffstat | 2 files changed, 132 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/test/Makefile.in Fri Jun 26 10:37:57 2009 +0000 +++ b/test/Makefile.in Wed Jul 01 05:52:17 2009 +0000 @@ -7,7 +7,7 @@ CFLAGS = @CFLAGS@ LIBS = @LIBS@ -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testresample$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testintersections$(EXE) testdraw2$(EXE) testdyngl$(EXE) testdyngles$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testgles$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE) testmmousetablet$(EXE) +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testresample$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testintersections$(EXE) testdraw2$(EXE) testdyngl$(EXE) testdyngles$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testgles$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE) testmmousetablet$(EXE) testime$(EXE) all: Makefile $(TARGETS) @@ -146,6 +146,9 @@ testmmousetablet$(EXE): $(srcdir)/testmmousetablet.c $(CC) -o $@ $? $(CFLAGS) $(LIBS) +testime$(EXE): $(srcdir)/testime.c + $(CC) -o $@ $? $(CFLAGS) -L../build/.libs $(LIBS) -lSDL_ttf + clean: rm -f $(TARGETS)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/testime.c Wed Jul 01 05:52:17 2009 +0000 @@ -0,0 +1,128 @@ +/* A simple program to test the Input Method support in the SDL library (1.3+) */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "SDL.h" +#include <SDL/SDL_ttf.h> + +#define DEFAULT_PTSIZE 30 +#define DEFAULT_FONT "DroidSansFallback.ttf" +#define MAX_TEXT_LENGTH 256 + +static void render_text(SDL_Surface *sur, + TTF_Font *font, + const char *text, + int x, int y, + SDL_Color color) +{ + SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color); + SDL_Rect dest = { x, y, textSur->w, textSur->h }; + + SDL_BlitSurface(textSur, NULL, sur, &dest); + SDL_FreeSurface(textSur); +} + +int main(int argc, char *argv[]) +{ + int width, height; + SDL_Surface *screen; + TTF_Font *font; + + width = 500, height = 250; + + SDL_putenv("SDL_VIDEO_WINDOW_POS=center"); + + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); + return -1; + } + + /* Initialize fonts */ + TTF_Init(); + + font = TTF_OpenFont(DEFAULT_FONT, DEFAULT_PTSIZE); + if (! font) + { + fprintf(stderr, "Failed to find font: %s\n", SDL_GetError()); + exit(-1); + } + + atexit(SDL_Quit); + + /* Create window */ + screen = SDL_SetVideoMode(width, height, 32, + SDL_HWSURFACE | SDL_DOUBLEBUF); + if (screen == NULL) + { + fprintf(stderr, "Unable to set %dx%d video: %s\n", + width, height, SDL_GetError()); + return -1; + } + + /* Prepare a rect for text input */ + SDL_Rect textRect = { 100, 80, 300, 50 }, markedRect; + Uint32 backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF); + SDL_Color textColor = { 0, 0, 0 }; + SDL_FillRect(screen, &textRect, backColor); + + markedRect = textRect; + SDL_StartTextInput(&markedRect); + + SDL_Flip(screen); + + SDL_Event event; + int done = 0, inputed = 0; + int w, h; + char text[MAX_TEXT_LENGTH]; + + while (! done && SDL_WaitEvent(&event)) + { + switch (event.type) + { + case SDL_KEYDOWN: + fprintf(stderr, + "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n", + event.key.which, event.key.keysym.scancode, + SDL_GetScancodeName(event.key.keysym.scancode), + event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym)); + break; + + case SDL_TEXTINPUT: + fprintf(stderr, "Keyboard %d: text input \"%s\"\n", + event.text.which, event.text.text); + + if (inputed < sizeof(text)) + { + strcpy(text + inputed, event.text.text); + inputed += strlen(event.text.text); + } + + fprintf(stderr, "text inputed: %s\n", text); + SDL_FillRect(screen, &textRect, backColor); + + render_text(screen, font, text, textRect.x, textRect.y, textColor); + TTF_SizeUTF8(font, text, &w, &h); + markedRect.x = textRect.x + w; + SDL_Flip(screen); + + SDL_StartTextInput(&markedRect); + break; + + case SDL_QUIT: + done = 1; + break; + + default: + break; + } + } + + TTF_CloseFont(font); + TTF_Quit(); + + return 0; +} +