Преглед на файлове

moved menu from dlang to lua

quantumde1 преди 1 седмица
родител
ревизия
9e5a6ef6f6
променени са 9 файла, в които са добавени 271 реда и са изтрити 330 реда
  1. 1 1
      conf/settings.conf
  2. 2 4
      source/app.d
  3. 1 2
      source/graphics/effects.d
  4. 26 14
      source/graphics/engine.d
  5. 1 2
      source/graphics/gamelogic.d
  6. 190 24
      source/scripts/lua.d
  7. 2 3
      source/system/config.d
  8. 26 260
      source/ui/menu.d
  9. 22 20
      source/variables.d

+ 1 - 1
conf/settings.conf

@@ -1,4 +1,4 @@
-SOUND:1
+SOUND:0
 FORWARD:W
 BACKWARD:S
 LEFT:A

+ 2 - 4
source/app.d

@@ -24,14 +24,12 @@ void main(string[] args)
     int screenWidth = GetScreenWidth();
     int screenHeight = GetScreenHeight();
     luaExec = "scripts/00_script.lua";
-    if (args.length > 1)
-    {
+    if (args.length > 1) {
         writeln("!!!If needed, there is first argument for choosing script to execute.!!!");
         luaExec = getcwd().to!string ~ "/" ~ args[1];
         engine_loader("tief blau", screenWidth, screenHeight);
     }
-    else
-    {
+    else {
         engine_loader("tief blau", screenWidth, screenHeight);
     }
 }

+ 1 - 2
source/graphics/effects.d

@@ -25,8 +25,7 @@ Texture2D[] loadAnimationFramesUI(const string archivePath, const string animati
         debug debugWriteln(frameFileName);
         char* image_data = get_file_data_from_archive(toStringz(archivePath),
                 toStringz(frameFileName), &image_size);
-        if (image_data == null)
-        {
+        if (image_data == null) {
             debug debugWriteln("exiting from load anim UI");
             break;
         }

+ 26 - 14
source/graphics/engine.d

@@ -2,26 +2,39 @@
 module graphics.engine;
 
 import raylib;
-import graphics.playback;
+
+//dlang imports
 import std.stdio;
 import std.math;
 import std.file;
-import graphics.gamelogic;
+import std.array;
 import std.string;
 import std.conv;
-import variables;
+import std.typecons;
 import std.random;
 import std.datetime;
+
+//graphics
+import graphics.playback;
+import graphics.gamelogic;
 import ui.menu;
-import std.typecons;
-import system.abstraction;
-import system.config;
+
+//dialogs
 import dialogs.dialogbox;
+
+//scripting imports
 import scripts.lua;
-import std.array;
+
+//engine internal functions
+import system.config;
 import system.abstraction;
+import variables;
 import system.cleanup;
 
+//C bindings
+import core.stdc.stdlib;
+import core.stdc.time;
+
 void engine_loader(string window_name, int screenWidth, int screenHeight)
 {
     // Initialization
@@ -49,21 +62,19 @@ void engine_loader(string window_name, int screenWidth, int screenHeight)
         {
         case GameState.MainMenu:
             debugWriteln("Showing menu.");
-            showMainMenu(currentGameState);
+            showMainMenu();
             break;
         case GameState.InGame:
-            import core.stdc.stdlib;
-            import core.stdc.time;
 
             gameInit();
             while (!WindowShouldClose())
             {
                 SetExitKey(0);
-                if (luaReload)
-                {
+                if (luaReload) {
                     int luaExecutionCode = luaInit(luaExec);
                     if (luaExecutionCode != EngineExitCodes.EXIT_OK) {
-                        debugWriteln("Engine stops execution according to error code: ", luaExecutionCode);
+                        debugWriteln("Engine stops execution according to error code: ", 
+                        luaExecutionCode.to!EngineExitCodes);
                         currentGameState = GameState.Exit;
                         break;
                     }
@@ -73,11 +84,12 @@ void engine_loader(string window_name, int screenWidth, int screenHeight)
                 BeginDrawing();
                 ClearBackground(Colors.BLACK);
                 // main logic
-                // effects logic
                 BeginMode2D(camera);
                 backgroundLogic();
+                // effects logic
                 effectsLogic();
                 EndMode2D();
+                //drawing dialogs
                 dialogLogic();
                 EndDrawing();
             }

+ 1 - 2
source/graphics/gamelogic.d

@@ -57,8 +57,7 @@ void effectsLogic()
 }
 
 void backgroundLogic() {
-    if (neededDraw2D)
-    {
+    if (neededDraw2D) {
         DrawTexturePro(backgroundTexture, Rectangle(0, 0, cast(float) backgroundTexture.width, cast(
                 float) backgroundTexture.height), Rectangle(0, 0, cast(float) GetScreenWidth(), cast(
                 float) GetScreenHeight()), Vector2(0, 0), 0.0, Colors.WHITE);

+ 190 - 24
source/scripts/lua.d

@@ -44,6 +44,7 @@ extern (C) nothrow int luaL_dialogBox(lua_State* L)
     for (int i = 0; i < textTableLength; i++) {
         lua_rawgeti(L, 1, i + 1);
         messageGlobal[i] = luaL_checkstring(L, -1).to!string;
+        backlogText ~= messageGlobal[i];
         lua_pop(L, 1);
     }
 
@@ -57,14 +58,18 @@ extern (C) nothrow int luaL_dialogBox(lua_State* L)
         choices[i] = luaL_checkstring(L, -1).to!string;
         lua_pop(L, 1);
     }
-    //get page on which choices must be shown
-    choicePage = cast(int)luaL_checkinteger(L, 3);
-    
+
+
+    //if provided, get page on which choices must be shown
+    if (lua_gettop(L) == 3) {
+        choicePage = cast(int)luaL_checkinteger(L, 3);
+    }
+
     //if provided, change speed of showing text
-    if (lua_gettop(L) == 4)
-    {
+    if (lua_gettop(L) == 4) {
         typingSpeed = cast(float) luaL_checknumber(L, 7);
     }
+
     return 0;
 }
 
@@ -93,14 +98,12 @@ extern (C) nothrow int luaL_load2Dbackground(lua_State* L)
     {
         int index = cast(int) luaL_checkinteger(L, 2);
         //if index too big, extending array
-        if (index >= backgrounds.length)
-        {
+        if (index >= backgrounds.length) {
             backgrounds.length = index + 1;
         }
 
         // if texture with same Index already loaded, unloading it
-        if (index < backgrounds.length && backgrounds[index].id != 0)
-        {
+        if (index < backgrounds.length && backgrounds[index].id != 0) {
             UnloadTexture(backgrounds[index]);
         }
         backgrounds[index] = LoadTexture(luaL_checkstring(L, 1));
@@ -126,6 +129,20 @@ extern (C) nothrow int luaL_draw2Dbackground(lua_State* L)
     return 0;
 }
 
+extern (C) nothrow int luaL_stopDraw2Dbackground(lua_State* L)
+{
+    try
+    {
+        backgroundTexture = Texture2D();
+        neededDraw2D = true;
+    }
+    catch (Exception e)
+    {
+        debugWriteln(e.msg);
+    }
+    return 0;
+}
+
 extern (C) nothrow int luaL_unload2Dbackground(lua_State* L)
 {
     UnloadTexture(backgrounds[cast(int) luaL_checkinteger(L, 1)]);
@@ -139,8 +156,7 @@ extern (C) nothrow int luaL_load2Dcharacter(lua_State *L) {
     {
         int count = cast(int) luaL_checkinteger(L, 2);
 
-        if (count >= characterTextures.length)
-        {
+        if (count >= characterTextures.length) {
             characterTextures.length = count + 1;
         }
         characterTextures[count].texture = LoadTexture(luaL_checkstring(L, 1));
@@ -267,7 +283,7 @@ extern (C) nothrow int luaL_isCameraMoving(lua_State *L) {
 extern (C) nothrow int luaL_loadUIAnimation(lua_State *L) {
     try {
         //loads from uifx folder HPFF files, in which png textures are stored
-        framesUI = loadAnimationFramesUI("res/uifx/"~to!string(luaL_checkstring(L, 1)), to!string(luaL_checkstring(L, 2)));
+        framesUI = loadAnimationFramesUI(to!string(luaL_checkstring(L, 1)), to!string(luaL_checkstring(L, 2)));
         if (lua_gettop(L) == 3) {
             frameDuration = luaL_checknumber(L, 3);
             debug debugWriteln("frameDuration: ", frameDuration);
@@ -344,6 +360,7 @@ extern (C) nothrow int luaL_setGameFont(lua_State* L)
     const char* x = luaL_checkstring(L, 1);
     debugWriteln("Setting custom font: ", x.to!string);
     int[512] codepoints = 0;
+    //configuring both cyrillic and latin fonts if available
     foreach (i; 0 .. 95)
     {
         codepoints[i] = 32 + i;
@@ -358,6 +375,7 @@ extern (C) nothrow int luaL_setGameFont(lua_State* L)
 
 extern (C) nothrow int luaL_getTime(lua_State* L)
 {
+    //getTime() returns current time.
     lua_pushnumber(L, GetTime());
     return 1;
 }
@@ -366,35 +384,30 @@ extern (C) nothrow int luaL_isKeyPressed(lua_State* L)
 {
     try
     {
-        if (IsKeyPressed(cast(int)(luaL_checkinteger(L, 1))))
-        {
-            lua_pushboolean(L, true);
-        }
-        else
-        {
-            lua_pushboolean(L, false);
-        }
+        int keyCode = cast(int)luaL_checkinteger(L, 1);
+        lua_pushboolean(L, IsKeyPressed(keyCode).to!bool);
+        return 1;
     }
     catch (Exception e)
     {
         debugWriteln(e.msg);
+        return 1;
     }
-    return 1;
 }
 
 extern (C) nothrow int luaL_loadScript(lua_State* L)
 {
-    for (int i = cast(int) characterTextures.length; i < characterTextures.length; i++)
+    for (int i = cast(int)characterTextures.length; i < characterTextures.length; i++)
     {
         UnloadTexture(characterTextures[i].texture);
     }
-    for (int i = cast(int) backgrounds.length; i < backgrounds.length; i++)
+    for (int i = cast(int)backgrounds.length; i < backgrounds.length; i++)
     {
         UnloadTexture(backgrounds[i]);
     }
     try
     {
-        luaExec = to!string(luaL_checkstring(L, 1));
+        luaExec = luaL_checkstring(L, 1).to!string;
         resetAllScriptValues();
     }
     catch (Exception e)
@@ -405,6 +418,148 @@ extern (C) nothrow int luaL_loadScript(lua_State* L)
     return 0;
 }
 
+extern (C) nothrow int luaL_setGameState(lua_State *L) {
+    currentGameState = cast(int)luaL_checkinteger(L, 1);
+    return 0;
+}
+
+/* raylib direct bindings for graphics */
+
+/* basic */
+
+extern (C) nothrow int luaL_loadTexture(lua_State *L) {
+    const char* fileName = luaL_checkstring(L, 1);
+    Texture2D texture = LoadTexture(fileName);
+    
+    Texture2D* texturePtr = cast(Texture2D*)lua_newuserdata(L, Texture2D.sizeof);
+    *texturePtr = texture;
+    
+    if (luaL_newmetatable(L, "Texture")) {
+        lua_pushcfunction(L, &luaL_textureGC);
+        lua_setfield(L, -2, "__gc");
+    }
+    
+    lua_setmetatable(L, -2);
+    return 1;
+}
+
+extern (C) nothrow int luaL_textureGC(lua_State *L) {
+    Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
+    UnloadTexture(*texture);
+    return 0;
+}
+
+extern (C) nothrow int luaL_drawTexture(lua_State *L) {
+    Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
+    int x = cast(int)luaL_checkinteger(L, 2);
+    int y = cast(int)luaL_checkinteger(L, 3);
+    Color color = Colors.WHITE;
+    
+    if (lua_gettop(L) >= 4 && lua_istable(L, 4)) {
+        lua_getfield(L, 4, "r");
+        color.r = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+        
+        lua_getfield(L, 4, "g");
+        color.g = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+        
+        lua_getfield(L, 4, "b");
+        color.b = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+        
+        lua_getfield(L, 4, "a");
+        color.a = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+    }
+    
+    DrawTexture(*texture, x, y, color);
+    return 0;
+}
+
+extern (C) nothrow int luaL_getTextureWidth(lua_State *L) {
+    Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
+    lua_pushinteger(L, texture.width);
+    return 1;
+}
+
+extern (C) nothrow int luaL_getTextureHeight(lua_State *L) {
+    Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
+    lua_pushinteger(L, texture.height);
+    return 1;
+}
+
+extern (C) nothrow int luaL_drawText(lua_State *L) {
+    const char* text = luaL_checkstring(L, 1);
+    int x = cast(int)luaL_checkinteger(L, 2);
+    int y = cast(int)luaL_checkinteger(L, 3);
+    int fontSize = cast(int)luaL_optinteger(L, 4, 20);
+    Color color = Colors.WHITE;
+    if (lua_istable(L, 5)) {
+        lua_getfield(L, 5, "r");
+        color.r = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+        
+        lua_getfield(L, 5, "g");
+        color.g = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+        
+        lua_getfield(L, 5, "b");
+        color.b = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+        
+        lua_getfield(L, 5, "a");
+        color.a = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+    }
+    
+    DrawTextEx(textFont, text, Vector2(x, y), fontSize, 1.0f, color);
+    
+    return 0;
+}
+
+extern (C) nothrow int luaL_measureTextX(lua_State *L) {
+    lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).x);
+    return 1;
+}
+
+extern (C) nothrow int luaL_measureTextY(lua_State *L) {
+    lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).y);
+    return 1;
+}
+
+/* extended */
+
+extern (C) nothrow int luaL_drawTextureEx(lua_State *L) {
+    Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
+    float x = luaL_checknumber(L, 2);
+    float y = luaL_checknumber(L, 3);
+    float rotation = luaL_optnumber(L, 4, 0);
+    float scale = luaL_optnumber(L, 5, 1);
+    Color color = Colors.WHITE;
+    if (lua_istable(L, 6)) {
+        lua_getfield(L, 6, "r");
+        color.r = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+        
+        lua_getfield(L, 6, "g");
+        color.g = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+        
+        lua_getfield(L, 6, "b");
+        color.b = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+        
+        lua_getfield(L, 6, "a");
+        color.a = cast(ubyte)lua_tointeger(L, -1);
+        lua_pop(L, 1);
+    }
+    DrawTextureEx(*texture, Vector2(x, y), rotation, scale, color);
+    
+    return 0;
+}
+
+
 /* Register functions */
 
 extern (C) nothrow void luaL_loader(lua_State* L)
@@ -433,6 +588,7 @@ extern (C) nothrow void luaL_loader(lua_State* L)
     lua_register(L, "unload2Dcharacter", &luaL_unload2Dcharacter);
     lua_register(L, "load2Dtexture", &luaL_load2Dbackground);
     lua_register(L, "draw2Dtexture", &luaL_draw2Dbackground);
+    lua_register(L, "stopDraw2Dtexture", &luaL_stopDraw2Dbackground);
     lua_register(L, "unload2Dtexture", &luaL_unload2Dbackground);
     lua_register(L, "getTime", &luaL_getTime);
     lua_register(L, "loadScript", &luaL_loadScript);
@@ -441,6 +597,16 @@ extern (C) nothrow void luaL_loader(lua_State* L)
     lua_register(L, "getScreenWidth", &luaL_getScreenWidth);
     lua_register(L, "isKeyPressed", &luaL_isKeyPressed);
     lua_register(L, "getLanguage", &luaL_getUsedLanguage);
+    lua_register(L, "setGameState", &luaL_setGameState);
+    //raylib direct bindings
+    lua_register(L, "loadTexture", &luaL_loadTexture);
+    lua_register(L, "drawTexture", &luaL_drawTexture);
+    lua_register(L, "drawTextureEx", &luaL_drawTextureEx);
+    lua_register(L, "drawText", &luaL_drawText);
+    lua_register(L, "measureTextX", &luaL_measureTextX);
+    lua_register(L, "measureTextY", &luaL_measureTextY);
+    lua_register(L, "getTextureWidth", &luaL_getTextureWidth);
+    lua_register(L, "getTextureHeight", &luaL_getTextureHeight);
 }
 
 int luaInit(string luaExec)

+ 2 - 3
source/system/config.d

@@ -14,8 +14,8 @@ nothrow char parseConf(string filename, string type)
     {
         auto file = File(filename);
         auto config = file.byLineCopy();
-
-        // Create a mapping of types to their corresponding prefixes
+        
+        //mappings
         auto typeMap = [
             "sound": "SOUND:",
             "backward": "BACKWARD:",
@@ -26,7 +26,6 @@ nothrow char parseConf(string filename, string type)
             "opmenu": "OPMENU:"
         ];
 
-        // Check if the provided type exists in the map
         if (type in typeMap)
         {
             auto prefix = typeMap[type];

+ 26 - 260
source/ui/menu.d

@@ -11,6 +11,9 @@ import std.string;
 import graphics.engine;
 import graphics.playback;
 import std.file;
+import scripts.lua;
+import std.conv;
+import graphics.gamelogic;
 
 enum
 {
@@ -39,18 +42,6 @@ void fadeEffect(float alpha, bool fadeIn, void delegate(float alpha) renderer)
     }
 }
 
-struct MenuState
-{
-    string[] options;
-    int selectedIndex;
-    float fadeAlpha;
-    float inactivityTimer;
-    Texture2D logoTexture;
-    Music menuMusic;
-    bool fromSave;
-    int logoX, logoY;
-}
-
 void renderText(float alpha, immutable(char)* text)
 {
     DrawTextEx(textFont, text,
@@ -62,15 +53,13 @@ void renderText(float alpha, immutable(char)* text)
 void renderLogo(float alpha, immutable(char)* name, bool fullscreen)
 {
     Texture2D companyLogo = LoadTexture(name);
-    if (fullscreen)
-    {
+    if (fullscreen) {
         DrawTexturePro(companyLogo,
             Rectangle(0, 0, cast(float) companyLogo.width, cast(float) companyLogo.height),
             Rectangle(0, 0, cast(float) GetScreenWidth(), cast(float) GetScreenHeight()),
             Vector2(0, 0), 0.0, Fade(Colors.WHITE, alpha));
     }
-    else
-    {
+    else {
         DrawTexture(companyLogo, GetScreenWidth() / 2, GetScreenHeight() / 2, Colors.WHITE);
     }
 }
@@ -81,14 +70,12 @@ void helloScreen()
     {
         bool play = false;
         debugWriteln("hello screen showing");
-        if (play == false)
-        {
+        if (play == false) {
             videoFinished = true;
             goto debug_lab;
         }
     }
-    else
-        {
+    else {
         fadeEffect(0.0f, true, (float alpha) {
             renderText(alpha, "powered by\n\nHimmel Engine");
         });
@@ -113,8 +100,7 @@ void helloScreen()
             debug debugWriteln("video found, playing");
             playVideo("/res/videos/soul_OP.moflex.mp4");
         }
-        else
-        {
+        else {
             debug debugWriteln("video not found, skipping");
             videoFinished = true;
         }
@@ -122,245 +108,25 @@ void helloScreen()
     debug_lab:
 }
 
-MenuState initMenuState()
-{
-    MenuState state;
-    state.fromSave = std.file.exists(getcwd() ~ "/save.txt");
-    state.options = [
-        "Start Game", 
-        "Sound: On",
-        "SFX: On",
-        "Fullscreen: On",
-        "Exit Game",
-    ];
-
-    if (state.fromSave)
-        state.options[MENU_ITEM_START] = "Continue";
-    if (!audioEnabled)
-        state.options[MENU_ITEM_SOUND] = "Sound: Off";
-
-    state.fadeAlpha = 0.0f;
-    state.inactivityTimer = 0.0f;
-    state.selectedIndex = 0;
-
-    state.logoTexture = LoadTexture("res/data/menu_logo.png");
-    state.logoX = (GetScreenWidth() - state.logoTexture.width) / 2;
-    state.logoY = (GetScreenHeight() - state.logoTexture.height) / 2 - 50;
-
-    state.menuMusic = LoadMusicStream("res/data/menu_music.mp3");
-    if (audioEnabled)
-    {
-        PlayMusicStream(state.menuMusic);
+int showMainMenu() {
+    int luaExecutionCode = luaInit("scripts/menu.lua");
+    if (luaExecutionCode != EngineExitCodes.EXIT_OK) {
+        debugWriteln("Engine stops execution according to error code: ", 
+        luaExecutionCode.to!EngineExitCodes);
+        currentGameState = GameState.Exit;
+        return luaExecutionCode;
     }
-    audio.declineSound = LoadSound("res/sfx/10002.wav");
-    audio.acceptSound = LoadSound("res/sfx/10003.wav");
-    audio.menuMoveSound = LoadSound("res/sfx/10004.wav");
-    audio.menuChangeSound = LoadSound("res/sfx/00152.wav");
-    audio.nonSound = LoadSound("res/sfx/00154.wav");
-    return state;
-}
-
-void cleanupMenu(ref MenuState state)
-{
-    UnloadTexture(state.logoTexture);
-    UnloadMusicStream(state.menuMusic);
-}
-
-void drawMenu(ref const MenuState state)
-{
-    BeginDrawing();
-    ClearBackground(Colors.BLACK);
-
-    DrawTextureEx(
-        state.logoTexture,
-        Vector2(state.logoX, state.logoY),
-        0.0f, 1.0f,
-        Fade(Colors.WHITE, state.fadeAlpha)
-    );
-
-    for (int i = 0; i < state.options.length; i++)
+    luaReload = false;
+    while (currentGameState == GameState.MainMenu)
     {
-        Color textColor = (i == state.selectedIndex) ? Colors.LIGHTGRAY : Colors.GRAY;
-        float textWidth = MeasureTextEx(textFont, toStringz(state.options[i]), 30, 0).x;
-        float textX = (GetScreenWidth() - textWidth) / 2;
-        int textY = state.logoY + state.logoTexture.height + 100 + (30 * i);
-
-        DrawTextEx(
-            textFont,
-            toStringz(state.options[i]),
-            Vector2(textX, textY),
-            30, 0,
-            Fade(textColor, state.fadeAlpha)
-        );
-    }
-
-    EndDrawing();
-}
-
-void handleInactivity(ref MenuState state)
-{
-    state.inactivityTimer += GetFrameTime();
-
-    if (state.inactivityTimer >= INACTIVITY_TIMEOUT)
-    {
-        while (state.fadeAlpha > 0.0f)
-        {
-            state.fadeAlpha -= FADE_SPEED_OUT;
-            if (state.fadeAlpha < 0.0f)
-                state.fadeAlpha = 0.0f;
-            drawMenu(state);
-        }
-
-        StopMusicStream(state.menuMusic);
-        playVideo("/res/videos/opening_old.mp4");
-        if (audioEnabled)
-        {
-            PlayMusicStream(state.menuMusic);
-        }
-
-        state.inactivityTimer = 0.0f;
-
-        while (state.fadeAlpha < 1.0f)
-        {
-            state.fadeAlpha += FADE_SPEED_IN;
-            if (state.fadeAlpha > 1.0f)
-                state.fadeAlpha = 1.0f;
-            drawMenu(state);
-        }
-    }
-}
-
-void handleMenuNavigation(ref MenuState state)
-{
-    bool moved = false;
-
-    if (IsKeyPressed(KeyboardKey.KEY_DOWN))
-    {
-        state.selectedIndex = cast(int)((state.selectedIndex + 1) % state.options.length);
-        state.inactivityTimer = 0;
-        moved = true;
-    }
-
-    if (IsKeyPressed(KeyboardKey.KEY_UP))
-    {
-        state.selectedIndex = cast(int)(
-            (state.selectedIndex - 1 + state.options.length) % state.options.length);
-        state.inactivityTimer = 0;
-        moved = true;
-    }
-
-    if (moved && sfxEnabled)
-    {
-        PlaySound(audio.menuMoveSound);
-    }
-}
-
-void handleMenuSettings(ref MenuState state)
-{
-    bool leftPressed = IsKeyPressed(KeyboardKey.KEY_LEFT);
-    bool rightPressed = IsKeyPressed(KeyboardKey.KEY_RIGHT);
-
-    if (!leftPressed && !rightPressed)
-        return;
-
-    state.inactivityTimer = 0;
-
-    if (sfxEnabled) PlaySound(audio.menuChangeSound);
-
-    switch (state.selectedIndex)
-    {
-    case MENU_ITEM_SOUND:
-        audioEnabled = rightPressed ? false : true;
-        state.options[MENU_ITEM_SOUND] = audioEnabled ? "Sound: On" : "Sound: Off";
-
-        if (audioEnabled)
-        {
-            PlayMusicStream(state.menuMusic);
-        }
-        else
-        {
-            StopMusicStream(state.menuMusic);
-        }
-        break;
-    case MENU_ITEM_SFX:
-        sfxEnabled = rightPressed ? false : true;
-        state.options[MENU_ITEM_SFX] = sfxEnabled ? "SFX: On" : "SFX: Off";
-        break;
-
-    case MENU_ITEM_FULLSCREEN:
-        fullscreenEnabled = rightPressed ? false : true;
-        state.options[MENU_ITEM_FULLSCREEN] = fullscreenEnabled ? "Fullscreen: On" : "Fullscreen: Off";
-        if (fullscreenEnabled) {
-            if (!IsWindowFullscreen()) {
-                ToggleFullscreen();
-                HideCursor();
-                
-            }
-        } else if (fullscreenEnabled == false) {
-            if (IsWindowFullscreen()) {
-                ToggleFullscreen();
-                ShowCursor();
-            }
-        }
-        break;
-    default:
-        break;
-    }
-}
-
-void showMainMenu(ref GameState currentGameState)
-{
-    MenuState state = initMenuState();
-
-    while (state.fadeAlpha < 1.0f)
-    {
-        state.fadeAlpha += FADE_SPEED_IN;
-        if (state.fadeAlpha > 1.0f)
-            state.fadeAlpha = 1.0f;
-        drawMenu(state);
-    }
-
-    while (!WindowShouldClose())
-    {
-        UpdateMusicStream(state.menuMusic);
-
-
-        handleInactivity(state);
-        handleMenuNavigation(state);
-        handleMenuSettings(state);
-
-        if (IsKeyPressed(KeyboardKey.KEY_ENTER) ||
-            IsKeyPressed(KeyboardKey.KEY_SPACE))
-        {
-            if (sfxEnabled) PlaySound(audio.acceptSound);
-            switch (state.selectedIndex)
-            {
-            case MENU_ITEM_START:
-                while (state.fadeAlpha > 0.0f)
-                {
-                    state.fadeAlpha -= FADE_SPEED_OUT;
-                    if (state.fadeAlpha < 0.0f)
-                        state.fadeAlpha = 0.0f;
-                    drawMenu(state);
-                }
-
-                cleanupMenu(state);
-                currentGameState = GameState.InGame;
-                debug debugWriteln("getting into game...");
-                return;
-
-            case MENU_ITEM_EXIT:
-                cleanupMenu(state);
-                currentGameState = GameState.Exit;
-                return;
-
-            default:
-                break;
-            }
-        }
-
-        drawMenu(state);
+        UpdateMusicStream(music);
+        luaEventLoop();
+        BeginDrawing();
+        BeginMode2D(camera);
+        backgroundLogic();
+        EndMode2D();
+        EndDrawing();
     }
-
-    cleanupMenu(state);
+    luaReload = true;
+    return 0;
 }

+ 22 - 20
source/variables.d

@@ -17,12 +17,6 @@ void resetAllScriptValues() {
 
 /* system */
 
-enum EngineExitCodes {
-    EXIT_FILE_NOT_FOUND = 2,
-    EXIT_SCRIPT_ERROR = 3,
-    EXIT_OK = 0,
-}
-
 struct SystemSettings {
     int sound_state;
     char right_button;
@@ -52,9 +46,15 @@ struct InterfaceAudio {
 }
 
 enum GameState {
-    MainMenu,
-    InGame,
-    Exit
+    MainMenu = 1,
+    InGame = 2,
+    Exit = 3
+}
+
+enum EngineExitCodes {
+    EXIT_FILE_NOT_FOUND = 2,
+    EXIT_SCRIPT_ERROR = 3,
+    EXIT_OK = 0,
 }
 
 Camera2D camera;
@@ -65,7 +65,7 @@ SystemSettings systemSettings;
 
 InterfaceAudio audio;
 
-GameState currentGameState;
+int currentGameState = 1;
 
 Font textFont;
 
@@ -74,11 +74,11 @@ Music music;
 
 /* booleans */
 
-bool hintNeeded;
+bool hintNeeded = false;
 
-bool playAnimation;
+bool playAnimation = false;
 
-bool audioEnabled;
+bool audioEnabled = false;
 
 bool sfxEnabled = true;
 
@@ -86,17 +86,17 @@ bool fullscreenEnabled = true;
 
 bool luaReload = true;
 
-bool videoFinished;
+bool videoFinished = false;
 
-bool neededDraw2D;
+bool neededDraw2D = false;
 
-bool isCameraMoving;
+bool isCameraMoving = false;
 
-bool neededCharacterDrawing;
+bool neededCharacterDrawing = false;
 
-bool showDialog;
+bool showDialog = false;
 
-bool isTextFullyDisplayed;
+bool isTextFullyDisplayed = false;
 
 
 /* strings */
@@ -107,6 +107,8 @@ string[] messageGlobal;
 
 string[] choices;
 
+string[] backlogText;
+
 string luaExec;
 
 string usedLang = "english";
@@ -143,7 +145,7 @@ int button;
 
 int selectedChoice = 0;
 
-int choicePage;
+int choicePage = -1;
 
 int currentFrame = 0;