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