cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

testmessage.c (5487B)


      1/*
      2  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
      3
      4  This software is provided 'as-is', without any express or implied
      5  warranty.  In no event will the authors be held liable for any damages
      6  arising from the use of this software.
      7
      8  Permission is granted to anyone to use this software for any purpose,
      9  including commercial applications, and to alter it and redistribute it
     10  freely.
     11*/
     12
     13/* Simple test of the SDL MessageBox API */
     14
     15#include <stdio.h>
     16#include <stdlib.h>
     17#include <signal.h>
     18
     19#include "SDL.h"
     20#include "SDL_thread.h"
     21
     22/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     23static void
     24quit(int rc)
     25{
     26    SDL_Quit();
     27    exit(rc);
     28}
     29
     30static int
     31button_messagebox(void *eventNumber)
     32{
     33    const SDL_MessageBoxButtonData buttons[] = {
     34        {
     35            SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
     36            0,
     37            "OK"
     38        },{
     39            SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
     40            1,
     41            "Cancel"
     42        },
     43    };
     44
     45    SDL_MessageBoxData data = {
     46        SDL_MESSAGEBOX_INFORMATION,
     47        NULL, /* no parent window */
     48        "Custom MessageBox",
     49        "This is a custom messagebox",
     50        2,
     51        buttons,
     52        NULL /* Default color scheme */
     53    };
     54
     55    int button = -1;
     56    int success = 0;
     57    if (eventNumber) {
     58        data.message = "This is a custom messagebox from a background thread.";
     59    }
     60
     61    success = SDL_ShowMessageBox(&data, &button);
     62    if (success == -1) {
     63        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
     64        if (eventNumber) {
     65            SDL_UserEvent event;
     66            event.type = (intptr_t)eventNumber;
     67            SDL_PushEvent((SDL_Event*)&event);
     68            return 1;
     69        } else {
     70            quit(2);
     71        }
     72    }
     73    SDL_Log("Pressed button: %d, %s\n", button, button == 1 ? "Cancel" : "OK");
     74
     75    if (eventNumber) {
     76        SDL_UserEvent event;
     77        event.type = (intptr_t)eventNumber;
     78        SDL_PushEvent((SDL_Event*)&event);
     79    }
     80
     81    return 0;
     82}
     83
     84int
     85main(int argc, char *argv[])
     86{
     87    int success;
     88
     89	/* Enable standard application logging */
     90    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
     91
     92    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
     93                "Simple MessageBox",
     94                "This is a simple error MessageBox",
     95                NULL);
     96    if (success == -1) {
     97        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
     98        quit(1);
     99    }
    100
    101    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
    102                "Simple MessageBox",
    103                "This is a simple MessageBox with a newline:\r\nHello world!",
    104                NULL);
    105    if (success == -1) {
    106        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
    107        quit(1);
    108    }
    109
    110    /* Google says this is Traditional Chinese for "beef with broccoli" */
    111    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
    112                "UTF-8 Simple MessageBox",
    113                "Unicode text: '牛肉西蘭花' ...",
    114                NULL);
    115    if (success == -1) {
    116        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
    117        quit(1);
    118    }
    119
    120    /* Google says this is Traditional Chinese for "beef with broccoli" */
    121    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
    122                "UTF-8 Simple MessageBox",
    123                "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
    124                NULL);
    125    if (success == -1) {
    126        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
    127        quit(1);
    128    }
    129
    130    button_messagebox(NULL);
    131
    132    /* Test showing a message box from a background thread.
    133
    134       On Mac OS X, the video subsystem needs to be initialized for this
    135       to work, since the message box events are dispatched by the Cocoa
    136       subsystem on the main thread.
    137     */
    138    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    139        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s\n", SDL_GetError());
    140        return (1);
    141    }
    142    {
    143        int status = 0;
    144        SDL_Event event;
    145        intptr_t eventNumber = SDL_RegisterEvents(1);
    146        SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
    147
    148        while (SDL_WaitEvent(&event))
    149        {
    150            if (event.type == eventNumber) {
    151                break;
    152            }
    153        }
    154
    155        SDL_WaitThread(thread, &status);
    156
    157        SDL_Log("Message box thread return %i\n", status);
    158    }
    159
    160    /* Test showing a message box with a parent window */
    161    {
    162        SDL_Event event;
    163        SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
    164
    165        success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
    166                    "Simple MessageBox",
    167                    "This is a simple error MessageBox with a parent window",
    168                    window);
    169        if (success == -1) {
    170            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
    171            quit(1);
    172        }
    173
    174        while (SDL_WaitEvent(&event))
    175        {
    176            if (event.type == SDL_QUIT || event.type == SDL_KEYUP) {
    177                break;
    178            }
    179        }
    180    }
    181
    182    SDL_Quit();
    183    return (0);
    184}