comparison XCodeiPhoneOS/Demos/src/keyboard.c @ 2412:a01958cd513d gsoc2008_iphone

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