Xcode-iOS/Demos/src/keyboard.c
changeset 11498 d4f87399777f
parent 11307 003462e40c4f
equal deleted inserted replaced
11497:65698434e30d 11498:d4f87399777f
     4  *  use however you want
     4  *  use however you want
     5  */
     5  */
     6 
     6 
     7 #include "SDL.h"
     7 #include "SDL.h"
     8 #include "common.h"
     8 #include "common.h"
       
     9 
       
    10 #define TEST_INPUT_RECT
     9 
    11 
    10 #define GLYPH_SIZE_IMAGE 16     /* size of glyphs (characters) in the bitmap font file */
    12 #define GLYPH_SIZE_IMAGE 16     /* size of glyphs (characters) in the bitmap font file */
    11 #define GLYPH_SIZE_SCREEN 32    /* size of glyphs (characters) as shown on the screen */
    13 #define GLYPH_SIZE_SCREEN 32    /* size of glyphs (characters) as shown on the screen */
    12 
    14 
    13 #define MAX_CHARS 1024
    15 #define MAX_CHARS 1024
   138     int y_padding = 32;         /* padding space at top of screen */
   140     int y_padding = 32;         /* padding space at top of screen */
   139     /* figure out the number of characters that can fit horizontally across the screen */
   141     /* figure out the number of characters that can fit horizontally across the screen */
   140     int max_x_chars = (renderW - 2 * x_padding) / GLYPH_SIZE_SCREEN;
   142     int max_x_chars = (renderW - 2 * x_padding) / GLYPH_SIZE_SCREEN;
   141     int line_separation = 5;    /* pixels between each line */
   143     int line_separation = 5;    /* pixels between each line */
   142     *x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
   144     *x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
       
   145 #ifdef TEST_INPUT_RECT
       
   146     *y = renderH - GLYPH_SIZE_SCREEN;
       
   147 #else
   143     *y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) + y_padding;
   148     *y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) + y_padding;
       
   149 #endif
   144 }
   150 }
   145 
   151 
   146 void
   152 void
   147 drawGlyph(int glyph, int positionIndex)
   153 drawGlyph(int glyph, int positionIndex)
   148 {
   154 {
   211     SDL_Event event;            /* last event received */
   217     SDL_Event event;            /* last event received */
   212     SDL_Scancode scancode;      /* scancode of last key we pushed */
   218     SDL_Scancode scancode;      /* scancode of last key we pushed */
   213     int width;
   219     int width;
   214     int height;
   220     int height;
   215     int done;
   221     int done;
       
   222     SDL_Rect textrect;
   216 
   223 
   217     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   224     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   218         printf("Error initializing SDL: %s", SDL_GetError());
   225         printf("Error initializing SDL: %s", SDL_GetError());
   219     }
   226     }
   220     /* create window */
   227     /* create window */
   221     window = SDL_CreateWindow("iOS keyboard test", 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
   228     window = SDL_CreateWindow("iOS keyboard test", 0, 0, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
   222     /* create renderer */
   229     /* create renderer */
   223     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
   230     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
   224 
   231 
   225     SDL_GetWindowSize(window, &width, &height);
   232     SDL_GetWindowSize(window, &width, &height);
   226     SDL_RenderSetLogicalSize(renderer, width, height);
   233     SDL_RenderSetLogicalSize(renderer, width, height);
   227 
   234 
   228     /* load up our font */
   235     /* load up our font */
   229     loadFont();
   236     loadFont();
       
   237     
       
   238     /* Show onscreen keyboard */
       
   239 #ifdef TEST_INPUT_RECT
       
   240     textrect.x = 0;
       
   241     textrect.y = height - GLYPH_SIZE_IMAGE;
       
   242     textrect.w = width;
       
   243     textrect.h = GLYPH_SIZE_IMAGE;
       
   244     SDL_SetTextInputRect(&textrect);
       
   245 #endif
       
   246     SDL_StartTextInput();
   230 
   247 
   231     done = 0;
   248     done = 0;
   232     while (!done) {
   249     while (!done) {
   233         while (SDL_PollEvent(&event)) {
   250         while (SDL_PollEvent(&event)) {
   234             switch (event.type) {
   251             switch (event.type) {
   235             case SDL_QUIT:
   252             case SDL_QUIT:
   236                 done = 1;
   253                 done = 1;
       
   254                 break;
       
   255             case SDL_WINDOWEVENT:
       
   256                 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
       
   257 					width = event.window.data1;
       
   258 					height = event.window.data2;
       
   259                     SDL_RenderSetLogicalSize(renderer, width, height);
       
   260 #ifdef TEST_INPUT_RECT
       
   261                     textrect.x = 0;
       
   262                     textrect.y = height - GLYPH_SIZE_IMAGE;
       
   263                     textrect.w = width;
       
   264                     textrect.h = GLYPH_SIZE_IMAGE;
       
   265                     SDL_SetTextInputRect(&textrect);
       
   266 #endif
       
   267                 }
   237                 break;
   268                 break;
   238             case SDL_KEYDOWN:
   269             case SDL_KEYDOWN:
   239                 if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
   270                 if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
   240                     if (numChars > 0) {
   271                     if (numChars > 0) {
   241                         numChars--;
   272                         numChars--;