Xcode-iOS/Demos/src/keyboard.c
changeset 11306 45fbdef6a227
parent 10370 d90965a3b100
child 11307 003462e40c4f
     1.1 --- a/Xcode-iOS/Demos/src/keyboard.c	Tue Aug 15 18:29:47 2017 -0300
     1.2 +++ b/Xcode-iOS/Demos/src/keyboard.c	Tue Aug 15 22:53:57 2017 -0300
     1.3 @@ -10,17 +10,16 @@
     1.4  #define GLYPH_SIZE_IMAGE 16     /* size of glyphs (characters) in the bitmap font file */
     1.5  #define GLYPH_SIZE_SCREEN 32    /* size of glyphs (characters) as shown on the screen */
     1.6  
     1.7 -static SDL_Texture *texture; /* texture where we'll hold our font */
     1.8 +#define MAX_CHARS 1024
     1.9  
    1.10 -/* function declarations */
    1.11 -void cleanup(void);
    1.12 -void drawBlank(int x, int y);
    1.13 +static SDL_Texture *texture; /* texture where we'll hold our font */
    1.14  
    1.15  static SDL_Renderer *renderer;
    1.16  static int numChars = 0;        /* number of characters we've typed so far */
    1.17 -static SDL_bool lastCharWasColon = 0;   /* we use this to detect sequences such as :) */
    1.18  static SDL_Color bg_color = { 50, 50, 100, 255 };       /* color of background */
    1.19  
    1.20 +static int glyphs[MAX_CHARS];
    1.21 +
    1.22  /* this structure maps a scancode to an index in our bitmap font.
    1.23     it also contains data about under which modifiers the mapping is valid
    1.24     (for example, we don't want shift + 1 to produce the character '1',
    1.25 @@ -107,7 +106,7 @@
    1.26      If there is no entry for the key, -1 is returned
    1.27  */
    1.28  int
    1.29 -keyToIndex(SDL_Keysym key)
    1.30 +keyToGlyphIndex(SDL_Keysym key)
    1.31  {
    1.32      int i, index = -1;
    1.33      for (i = 0; i < TABLE_SIZE; i++) {
    1.34 @@ -141,55 +140,19 @@
    1.35      int max_x_chars = (renderW - 2 * x_padding) / GLYPH_SIZE_SCREEN;
    1.36      int line_separation = 5;    /* pixels between each line */
    1.37      *x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
    1.38 -    *y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) +
    1.39 -        y_padding;
    1.40 +    *y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) + y_padding;
    1.41  }
    1.42  
    1.43  void
    1.44 -drawIndex(int index)
    1.45 +drawGlyph(int glyph, int positionIndex)
    1.46  {
    1.47      int x, y;
    1.48 -    getPositionForCharNumber(numChars, &x, &y);
    1.49 -    SDL_Rect srcRect =
    1.50 -        { GLYPH_SIZE_IMAGE * index, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
    1.51 +    getPositionForCharNumber(positionIndex, &x, &y);
    1.52 +    SDL_Rect srcRect = { GLYPH_SIZE_IMAGE * glyph, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
    1.53      SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
    1.54 -    drawBlank(x, y);
    1.55      SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
    1.56  }
    1.57  
    1.58 -/*  draws the cursor icon at the current end position of the text */
    1.59 -void
    1.60 -drawCursor(void)
    1.61 -{
    1.62 -    drawIndex(29);              /* cursor is at index 29 in the bitmap font */
    1.63 -}
    1.64 -
    1.65 -/* paints over a glyph sized region with the background color
    1.66 -   in effect it erases the area
    1.67 -*/
    1.68 -void
    1.69 -drawBlank(int x, int y)
    1.70 -{
    1.71 -    SDL_Rect rect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
    1.72 -    SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
    1.73 -    SDL_RenderFillRect(renderer, &rect);
    1.74 -}
    1.75 -
    1.76 -/* moves backwards one character, erasing the last one put down */
    1.77 -void
    1.78 -backspace(void)
    1.79 -{
    1.80 -    int x, y;
    1.81 -    if (numChars > 0) {
    1.82 -        getPositionForCharNumber(numChars, &x, &y);
    1.83 -        drawBlank(x, y);
    1.84 -        numChars--;
    1.85 -        getPositionForCharNumber(numChars, &x, &y);
    1.86 -        drawBlank(x, y);
    1.87 -        drawCursor();
    1.88 -    }
    1.89 -}
    1.90 -
    1.91  /* this function loads our font into an SDL_Texture and returns the SDL_Texture  */
    1.92  SDL_Texture*
    1.93  loadFont(void)
    1.94 @@ -214,8 +177,7 @@
    1.95                                   Bmask, Amask);
    1.96          SDL_BlitSurface(surface, NULL, converted, NULL);
    1.97          /* create our texture */
    1.98 -        texture =
    1.99 -            SDL_CreateTextureFromSurface(renderer, converted);
   1.100 +        texture = SDL_CreateTextureFromSurface(renderer, converted);
   1.101          if (texture == 0) {
   1.102              printf("texture creation failed: %s\n", SDL_GetError());
   1.103          } else {
   1.104 @@ -228,13 +190,27 @@
   1.105      }
   1.106  }
   1.107  
   1.108 +void
   1.109 +draw()
   1.110 +{
   1.111 +    SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
   1.112 +    SDL_RenderClear(renderer);
   1.113 +
   1.114 +    for (int i = 0; i < numChars; i++) {
   1.115 +        drawGlyph(glyphs[i], i);
   1.116 +    }
   1.117 +
   1.118 +    drawGlyph(29, numChars); /* cursor is at index 29 in the bitmap font */
   1.119 +
   1.120 +    SDL_RenderPresent(renderer);
   1.121 +}
   1.122 +
   1.123  int
   1.124  main(int argc, char *argv[])
   1.125  {
   1.126      int index;                  /* index of last key we pushed in the bitmap font */
   1.127      SDL_Window *window;
   1.128      SDL_Event event;            /* last event received */
   1.129 -    SDL_Keymod mod;             /* key modifiers of last key we pushed */
   1.130      SDL_Scancode scancode;      /* scancode of last key we pushed */
   1.131      int width;
   1.132      int height;
   1.133 @@ -245,7 +221,7 @@
   1.134      /* create window */
   1.135      window = SDL_CreateWindow("iPhone keyboard test", 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
   1.136      /* create renderer */
   1.137 -    renderer = SDL_CreateRenderer(window, -1, 0);
   1.138 +    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
   1.139  
   1.140      SDL_GetWindowSize(window, &width, &height);
   1.141      SDL_RenderSetLogicalSize(renderer, width, height);
   1.142 @@ -253,65 +229,45 @@
   1.143      /* load up our font */
   1.144      loadFont();
   1.145  
   1.146 -    /* draw the background, we'll just paint over it */
   1.147 -    SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
   1.148 -    SDL_RenderFillRect(renderer, NULL);
   1.149 -    SDL_RenderPresent(renderer);
   1.150 +    int done = 0;
   1.151  
   1.152 -    int done = 0;
   1.153 -    /* loop till we get SDL_Quit */
   1.154 -    while (!done && SDL_WaitEvent(&event)) {
   1.155 -        switch (event.type) {
   1.156 -        case SDL_QUIT:
   1.157 -            done = 1;
   1.158 -            break;
   1.159 -        case SDL_KEYDOWN:
   1.160 -            index = keyToIndex(event.key.keysym);
   1.161 -            scancode = event.key.keysym.scancode;
   1.162 -            mod = event.key.keysym.mod;
   1.163 -            if (scancode == SDL_SCANCODE_DELETE) {
   1.164 -                /* if user hit delete, delete the last character */
   1.165 -                backspace();
   1.166 -                lastCharWasColon = 0;
   1.167 -            } else if (lastCharWasColon && scancode == SDL_SCANCODE_0
   1.168 -                       && (mod & KMOD_SHIFT)) {
   1.169 -                /* if our last key was a colon and this one is a close paren, the make a hoppy face */
   1.170 -                backspace();
   1.171 -                drawIndex(32);  /* index for happy face */
   1.172 -                numChars++;
   1.173 -                drawCursor();
   1.174 -                lastCharWasColon = 0;
   1.175 -            } else if (index != -1) {
   1.176 -                /* if we aren't doing a happy face, then just draw the normal character */
   1.177 -                drawIndex(index);
   1.178 -                numChars++;
   1.179 -                drawCursor();
   1.180 -                lastCharWasColon =
   1.181 -                    (event.key.keysym.scancode == SDL_SCANCODE_SEMICOLON
   1.182 -                     && (event.key.keysym.mod & KMOD_SHIFT));
   1.183 +    while (!done) {
   1.184 +        while (SDL_PollEvent(&event)) {
   1.185 +            switch (event.type) {
   1.186 +            case SDL_QUIT:
   1.187 +                done = 1;
   1.188 +                break;
   1.189 +            case SDL_TEXTINPUT:
   1.190 +                break;
   1.191 +            case SDL_KEYDOWN:
   1.192 +                if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
   1.193 +                    if (numChars > 0) {
   1.194 +                        numChars--;
   1.195 +                    }
   1.196 +                } else if (numChars + 1 < MAX_CHARS) {
   1.197 +                    int index = keyToGlyphIndex(event.key.keysym);
   1.198 +                    if (index >= 0) {
   1.199 +                        glyphs[numChars++] = index;
   1.200 +                    }
   1.201 +                }
   1.202 +                break;
   1.203 +            case SDL_MOUSEBUTTONUP:
   1.204 +                /* mouse up toggles onscreen keyboard visibility */
   1.205 +                if (SDL_IsTextInputActive()) {
   1.206 +                    SDL_StopTextInput();
   1.207 +                } else {
   1.208 +                    SDL_StartTextInput();
   1.209 +                }
   1.210 +                break;
   1.211              }
   1.212 -            /* check if the key was a colon */
   1.213 -            /* draw our updates to the screen */
   1.214 -            SDL_RenderPresent(renderer);
   1.215 -            break;
   1.216 -        case SDL_MOUSEBUTTONUP:
   1.217 -            /*      mouse up toggles onscreen keyboard visibility */
   1.218 -            if (SDL_IsTextInputActive()) {
   1.219 -                SDL_StopTextInput();
   1.220 -            } else {
   1.221 -                SDL_StartTextInput();
   1.222 -            }
   1.223 -            break;
   1.224          }
   1.225 +
   1.226 +        draw();
   1.227 +        SDL_Delay(15);
   1.228      }
   1.229 -    cleanup();
   1.230 +
   1.231 +    SDL_DestroyTexture(texture);
   1.232 +    SDL_DestroyRenderer(renderer);
   1.233 +    SDL_DestroyWindow(window);
   1.234      return 0;
   1.235  }
   1.236 -
   1.237 -/* clean up after ourselves like a good kiddy */
   1.238 -void
   1.239 -cleanup(void)
   1.240 -{
   1.241 -    SDL_DestroyTexture(texture);
   1.242 -    SDL_Quit();
   1.243 -}