Ejemplo n.º 1
0
int SDL_MAIN_FUNC(int argc, char *argv[])
{
    CLogger logger; // single instance of logger

    // Workaround for character encoding in argv on Windows
    #if PLATFORM_WINDOWS
    int wargc = 0;
    wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);
    if (wargv == nullptr)
    {
        logger.Error("CommandLineToArgvW failed\n");
        return 1;
    }

    std::vector<std::vector<char>> windowsArgs;
    for (int i = 0; i < wargc; i++)
    {
        std::wstring warg = wargv[i];
        std::string arg = CSystemUtilsWindows::UTF8_Encode(warg);
        std::vector<char> argVec(arg.begin(), arg.end());
        argVec.push_back('\0');
        windowsArgs.push_back(std::move(argVec));
    }

    auto windowsArgvPtrs = MakeUniqueArray<char*>(wargc);
    for (int i = 0; i < wargc; i++)
        windowsArgvPtrs[i] = windowsArgs[i].data();

    argv = windowsArgvPtrs.get();

    LocalFree(wargv);
    #endif

    logger.Info("%s starting\n", COLOBOT_FULLNAME);

    auto systemUtils = CSystemUtils::Create(); // platform-specific utils
    systemUtils->Init();

    CSignalHandlers::Init(systemUtils.get());

    CResourceManager manager(argv[0]);

    // Initialize static string arrays
    InitializeRestext();
    InitializeEventTypeTexts();

    int code = 0;
    CApplication app(systemUtils.get()); // single instance of the application

    ParseArgsStatus status = app.ParseArguments(argc, argv);
    if (status == PARSE_ARGS_FAIL)
    {
        systemUtils->SystemDialog(SDT_ERROR, "COLOBOT - Fatal Error", "Invalid commandline arguments!\n");
        return app.GetExitCode();
    }
    else if (status == PARSE_ARGS_HELP)
    {
        return app.GetExitCode();
    }

    if (! app.Create())
    {
        code = app.GetExitCode();
        if (code != 0 && !app.GetErrorMessage().empty())
        {
            systemUtils->SystemDialog(SDT_ERROR, "COLOBOT - Fatal Error", app.GetErrorMessage());
        }
        logger.Info("Didn't run main loop. Exiting with code %d\n", code);
        return code;
    }

    code = app.Run();

    logger.Info("Exiting with code %d\n", code);

    return code;
}
Ejemplo n.º 2
0
int SDL_MAIN_FUNC(int argc, char *argv[])
{
    CLogger logger; // single istance of logger

    // Workaround for character encoding in argv on Windows
    #if PLATFORM_WINDOWS
    int wargc;
    wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);
    if(wargv == nullptr)
    {
        logger.Error("CommandLineToArgvW failed\n");
        return 1;
    }
    argv = new char*[wargc];
    for(int i = 0; i < wargc; i++) {
        std::wstring warg = wargv[i];
        std::string arg = CSystemUtilsWindows::UTF8_Encode(warg);
        argv[i] = new char[arg.length()+1];
        strcpy(argv[i], arg.c_str());
    }
    LocalFree(wargv);
    #endif

    CResourceManager manager(argv[0]);

    // Initialize static string arrays
    InitializeRestext();
    InitializeEventTypeTexts();

    logger.Info("%s starting\n", COLOBOT_FULLNAME);
    
    int code = 0;
    while(true) {
        CSystemUtils* systemUtils = CSystemUtils::Create(); // platform-specific utils
        systemUtils->Init();
        
        CApplication* app = new CApplication(); // single instance of the application

        ParseArgsStatus status = app->ParseArguments(argc, argv);
        if (status == PARSE_ARGS_FAIL)
        {
            systemUtils->SystemDialog(SDT_ERROR, "COLOBOT - Fatal Error", "Invalid commandline arguments!\n");
            return app->GetExitCode();
        }
        else if (status == PARSE_ARGS_HELP)
        {
            return app->GetExitCode();
        }


        if (! app->Create())
        {
            app->Destroy(); // ensure a clean exit
            code = app->GetExitCode();
            if ( code != 0 && !app->GetErrorMessage().empty() )
            {
                systemUtils->SystemDialog(SDT_ERROR, "COLOBOT - Fatal Error", app->GetErrorMessage());
            }
            logger.Info("Didn't run main loop. Exiting with code %d\n", code);
            return code;
        }

        code = app->Run();
        bool restarting = app->IsRestarting();

        delete app;
        delete systemUtils;
        if(!restarting) break;
    }

    logger.Info("Exiting with code %d\n", code);

    #if PLATFORM_WINDOWS
    // See the workaround above
    delete[] argv;
    #endif

    return code;
}