StudioCherno / Walnut

Walnut is a simple application framework for Vulkan and Dear ImGui apps
MIT License
1.99k stars 367 forks source link

Is the create and delete of the app intentional in EntryPoint.h? #73

Open glenpierce opened 5 months ago

glenpierce commented 5 months ago

In EntryPoint.h,

https://github.com/StudioCherno/Walnut/blob/3b8e414fdecfc6c8b58816106fe8d912bd172e31/Walnut/src/Walnut/EntryPoint.h#L10-L20

This code appears to have a logical error, as it creates and deletes a new Walnut::Application object in each iteration of the while loop. This means that the application is constantly being restarted and terminated, which is probably not the intended behavior. A more reasonable approach would be to create the application object once before the loop, and delete it once after the loop, like this:

namespace Walnut {

    int Main(int argc, char** argv)
    {
        Walnut::Application* app = Walnut::CreateApplication(argc, argv);

        while (g_ApplicationRunning)
        {
            app->Run();
        }

            delete app;

            return 0;
    }

}

Please correct me if I'm mistaken here, Or maybe there's a good reason for it, I'm very new to this project.

glenpierce commented 5 months ago

Update: I got courageous and tried it. My solution is... not good. It causes the app to crash when I try to exit/close the window.

If anyone has the time to explain why this loop is doing this, I would be quite thankful. This is a bit confusing to me.

learn-more commented 4 months ago

g_ApplicationRunning is set to false when exiting the window. An educated guess would be that this loop is here to facilitate easy application restarts, when e.g. changing to fullscreen or whatnot.

prodskimaya commented 4 months ago

It's because the while loop is "paused" while the run() function is being called. When the run() function is completed, then the app is deleted.