Xcode-iOS/Demos/src/keyboard.c
author Alex Szpakowski
Tue, 15 Aug 2017 23:00:54 -0300
changeset 11307 003462e40c4f
parent 11306 45fbdef6a227
child 11498 d4f87399777f
permissions -rw-r--r--
More cleanup of the iOS keyboard demo.
     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     SDL_Surface *surface = SDL_LoadBMP("kromasky_16x16.bmp");
   161 
   162     if (!surface) {
   163         printf("Error loading bitmap: %s\n", SDL_GetError());
   164         return 0;
   165     } else {
   166         /* set the transparent color for the bitmap font (hot pink) */
   167         SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, 238, 0, 252));
   168         /* now we convert the surface to our desired pixel format */
   169         int format = SDL_PIXELFORMAT_ABGR8888;  /* desired texture format */
   170         Uint32 Rmask, Gmask, Bmask, Amask;      /* masks for desired format */
   171         int bpp;                /* bits per pixel for desired format */
   172         SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask,
   173                                    &Amask);
   174         SDL_Surface *converted =
   175             SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask,
   176                                  Bmask, Amask);
   177         SDL_BlitSurface(surface, NULL, converted, NULL);
   178         /* create our texture */
   179         texture = SDL_CreateTextureFromSurface(renderer, converted);
   180         if (texture == 0) {
   181             printf("texture creation failed: %s\n", SDL_GetError());
   182         } else {
   183             /* set blend mode for our texture */
   184             SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
   185         }
   186         SDL_FreeSurface(surface);
   187         SDL_FreeSurface(converted);
   188         return texture;
   189     }
   190 }
   191 
   192 void
   193 draw()
   194 {
   195     SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
   196     SDL_RenderClear(renderer);
   197 
   198     for (int i = 0; i < numChars; i++) {
   199         drawGlyph(glyphs[i], i);
   200     }
   201 
   202     drawGlyph(29, numChars); /* cursor is at index 29 in the bitmap font */
   203 
   204     SDL_RenderPresent(renderer);
   205 }
   206 
   207 int
   208 main(int argc, char *argv[])
   209 {
   210     SDL_Window *window;
   211     SDL_Event event;            /* last event received */
   212     SDL_Scancode scancode;      /* scancode of last key we pushed */
   213     int width;
   214     int height;
   215     int done;
   216 
   217     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   218         printf("Error initializing SDL: %s", SDL_GetError());
   219     }
   220     /* create window */
   221     window = SDL_CreateWindow("iOS keyboard test", 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
   222     /* create renderer */
   223     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
   224 
   225     SDL_GetWindowSize(window, &width, &height);
   226     SDL_RenderSetLogicalSize(renderer, width, height);
   227 
   228     /* load up our font */
   229     loadFont();
   230 
   231     done = 0;
   232     while (!done) {
   233         while (SDL_PollEvent(&event)) {
   234             switch (event.type) {
   235             case SDL_QUIT:
   236                 done = 1;
   237                 break;
   238             case SDL_KEYDOWN:
   239                 if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
   240                     if (numChars > 0) {
   241                         numChars--;
   242                     }
   243                 } else if (numChars + 1 < MAX_CHARS) {
   244                     int index = keyToGlyphIndex(event.key.keysym);
   245                     if (index >= 0) {
   246                         glyphs[numChars++] = index;
   247                     }
   248                 }
   249                 break;
   250             case SDL_MOUSEBUTTONUP:
   251                 /* mouse up toggles onscreen keyboard visibility */
   252                 if (SDL_IsTextInputActive()) {
   253                     SDL_StopTextInput();
   254                 } else {
   255                     SDL_StartTextInput();
   256                 }
   257                 break;
   258             }
   259         }
   260 
   261         draw();
   262         SDL_Delay(15);
   263     }
   264 
   265     SDL_DestroyTexture(texture);
   266     SDL_DestroyRenderer(renderer);
   267     SDL_DestroyWindow(window);
   268     SDL_Quit();
   269     return 0;
   270 }