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