C++ - Restarting a game by calling the main() function

╄→гoц情女王★ 提交于 2019-12-07 13:53:24

问题


I'm building a small game. One of the input options is to restart the game. The only way I could think of doing this was to call the main function from within the main function

int main(int argc, char argv[]) {
 ...
 if (input == "restart") {
  main(argc, argv);
 }

Is this bad form? Will it even work?


回答1:


You can't call main() recursively. That's actually undefined behavior.

Use a loop instead:

int main() {
     bool restart = false;
     do {
         // Do stuff ...

         // Set restart according some condition inside of the loop
         if(condition == true) {
             restart = true;
         } // (or simplyfied restart = condtion;)
     } while(restart);
}



回答2:


No, the C++ standard disallows calling main manually.

To cite the standard (C++11: 3.6.1 Main Function)

The function main shall not be used within a program. The linkage (3.5) of main is implementation-defined. A program that defines main as deleted or that declares main to be inline, static, or constexpr is ill- formed. The name main is not otherwise reserved.




回答3:


Do not do this. From http://en.cppreference.com/w/cpp/language/main_function

The main function has several special properties:

1) It cannot be used anywhere in the program

a) in particular, it cannot be called recursively




回答4:


Since recursively calling main is impossible in C++ and would not really solve the problem, here's my 2 cents on how to deal with the problem:

Basically, any large program is a loop that might look like this:

int main()
{
    bool quit = false;

    //Initialise and aquire resources...
    while (!quit)
    {
        //Run game and set quit if user wants to quit...
    }
    //free resources, should be automatic when RAII is adhered.
}

Your game should already look something like this, since any program that is not a loop will immidiately quit and won't be much of a game. Just change the structure to this:

int main()
{
    bool quit = false;
    bool restart = false;

    while (!quit)
    {   
        Restart = false;
        //Initialise and aquire resources...
        while (!quit && !restart)
        {
            //Run game and update quit and restart according to user input.            
        }
        //free resources, should be automatic when RAII is adhered.
    }
}



回答5:


you can use GOTO but this is not a good way of programming in general. As the guys mentioned to use booleans or loops to check the current state or any other way instead of goto because it causes sometimes problems in the compiler. However it is still available in C and not C++ (AFAIK)




回答6:


If in addition to reloading internal resources, you also need to reload external things like a libraries that the game links to you can do this by re-launching the game in a thread, detaching the thread and then shutting down.

I use this in a game I've made where I have automatic updates, to start the new updated executable and libraries.

int main() {

    //initialize the game

    bool restart=false, quit=false;

    while (!quit) {
        //Main loop of the game
    }

    if (restart) {
        #ifdef _WIN32
            std::thread relaunch([](){ system("start SpeedBlocks.exe"); });
        #elif __APPLE__
            std::thread relaunch([](){
                std::string cmd = "open " + resourcePath() + "../../../SpeedBlocks.app";
                system(cmd.c_str());
            });
        #else
            std::thread relaunch([](){ system("./SpeedBlocks"); });
        #endif
        relaunch.detach();
    }

    return 0;
}

A bit of a hack, but it gets the job done. The #ifdefs just make it use the correct launch cmd for Windows/Max/Linux.



来源:https://stackoverflow.com/questions/36363704/c-restarting-a-game-by-calling-the-main-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!