/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 Sri Harsha Chilakapati
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.shc.silenceengine.backend.lwjgl;

import com.shc.silenceengine.backend.lwjgl.glfw.GLFW3;
import com.shc.silenceengine.backend.lwjgl.glfw.Window;
import com.shc.silenceengine.core.Game;
import com.shc.silenceengine.core.SilenceEngine;
import com.shc.silenceengine.io.FilePath;
import com.shc.silenceengine.utils.TaskManager;
import com.shc.silenceengine.utils.functional.SimpleCallback;
import org.lwjgl.system.Configuration;

/**
 * The LwjglRuntime initializes the LWJGL library, and starts the native event loop.
 *
 * @author Sri Harsha Chilakapati
 */
public final class LwjglRuntime
{
    private LwjglRuntime()
    {
    }

    public static void start(Game game)
    {
        Configuration.DEBUG.set(Game.DEVELOPMENT);

        SilenceEngine.log = new LwjglLogDevice();
        SilenceEngine.io = new LwjglIODevice();
        SilenceEngine.display = new LwjglDisplayDevice();
        SilenceEngine.input = new LwjglInputDevice();
        SilenceEngine.graphics = new LwjglGraphicsDevice();
        SilenceEngine.audio = new LwjglAudioDevice();

        // Set AWT fix on Mac OS X
        if (SilenceEngine.display.getPlatform() == SilenceEngine.Platform.MACOSX)
            System.setProperty("java.awt.headless", "true");

        Window window = ((LwjglDisplayDevice) SilenceEngine.display).window;

        final SimpleCallback[] performLoopFrame = {
                () ->
                {
                    // Assume 100 seconds so that tasks are force flushed
                    TaskManager.forceUpdateTasks(100);
                    TaskManager.forceRenderTasks(100);
                }
        };

        // Initiate setting the icon
        SilenceEngine.display.setIcon(FilePath.getResourceFile("engine_resources/icon.png"), () ->
        {
            // Initialize SilenceEngine
            SilenceEngine.init();

            // Call the game's init method
            game.init();

            // Replace the callback so that we now perform the game loop instead of flushing
            // tasks in TaskManager.
            performLoopFrame[0] = SilenceEngine.gameLoop::performLoopFrame;

            // Raise a resize event now
            SilenceEngine.eventManager.raiseResizeEvent();
        });

        // The native event loop
        while (!window.shouldClose())
        {
            GLFW3.pollEvents();
            LwjglInputDevice.pollControllers();

            performLoopFrame[0].invoke();
            window.swapBuffers();
        }

        // Raise the dispose event finally
        SilenceEngine.eventManager.raiseDisposeEvent();
    }
}

@@ -2782,16 +2782,193 @@
     %7D;%0A%0A
+        final boolean%5B%5D gameDone = %7B false %7D;%0A%0A        // Stop events from happening until game is initialized%0A        SilenceEngine.eventManager.waitUntil(() -%3E gameDone%5B0%5D);%0A%0A
         
@@ -3239,32 +3239,64 @@
     game.init();
+%0A            gameDone%5B0%5D = true;
 %0A%0A            //

Stop events from being fired until game is initialized in LWJGL backend