test/testmessage.c
changeset 7088 664d8532219b
parent 6758 2a218048b45f
child 7456 193c9a1723ad
     1.1 --- a/test/testmessage.c	Mon Apr 22 18:14:26 2013 -0700
     1.2 +++ b/test/testmessage.c	Mon Apr 22 18:14:32 2013 -0700
     1.3 @@ -29,13 +29,67 @@
     1.4      exit(rc);
     1.5  }
     1.6  
     1.7 +static int
     1.8 +button_messagebox(void *eventNumber)
     1.9 +{
    1.10 +    const SDL_MessageBoxButtonData buttons[] = {
    1.11 +        {
    1.12 +            SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
    1.13 +            0,
    1.14 +            "OK"
    1.15 +        },{
    1.16 +            SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
    1.17 +            1,
    1.18 +            "Cancel"
    1.19 +        },
    1.20 +    };
    1.21 +
    1.22 +    SDL_MessageBoxData data = {
    1.23 +        SDL_MESSAGEBOX_INFORMATION,
    1.24 +        NULL, // no parent window
    1.25 +        "Custom MessageBox",
    1.26 +        "This is a custom messagebox",
    1.27 +        2,
    1.28 +        buttons,
    1.29 +        NULL // Default color scheme
    1.30 +    };
    1.31 +
    1.32 +    int button = -1;
    1.33 +    int success = 0;
    1.34 +    if (eventNumber) {
    1.35 +        data.message = "This is a custom messagebox from a background thread.";
    1.36 +    }
    1.37 +
    1.38 +    success =SDL_ShowMessageBox(&data, &button);
    1.39 +    if (success == -1) {
    1.40 +        printf("Error Presenting MessageBox: %s\n", SDL_GetError());
    1.41 +        if (eventNumber) {
    1.42 +            SDL_UserEvent event;
    1.43 +            event.type = (intptr_t)eventNumber;
    1.44 +            SDL_PushEvent((SDL_Event*)&event);
    1.45 +            return 1;
    1.46 +        } else {
    1.47 +            quit(2);
    1.48 +        }
    1.49 +    }
    1.50 +    printf("Pressed button: %d, %s\n", button, button == 1 ? "Cancel" : "OK");
    1.51 +
    1.52 +    if (eventNumber) {
    1.53 +        SDL_UserEvent event;
    1.54 +        event.type = (intptr_t)eventNumber;
    1.55 +        SDL_PushEvent((SDL_Event*)&event);
    1.56 +    }
    1.57 +
    1.58 +    return 0;
    1.59 +}
    1.60 +
    1.61  int
    1.62  main(int argc, char *argv[])
    1.63  {
    1.64      int success;
    1.65  
    1.66      /* Load the SDL library */
    1.67 -    if (SDL_Init(0) < 0) {
    1.68 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    1.69          fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    1.70          return (1);
    1.71      }
    1.72 @@ -78,36 +132,27 @@
    1.73          quit(1);
    1.74      }
    1.75  
    1.76 +    button_messagebox(NULL);
    1.77 +
    1.78 +    /* Technically this isn't a supported operation for the API, but it doesn't
    1.79 +     * hurt for it to work.
    1.80 +     */
    1.81      {
    1.82 -        const SDL_MessageBoxButtonData buttons[] = {
    1.83 -            {
    1.84 -                SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
    1.85 -                0,
    1.86 -                "OK"
    1.87 -            },{
    1.88 -                SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
    1.89 -                1,
    1.90 -                "Cancel"
    1.91 -            },
    1.92 -        };
    1.93 +        int status = 0;
    1.94 +        SDL_Event event;
    1.95 +        intptr_t eventNumber = SDL_RegisterEvents(1);
    1.96 +        SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
    1.97  
    1.98 -        SDL_MessageBoxData data = {
    1.99 -            SDL_MESSAGEBOX_INFORMATION,
   1.100 -            NULL, // no parent window
   1.101 -            "Custom MessageBox",
   1.102 -            "This is a custom messagebox",
   1.103 -            2,
   1.104 -            buttons,
   1.105 -            NULL // Default color scheme
   1.106 -        };
   1.107 +        while (SDL_WaitEvent(&event))
   1.108 +        {
   1.109 +            if (event.type == eventNumber) {
   1.110 +                break;
   1.111 +            }
   1.112 +        }
   1.113  
   1.114 -        int button = -1;
   1.115 -        success = SDL_ShowMessageBox(&data, &button);
   1.116 -        if (success == -1) {
   1.117 -            printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   1.118 -            quit(2);
   1.119 -        }
   1.120 -        printf("Pressed button: %d, %s\n", button, button == 1 ? "Cancel" : "OK");
   1.121 +        SDL_WaitThread(thread, &status);
   1.122 +
   1.123 +        printf("Message box thread return %i\n", status);
   1.124      }
   1.125  
   1.126      SDL_Quit();