SDL跨平台Linux消息框

4
我正在开发一个跨平台的游戏引擎 - 使用SDL非常好。但我想要一种简单的方法来向用户显示消息框,而不必依赖于SDL或OpenGL(用于渲染屏幕),例如:如果窗口被销毁或尚未创建,那么我无法将消息呈现到屏幕上怎么办?
我已经实现了一个消息框函数,并为每个平台实现了多个版本:Windows实现使用MessageBox,Mac OS X实现使用Cocoa的NSAlert,对于Linux实现,我不知道可以使用什么。我在考虑X11,因为这是SDL在Linux上使用的窗口管理器。
我尝试了其他答案,但它们要么太模糊,要么需要我重新调整整个游戏引擎,使用X11之类的东西。我正在寻找一种独立于应用程序的解决方案(如Windows MessageBox函数,可用于控制台应用程序)。
注意:所有Mac和Windows实现的代码都正常工作,只需要帮助我完成Linux实现。
哦,当我在Mac OS X上编译时,我利用Objective-C ++,这样我就可以将Cocoa(Objective-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

编辑:出于几个原因,我不想使用Qt:它太重了,它不能在我的主要电脑上工作,而且它不能给我足够的程序控制权。无论如何,我正在尝试从头开始制作这个游戏引擎作为一个爱好项目,而不依赖于其他库(我最终将用自己的代码替换SDL)。


1
我将瞄准超出Qt支持的更多平台。此外,Qt对我来说太重了,而且在诸如消息循环等方面没有足够的控制权。- 我已经尝试过Qt了,但它无法在我的主要开发计算机上运行。 - PersonWithName
实际问题是什么?在Linux上显示消息框有几种可能性,例如使用Xlib或OpenMotif,而不需要臃肿的库。 - scai
1
有比Qt更多的工具包,甚至有些非常轻量级。但是如果您不想使用跨平台工具包,则必须为每个平台自己编写本机代码,这可能需要很多工作。最肯定的是,您无法将它们全部捆绑在单个#else部分中。首先,您应该研究Linux和BSD的X窗口系统 - Some programmer dude
接下来在我的议程中:研究X窗口系统。哦,X窗口系统和X11是不同的东西吗? - PersonWithName
@JoachimPileborg:你应该把它写成答案。 - legends2k
显示剩余2条评论
2个回答

2
我创建了一个简单的包装函数,使用SDL 2.0的SDL_ShowMessageBox,并替换了我之前提交的代码,它可以在Linux、Mac和Windows上运行。
SDL 2.0可以在以下网址找到(http://www.libsdl.org/tmp/download-2.0.php)。
你必须在Linux上自己构建SDL 2 - 只需在提供的页面中下载源代码,然后解压缩存档并按照INSTALL.txt中的安装说明进行操作(构建SDL 2后,库被放置在/usr/local/lib文件夹中,您可能需要移动它们或告诉链接器它们在哪里(包含文件在include目录中)。
以下是代码:
示例(使用我的函数):
int i = showMessageBox(mySDLWindow, "Message", "Title", 3, MB_BUTTONS("BUTTON 1", "BUTTON 2", "BUTTON 3"), 0);
 printf("Button %i was pressed", i + 1);

messagebox.h:

//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);

messagebox.cpp:

//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;
}

你可能需要移动它们或告诉链接器它们的位置......如果您使用Debian-ish发行版,则/usr/local应该在需要pkg-config来收集SDL2信息文件的各个PATH中,sudo ldconfig通常也不会有影响。 - genpfault
是的,我使用了ldconfig将usr/local/lib添加到路径中。 - PersonWithName

1

我正在使用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
}

这不是世界上最强大的代码,但至少在我们的游戏中,我从未见过它失败。从代码角度来看,它没有依赖关系,但是您必须确保不要在字符串中使用会破坏命令行的字符,例如转义字符<,>,&,!\,以及所有非ASCII字符。
请注意,SDL 2.0具有SDL_ShowMessageBox功能。

这取决于是否安装了gdialogkdialog。还有xmessage,它可能在更多的系统上可用,但不太花哨。 - scai
我不知道SDL 2.0已经发布了。我在哪里可以获取.lib和头文件?(我正在使用GCC / MinGW)。 - PersonWithName

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接