我正在开发一个跨平台的游戏引擎 - 使用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
我已经实现了一个消息框函数,并为每个平台实现了多个版本: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)。
#else
部分中。首先,您应该研究Linux和BSD的X窗口系统。 - Some programmer dude