sdlterm/tests/test_sdl_events.cc
changeset 72 6e0656600754
equal deleted inserted replaced
71:cfd3445107b4 72:6e0656600754
       
     1 #include <SDL/SDL.h>
       
     2 
       
     3 Uint32 timeout_callback(Uint32 interval, void *param)
       
     4 {
       
     5     SDL_Event event;
       
     6     event.type = SDL_USEREVENT;
       
     7     event.user.code = 1;
       
     8     SDL_PushEvent(&event);
       
     9     return 0;
       
    10 }
       
    11 
       
    12 bool handle_events(Uint32 timeout)
       
    13 {
       
    14     SDL_Event event;
       
    15     SDL_TimerID timer_id = NULL;
       
    16     bool done = false;
       
    17 
       
    18     if (timeout)
       
    19     {
       
    20         timer_id = SDL_AddTimer(timeout, timeout_callback, NULL);
       
    21     }
       
    22 
       
    23     // wait for events and handle them
       
    24     if (SDL_WaitEvent(&event))
       
    25     {
       
    26         do
       
    27         {
       
    28             switch (event.type)
       
    29             {
       
    30             case SDL_USEREVENT:
       
    31                 // timeout
       
    32                 if (event.user.code == 1)
       
    33                 {
       
    34                     SDL_RemoveTimer(timer_id);
       
    35                     printf("timeout\n");
       
    36                 }
       
    37                 break; // continue loop
       
    38 
       
    39             case SDL_QUIT:
       
    40                 printf("quit\n");
       
    41                 done = true;
       
    42                 break;
       
    43 
       
    44             case SDL_KEYUP:
       
    45                 printf("keyup\n");
       
    46                 if (event.key.keysym.sym == SDLK_ESCAPE)
       
    47                 {
       
    48                     done = true;
       
    49                 }
       
    50                 break;
       
    51 
       
    52             case SDL_MOUSEBUTTONDOWN:
       
    53                 printf("mouse button down\n");
       
    54                 break;
       
    55 
       
    56             case SDL_MOUSEBUTTONUP:
       
    57                 printf("mouse button up\n");
       
    58                 break;
       
    59             }
       
    60         }
       
    61         while (SDL_PollEvent(&event));
       
    62     }
       
    63     // remove timer when other event came before timeout
       
    64     if (timeout)
       
    65     {
       
    66         SDL_RemoveTimer(timer_id);
       
    67     }
       
    68     return done;
       
    69 }
       
    70 
       
    71 int main(int argc, char **argv)
       
    72 {
       
    73     SDL_Surface* screen = NULL;
       
    74 
       
    75     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)
       
    76     {
       
    77         fprintf(stderr, "SDL_Init: %s\n", SDL_GetError());
       
    78         exit(1);
       
    79     }
       
    80 
       
    81     atexit(SDL_Quit);
       
    82 
       
    83     screen = SDL_SetVideoMode(800, 480, 16, SDL_SWSURFACE);
       
    84     if (screen == NULL)
       
    85     {
       
    86         fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
       
    87         exit(1);
       
    88     }
       
    89 
       
    90     bool done = false;
       
    91     while (!done)
       
    92     {
       
    93         done = handle_events(1000);
       
    94     }
       
    95 
       
    96     return (0);
       
    97 }