Xcode-iPhoneOS/Demos/src/keyboard.c
changeset 5364 fec676157db5
parent 5081 25d4feb7c127
equal deleted inserted replaced
5363:96799a165806 5364:fec676157db5
    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 
       
    26 static SDL_Renderer *renderer;
    26 static int numChars = 0;        /* number of characters we've typed so far */
    27 static int numChars = 0;        /* number of characters we've typed so far */
    27 static SDL_bool lastCharWasColon = 0;   /* we use this to detect sequences such as :) */
    28 static SDL_bool lastCharWasColon = 0;   /* we use this to detect sequences such as :) */
    28 static SDL_Color bg_color = { 50, 50, 100, 255 };       /* color of background */
    29 static SDL_Color bg_color = { 50, 50, 100, 255 };       /* color of background */
    29 
    30 
    30 /* this structure maps a scancode to an index in our bitmap font.
    31 /* this structure maps a scancode to an index in our bitmap font.
   155     getPositionForCharNumber(numChars, &x, &y);
   156     getPositionForCharNumber(numChars, &x, &y);
   156     SDL_Rect srcRect =
   157     SDL_Rect srcRect =
   157         { GLYPH_SIZE_IMAGE * index, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
   158         { GLYPH_SIZE_IMAGE * index, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
   158     SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
   159     SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
   159     drawBlank(x, y);
   160     drawBlank(x, y);
   160     SDL_RenderCopy(texture, &srcRect, &dstRect);
   161     SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
   161 }
   162 }
   162 
   163 
   163 /*  draws the cursor icon at the current end position of the text */
   164 /*  draws the cursor icon at the current end position of the text */
   164 void
   165 void
   165 drawCursor(void)
   166 drawCursor(void)
   172 */
   173 */
   173 void
   174 void
   174 drawBlank(int x, int y)
   175 drawBlank(int x, int y)
   175 {
   176 {
   176     SDL_Rect rect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
   177     SDL_Rect rect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
   177     SDL_SetRenderDrawColor(bg_color.r, bg_color.g, bg_color.b,
   178     SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b,
   178                            bg_color.unused);
   179                            bg_color.unused);
   179     SDL_RenderFill(&rect);
   180     SDL_RenderFillRect(renderer, &rect);
   180 }
   181 }
   181 
   182 
   182 /* moves backwards one character, erasing the last one put down */
   183 /* moves backwards one character, erasing the last one put down */
   183 void
   184 void
   184 backspace(void)
   185 backspace(void)
   217             SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask,
   218             SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask,
   218                                  Bmask, Amask);
   219                                  Bmask, Amask);
   219         SDL_BlitSurface(surface, NULL, converted, NULL);
   220         SDL_BlitSurface(surface, NULL, converted, NULL);
   220         /* create our texture */
   221         /* create our texture */
   221         texture =
   222         texture =
   222             SDL_CreateTextureFromSurface(SDL_PIXELFORMAT_ABGR8888, converted);
   223             SDL_CreateTextureFromSurface(renderer, converted);
   223         if (texture == 0) {
   224         if (texture == 0) {
   224             printf("texture creation failed: %s\n", SDL_GetError());
   225             printf("texture creation failed: %s\n", SDL_GetError());
   225         } else {
   226         } else {
   226             /* set blend mode for our texture */
   227             /* set blend mode for our texture */
   227             SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
   228             SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
   246         printf("Error initializing SDL: %s", SDL_GetError());
   247         printf("Error initializing SDL: %s", SDL_GetError());
   247     }
   248     }
   248     /* create window */
   249     /* create window */
   249     window = SDL_CreateWindow("iPhone keyboard test", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
   250     window = SDL_CreateWindow("iPhone keyboard test", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
   250     /* create renderer */
   251     /* create renderer */
   251     SDL_CreateRenderer(window, 0, 0);
   252     renderer = SDL_CreateRenderer(window, -1, 0);
   252 
   253 
   253     /* load up our font */
   254     /* load up our font */
   254     loadFont();
   255     loadFont();
   255 
   256 
   256     /* draw the background, we'll just paint over it */
   257     /* draw the background, we'll just paint over it */
   257     SDL_SetRenderDrawColor(bg_color.r, bg_color.g, bg_color.b,
   258     SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b,
   258                            bg_color.unused);
   259                            bg_color.unused);
   259     SDL_RenderFill(NULL);
   260     SDL_RenderFillRect(renderer, NULL);
   260     SDL_RenderPresent();
   261     SDL_RenderPresent(renderer);
   261 
   262 
   262     int done = 0;
   263     int done = 0;
   263     /* loop till we get SDL_Quit */
   264     /* loop till we get SDL_Quit */
   264     while (SDL_WaitEvent(&event)) {
   265     while (SDL_WaitEvent(&event)) {
   265         switch (event.type) {
   266         switch (event.type) {
   291                     (event.key.keysym.scancode == SDL_SCANCODE_SEMICOLON
   292                     (event.key.keysym.scancode == SDL_SCANCODE_SEMICOLON
   292                      && (event.key.keysym.mod & KMOD_SHIFT));
   293                      && (event.key.keysym.mod & KMOD_SHIFT));
   293             }
   294             }
   294             /* check if the key was a colon */
   295             /* check if the key was a colon */
   295             /* draw our updates to the screen */
   296             /* draw our updates to the screen */
   296             SDL_RenderPresent();
   297             SDL_RenderPresent(renderer);
   297             break;
   298             break;
   298 #ifdef __IPHONEOS__
   299 #ifdef __IPHONEOS__
   299         case SDL_MOUSEBUTTONUP:
   300         case SDL_MOUSEBUTTONUP:
   300             /*      mouse up toggles onscreen keyboard visibility
   301             /*      mouse up toggles onscreen keyboard visibility
   301                this function is available ONLY on iPhone OS
   302                this function is available ONLY on iPhone OS