sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

keyboard.c (11029B)


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