3

私はクロスプラットフォームのゲームエンジンに取り組んでいます-これはうまく機能しています(私はSDLを使用しています)。ただし、SDL または OpenGL (画面にレンダリングする) に依存することなく、メッセージ ボックスをユーザーに表示する簡単な方法が必要です。たとえば、ウィンドウが破棄されているか、まだ作成されていないため、メッセージをレンダリングできないスクリーン?

プラットフォームごとに複数の実装でメッセージ ボックス関数を実装しました。Windows の実装では MessageBox を使用し、Mac OS X の実装では Cocoa の NSAlert を使用していますが、Linux の実装に何を使用できるかわかりません。X11 を考えていたのは、SDL が Linux でのウィンドウ処理に使用するものだからです。

他の回答を試してみましたが、あいまいすぎるか、ゲーム エンジン全体を X11 などでリグする必要があります。アプリケーションに依存しないソリューションを見つけようとしています (コンソール アプリケーションで使用できる Windows MessageBox 関数など)。

注: Mac と Windows の実装のコードはすべて問題なく動作しますが、Linux の実装については助けが必要です。

ああ、Mac OS XI でコンパイルするときは、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 を自分のコードに置き換える予定です)。

4

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 に配置されます)。フォルダー - それらを移動するか、リンカーにそれらの場所を伝える必要がある場合があります (インクルード ファイルはインクルード ディレクトリにあります)。

コードは次のとおりです。

例(私の機能を使用):

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;
}
于 2013-07-02T02:01:04.460 に答える
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 があることにも注意してください。

于 2013-06-26T12:15:42.763 に答える