C++ 用于Linux的SDL跨平台消息框

C++ 用于Linux的SDL跨平台消息框,c++,linux,windows,macos,sdl,C++,Linux,Windows,Macos,Sdl,我正在开发一个跨平台的游戏引擎,它工作得很好(我正在使用SDL)。但是,我需要一种简单的方式向用户显示消息框,而不必依赖SDL或OpenGL(渲染到屏幕上),例如,如果窗口被破坏或尚未创建,那么我无法将消息渲染到屏幕上会怎么样 我已经为每个平台实现了一个消息框函数,其中包含多个实现:Windows实现使用MessageBox,Mac OS X实现使用Cocoa的NSAlert,我不知道linux实现可以使用什么。我之所以考虑X11,是因为SDL在linux上使用X11进行窗口操作 我尝试过其他答

我正在开发一个跨平台的游戏引擎,它工作得很好(我正在使用SDL)。但是,我需要一种简单的方式向用户显示消息框,而不必依赖SDL或OpenGL(渲染到屏幕上),例如,如果窗口被破坏或尚未创建,那么我无法将消息渲染到屏幕上会怎么样

我已经为每个平台实现了一个消息框函数,其中包含多个实现:Windows实现使用MessageBox,Mac OS X实现使用Cocoa的NSAlert,我不知道linux实现可以使用什么。我之所以考虑X11,是因为SDL在linux上使用X11进行窗口操作

我尝试过其他答案,但它们要么太模糊,要么要求我用X11或其他东西重新装备我的整个游戏引擎。我试图找到一个独立于应用程序的解决方案(比如可以在控制台应用程序中使用的windows MessageBox函数)

注意:Mac和Windows实现的所有代码都运行良好,这只是我需要帮助的Linux实现

哦,当我在Mac OS X上编译时,我利用ObjuleC++,所以我可以将COCOA(Objtovi-C)与我的C++ MSGBOX()函数混合。 以下是迄今为止我为Windows&Mac实现提供的代码:

msgbox.h

#ifndef MSGBOX_H
#define MSGBOX_H

//Cross-platform message box method.
#include "platform.h"
#include "string.h"

//This is my own cross platform enum for message boxes.
//This enumeration 'overlaps' with some declarations in windows.h but that is fine.
enum    //Message box values.
{
    MB_OK,  //For OK message box and return value.
    MB_OKCANCEL,
    MB_YESNO,
    MB_RETRYCANCEL,
    MB_YESNOCANCEL,
    MB_ABORTRETRYIGNORE,
    MB_CANCELTRYCONTINUE,
    MB_CANCEL,
    MB_YES,
    MB_NO,
    MB_RETRY,
    MB_IGNORE,
    MB_TRYAGAIN,
    MB_CONTINUE,
    MB_ABORT,
};

//The message box function (multiple implementations for each platform).
int msgbox(string msg, string title, int buttons);

#endif // MSGBOX_H
msgbox.cpp

#include "msgbox.h"

#if CURRENT_PLATFORM == PLATFORM_WINDOWS    //We can use the windows API for our messagebox.

#include <windows.h>    //For the message box function.
#define IDTRYAGAIN 10   //Some fixes to help this application compile.
#define IDCONTINUE 11

int msgbox(string msg, string title, int buttons)
{
    //Display the mesagebox.
    int retval = MessageBox(NULL, msg.c_str(), title.c_str(), buttons | MB_ICONEXCLAMATION | MB_SYSTEMMODAL);

    //Map the windows return value to ours.
    switch(retval)
    {
    case IDOK:      return MB_OK;
    case IDCANCEL:  return MB_CANCEL;
    case IDYES:     return MB_YES;
    case IDNO:      return MB_NO;
    case IDRETRY:   return MB_RETRY;
    case IDIGNORE:  return MB_IGNORE;
    case IDTRYAGAIN:return MB_TRYAGAIN;
    case IDCONTINUE:return MB_CONTINUE;
    }
}

#elif CURRENT_PLATFORM == PLATFORM_MACOSX   //Use Cocoa to display the message box.

int msgbox(string msg, string title, int buttons)
{
    NSString* defbutton = nil;
    NSString* altbutton = nil;
    NSString* otherbutton = nil;

    switch(buttons)
    {
    default:
    case MB_OK:
        defbutton = @"Ok";
        break;

    case MB_OKCANCEL:
        defbutton = @"Ok";
        altbutton = @"Cancel";
        break;

    case MB_RETRYCANCEL:
        defbutton = @"Retry";
        altbutton = @"Cancel";
        break;

    case MB_YESNO:
        defbutton = @"Yes";
        altbutton = @"No";
        break;

    case MB_YESNOCANCEL:
        defbutton = @"Yes";
        altbutton = @"No";
        otherbutton = @"Cancel";
        break;

    case MB_ABORTRETRYIGNORE:
        defbutton = @"Abort";
        altbutton = @"Retry";
        otherbutton = @"Ignore";
        break;

    case MB_CANCELTRYCONTINUE:
        defbutton = @"Cancel";
        altbutton = @"Try Again";
        otherbutton = @"Continue";
        break;
    }

    NSAlert* alert = [NSAlert alertWithMessageText:[NSString     stringWithCString:title.c_str() encoding:[NSString defaultCStringEncoding]]
                                 defaultButton:defbutton
                               alternateButton:altbutton
                                   otherButton:otherbutton
                     informativeTextWithFormat:@"%s", msg.c_str()];

    //brings this 'application' to the front.
    [[NSRunningApplication currentApplication]     activateWithOptions:NSApplicationActivateIgnoringOtherApps];
    NSInteger retval = [alert runModal];

    //Convert the NSAlert return values into my MB_* return values.
    if(retval == NSAlertDefaultReturn)
    {
        switch(buttons)
        {
        case MB_OK:
        case MB_OKCANCEL:
            return MB_OK;

        case MB_YESNO:
        case MB_YESNOCANCEL:
            return MB_YES;

        case MB_ABORTRETRYIGNORE:
            return MB_ABORT;

        case MB_CANCELTRYCONTINUE:
            return MB_CANCEL;

        case MB_RETRYCANCEL:
            return MB_RETRY;
        }
    } else if(retval == NSAlertAlternateReturn)
    {
        switch(buttons)
        {
        case MB_OKCANCEL:
        case MB_RETRYCANCEL:
            return MB_CANCEL;

        case MB_YESNO:
        case MB_YESNOCANCEL:
            return MB_NO;

        case MB_ABORTRETRYIGNORE:
            return MB_RETRY;

        case MB_CANCELTRYCONTINUE:
            return MB_TRYAGAIN;
        }
    } else if(retval == NSAlertOtherReturn)
    {
        switch(buttons)
        {
        case MB_YESNOCANCEL:
            return MB_CANCEL;

        case MB_ABORTRETRYIGNORE:
            return MB_IGNORE;

        case MB_CANCELTRYCONTINUE:
            return MB_CONTINUE;
        }
    }

    return NULL;
}

#else

int msgbox(string msg, string title, int buttons)
{
    //WHAT DO I DO??????
    return 0;
}

//#error No implementation of message boxes on current platform!
#endif // CURRENT_PLATFORM
#包括“msgbox.h”
#如果当前_PLATFORM==PLATFORM_WINDOWS//我们可以将WINDOWS API用于messagebox。
#包含//用于消息框功能。
#定义IDTRYAGAIN 10//一些修复程序以帮助此应用程序编译。
#定义ID11
int msgbox(字符串消息、字符串标题、int按钮)
{
//显示mesagebox。
int retval=MessageBox(NULL,msg.c_str(),title.c_str(),按钮| MB|iconnexClation | MB|SYSTEMMODAL);
//将windows返回值映射到我们的。
开关(返回)
{
案例IDOK:返回MB_OK;
case IDCANCEL:返回MB_CANCEL;
案例:返回mbu YES;
案例编号:返回MB_编号;
案例IDRETRY:返回MB_重试;
案例IDIGNORE:返回MB_IGNORE;
案例IDTRYAGAIN:返回MB_TRYAGAIN;
case IDCONTINUE:返回MB_CONTINUE;
}
}
#elif CURRENT_PLATFORM==PLATFORM_MACOSX//使用Cocoa显示消息框。
int msgbox(字符串消息、字符串标题、int按钮)
{
NSString*defbutton=nil;
NSString*altbutton=nil;
NSString*otherbutton=nil;
开关(按钮)
{
违约:
案例MB_OK:
defbutton=@“确定”;
打破
案例MB_Oku取消:
defbutton=@“确定”;
altbutton=@“取消”;
打破
案例MB_RETRYCANCEL:
defbutton=@“重试”;
altbutton=@“取消”;
打破
案例MB_YESNO:
defbutton=@“是”;
altbutton=@“否”;
打破
案例MB_是否取消:
defbutton=@“是”;
altbutton=@“否”;
otherbutton=@“取消”;
打破
案例MB_ABORTRETRYIGNORE:
defbutton=@“中止”;
altbutton=@“重试”;
otherbutton=@“忽略”;
打破
案例MB_取消继续:
defbutton=@“取消”;
altbutton=@“重试”;
otherbutton=@“继续”;
打破
}
NSAlert*alert=[NSAlert alertWithMessageText:[NSString stringWithCString:title.c_str()编码:[NSString defaultCStringEncoding]]
defaultButton:defbutton
备用按钮:备用按钮
otherButton:otherButton
informativeTextWithFormat:@“%s”,msg.c_str();
//将此“应用程序”带到前端。
[[NSRunningApplication currentApplication]激活选项:非应用程序激活信号或其他应用程序];
NSInteger retval=[alert runModal];
//将NSAlert返回值转换为my MB_*返回值。
if(retval==NSAlertDefaultReturn)
{
开关(按钮)
{
案例MB_OK:
案例MB_Oku取消:
返回MB_OK;
案例MB_YESNO:
案例MB_是否取消:
返回MB_YES;
案例MB_ABORTRETRYIGNORE:
返回mbu中止;
案例MB_取消继续:
返回mbu取消;
案例MB_RETRYCANCEL:
返回mbu重试;
}
}否则如果(retval==nsalerterneturn)
{
开关(按钮)
{
案例MB_Oku取消:
案例MB_RETRYCANCEL:
返回mbu取消;
案例MB_YESNO:
案例MB_是否取消:
返回MB_编号;
案例MB_ABORTRETRYIGNORE:
返回mbu重试;
案例MB_取消继续:
返回MB_TRYAGAIN;
}
}else if(retval==NSAlertOtherReturn)
{
开关(按钮)
{
案例MB_是否取消:
返回mbu取消;
案例MB_ABORTRETRYIGNORE:
返回mbu忽略;
案例MB_取消继续:
返回mbu继续;
}
}
返回NULL;
}
#否则
int msgbox(字符串消息、字符串标题、int按钮)
{
//我该怎么办??????
返回0;
}
//#错误:当前平台上未实现消息框!
#endif//CURRENT\u平台

编辑:我不喜欢使用Qt有几个原因:它太重,不能在我的主计算机上工作&它不能给我足够的控制程序。无论如何,我正在尝试将这个游戏引擎作为一个业余项目,而不依赖于其他库(我最终将用我自己的代码替换SDL)。

我正在使用gdialog/kdialog,并在命令行上传递消息。代码如下:

#include <cstdlib>
#include <string>

const char * getDialogCommand() {
  if (::system(NULL)) {
    if (::system("which gdialog") == 0)
      return "gdialog";
    else if (::system("which kdialog") == 0)
      return "kdialog";
  }
  return NULL;
}

void showWarning(const std::string & warning) {
  const char * dialogCommand = getDialogCommand();
  if (dialogCommand) {
    std::string command = dialogCommand;
    command += " --title \"Message Box Title\" --msgbox \"" + warning + "\"";
    int result = ::system(command.c_str());
    if (result == 0)
      return; // success
  }

  // fail-safe method here, using stdio perhaps, depends on your application
}
#包括
#包括
const char*getDialogCommand(){
如果(::系统(空)){
if(::system(“哪个gdialog”)==0)
返回“gdialog”;
else if(::系统(“哪个kdialog”)==0)
返回“kdialog”;
}
返回NULL;
}
void showWarning(const std::string和warning){
const char*dialogCommand=getDialogCommand();
如果(对话框命令){
int i = showMessageBox(mySDLWindow, "Message", "Title", 3, MB_BUTTONS("BUTTON 1", "BUTTON 2", "BUTTON 3"), 0);
 printf("Button %i was pressed", i + 1);
//Cross-platform message box method.
#include <string>
#include <SDL/SDL.h> //SDL 2.0 header file

//Helper macro
#define MB_BUTTONS(...) ((char*[]) {__VA_ARGS__})

//Flexible message box function.
//Returns the index of button pressed on success or a negative value on a failure.
//The parent argument can be set to NULL if not available.
int showMessageBox(SDL_Window *parent, std::string msg, std::string title,
                     int count, char* buttons[], int defbutton = 0);
//Complex function
int showMessageBox(SDL_Window *parent, string msg, string title,
                     int count, char* buttons[], int defbutton)
{
    //Variables.
    int resultButton = 0;
    SDL_MessageBoxData mbdata;

    //Set the message box information.
    mbdata.flags = SDL_MESSAGEBOX_INFORMATION;
    mbdata.message = msg.c_str();
    mbdata.title = title.c_str();
    mbdata.colorScheme = NULL;
    mbdata.window = parent;
    mbdata.numbuttons = count;

    //Allocate buttons.
    SDL_MessageBoxButtonData *butarray = new SDL_MessageBoxButtonData[mbdata.numbuttons];

    //Set the button values.
    for(unsigned char i = 0; i < mbdata.numbuttons; i++)
    {
        //Is this button the default button?
        if(i == defbutton)
        {
            butarray[i].flags = SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
        } else
        {
            butarray[i].flags = 0;
        }

        //Set button text.
        if(buttons[i] != NULL)
        {
            butarray[i].text = buttons[i];
        }

        //Set button ID.
        butarray[i].buttonid = i;
    }

    //Set the message box data's button array.
    mbdata.buttons = butarray;

    //Display the message box.
    int retval = SDL_ShowMessageBox(&mbdata, &resultButton);

    //Deallocate the buttons array to prevent memory leaks.
    delete[] butarray;        

    //Return the result (-1 on failure or if the dialog was closed).
    return retval < 0 ? -1 : resultButton;
}