Xcode-iOS/Demos/src/keyboard.c
author Alex Szpakowski
Tue, 15 Aug 2017 22:53:57 -0300
changeset 11306 45fbdef6a227
parent 10370 d90965a3b100
child 11307 003462e40c4f
permissions -rw-r--r--
Improve iOS keyboard demo code a bit.
     1 /*
     2  *  keyboard.c
     3  *  written by Holmes Futrell
     4  *  use however you want
     5  */
     6 
     7 #include "SDL.h"
     8 #include "common.h"
     9 
    10 #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 */
    12 
    13 #define MAX_CHARS 1024
    14 
    15 static SDL_Texture *texture; /* texture where we'll hold our font */
    16 
    17 static SDL_Renderer *renderer;
    18 static int numChars = 0;        /* number of characters we've typed so far */
    19 static SDL_Color bg_color = { 50, 50, 100, 255 };       /* color of background */
    20 
    21 static int glyphs[MAX_CHARS];
    22 
    23 /* this structure maps a scancode to an index in our bitmap font.
    24    it also contains data about under which modifiers the mapping is valid
    25    (for example, we don't want shift + 1 to produce the character '1',
    26    but rather the character '!')
    27 */
    28 typedef struct
    29 {
    30     SDL_Scancode scancode;      /* scancode of the key we want to map */
    31     int allow_no_mod;           /* is the map valid if the key has no modifiers? */
    32     SDL_Keymod mod;             /* what modifiers are allowed for the mapping */
    33     int index;                  /* what index in the font does the scancode map to */
    34 } fontMapping;
    35 
    36 #define TABLE_SIZE 51           /* size of our table which maps keys and modifiers to font indices */
    37 
    38 /* Below is the table that defines the mapping between scancodes and modifiers to indices in the
    39    bitmap font.  As an example, then line '{ SDL_SCANCODE_A, 1, KMOD_SHIFT, 33 }' means, map
    40    the key A (which has scancode SDL_SCANCODE_A) to index 33 in the font (which is a picture of an A),
    41    The '1' means that the mapping is valid even if there are no modifiers, and KMOD_SHIFT means the
    42    mapping is also valid if the user is holding shift.
    43 */
    44 fontMapping map[TABLE_SIZE] = {
    45 
    46     {SDL_SCANCODE_A, 1, KMOD_SHIFT, 33},        /* A */
    47     {SDL_SCANCODE_B, 1, KMOD_SHIFT, 34},        /* B */
    48     {SDL_SCANCODE_C, 1, KMOD_SHIFT, 35},        /* C */
    49     {SDL_SCANCODE_D, 1, KMOD_SHIFT, 36},        /* D */
    50     {SDL_SCANCODE_E, 1, KMOD_SHIFT, 37},        /* E */
    51     {SDL_SCANCODE_F, 1, KMOD_SHIFT, 38},        /* F */
    52     {SDL_SCANCODE_G, 1, KMOD_SHIFT, 39},        /* G */
    53     {SDL_SCANCODE_H, 1, KMOD_SHIFT, 40},        /* H */
    54     {SDL_SCANCODE_I, 1, KMOD_SHIFT, 41},        /* I */
    55     {SDL_SCANCODE_J, 1, KMOD_SHIFT, 42},        /* J */
    56     {SDL_SCANCODE_K, 1, KMOD_SHIFT, 43},        /* K */
    57     {SDL_SCANCODE_L, 1, KMOD_SHIFT, 44},        /* L */
    58     {SDL_SCANCODE_M, 1, KMOD_SHIFT, 45},        /* M */
    59     {SDL_SCANCODE_N, 1, KMOD_SHIFT, 46},        /* N */
    60     {SDL_SCANCODE_O, 1, KMOD_SHIFT, 47},        /* O */
    61     {SDL_SCANCODE_P, 1, KMOD_SHIFT, 48},        /* P */
    62     {SDL_SCANCODE_Q, 1, KMOD_SHIFT, 49},        /* Q */
    63     {SDL_SCANCODE_R, 1, KMOD_SHIFT, 50},        /* R */
    64     {SDL_SCANCODE_S, 1, KMOD_SHIFT, 51},        /* S */
    65     {SDL_SCANCODE_T, 1, KMOD_SHIFT, 52},        /* T */
    66     {SDL_SCANCODE_U, 1, KMOD_SHIFT, 53},        /* U */
    67     {SDL_SCANCODE_V, 1, KMOD_SHIFT, 54},        /* V */
    68     {SDL_SCANCODE_W, 1, KMOD_SHIFT, 55},        /* W */
    69     {SDL_SCANCODE_X, 1, KMOD_SHIFT, 56},        /* X */
    70     {SDL_SCANCODE_Y, 1, KMOD_SHIFT, 57},        /* Y */
    71     {SDL_SCANCODE_Z, 1, KMOD_SHIFT, 58},        /* Z */
    72     {SDL_SCANCODE_0, 1, 0, 16}, /* 0 */
    73     {SDL_SCANCODE_1, 1, 0, 17}, /* 1 */
    74     {SDL_SCANCODE_2, 1, 0, 18}, /* 2 */
    75     {SDL_SCANCODE_3, 1, 0, 19}, /* 3 */
    76     {SDL_SCANCODE_4, 1, 0, 20}, /* 4 */
    77     {SDL_SCANCODE_5, 1, 0, 21}, /* 5 */
    78     {SDL_SCANCODE_6, 1, 0, 22}, /* 6 */
    79     {SDL_SCANCODE_7, 1, 0, 23}, /* 7 */
    80     {SDL_SCANCODE_8, 1, 0, 24}, /* 8 */
    81     {SDL_SCANCODE_9, 1, 0, 25}, /* 9 */
    82     {SDL_SCANCODE_SPACE, 1, 0, 0},      /* ' ' */
    83     {SDL_SCANCODE_1, 0, KMOD_SHIFT, 1}, /* ! */
    84     {SDL_SCANCODE_SLASH, 0, KMOD_SHIFT, 31},    /* ? */
    85     {SDL_SCANCODE_SLASH, 1, 0, 15},     /* / */
    86     {SDL_SCANCODE_COMMA, 1, 0, 12},     /* , */
    87     {SDL_SCANCODE_SEMICOLON, 1, 0, 27}, /* ; */
    88     {SDL_SCANCODE_SEMICOLON, 0, KMOD_SHIFT, 26},        /* : */
    89     {SDL_SCANCODE_PERIOD, 1, 0, 14},    /* . */
    90     {SDL_SCANCODE_MINUS, 1, 0, 13},     /* - */
    91     {SDL_SCANCODE_EQUALS, 0, KMOD_SHIFT, 11},   /* = */
    92     {SDL_SCANCODE_APOSTROPHE, 1, 0, 7}, /* ' */
    93     {SDL_SCANCODE_APOSTROPHE, 0, KMOD_SHIFT, 2},        /* " */
    94     {SDL_SCANCODE_5, 0, KMOD_SHIFT, 5}, /* % */
    95 
    96 };
    97 
    98 /*
    99     This function maps an SDL_KeySym to an index in the bitmap font.
   100     It does so by scanning through the font mapping table one entry
   101     at a time.
   102 
   103     If a match is found (scancode and allowed modifiers), the proper
   104     index is returned.
   105 
   106     If there is no entry for the key, -1 is returned
   107 */
   108 int
   109 keyToGlyphIndex(SDL_Keysym key)
   110 {
   111     int i, index = -1;
   112     for (i = 0; i < TABLE_SIZE; i++) {
   113         fontMapping compare = map[i];
   114         if (key.scancode == compare.scancode) {
   115             /* if this entry is valid with no key mod and we have no keymod, or if
   116                the key's modifiers are allowed modifiers for that mapping */
   117             if ((compare.allow_no_mod && key.mod == 0)
   118                 || (key.mod & compare.mod)) {
   119                 index = compare.index;
   120                 break;
   121             }
   122         }
   123     }
   124     return index;
   125 }
   126 
   127 /*
   128     This function returns and x,y position for a given character number.
   129     It is used for positioning each character of text
   130 */
   131 void
   132 getPositionForCharNumber(int n, int *x, int *y)
   133 {
   134     int renderW, renderH;
   135     SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
   136 
   137     int x_padding = 16;         /* padding space on left and right side of screen */
   138     int y_padding = 32;         /* padding space at top of screen */
   139     /* 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;
   141     int line_separation = 5;    /* pixels between each line */
   142     *x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
   143     *y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) + y_padding;
   144 }
   145 
   146 void
   147 drawGlyph(int glyph, int positionIndex)
   148 {
   149     int x, y;
   150     getPositionForCharNumber(positionIndex, &x, &y);
   151     SDL_Rect srcRect = { GLYPH_SIZE_IMAGE * glyph, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
   152     SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
   153     SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
   154 }
   155 
   156 /* this function loads our font into an SDL_Texture and returns the SDL_Texture  */
   157 SDL_Texture*
   158 loadFont(void)
   159 {
   160 
   161     SDL_Surface *surface = SDL_LoadBMP("kromasky_16x16.bmp");
   162 
   163     if (!surface) {
   164         printf("Error loading bitmap: %s\n", SDL_GetError());
   165         return 0;
   166     } else {
   167         /* set the transparent color for the bitmap font (hot pink) */
   168         SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, 238, 0, 252));
   169         /* now we convert the surface to our desired pixel format */
   170         int format = SDL_PIXELFORMAT_ABGR8888;  /* desired texture format */
   171         Uint32 Rmask, Gmask, Bmask, Amask;      /* masks for desired format */
   172         int bpp;                /* bits per pixel for desired format */
   173         SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask,
   174                                    &Amask);
   175         SDL_Surface *converted =
   176             SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask,
   177                                  Bmask, Amask);
   178         SDL_BlitSurface(surface, NULL, converted, NULL);
   179         /* create our texture */
   180         texture = SDL_CreateTextureFromSurface(renderer, converted);
   181         if (texture == 0) {
   182             printf("texture creation failed: %s\n", SDL_GetError());
   183         } else {
   184             /* set blend mode for our texture */
   185             SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
   186         }
   187         SDL_FreeSurface(surface);
   188         SDL_FreeSurface(converted);
   189         return texture;
   190     }
   191 }
   192 
   193 void
   194 draw()
   195 {
   196     SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
   197     SDL_RenderClear(renderer);
   198 
   199     for (int i = 0; i < numChars; i++) {
   200         drawGlyph(glyphs[i], i);
   201     }
   202 
   203     drawGlyph(29, numChars); /* cursor is at index 29 in the bitmap font */
   204 
   205     SDL_RenderPresent(renderer);
   206 }
   207 
   208 int
   209 main(int argc, char *argv[])
   210 {
   211     int index;                  /* index of last key we pushed in the bitmap font */
   212     SDL_Window *window;
   213     SDL_Event event;            /* last event received */
   214     SDL_Scancode scancode;      /* scancode of last key we pushed */
   215     int width;
   216     int height;
   217 
   218     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   219         printf("Error initializing SDL: %s", SDL_GetError());
   220     }
   221     /* create window */
   222     window = SDL_CreateWindow("iPhone keyboard test", 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
   223     /* create renderer */
   224     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
   225 
   226     SDL_GetWindowSize(window, &width, &height);
   227     SDL_RenderSetLogicalSize(renderer, width, height);
   228 
   229     /* load up our font */
   230     loadFont();
   231 
   232     int done = 0;
   233 
   234     while (!done) {
   235         while (SDL_PollEvent(&event)) {
   236             switch (event.type) {
   237             case SDL_QUIT:
   238                 done = 1;
   239                 break;
   240             case SDL_TEXTINPUT:
   241                 break;
   242             case SDL_KEYDOWN:
   243                 if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
   244                     if (numChars > 0) {
   245                         numChars--;
   246                     }
   247                 } else if (numChars + 1 < MAX_CHARS) {
   248                     int index = keyToGlyphIndex(event.key.keysym);
   249                     if (index >= 0) {
   250                         glyphs[numChars++] = index;
   251                     }
   252                 }
   253                 break;
   254             case SDL_MOUSEBUTTONUP:
   255                 /* mouse up toggles onscreen keyboard visibility */
   256                 if (SDL_IsTextInputActive()) {
   257                     SDL_StopTextInput();
   258                 } else {
   259                     SDL_StartTextInput();
   260                 }
   261                 break;
   262             }
   263         }
   264 
   265         draw();
   266         SDL_Delay(15);
   267     }
   268 
   269     SDL_DestroyTexture(texture);
   270     SDL_DestroyRenderer(renderer);
   271     SDL_DestroyWindow(window);
   272     return 0;
   273 }