Improve iOS keyboard demo code a bit.

This commit is contained in:
Alex Szpakowski 2017-08-15 22:53:57 -03:00
parent 2e4248ed44
commit a0a09f646c
2 changed files with 66 additions and 112 deletions

View File

@ -10,17 +10,16 @@
#define GLYPH_SIZE_IMAGE 16 /* size of glyphs (characters) in the bitmap font file */ #define GLYPH_SIZE_IMAGE 16 /* size of glyphs (characters) in the bitmap font file */
#define GLYPH_SIZE_SCREEN 32 /* size of glyphs (characters) as shown on the screen */ #define GLYPH_SIZE_SCREEN 32 /* size of glyphs (characters) as shown on the screen */
static SDL_Texture *texture; /* texture where we'll hold our font */ #define MAX_CHARS 1024
/* function declarations */ static SDL_Texture *texture; /* texture where we'll hold our font */
void cleanup(void);
void drawBlank(int x, int y);
static SDL_Renderer *renderer; static SDL_Renderer *renderer;
static int numChars = 0; /* number of characters we've typed so far */ static int numChars = 0; /* number of characters we've typed so far */
static SDL_bool lastCharWasColon = 0; /* we use this to detect sequences such as :) */
static SDL_Color bg_color = { 50, 50, 100, 255 }; /* color of background */ static SDL_Color bg_color = { 50, 50, 100, 255 }; /* color of background */
static int glyphs[MAX_CHARS];
/* this structure maps a scancode to an index in our bitmap font. /* this structure maps a scancode to an index in our bitmap font.
it also contains data about under which modifiers the mapping is valid it also contains data about under which modifiers the mapping is valid
(for example, we don't want shift + 1 to produce the character '1', (for example, we don't want shift + 1 to produce the character '1',
@ -107,7 +106,7 @@ fontMapping map[TABLE_SIZE] = {
If there is no entry for the key, -1 is returned If there is no entry for the key, -1 is returned
*/ */
int int
keyToIndex(SDL_Keysym key) keyToGlyphIndex(SDL_Keysym key)
{ {
int i, index = -1; int i, index = -1;
for (i = 0; i < TABLE_SIZE; i++) { for (i = 0; i < TABLE_SIZE; i++) {
@ -141,55 +140,19 @@ getPositionForCharNumber(int n, int *x, int *y)
int max_x_chars = (renderW - 2 * x_padding) / GLYPH_SIZE_SCREEN; int max_x_chars = (renderW - 2 * x_padding) / GLYPH_SIZE_SCREEN;
int line_separation = 5; /* pixels between each line */ int line_separation = 5; /* pixels between each line */
*x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding; *x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
*y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) + *y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) + y_padding;
y_padding;
} }
void void
drawIndex(int index) drawGlyph(int glyph, int positionIndex)
{ {
int x, y; int x, y;
getPositionForCharNumber(numChars, &x, &y); getPositionForCharNumber(positionIndex, &x, &y);
SDL_Rect srcRect = SDL_Rect srcRect = { GLYPH_SIZE_IMAGE * glyph, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
{ GLYPH_SIZE_IMAGE * index, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN }; SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
drawBlank(x, y);
SDL_RenderCopy(renderer, texture, &srcRect, &dstRect); SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
} }
/* draws the cursor icon at the current end position of the text */
void
drawCursor(void)
{
drawIndex(29); /* cursor is at index 29 in the bitmap font */
}
/* paints over a glyph sized region with the background color
in effect it erases the area
*/
void
drawBlank(int x, int y)
{
SDL_Rect rect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
SDL_RenderFillRect(renderer, &rect);
}
/* moves backwards one character, erasing the last one put down */
void
backspace(void)
{
int x, y;
if (numChars > 0) {
getPositionForCharNumber(numChars, &x, &y);
drawBlank(x, y);
numChars--;
getPositionForCharNumber(numChars, &x, &y);
drawBlank(x, y);
drawCursor();
}
}
/* this function loads our font into an SDL_Texture and returns the SDL_Texture */ /* this function loads our font into an SDL_Texture and returns the SDL_Texture */
SDL_Texture* SDL_Texture*
loadFont(void) loadFont(void)
@ -214,8 +177,7 @@ loadFont(void)
Bmask, Amask); Bmask, Amask);
SDL_BlitSurface(surface, NULL, converted, NULL); SDL_BlitSurface(surface, NULL, converted, NULL);
/* create our texture */ /* create our texture */
texture = texture = SDL_CreateTextureFromSurface(renderer, converted);
SDL_CreateTextureFromSurface(renderer, converted);
if (texture == 0) { if (texture == 0) {
printf("texture creation failed: %s\n", SDL_GetError()); printf("texture creation failed: %s\n", SDL_GetError());
} else { } else {
@ -228,13 +190,27 @@ loadFont(void)
} }
} }
void
draw()
{
SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
SDL_RenderClear(renderer);
for (int i = 0; i < numChars; i++) {
drawGlyph(glyphs[i], i);
}
drawGlyph(29, numChars); /* cursor is at index 29 in the bitmap font */
SDL_RenderPresent(renderer);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int index; /* index of last key we pushed in the bitmap font */ int index; /* index of last key we pushed in the bitmap font */
SDL_Window *window; SDL_Window *window;
SDL_Event event; /* last event received */ SDL_Event event; /* last event received */
SDL_Keymod mod; /* key modifiers of last key we pushed */
SDL_Scancode scancode; /* scancode of last key we pushed */ SDL_Scancode scancode; /* scancode of last key we pushed */
int width; int width;
int height; int height;
@ -245,7 +221,7 @@ main(int argc, char *argv[])
/* create window */ /* create window */
window = SDL_CreateWindow("iPhone keyboard test", 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI); window = SDL_CreateWindow("iPhone keyboard test", 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
/* create renderer */ /* create renderer */
renderer = SDL_CreateRenderer(window, -1, 0); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
SDL_GetWindowSize(window, &width, &height); SDL_GetWindowSize(window, &width, &height);
SDL_RenderSetLogicalSize(renderer, width, height); SDL_RenderSetLogicalSize(renderer, width, height);
@ -253,46 +229,27 @@ main(int argc, char *argv[])
/* load up our font */ /* load up our font */
loadFont(); loadFont();
/* draw the background, we'll just paint over it */
SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
SDL_RenderFillRect(renderer, NULL);
SDL_RenderPresent(renderer);
int done = 0; int done = 0;
/* loop till we get SDL_Quit */
while (!done && SDL_WaitEvent(&event)) { while (!done) {
while (SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
case SDL_QUIT: case SDL_QUIT:
done = 1; done = 1;
break; break;
case SDL_TEXTINPUT:
break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
index = keyToIndex(event.key.keysym); if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
scancode = event.key.keysym.scancode; if (numChars > 0) {
mod = event.key.keysym.mod; numChars--;
if (scancode == SDL_SCANCODE_DELETE) { }
/* if user hit delete, delete the last character */ } else if (numChars + 1 < MAX_CHARS) {
backspace(); int index = keyToGlyphIndex(event.key.keysym);
lastCharWasColon = 0; if (index >= 0) {
} else if (lastCharWasColon && scancode == SDL_SCANCODE_0 glyphs[numChars++] = index;
&& (mod & KMOD_SHIFT)) { }
/* if our last key was a colon and this one is a close paren, the make a hoppy face */
backspace();
drawIndex(32); /* index for happy face */
numChars++;
drawCursor();
lastCharWasColon = 0;
} else if (index != -1) {
/* if we aren't doing a happy face, then just draw the normal character */
drawIndex(index);
numChars++;
drawCursor();
lastCharWasColon =
(event.key.keysym.scancode == SDL_SCANCODE_SEMICOLON
&& (event.key.keysym.mod & KMOD_SHIFT));
} }
/* check if the key was a colon */
/* draw our updates to the screen */
SDL_RenderPresent(renderer);
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
/* mouse up toggles onscreen keyboard visibility */ /* mouse up toggles onscreen keyboard visibility */
@ -304,14 +261,13 @@ main(int argc, char *argv[])
break; break;
} }
} }
cleanup();
return 0; draw();
SDL_Delay(15);
} }
/* clean up after ourselves like a good kiddy */
void
cleanup(void)
{
SDL_DestroyTexture(texture); SDL_DestroyTexture(texture);
SDL_Quit(); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
return 0;
} }

View File

@ -179,15 +179,13 @@ UIKit_ComputeViewFrame(SDL_Window *window, UIScreen *screen)
#if !TARGET_OS_TV && (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0) #if !TARGET_OS_TV && (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0)
BOOL hasiOS7 = UIKit_IsSystemVersionAtLeast(7.0); BOOL hasiOS7 = UIKit_IsSystemVersionAtLeast(7.0);
if (hasiOS7 || (window->flags & (SDL_WINDOW_BORDERLESS|SDL_WINDOW_FULLSCREEN))) {
/* The view should always show behind the status bar in iOS 7+. */ /* The view should always show behind the status bar in iOS 7+. */
return screen.bounds; if (!hasiOS7 && !(window->flags & (SDL_WINDOW_BORDERLESS|SDL_WINDOW_FULLSCREEN))) {
} else {
return screen.applicationFrame; return screen.applicationFrame;
} }
#else
return screen.bounds;
#endif #endif
return screen.bounds;
} }
/* /*