test/checkkeys.c
changeset 9278 8900afb78a19
parent 8149 681eb46b8ac4
child 9580 d37ef6990bf9
equal deleted inserted replaced
9277:f0bd40782fc7 9278:8900afb78a19
    17 
    17 
    18 #include <stdio.h>
    18 #include <stdio.h>
    19 #include <stdlib.h>
    19 #include <stdlib.h>
    20 #include <string.h>
    20 #include <string.h>
    21 
    21 
       
    22 #ifdef __EMSCRIPTEN__
       
    23 #include <emscripten/emscripten.h>
       
    24 #endif
       
    25 
    22 #include "SDL.h"
    26 #include "SDL.h"
       
    27 
       
    28 int done;
    23 
    29 
    24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    30 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    25 static void
    31 static void
    26 quit(int rc)
    32 quit(int rc)
    27 {
    33 {
   126         SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
   132         SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
   127     }
   133     }
   128     SDL_Log("Text (%s): \"%s%s\"\n", expanded, *text == '"' ? "\\" : "", text);
   134     SDL_Log("Text (%s): \"%s%s\"\n", expanded, *text == '"' ? "\\" : "", text);
   129 }
   135 }
   130 
   136 
       
   137 void
       
   138 loop()
       
   139 {
       
   140     SDL_Event event;
       
   141     /* Check for events */
       
   142     /*SDL_WaitEvent(&event); emscripten does not like waiting*/
       
   143 
       
   144     while (SDL_PollEvent(&event)) {
       
   145         switch (event.type) {
       
   146         case SDL_KEYDOWN:
       
   147         //case SDL_KEYUP:
       
   148 		    PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
       
   149             break;
       
   150         case SDL_TEXTINPUT:
       
   151             PrintText(event.text.text);
       
   152             break;
       
   153         case SDL_MOUSEBUTTONDOWN:
       
   154             /* Any button press quits the app... */
       
   155         case SDL_QUIT:
       
   156             done = 1;
       
   157             break;
       
   158         default:
       
   159             break;
       
   160         }
       
   161     }
       
   162 }
       
   163 
   131 int
   164 int
   132 main(int argc, char *argv[])
   165 main(int argc, char *argv[])
   133 {
   166 {
   134     SDL_Window *window;
   167     SDL_Window *window;
   135     SDL_Event event;
       
   136     int done;
       
   137 	
   168 	
   138 	/* Enable standard application logging */
   169 	/* Enable standard application logging */
   139 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   170 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   140 
   171 
   141     /* Initialize SDL */
   172     /* Initialize SDL */
   161 
   192 
   162     SDL_StartTextInput();
   193     SDL_StartTextInput();
   163 
   194 
   164     /* Watch keystrokes */
   195     /* Watch keystrokes */
   165     done = 0;
   196     done = 0;
       
   197 
       
   198 #ifdef __EMSCRIPTEN__
       
   199     emscripten_set_main_loop(loop, 0, 1);
       
   200 #else
   166     while (!done) {
   201     while (!done) {
   167         /* Check for events */
   202         loop();
   168         SDL_WaitEvent(&event);
   203     }
   169         switch (event.type) {
   204 #endif
   170         case SDL_KEYDOWN:
       
   171         case SDL_KEYUP:
       
   172 			PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
       
   173             break;
       
   174         case SDL_TEXTINPUT:
       
   175             PrintText(event.text.text);
       
   176             break;
       
   177         case SDL_MOUSEBUTTONDOWN:
       
   178             /* Any button press quits the app... */
       
   179         case SDL_QUIT:
       
   180             done = 1;
       
   181             break;
       
   182         default:
       
   183             break;
       
   184         }
       
   185     }
       
   186 
   205 
   187     SDL_Quit();
   206     SDL_Quit();
   188     return (0);
   207     return (0);
   189 }
   208 }
   190 
   209