test/testmessage.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Apr 2013 00:26:17 -0700
changeset 7110 2a98852fd58d
parent 7088 664d8532219b
child 7456 193c9a1723ad
permissions -rw-r--r--
Fixed bug 1582 - Allow disabling SDL_VIDEO_DRIVER_WINDOWS

Marcus von Appen

Trying to build SDL 2.x (HG) on Win32 platforms with either VS.NET or MinGW requires one to have the video subsystem and SDL_VIDEO_DRIVER_WINDOWS flag enabled due to the helper window creation routines.

The attached patch changes the helper window creation behaviour, so that one can build SDL2 without the video subsystem or Windows video drivers on Win32 platforms.
slouken@6614
     1
/*
slouken@6614
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@6614
     3
slouken@6614
     4
  This software is provided 'as-is', without any express or implied
slouken@6614
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@6614
     6
  arising from the use of this software.
slouken@6614
     7
slouken@6614
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@6614
     9
  including commercial applications, and to alter it and redistribute it
slouken@6614
    10
  freely.
slouken@6614
    11
*/
slouken@6614
    12
slouken@6614
    13
/* Simple test of the SDL MessageBox API*/
slouken@6614
    14
slouken@6614
    15
#include <stdio.h>
slouken@6614
    16
#include <stdlib.h>
slouken@6614
    17
#include <signal.h>
slouken@6614
    18
slouken@6614
    19
#include "SDL.h"
slouken@6614
    20
#include "SDL_thread.h"
slouken@6614
    21
slouken@6614
    22
static int alive = 0;
slouken@6614
    23
slouken@6614
    24
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@6614
    25
static void
slouken@6614
    26
quit(int rc)
slouken@6614
    27
{
slouken@6614
    28
    SDL_Quit();
slouken@6614
    29
    exit(rc);
slouken@6614
    30
}
slouken@6614
    31
jorgen@7088
    32
static int
jorgen@7088
    33
button_messagebox(void *eventNumber)
jorgen@7088
    34
{
jorgen@7088
    35
    const SDL_MessageBoxButtonData buttons[] = {
jorgen@7088
    36
        {
jorgen@7088
    37
            SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
jorgen@7088
    38
            0,
jorgen@7088
    39
            "OK"
jorgen@7088
    40
        },{
jorgen@7088
    41
            SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
jorgen@7088
    42
            1,
jorgen@7088
    43
            "Cancel"
jorgen@7088
    44
        },
jorgen@7088
    45
    };
jorgen@7088
    46
jorgen@7088
    47
    SDL_MessageBoxData data = {
jorgen@7088
    48
        SDL_MESSAGEBOX_INFORMATION,
jorgen@7088
    49
        NULL, // no parent window
jorgen@7088
    50
        "Custom MessageBox",
jorgen@7088
    51
        "This is a custom messagebox",
jorgen@7088
    52
        2,
jorgen@7088
    53
        buttons,
jorgen@7088
    54
        NULL // Default color scheme
jorgen@7088
    55
    };
jorgen@7088
    56
jorgen@7088
    57
    int button = -1;
jorgen@7088
    58
    int success = 0;
jorgen@7088
    59
    if (eventNumber) {
jorgen@7088
    60
        data.message = "This is a custom messagebox from a background thread.";
jorgen@7088
    61
    }
jorgen@7088
    62
jorgen@7088
    63
    success =SDL_ShowMessageBox(&data, &button);
jorgen@7088
    64
    if (success == -1) {
jorgen@7088
    65
        printf("Error Presenting MessageBox: %s\n", SDL_GetError());
jorgen@7088
    66
        if (eventNumber) {
jorgen@7088
    67
            SDL_UserEvent event;
jorgen@7088
    68
            event.type = (intptr_t)eventNumber;
jorgen@7088
    69
            SDL_PushEvent((SDL_Event*)&event);
jorgen@7088
    70
            return 1;
jorgen@7088
    71
        } else {
jorgen@7088
    72
            quit(2);
jorgen@7088
    73
        }
jorgen@7088
    74
    }
jorgen@7088
    75
    printf("Pressed button: %d, %s\n", button, button == 1 ? "Cancel" : "OK");
jorgen@7088
    76
jorgen@7088
    77
    if (eventNumber) {
jorgen@7088
    78
        SDL_UserEvent event;
jorgen@7088
    79
        event.type = (intptr_t)eventNumber;
jorgen@7088
    80
        SDL_PushEvent((SDL_Event*)&event);
jorgen@7088
    81
    }
jorgen@7088
    82
jorgen@7088
    83
    return 0;
jorgen@7088
    84
}
jorgen@7088
    85
slouken@6614
    86
int
slouken@6614
    87
main(int argc, char *argv[])
slouken@6614
    88
{
slouken@6614
    89
    int success;
slouken@6614
    90
slouken@6614
    91
    /* Load the SDL library */
jorgen@7088
    92
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@6614
    93
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@6614
    94
        return (1);
slouken@6614
    95
    }
slouken@6614
    96
slouken@6614
    97
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
slouken@6614
    98
                "Simple MessageBox",
slouken@6614
    99
                "This is a simple error MessageBox",
slouken@6614
   100
                NULL);
slouken@6614
   101
    if (success == -1) {
slouken@6614
   102
        printf("Error Presenting MessageBox: %s\n", SDL_GetError());
slouken@6614
   103
        quit(1);
slouken@6614
   104
    }
slouken@6614
   105
icculus@6758
   106
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
icculus@6758
   107
                "Simple MessageBox",
icculus@6758
   108
                "This is a simple MessageBox with a newline:\r\nHello world!",
icculus@6758
   109
                NULL);
icculus@6758
   110
    if (success == -1) {
icculus@6758
   111
        printf("Error Presenting MessageBox: %s\n", SDL_GetError());
icculus@6758
   112
        quit(1);
icculus@6758
   113
    }
icculus@6758
   114
icculus@6722
   115
    /* Google says this is Traditional Chinese for "beef with broccoli" */
icculus@6722
   116
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
icculus@6722
   117
                "UTF-8 Simple MessageBox",
icculus@6722
   118
                "Unicode text: '牛肉西蘭花' ...",
icculus@6722
   119
                NULL);
icculus@6722
   120
    if (success == -1) {
icculus@6722
   121
        printf("Error Presenting MessageBox: %s\n", SDL_GetError());
icculus@6722
   122
        quit(1);
icculus@6722
   123
    }
icculus@6722
   124
icculus@6758
   125
    /* Google says this is Traditional Chinese for "beef with broccoli" */
icculus@6758
   126
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
icculus@6758
   127
                "UTF-8 Simple MessageBox",
icculus@6758
   128
                "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
icculus@6758
   129
                NULL);
icculus@6758
   130
    if (success == -1) {
icculus@6758
   131
        printf("Error Presenting MessageBox: %s\n", SDL_GetError());
icculus@6758
   132
        quit(1);
icculus@6758
   133
    }
icculus@6758
   134
jorgen@7088
   135
    button_messagebox(NULL);
jorgen@7088
   136
jorgen@7088
   137
    /* Technically this isn't a supported operation for the API, but it doesn't
jorgen@7088
   138
     * hurt for it to work.
jorgen@7088
   139
     */
slouken@6614
   140
    {
jorgen@7088
   141
        int status = 0;
jorgen@7088
   142
        SDL_Event event;
jorgen@7088
   143
        intptr_t eventNumber = SDL_RegisterEvents(1);
jorgen@7088
   144
        SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
slouken@6614
   145
jorgen@7088
   146
        while (SDL_WaitEvent(&event))
jorgen@7088
   147
        {
jorgen@7088
   148
            if (event.type == eventNumber) {
jorgen@7088
   149
                break;
jorgen@7088
   150
            }
jorgen@7088
   151
        }
slouken@6614
   152
jorgen@7088
   153
        SDL_WaitThread(thread, &status);
jorgen@7088
   154
jorgen@7088
   155
        printf("Message box thread return %i\n", status);
slouken@6614
   156
    }
slouken@6614
   157
slouken@6614
   158
    SDL_Quit();
slouken@6614
   159
    return (0);
slouken@6614
   160
}