我正在开发跨平台游戏引擎 - 它运行良好(我使用的是 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 实现的代码:
消息框.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
消息框.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)。
最佳答案
我创建了一个简单的包装函数,它使用 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 文件夹 - 您可能需要移动它们或告诉您的链接器它们在哪里(包含文件在包含目录中)。
代码如下:
示例(使用我的函数):
int i = showMessageBox(mySDLWindow, "Message", "Title", 3, MB_BUTTONS("BUTTON 1", "BUTTON 2", "BUTTON 3"), 0);
printf("Button %i was pressed", i + 1);
消息框.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);
消息框.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;
}
关于c++ - 适用于 Linux 的 SDL 跨平台消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17319180/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
当我使用has_one时,它工作得很好,但在has_many上却不行。在这里您可以看到object_id不同,因为它运行了另一个SQL来再次获取它。ruby-1.9.2-p290:001>e=Employee.create(name:'rafael',active:false)ruby-1.9.2-p290:002>b=Badge.create(number:1,employee:e)ruby-1.9.2-p290:003>a=Address.create(street:"123MarketSt",city:"SanDiego",employee:e)ruby-1.9.2-p290
我的工作要求我为某些测试自动生成电子邮件。我一直在四处寻找,但未能找到可以快速实现的合理解决方案。它需要在outlook而不是其他邮件服务器中,因为我们有一些奇怪的身份验证规则,我们需要保存草稿而不是仅仅发送邮件的选项。显然win32ole可以做到这一点,但我找不到任何相当简单的例子。 最佳答案 假设存储了Outlook凭据并且您设置为自动登录到Outlook,WIN32OLE可以很好地完成此操作:require'win32ole'outlook=WIN32OLE.new('Outlook.Application')message=
我正在使用Ruby,我正在与一个网络端点通信,该端点在发送消息本身之前需要格式化“header”。header中的第一个字段必须是消息长度,它被定义为网络字节顺序中的2二进制字节消息长度。比如我的消息长度是1024。如何将1024表示为二进制双字节? 最佳答案 Ruby(以及Perl和Python等)中字节整理的标准工具是pack和unpack。ruby的packisinArray.您的长度应该是两个字节长,并且按网络字节顺序排列,这听起来像是n格式说明符的工作:n|Integer|16-bitunsigned,network(bi
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我