<commit_before>#include <ege/engine.hxx>
#include <ege/exception.hxx>
#include <ege/graphic/gpu/context.hxx>
#include <ege/util/log/logger.hxx>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <atomic>


using namespace ege;


engine::Resources* ege::engine::resources;


namespace global
{
        static std::atomic_bool started( false );
        static engine::Configurations configurations;
        static hardware::Keyboard* keyboard;
        static hardware::Monitor* monitor;
        static util::fps::Analyzer* fpsAnalyzer;
        static util::fps::Moderator* fpsModerator;
        static util::log::Logger logger;
        static bool restartRequired;
}


static void initializeStaticMembers()
{
        ege::engine::resources = nullptr;
        global::configurations.createInitialScene = [] () { return nullptr; };
        global::keyboard = nullptr;
        global::monitor = nullptr;
        global::fpsAnalyzer = nullptr;
        global::fpsModerator = nullptr;
        global::logger.setLevel( util::log::Level::INFO );
        global::restartRequired = false;
}


engine::Resources::Resources():
        keyboard( global::keyboard ),
        monitor( global::monitor ),
        fpsAnalyzer( global::fpsAnalyzer ),
        fpsModerator( global::fpsModerator ),
        logger( &global::logger )
{

}


void engine::start( const std::function< void( engine::Configurations& ) > &configure )
{
        try
        {
                if ( global::started.exchange( true ) )
                        Exception::throwNew( "an attempt to start engine was done, but engine is already started" );

                while ( true )
                {
                        global::logger.log( util::log::Level::INFO, "engine started" );
                        initializeStaticMembers();
                        configure( global::configurations );
                        Engine* engine = new Engine();
                        engine->start();
                        delete engine;

                        if ( global::restartRequired )
                        {
                                global::logger.log( util::log::Level::INFO, "engine restarting" );
                                continue;
                        }

                        global::logger.log( util::log::Level::INFO, "engine stopped" );
                        break;
                }

                global::started = false;
        }
        catch ( ege::Exception* e )
        {
                e->consume();
        }
}


Engine::Engine()
{
        glfwInit();
        global::monitor = new hardware::Monitor( glfwGetPrimaryMonitor() );
        global::fpsAnalyzer = new util::fps::Analyzer();
        global::fpsModerator = new util::fps::Moderator( *global::fpsAnalyzer, 60 );
        global::keyboard = new hardware::Keyboard();
        ege::engine::resources = new engine::Resources();
}


Engine::~Engine()
{
        delete ege::engine::resources;
        delete global::fpsModerator;
        delete global::fpsAnalyzer;
        delete global::monitor;
        delete global::keyboard;
        glfwTerminate();

        GLenum glError = GL_NO_ERROR;

        if ( ( glError = glGetError() ) != GL_NO_ERROR )
                Exception::throwNew( "GL error (%d) during engine termination", glError );
}


void Engine::start()
{
        GLenum glError = GL_NO_ERROR;

        global::monitor->createGPUContext();
        graphic::gpu::frameBuffer::setClearColor( 0.0, 0.0, 0.0, 0.0 );
        global::monitor->getGPUContext().getDefaultFrameBuffer().bindAsDrawTarget();
        game::Scene* currentScene = global::configurations.createInitialScene();
        void* glfwWindows[ 1 ] = { global::monitor->getGPUContext().glfwContext };
        global::keyboard->listenOnWindows( glfwWindows, 1 );

        if ( currentScene == nullptr )
                Exception::throwNew( "no initial scenario defined" );

        global::fpsAnalyzer->calculateDeltaAndMark();
        global::fpsAnalyzer->setLastDelta( 1.0f / 60.0f );

        if ( ( glError = glGetError() ) != GL_NO_ERROR )
                Exception::throwNew( "GL error (%d) during engine initialization", glError );

        global::logger.log( util::log::Level::INFO, "engine loop started" );

        while ( true )
        {
                glfwPollEvents();
                glfwPollEvents(); // solve bug: glfw perform a key repressed after key repeating

                if ( glfwWindowShouldClose( static_cast< GLFWwindow* >( global::monitor->getGPUContext().glfwContext ) ) )
                {
                        currentScene->shouldClose();

                        if ( currentScene->stopEngine )
                                goto stop_engine_label;
                }

                currentScene->update( global::fpsAnalyzer->getLastDelta() );

                if ( currentScene->stopEngine )
                {
stop_engine_label:
                        global::restartRequired = currentScene->restartEngine;
                        break;
                }

                if ( currentScene->nextScene != nullptr )
                {
                        currentScene = currentScene->nextScene;
                }
                else
                {
                        currentScene->render();
                        global::monitor->getGPUContext().swapBuffers();
                        global::fpsModerator->moderate();
                }

                if ( ( glError = glGetError() ) != GL_NO_ERROR )
                        Exception::throwNew( "GL error (%d) during engine execution", glError );

                global::fpsAnalyzer->calculateDeltaAndMark();
        }

        global::logger.log( util::log::Level::INFO, "engine loop stopped" );
}<commit_msg>engine logs time used to initialize and configure at start<commit_after>#include <ege/engine.hxx>
#include <ege/exception.hxx>
#include <ege/graphic/gpu/context.hxx>
#include <ege/util/log/logger.hxx>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <atomic>
#include <chrono>


using namespace ege;


engine::Resources* ege::engine::resources;


namespace global
{
        static std::atomic_bool started( false );
        static engine::Configurations configurations;
        static hardware::Keyboard* keyboard;
        static hardware::Monitor* monitor;
        static util::fps::Analyzer* fpsAnalyzer;
        static util::fps::Moderator* fpsModerator;
        static util::log::Logger logger;
        static game::Scene* currentScene;
        static bool restartRequired;
}


static void initializeStaticMembers()
{
        ege::engine::resources = nullptr;
        global::configurations.createInitialScene = [] () { return nullptr; };
        global::keyboard = nullptr;
        global::monitor = nullptr;
        global::fpsAnalyzer = nullptr;
        global::fpsModerator = nullptr;
        global::logger.setLevel( util::log::Level::INFO );
        global::currentScene = nullptr;
        global::restartRequired = false;
}


engine::Resources::Resources():
        keyboard( global::keyboard ),
        monitor( global::monitor ),
        fpsAnalyzer( global::fpsAnalyzer ),
        fpsModerator( global::fpsModerator ),
        logger( &global::logger )
{

}


void engine::start( const std::function< void( engine::Configurations& ) > &configure )
{
        try
        {
                if ( global::started.exchange( true ) )
                        Exception::throwNew( "an attempt to start engine was done, but engine is already started" );

                while ( true )
                {
                        std::chrono::time_point< std::chrono::system_clock > start = std::chrono::system_clock::now();
                        global::logger.log( util::log::Level::INFO, "engine started" );
                        initializeStaticMembers();
                        configure( global::configurations );
                        Engine* engine = new Engine();
                        std::chrono::duration< float > delta = std::chrono::system_clock::now() - start;
                        global::logger.log( util::log::Level::INFO, "engine initialized and configured (in %.3fs)", delta.count() );
                        engine->start();
                        delete engine;
                        global::logger.log( util::log::Level::INFO, "engine stopped" );

                        if ( ! global::restartRequired )
                                break;

                        global::logger.log( util::log::Level::INFO, "engine restarting" );
                }

                global::started = false;
        }
        catch ( ege::Exception* e )
        {
                e->consume();
        }
}


Engine::Engine()
{
        glfwInit();
        global::monitor = new hardware::Monitor( glfwGetPrimaryMonitor() );
        global::fpsAnalyzer = new util::fps::Analyzer();
        global::fpsModerator = new util::fps::Moderator( *global::fpsAnalyzer, 60 );
        global::keyboard = new hardware::Keyboard();
        ege::engine::resources = new engine::Resources();

        global::monitor->createGPUContext();
        graphic::gpu::frameBuffer::setClearColor( 0.0, 0.0, 0.0, 0.0 );
        global::monitor->getGPUContext().getDefaultFrameBuffer().bindAsDrawTarget();
        void* glfwWindows[ 1 ] = { global::monitor->getGPUContext().glfwContext };
        global::keyboard->listenOnWindows( glfwWindows, 1 );

        global::currentScene = global::configurations.createInitialScene();

        if ( global::currentScene == nullptr )
                Exception::throwNew( "no initial scenario defined" );

        GLenum glError = glGetError();

        if ( glError != GL_NO_ERROR )
                Exception::throwNew( "GL error (%d) during engine initialization", glError );

        global::fpsAnalyzer->calculateDeltaAndMark();
        global::fpsAnalyzer->setLastDelta( 1.0f / 60.0f );
}


Engine::~Engine()
{
        delete ege::engine::resources;
        delete global::fpsModerator;
        delete global::fpsAnalyzer;
        delete global::monitor;
        delete global::keyboard;
        glfwTerminate();

        GLenum glError = glGetError();

        if ( glError != GL_NO_ERROR )
                Exception::throwNew( "GL error (%d) during engine termination", glError );
}


void Engine::start()
{
        global::logger.log( util::log::Level::INFO, "engine loop started" );

        while ( true )
        {
                glfwPollEvents();
                glfwPollEvents(); // solve bug: glfw perform a key repressed after key repeating

                if ( glfwWindowShouldClose( static_cast< GLFWwindow* >( global::monitor->getGPUContext().glfwContext ) ) )
                {
                        global::currentScene->shouldClose();

                        if ( global::currentScene->stopEngine )
                                goto stop_engine_label;
                }

                global::currentScene->update( global::fpsAnalyzer->getLastDelta() );

                if ( global::currentScene->stopEngine )
                {
stop_engine_label:
                        global::restartRequired = global::currentScene->restartEngine;
                        break;
                }

                if ( global::currentScene->nextScene != nullptr )
                {
                        global::currentScene = global::currentScene->nextScene;
                }
                else
                {
                        global::currentScene->render();
                        global::monitor->getGPUContext().swapBuffers();
                        global::fpsModerator->moderate();
                }

                GLenum glError = glGetError();

                if ( glError != GL_NO_ERROR )
                        Exception::throwNew( "GL error (%d) during engine execution", glError );

                global::fpsAnalyzer->calculateDeltaAndMark();
        }

        global::logger.log( util::log::Level::INFO, "engine loop stopped" );
}
{'fraction_non_alphanumeric': 0.1220488862934846, 'fraction_numerical': 0.003086677233669809, 'mean_word_length': 1.9607310447023956, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1601916', 'n_tokens_mistral': 3195, 'n_tokens_neox': 3006, 'n_words': 616}