comparison Xcode-iPhoneOS/Demos/src/keyboard.c @ 3685:64ce267332c6

Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 21 Jan 2010 06:21:52 +0000
parents 20326ba2bda2
children 25d4feb7c127
comparison
equal deleted inserted replaced
3684:cc564f08884f 3685:64ce267332c6
8 #import "common.h" 8 #import "common.h"
9 9
10 #define GLYPH_SIZE_IMAGE 16 /* size of glyphs (characters) in the bitmap font file */ 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 */ 11 #define GLYPH_SIZE_SCREEN 32 /* size of glyphs (characters) as shown on the screen */
12 12
13 static SDL_TextureID textureID; /* texture where we'll hold our font */ 13 static SDL_Texture *texture; /* texture where we'll hold our font */
14 14
15 /* iPhone SDL addition keyboard related function definitions */ 15 /* iPhone SDL addition keyboard related function definitions */
16 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_WindowID windowID); 16 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_Window * window);
17 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardHide(SDL_WindowID windowID); 17 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardHide(SDL_Window * window);
18 extern DECLSPEC SDL_bool SDLCALL SDL_iPhoneKeyboardIsShown(SDL_WindowID 18 extern DECLSPEC SDL_bool SDLCALL SDL_iPhoneKeyboardIsShown(SDL_Window *
19 windowID); 19 window);
20 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardToggle(SDL_WindowID windowID); 20 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardToggle(SDL_Window * window);
21 21
22 /* function declarations */ 22 /* function declarations */
23 void cleanup(void); 23 void cleanup(void);
24 void drawBlank(int x, int y); 24 void drawBlank(int x, int y);
25 25
155 getPositionForCharNumber(numChars, &x, &y); 155 getPositionForCharNumber(numChars, &x, &y);
156 SDL_Rect srcRect = 156 SDL_Rect srcRect =
157 { GLYPH_SIZE_IMAGE * index, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE }; 157 { GLYPH_SIZE_IMAGE * index, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
158 SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN }; 158 SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
159 drawBlank(x, y); 159 drawBlank(x, y);
160 SDL_RenderCopy(textureID, &srcRect, &dstRect); 160 SDL_RenderCopy(texture, &srcRect, &dstRect);
161 } 161 }
162 162
163 /* draws the cursor icon at the current end position of the text */ 163 /* draws the cursor icon at the current end position of the text */
164 void 164 void
165 drawCursor(void) 165 drawCursor(void)
192 drawBlank(x, y); 192 drawBlank(x, y);
193 drawCursor(); 193 drawCursor();
194 } 194 }
195 } 195 }
196 196
197 /* this function loads our font into an SDL_Texture and returns the SDL_TextureID */ 197 /* this function loads our font into an SDL_Texture and returns the SDL_Texture */
198 SDL_TextureID 198 SDL_Texture*
199 loadFont(void) 199 loadFont(void)
200 { 200 {
201 201
202 SDL_Surface *surface = SDL_LoadBMP("kromasky_16x16.bmp"); 202 SDL_Surface *surface = SDL_LoadBMP("kromasky_16x16.bmp");
203 203
216 SDL_Surface *converted = 216 SDL_Surface *converted =
217 SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask, 217 SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask,
218 Bmask, Amask); 218 Bmask, Amask);
219 SDL_BlitSurface(surface, NULL, converted, NULL); 219 SDL_BlitSurface(surface, NULL, converted, NULL);
220 /* create our texture */ 220 /* create our texture */
221 textureID = 221 texture =
222 SDL_CreateTextureFromSurface(SDL_PIXELFORMAT_ABGR8888, converted); 222 SDL_CreateTextureFromSurface(SDL_PIXELFORMAT_ABGR8888, converted);
223 if (textureID == 0) { 223 if (texture == 0) {
224 printf("texture creation failed: %s\n", SDL_GetError()); 224 printf("texture creation failed: %s\n", SDL_GetError());
225 } else { 225 } else {
226 /* set blend mode for our texture */ 226 /* set blend mode for our texture */
227 SDL_SetTextureBlendMode(textureID, SDL_BLENDMODE_BLEND); 227 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
228 } 228 }
229 SDL_FreeSurface(surface); 229 SDL_FreeSurface(surface);
230 SDL_FreeSurface(converted); 230 SDL_FreeSurface(converted);
231 return textureID; 231 return texture;
232 } 232 }
233 } 233 }
234 234
235 int 235 int
236 main(int argc, char *argv[]) 236 main(int argc, char *argv[])
237 { 237 {
238 238
239 int index; /* index of last key we pushed in the bitmap font */ 239 int index; /* index of last key we pushed in the bitmap font */
240 SDL_Window *window;
240 SDL_Event event; /* last event received */ 241 SDL_Event event; /* last event received */
241 SDLMod mod; /* key modifiers of last key we pushed */ 242 SDLMod mod; /* key modifiers of last key we pushed */
242 SDL_scancode scancode; /* scancode of last key we pushed */ 243 SDL_scancode scancode; /* scancode of last key we pushed */
243 244
244 if (SDL_Init(SDL_INIT_VIDEO) < 0) { 245 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
245 printf("Error initializing SDL: %s", SDL_GetError()); 246 printf("Error initializing SDL: %s", SDL_GetError());
246 } 247 }
247 /* create window */ 248 /* create window */
248 SDL_WindowID windowID = 249 window = SDL_CreateWindow("iPhone keyboard test", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
249 SDL_CreateWindow("iPhone keyboard test", 0, 0, SCREEN_WIDTH,
250 SCREEN_HEIGHT, 0);
251 /* create renderer */ 250 /* create renderer */
252 SDL_CreateRenderer(windowID, 0, 0); 251 SDL_CreateRenderer(window, 0, 0);
253 252
254 /* load up our font */ 253 /* load up our font */
255 loadFont(); 254 loadFont();
256 255
257 /* draw the background, we'll just paint over it */ 256 /* draw the background, we'll just paint over it */
299 #ifdef __IPHONEOS__ 298 #ifdef __IPHONEOS__
300 case SDL_MOUSEBUTTONUP: 299 case SDL_MOUSEBUTTONUP:
301 /* mouse up toggles onscreen keyboard visibility 300 /* mouse up toggles onscreen keyboard visibility
302 this function is available ONLY on iPhone OS 301 this function is available ONLY on iPhone OS
303 */ 302 */
304 SDL_iPhoneKeyboardToggle(windowID); 303 SDL_iPhoneKeyboardToggle(window);
305 break; 304 break;
306 #endif 305 #endif
307 } 306 }
308 } 307 }
309 cleanup(); 308 cleanup();
312 311
313 /* clean up after ourselves like a good kiddy */ 312 /* clean up after ourselves like a good kiddy */
314 void 313 void
315 cleanup(void) 314 cleanup(void)
316 { 315 {
317 SDL_DestroyTexture(textureID); 316 SDL_DestroyTexture(texture);
318 SDL_Quit(); 317 SDL_Quit();
319 } 318 }