keyboard.c contains the source code for the bitmap font keyboard example. gsoc2008_iphone
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Sat, 02 Aug 2008 00:54:30 +0000
branchgsoc2008_iphone
changeset 2412a01958cd513d
parent 2411 ec93e20ff04e
child 2413 ebeb6ca03766
keyboard.c contains the source code for the bitmap font keyboard example.
XCodeiPhoneOS/Demos/src/keyboard.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/XCodeiPhoneOS/Demos/src/keyboard.c	Sat Aug 02 00:54:30 2008 +0000
     1.3 @@ -0,0 +1,279 @@
     1.4 +/*
     1.5 + *	keyboard.c
     1.6 + *	written by Holmes Futrell
     1.7 + *	use however you want
     1.8 + */
     1.9 +
    1.10 +#import "SDL.h"
    1.11 +#import "common.h"
    1.12 +
    1.13 +#define GLYPH_SIZE_IMAGE 16		/* size of glyphs (characters) in the bitmap font file */
    1.14 +#define GLYPH_SIZE_SCREEN 32	/* size of glyphs (characters) as shown on the screen */
    1.15 +
    1.16 +static SDL_TextureID textureID; /* texture where we'll hold our font */
    1.17 +
    1.18 +/* iPhone SDL addition keyboard related function definitions */
    1.19 +extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_WindowID windowID);
    1.20 +extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardHide(SDL_WindowID windowID);
    1.21 +extern DECLSPEC SDL_bool SDLCALL SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID);
    1.22 +extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardToggle(SDL_WindowID windowID);
    1.23 +
    1.24 +/* function declarations */
    1.25 +void cleanup(void);
    1.26 +void drawBlank(int x, int y);
    1.27 +
    1.28 +static int numChars = 0;						  /* number of characters we've typed so far */
    1.29 +static SDL_bool lastCharWasColon = 0;			  /* we use this to detect sequences such as :) */
    1.30 +static SDL_Color bg_color = { 50, 50, 100, 255 }; /* color of background */
    1.31 +
    1.32 +/* this structure maps a scancode to an index in our bitmap font.
    1.33 +   it also contains data about under which modifiers the mapping is valid
    1.34 +   (for example, we don't want shift + 1 to produce the character '1',
    1.35 +   but rather the character '!')
    1.36 +*/
    1.37 +typedef struct {
    1.38 +	SDL_scancode scancode;	/* scancode of the key we want to map */	
    1.39 +	int allow_no_mod;		/* is the map valid if the key has no modifiers? */
    1.40 +	SDLMod mod;				/* what modifiers are allowed for the mapping */
    1.41 +	int index;				/* what index in the font does the scancode map to */
    1.42 +} fontMapping;
    1.43 +
    1.44 +#define TABLE_SIZE 51		/* size of our table which maps keys and modifiers to font indices */
    1.45 +
    1.46 +/* Below is the table that defines the mapping between scancodes and modifiers to indices in the
    1.47 +   bitmap font.  As an example, then line '{ SDL_SCANCODE_A, 1, KMOD_SHIFT, 33 }' means, map
    1.48 +   the key A (which has scancode SDL_SCANCODE_A) to index 33 in the font (which is a picture of an A),
    1.49 +   The '1' means that the mapping is valid even if there are no modifiers, and KMOD_SHIFT means the
    1.50 +   mapping is also valid if the user is holding shift.
    1.51 +*/
    1.52 +fontMapping map[TABLE_SIZE] = {
    1.53 +
    1.54 +{ SDL_SCANCODE_A, 1, KMOD_SHIFT, 33 },			/* A */
    1.55 +{ SDL_SCANCODE_B, 1, KMOD_SHIFT, 34 },			/* B */ 
    1.56 +{ SDL_SCANCODE_C, 1, KMOD_SHIFT, 35 },			/* C */
    1.57 +{ SDL_SCANCODE_D, 1, KMOD_SHIFT, 36 },			/* D */
    1.58 +{ SDL_SCANCODE_E, 1, KMOD_SHIFT, 37 },			/* E */
    1.59 +{ SDL_SCANCODE_F, 1, KMOD_SHIFT, 38 },			/* F */
    1.60 +{ SDL_SCANCODE_G, 1, KMOD_SHIFT, 39 },			/* G */
    1.61 +{ SDL_SCANCODE_H, 1, KMOD_SHIFT, 40 },			/* H */
    1.62 +{ SDL_SCANCODE_I, 1, KMOD_SHIFT, 41 },			/* I */
    1.63 +{ SDL_SCANCODE_J, 1, KMOD_SHIFT, 42 },			/* J */
    1.64 +{ SDL_SCANCODE_K, 1, KMOD_SHIFT, 43 },			/* K */
    1.65 +{ SDL_SCANCODE_L, 1, KMOD_SHIFT, 44 },			/* L */
    1.66 +{ SDL_SCANCODE_M, 1, KMOD_SHIFT, 45 },			/* M */
    1.67 +{ SDL_SCANCODE_N, 1, KMOD_SHIFT, 46 },			/* N */
    1.68 +{ SDL_SCANCODE_O, 1, KMOD_SHIFT, 47 },			/* O */
    1.69 +{ SDL_SCANCODE_P, 1, KMOD_SHIFT, 48 },			/* P */
    1.70 +{ SDL_SCANCODE_Q, 1, KMOD_SHIFT, 49 },			/* Q */
    1.71 +{ SDL_SCANCODE_R, 1, KMOD_SHIFT, 50 },			/* R */
    1.72 +{ SDL_SCANCODE_S, 1, KMOD_SHIFT, 51 },			/* S */
    1.73 +{ SDL_SCANCODE_T, 1, KMOD_SHIFT, 52 },			/* T */
    1.74 +{ SDL_SCANCODE_U, 1, KMOD_SHIFT, 53 },			/* U */
    1.75 +{ SDL_SCANCODE_V, 1, KMOD_SHIFT, 54 },			/* V */
    1.76 +{ SDL_SCANCODE_W, 1, KMOD_SHIFT, 55 },			/* W */
    1.77 +{ SDL_SCANCODE_X, 1, KMOD_SHIFT, 56 },			/* X */
    1.78 +{ SDL_SCANCODE_Y, 1, KMOD_SHIFT, 57 },			/* Y */
    1.79 +{ SDL_SCANCODE_Z, 1, KMOD_SHIFT, 58 },			/* Z */
    1.80 +{ SDL_SCANCODE_0, 1, 0, 16 },					/* 0 */
    1.81 +{ SDL_SCANCODE_1, 1, 0, 17 },					/* 1 */
    1.82 +{ SDL_SCANCODE_2, 1, 0, 18 },					/* 2 */
    1.83 +{ SDL_SCANCODE_3, 1, 0, 19 },					/* 3 */
    1.84 +{ SDL_SCANCODE_4, 1, 0, 20 },					/* 4 */
    1.85 +{ SDL_SCANCODE_5, 1, 0, 21 },					/* 5 */
    1.86 +{ SDL_SCANCODE_6, 1, 0, 22 },					/* 6 */
    1.87 +{ SDL_SCANCODE_7, 1, 0, 23 },					/* 7 */
    1.88 +{ SDL_SCANCODE_8, 1, 0, 24 },					/* 8 */
    1.89 +{ SDL_SCANCODE_9, 1, 0, 25 },					/* 9 */
    1.90 +{ SDL_SCANCODE_SPACE, 1, 0, 0 },				/*' '*/
    1.91 +{ SDL_SCANCODE_1, 0, KMOD_SHIFT, 1 },			/* ! */
    1.92 +{ SDL_SCANCODE_SLASH, 0, KMOD_SHIFT, 31},		/* ? */
    1.93 +{ SDL_SCANCODE_SLASH, 1, 0, 15},				/* / */
    1.94 +{ SDL_SCANCODE_COMMA, 1, 0, 12},				/* , */
    1.95 +{ SDL_SCANCODE_SEMICOLON, 1, 0, 27},			/* ; */
    1.96 +{ SDL_SCANCODE_SEMICOLON, 0, KMOD_SHIFT, 26},	/* : */
    1.97 +{ SDL_SCANCODE_PERIOD, 1, 0, 14},				/* . */
    1.98 +{ SDL_SCANCODE_MINUS, 1, 0, 13},				/* - */
    1.99 +{ SDL_SCANCODE_EQUALS, 0, KMOD_SHIFT, 11},		/* = */
   1.100 +{ SDL_SCANCODE_APOSTROPHE, 1, 0, 7},			/* ' */
   1.101 +{ SDL_SCANCODE_APOSTROPHE, 0, KMOD_SHIFT, 2},	/* " */
   1.102 +{ SDL_SCANCODE_5, 0, KMOD_SHIFT, 5},			/* % */
   1.103 +
   1.104 +};
   1.105 +
   1.106 +/*
   1.107 +	This function maps an SDL_keysym to an index in the bitmap font.
   1.108 +	It does so by scanning through the font mapping table one entry
   1.109 +	at a time.
   1.110 + 
   1.111 +	If a match is found (scancode and allowed modifiers), the proper
   1.112 +	index is returned.
   1.113 + 
   1.114 +	If there is no entry for the key, -1 is returned
   1.115 +*/
   1.116 +int keyToIndex(SDL_keysym key) {
   1.117 +	int i, index = -1;
   1.118 +	for (i=0; i<TABLE_SIZE; i++) {
   1.119 +		fontMapping compare = map[i];
   1.120 +		if (key.scancode == compare.scancode) {
   1.121 +			/* if this entry is valid with no key mod and we have no keymod, or if
   1.122 +			   the key's modifiers are allowed modifiers for that mapping */
   1.123 +			if ((compare.allow_no_mod && key.mod == 0) ||  ( key.mod & compare.mod ) ) {
   1.124 +				index = compare.index;
   1.125 +				break;
   1.126 +			}
   1.127 +		}
   1.128 +	}
   1.129 +	return index;
   1.130 +}
   1.131 +/* 
   1.132 +	This function returns and x,y position for a given character number.
   1.133 +    It is used for positioning each character of text
   1.134 +*/
   1.135 +void getPositionForCharNumber(int n, int *x, int *y) {
   1.136 +	int x_padding = 16; /* padding space on left and right side of screen */
   1.137 +	int y_padding = 32; /* padding space at top of screen */
   1.138 +	/* figure out the number of characters that can fit horizontally across the screen */
   1.139 +	int max_x_chars = (SCREEN_WIDTH - 2 * x_padding) / GLYPH_SIZE_SCREEN;
   1.140 +	int line_separation = 5; /* pixels between each line */
   1.141 +	*x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
   1.142 +	*y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) + y_padding;
   1.143 +}
   1.144 +void drawIndex(int index) {
   1.145 +	int x, y;
   1.146 +	getPositionForCharNumber(numChars, &x, &y);
   1.147 +	SDL_Rect srcRect = { GLYPH_SIZE_IMAGE * index, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
   1.148 +	SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
   1.149 +	drawBlank(x, y);
   1.150 +	SDL_RenderCopy(textureID, &srcRect, &dstRect);
   1.151 +}
   1.152 +/*  draws the cursor icon at the current end position of the text */
   1.153 +void drawCursor(void) {
   1.154 +	drawIndex(29);	/* cursor is at index 29 in the bitmap font */
   1.155 +}
   1.156 +/* paints over a glyph sized region with the background color
   1.157 +   in effect it erases the area
   1.158 +*/
   1.159 +void drawBlank(int x, int y) {
   1.160 +	SDL_Rect rect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
   1.161 +	SDL_RenderFill(bg_color.r, bg_color.g, bg_color.b, bg_color.unused, &rect);
   1.162 +}
   1.163 +/* moves backwards one character, erasing the last one put down */
   1.164 +void backspace(void) {
   1.165 +	int x, y;
   1.166 +	if (numChars > 0) {
   1.167 +		getPositionForCharNumber(numChars, &x, &y);
   1.168 +		drawBlank(x, y);
   1.169 +		numChars--;
   1.170 +		getPositionForCharNumber(numChars, &x, &y);
   1.171 +		drawBlank(x, y);
   1.172 +		drawCursor();
   1.173 +	}
   1.174 +}
   1.175 +/* this function loads our font into an SDL_Texture and returns the SDL_TextureID */
   1.176 +SDL_TextureID loadFont(void) {
   1.177 +	
   1.178 +	SDL_Surface *surface = SDL_LoadBMP("kromasky_16x16.bmp");
   1.179 +
   1.180 +	if (!surface) {
   1.181 +		printf("Error loading bitmap: %s\n", SDL_GetError());
   1.182 +		return 0;
   1.183 +	} 
   1.184 +	else {
   1.185 +		/* set the transparent color for the bitmap font (hot pink) */
   1.186 +		SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, 238, 0, 252 ));
   1.187 +		/* now we convert the surface to our desired pixel format */
   1.188 +		int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */
   1.189 +		Uint32 Rmask, Gmask, Bmask, Amask;	   /* masks for desired format */
   1.190 +		int bpp;							   /* bits per pixel for desired format */
   1.191 +		SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
   1.192 +		SDL_Surface *converted = SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask, Bmask, Amask);	
   1.193 +		SDL_BlitSurface(surface, NULL, converted, NULL);
   1.194 +		/* create our texture */
   1.195 +		textureID = SDL_CreateTextureFromSurface(SDL_PIXELFORMAT_ABGR8888, converted);
   1.196 +		if (textureID == 0) {
   1.197 +			printf("texture creation failed: %s\n", SDL_GetError());
   1.198 +		}
   1.199 +		else {
   1.200 +			/* set blend mode for our texture */
   1.201 +			SDL_SetTextureBlendMode(textureID, SDL_TEXTUREBLENDMODE_BLEND);								  
   1.202 +		}
   1.203 +		SDL_FreeSurface(surface);
   1.204 +		SDL_FreeSurface(converted);
   1.205 +		return textureID;
   1.206 +	}
   1.207 +}
   1.208 +
   1.209 +int main(int argc, char *argv[]) {
   1.210 +	
   1.211 +	int index;				/* index of last key we pushed in the bitmap font */
   1.212 +	SDL_Event event;		/* last event received */
   1.213 +	SDLMod mod;				/* key modifiers of last key we pushed */
   1.214 +	SDL_scancode scancode;  /* scancode of last key we pushed */
   1.215 +	
   1.216 +	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   1.217 +		printf("Error initializing SDL: %s", SDL_GetError());
   1.218 +	}
   1.219 +	/* create window */
   1.220 +	SDL_WindowID windowID = SDL_CreateWindow("iPhone keyboard test", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
   1.221 +	/* create renderer */
   1.222 +	SDL_CreateRenderer(windowID, 0, 0);		
   1.223 +		
   1.224 +	/* load up our font */
   1.225 +	loadFont();
   1.226 +
   1.227 +	/* draw the background, we'll just paint over it */
   1.228 +	SDL_RenderFill(bg_color.r, bg_color.g, bg_color.b, bg_color.unused, NULL);
   1.229 +	SDL_RenderPresent();
   1.230 +	
   1.231 +	int done = 0;
   1.232 +	/* loop till we get SDL_Quit */
   1.233 +	while (SDL_WaitEvent(&event)) {
   1.234 +		switch (event.type) {
   1.235 +			case SDL_QUIT:
   1.236 +				done = 1;
   1.237 +				break;
   1.238 +			case SDL_KEYDOWN:
   1.239 +				index = keyToIndex(event.key.keysym);
   1.240 +				scancode = event.key.keysym.scancode;
   1.241 +				mod		 = event.key.keysym.mod;
   1.242 +				if (scancode == SDL_SCANCODE_DELETE) {
   1.243 +					/* if user hit delete, delete the last character */
   1.244 +					backspace();
   1.245 +					lastCharWasColon = 0;
   1.246 +				}
   1.247 +				else if (lastCharWasColon && scancode == SDL_SCANCODE_0 && (mod & KMOD_SHIFT)) {
   1.248 +					/* if our last key was a colon and this one is a close paren, the make a hoppy face */
   1.249 +					backspace();
   1.250 +					drawIndex(32); /* index for happy face */
   1.251 +					numChars++;
   1.252 +					drawCursor();
   1.253 +					lastCharWasColon = 0;
   1.254 +				}
   1.255 +				else if (index != -1) {
   1.256 +					/* if we aren't doing a happy face, then just draw the normal character */
   1.257 +					drawIndex(index);
   1.258 +					numChars++;
   1.259 +					drawCursor();
   1.260 +					lastCharWasColon = (event.key.keysym.scancode == SDL_SCANCODE_SEMICOLON\
   1.261 +										&& (event.key.keysym.mod & KMOD_SHIFT));
   1.262 +				}
   1.263 +				/* check if the key was a colon */
   1.264 +				/* draw our updates to the screen */
   1.265 +				SDL_RenderPresent();
   1.266 +				break;
   1.267 +			case SDL_MOUSEBUTTONUP:
   1.268 +				/* mouse up toggles keyboard */
   1.269 +				SDL_iPhoneKeyboardToggle(windowID);
   1.270 +				break;
   1.271 +		}
   1.272 +	}
   1.273 +	cleanup();
   1.274 +	return 0;
   1.275 +}
   1.276 +
   1.277 +/* clean up after ourselves like a good kiddy */
   1.278 +void cleanup(void) {
   1.279 +	SDL_DestroyTexture(textureID);
   1.280 +	SDL_Quit();
   1.281 +}
   1.282 +