Xcode-iOS/Demos/src/keyboard.c
author Gabriel Jacobo
Wed, 21 Aug 2013 09:47:10 -0300
changeset 7678 286c42d7c5ed
parent 7290 36c04a8c6545
child 10126 21cbbf2beb93
permissions -rw-r--r--
OCD fixes: Adds a space after /* (glory to regular expressions!)
     1 /*
     2  *  keyboard.c
     3  *  written by Holmes Futrell
     4  *  use however you want
     5  */
     6 
     7 #import "SDL.h"
     8 #import "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 static SDL_Texture *texture; /* texture where we'll hold our font */
    14 
    15 /* function declarations */
    16 void cleanup(void);
    17 void drawBlank(int x, int y);
    18 
    19 static SDL_Renderer *renderer;
    20 static int numChars = 0;        /* number of characters we've typed so far */
    21 static SDL_bool lastCharWasColon = 0;   /* we use this to detect sequences such as :) */
    22 static SDL_Color bg_color = { 50, 50, 100, 255 };       /* color of background */
    23 
    24 /* this structure maps a scancode to an index in our bitmap font.
    25    it also contains data about under which modifiers the mapping is valid
    26    (for example, we don't want shift + 1 to produce the character '1',
    27    but rather the character '!')
    28 */
    29 typedef struct
    30 {
    31     SDL_Scancode scancode;      /* scancode of the key we want to map */
    32     int allow_no_mod;           /* is the map valid if the key has no modifiers? */
    33     SDL_Keymod mod;             /* what modifiers are allowed for the mapping */
    34     int index;                  /* what index in the font does the scancode map to */
    35 } fontMapping;
    36 
    37 #define TABLE_SIZE 51           /* size of our table which maps keys and modifiers to font indices */
    38 
    39 /* Below is the table that defines the mapping between scancodes and modifiers to indices in the
    40    bitmap font.  As an example, then line '{ SDL_SCANCODE_A, 1, KMOD_SHIFT, 33 }' means, map
    41    the key A (which has scancode SDL_SCANCODE_A) to index 33 in the font (which is a picture of an A),
    42    The '1' means that the mapping is valid even if there are no modifiers, and KMOD_SHIFT means the
    43    mapping is also valid if the user is holding shift.
    44 */
    45 fontMapping map[TABLE_SIZE] = {
    46 
    47     {SDL_SCANCODE_A, 1, KMOD_SHIFT, 33},        /* A */
    48     {SDL_SCANCODE_B, 1, KMOD_SHIFT, 34},        /* B */
    49     {SDL_SCANCODE_C, 1, KMOD_SHIFT, 35},        /* C */
    50     {SDL_SCANCODE_D, 1, KMOD_SHIFT, 36},        /* D */
    51     {SDL_SCANCODE_E, 1, KMOD_SHIFT, 37},        /* E */
    52     {SDL_SCANCODE_F, 1, KMOD_SHIFT, 38},        /* F */
    53     {SDL_SCANCODE_G, 1, KMOD_SHIFT, 39},        /* G */
    54     {SDL_SCANCODE_H, 1, KMOD_SHIFT, 40},        /* H */
    55     {SDL_SCANCODE_I, 1, KMOD_SHIFT, 41},        /* I */
    56     {SDL_SCANCODE_J, 1, KMOD_SHIFT, 42},        /* J */
    57     {SDL_SCANCODE_K, 1, KMOD_SHIFT, 43},        /* K */
    58     {SDL_SCANCODE_L, 1, KMOD_SHIFT, 44},        /* L */
    59     {SDL_SCANCODE_M, 1, KMOD_SHIFT, 45},        /* M */
    60     {SDL_SCANCODE_N, 1, KMOD_SHIFT, 46},        /* N */
    61     {SDL_SCANCODE_O, 1, KMOD_SHIFT, 47},        /* O */
    62     {SDL_SCANCODE_P, 1, KMOD_SHIFT, 48},        /* P */
    63     {SDL_SCANCODE_Q, 1, KMOD_SHIFT, 49},        /* Q */
    64     {SDL_SCANCODE_R, 1, KMOD_SHIFT, 50},        /* R */
    65     {SDL_SCANCODE_S, 1, KMOD_SHIFT, 51},        /* S */
    66     {SDL_SCANCODE_T, 1, KMOD_SHIFT, 52},        /* T */
    67     {SDL_SCANCODE_U, 1, KMOD_SHIFT, 53},        /* U */
    68     {SDL_SCANCODE_V, 1, KMOD_SHIFT, 54},        /* V */
    69     {SDL_SCANCODE_W, 1, KMOD_SHIFT, 55},        /* W */
    70     {SDL_SCANCODE_X, 1, KMOD_SHIFT, 56},        /* X */
    71     {SDL_SCANCODE_Y, 1, KMOD_SHIFT, 57},        /* Y */
    72     {SDL_SCANCODE_Z, 1, KMOD_SHIFT, 58},        /* Z */
    73     {SDL_SCANCODE_0, 1, 0, 16}, /* 0 */
    74     {SDL_SCANCODE_1, 1, 0, 17}, /* 1 */
    75     {SDL_SCANCODE_2, 1, 0, 18}, /* 2 */
    76     {SDL_SCANCODE_3, 1, 0, 19}, /* 3 */
    77     {SDL_SCANCODE_4, 1, 0, 20}, /* 4 */
    78     {SDL_SCANCODE_5, 1, 0, 21}, /* 5 */
    79     {SDL_SCANCODE_6, 1, 0, 22}, /* 6 */
    80     {SDL_SCANCODE_7, 1, 0, 23}, /* 7 */
    81     {SDL_SCANCODE_8, 1, 0, 24}, /* 8 */
    82     {SDL_SCANCODE_9, 1, 0, 25}, /* 9 */
    83     {SDL_SCANCODE_SPACE, 1, 0, 0},      /* ' ' */
    84     {SDL_SCANCODE_1, 0, KMOD_SHIFT, 1}, /* ! */
    85     {SDL_SCANCODE_SLASH, 0, KMOD_SHIFT, 31},    /* ? */
    86     {SDL_SCANCODE_SLASH, 1, 0, 15},     /* / */
    87     {SDL_SCANCODE_COMMA, 1, 0, 12},     /* , */
    88     {SDL_SCANCODE_SEMICOLON, 1, 0, 27}, /* ; */
    89     {SDL_SCANCODE_SEMICOLON, 0, KMOD_SHIFT, 26},        /* : */
    90     {SDL_SCANCODE_PERIOD, 1, 0, 14},    /* . */
    91     {SDL_SCANCODE_MINUS, 1, 0, 13},     /* - */
    92     {SDL_SCANCODE_EQUALS, 0, KMOD_SHIFT, 11},   /* = */
    93     {SDL_SCANCODE_APOSTROPHE, 1, 0, 7}, /* ' */
    94     {SDL_SCANCODE_APOSTROPHE, 0, KMOD_SHIFT, 2},        /* " */
    95     {SDL_SCANCODE_5, 0, KMOD_SHIFT, 5}, /* % */
    96 
    97 };
    98 
    99 /*
   100     This function maps an SDL_KeySym to an index in the bitmap font.
   101     It does so by scanning through the font mapping table one entry
   102     at a time.
   103 
   104     If a match is found (scancode and allowed modifiers), the proper
   105     index is returned.
   106 
   107     If there is no entry for the key, -1 is returned
   108 */
   109 int
   110 keyToIndex(SDL_Keysym key)
   111 {
   112     int i, index = -1;
   113     for (i = 0; i < TABLE_SIZE; i++) {
   114         fontMapping compare = map[i];
   115         if (key.scancode == compare.scancode) {
   116             /* if this entry is valid with no key mod and we have no keymod, or if
   117                the key's modifiers are allowed modifiers for that mapping */
   118             if ((compare.allow_no_mod && key.mod == 0)
   119                 || (key.mod & compare.mod)) {
   120                 index = compare.index;
   121                 break;
   122             }
   123         }
   124     }
   125     return index;
   126 }
   127 
   128 /*
   129     This function returns and x,y position for a given character number.
   130     It is used for positioning each character of text
   131 */
   132 void
   133 getPositionForCharNumber(int n, int *x, int *y)
   134 {
   135     int x_padding = 16;         /* padding space on left and right side of screen */
   136     int y_padding = 32;         /* padding space at top of screen */
   137     /* figure out the number of characters that can fit horizontally across the screen */
   138     int max_x_chars = (SCREEN_WIDTH - 2 * x_padding) / GLYPH_SIZE_SCREEN;
   139     int line_separation = 5;    /* pixels between each line */
   140     *x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
   141     *y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) +
   142         y_padding;
   143 }
   144 
   145 void
   146 drawIndex(int index)
   147 {
   148     int x, y;
   149     getPositionForCharNumber(numChars, &x, &y);
   150     SDL_Rect srcRect =
   151         { GLYPH_SIZE_IMAGE * index, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
   152     SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
   153     drawBlank(x, y);
   154     SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
   155 }
   156 
   157 /*  draws the cursor icon at the current end position of the text */
   158 void
   159 drawCursor(void)
   160 {
   161     drawIndex(29);              /* cursor is at index 29 in the bitmap font */
   162 }
   163 
   164 /* paints over a glyph sized region with the background color
   165    in effect it erases the area
   166 */
   167 void
   168 drawBlank(int x, int y)
   169 {
   170     SDL_Rect rect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
   171     SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
   172     SDL_RenderFillRect(renderer, &rect);
   173 }
   174 
   175 /* moves backwards one character, erasing the last one put down */
   176 void
   177 backspace(void)
   178 {
   179     int x, y;
   180     if (numChars > 0) {
   181         getPositionForCharNumber(numChars, &x, &y);
   182         drawBlank(x, y);
   183         numChars--;
   184         getPositionForCharNumber(numChars, &x, &y);
   185         drawBlank(x, y);
   186         drawCursor();
   187     }
   188 }
   189 
   190 /* this function loads our font into an SDL_Texture and returns the SDL_Texture  */
   191 SDL_Texture*
   192 loadFont(void)
   193 {
   194 
   195     SDL_Surface *surface = SDL_LoadBMP("kromasky_16x16.bmp");
   196 
   197     if (!surface) {
   198         printf("Error loading bitmap: %s\n", SDL_GetError());
   199         return 0;
   200     } else {
   201         /* set the transparent color for the bitmap font (hot pink) */
   202         SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, 238, 0, 252));
   203         /* now we convert the surface to our desired pixel format */
   204         int format = SDL_PIXELFORMAT_ABGR8888;  /* desired texture format */
   205         Uint32 Rmask, Gmask, Bmask, Amask;      /* masks for desired format */
   206         int bpp;                /* bits per pixel for desired format */
   207         SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask,
   208                                    &Amask);
   209         SDL_Surface *converted =
   210             SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask,
   211                                  Bmask, Amask);
   212         SDL_BlitSurface(surface, NULL, converted, NULL);
   213         /* create our texture */
   214         texture =
   215             SDL_CreateTextureFromSurface(renderer, converted);
   216         if (texture == 0) {
   217             printf("texture creation failed: %s\n", SDL_GetError());
   218         } else {
   219             /* set blend mode for our texture */
   220             SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
   221         }
   222         SDL_FreeSurface(surface);
   223         SDL_FreeSurface(converted);
   224         return texture;
   225     }
   226 }
   227 
   228 int
   229 main(int argc, char *argv[])
   230 {
   231 
   232     int index;                  /* index of last key we pushed in the bitmap font */
   233     SDL_Window *window;
   234     SDL_Event event;            /* last event received */
   235     SDL_Keymod mod;             /* key modifiers of last key we pushed */
   236     SDL_Scancode scancode;      /* scancode of last key we pushed */
   237 
   238     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   239         printf("Error initializing SDL: %s", SDL_GetError());
   240     }
   241     /* create window */
   242     window = SDL_CreateWindow("iPhone keyboard test", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
   243     /* create renderer */
   244     renderer = SDL_CreateRenderer(window, -1, 0);
   245 
   246     /* load up our font */
   247     loadFont();
   248 
   249     /* draw the background, we'll just paint over it */
   250     SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
   251     SDL_RenderFillRect(renderer, NULL);
   252     SDL_RenderPresent(renderer);
   253 
   254     int done = 0;
   255     /* loop till we get SDL_Quit */
   256     while (SDL_WaitEvent(&event)) {
   257         switch (event.type) {
   258         case SDL_QUIT:
   259             done = 1;
   260             break;
   261         case SDL_KEYDOWN:
   262             index = keyToIndex(event.key.keysym);
   263             scancode = event.key.keysym.scancode;
   264             mod = event.key.keysym.mod;
   265             if (scancode == SDL_SCANCODE_DELETE) {
   266                 /* if user hit delete, delete the last character */
   267                 backspace();
   268                 lastCharWasColon = 0;
   269             } else if (lastCharWasColon && scancode == SDL_SCANCODE_0
   270                        && (mod & KMOD_SHIFT)) {
   271                 /* if our last key was a colon and this one is a close paren, the make a hoppy face */
   272                 backspace();
   273                 drawIndex(32);  /* index for happy face */
   274                 numChars++;
   275                 drawCursor();
   276                 lastCharWasColon = 0;
   277             } else if (index != -1) {
   278                 /* if we aren't doing a happy face, then just draw the normal character */
   279                 drawIndex(index);
   280                 numChars++;
   281                 drawCursor();
   282                 lastCharWasColon =
   283                     (event.key.keysym.scancode == SDL_SCANCODE_SEMICOLON
   284                      && (event.key.keysym.mod & KMOD_SHIFT));
   285             }
   286             /* check if the key was a colon */
   287             /* draw our updates to the screen */
   288             SDL_RenderPresent(renderer);
   289             break;
   290         case SDL_MOUSEBUTTONUP:
   291             /*      mouse up toggles onscreen keyboard visibility */
   292             if (SDL_IsTextInputActive()) {
   293                 SDL_StopTextInput();
   294             } else {
   295                 SDL_StartTextInput();
   296             }
   297             break;
   298         }
   299     }
   300     cleanup();
   301     return 0;
   302 }
   303 
   304 /* clean up after ourselves like a good kiddy */
   305 void
   306 cleanup(void)
   307 {
   308     SDL_DestroyTexture(texture);
   309     SDL_Quit();
   310 }