module system.mainloop; import variables; import ui.menu; import system.debugwriteln; import graphics.gamelogic; import raylib; import std.stdio; import std.conv; import scripts.lua.core; import std.algorithm; import scripts.hbs; import system.cleanup; void mainLoop() { renderTexture = LoadRenderTexture(renderTextureWidth, renderTextureHeight); float renderScale = min( cast(float)GetScreenWidth() / renderTextureWidth, cast(float)GetScreenHeight() / renderTextureHeight ); debugWriteln("renderScale: ", renderScale); while (true) { switch (currentGameState) { case GameState.MainMenu: debugWriteln("Showing menu."); showMainMenu(); break; case GameState.InGame: gameInit(); while (!WindowShouldClose()) { if (luaReload) { resetAllScriptValues(); int luaExecutionCode = luaInit(luaExec); if (luaExecutionCode != EngineExitCodes.EXIT_OK) { writeln("[ERROR] Engine stops Lua execution according to error code: ", luaExecutionCode.to!EngineExitCodes); currentGameState = GameState.Exit; break; } luaReload = false; } if (hbsCalled == true) { executer(); } SetExitKey(0); ClearBackground(Colors.BLACK); BeginTextureMode(renderTexture); BeginDrawing(); ClearBackground(Colors.BLACK); /* 3D part */ if (pauseParserLua == false) { if (is3dEnabled == true) { luaEventLoopPre2D(); BeginMode3D(camera); luaEventLoop3D(); EndMode3D(); EndTextureMode(); DrawTexturePro( renderTexture.texture, Rectangle(0, 0, renderTextureWidth, -renderTextureHeight), Rectangle( (GetScreenWidth() - (renderTextureWidth * renderScale)) * 0.5f, (GetScreenHeight() - (renderTextureHeight * renderScale)) * 0.5f, renderTextureWidth * renderScale, renderTextureHeight * renderScale ), Vector2(0, 0), 0.0f, Colors.WHITE ); } else { EndTextureMode(); } if (is2dEnabled == true) { luaEventLoopPost2D(); /* 2D part */ // background display logic backgroundLogic(); // character display logic characterLogic(); // effects logic effectsLogic(); //drawing dialogs dialogLogic(); } } if (overlayLoop !is null) { overlayLoop(); } EndDrawing(); } break; case GameState.Exit: unloadResourcesOnExit(); CloseAudioDevice(); debugWriteln("closing window!"); CloseWindow(); return; default: break; } } }