Browse Source

changed background drawing logic

quantumde1 5 days ago
parent
commit
46de4d1222
4 changed files with 40 additions and 31 deletions
  1. 13 6
      source/graphics/gamelogic.d
  2. 20 17
      source/scripts/lua.d
  3. 2 2
      source/system/cleanup.d
  4. 5 6
      source/variables.d

+ 13 - 6
source/graphics/gamelogic.d

@@ -57,13 +57,20 @@ void effectsLogic()
 }
 
 void backgroundLogic() {
-    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);
+    for (int i = 0; i < backgroundTextures.length; i++) {
+        if (backgroundTextures[i].drawTexture == true) {
+            float centeredX = backgroundTextures[i].x - (backgroundTextures[i].width * backgroundTextures[i].scale / 2);
+            float centeredY = backgroundTextures[i].y - (backgroundTextures[i].height * backgroundTextures[i].scale / 2);
+            DrawTextureEx(backgroundTextures[i].texture,
+            Vector2(centeredX, centeredY),
+            0.0,
+            backgroundTextures[i].scale,
+            Colors.WHITE
+            );
+        }
     }
-    for (int i = 0; i < characterTextures.length; i++)
-    {
+
+    for (int i = 0; i < characterTextures.length; i++) {
         if (characterTextures[i].drawTexture == true) {
             float centeredX = characterTextures[i].x - (characterTextures[i].width * characterTextures[i].scale / 2);
             float centeredY = characterTextures[i].y - (characterTextures[i].height * characterTextures[i].scale / 2);

+ 20 - 17
source/scripts/lua.d

@@ -96,17 +96,12 @@ extern (C) nothrow int luaL_load2Dbackground(lua_State* L)
 {
     try
     {
-        int index = cast(int) luaL_checkinteger(L, 2);
-        //if index too big, extending array
-        if (index >= backgrounds.length) {
-            backgrounds.length = index + 1;
+        int count = cast(int)luaL_checkinteger(L, 2);
+        if (count >= backgroundTextures.length) {
+            backgroundTextures.length = count + 1;
         }
-
-        // if texture with same Index already loaded, unloading it
-        if (index < backgrounds.length && backgrounds[index].id != 0) {
-            UnloadTexture(backgrounds[index]);
-        }
-        backgrounds[index] = LoadTexture(luaL_checkstring(L, 1));
+        char* filename = cast(char*)luaL_checkstring(L, 1);
+        backgroundTextures[count].texture = LoadTexture(filename);
     }
     catch (Exception e)
     {
@@ -119,8 +114,15 @@ extern (C) nothrow int luaL_draw2Dbackground(lua_State* L)
 {
     try
     {
-        backgroundTexture = backgrounds[luaL_checkinteger(L, 1)];
-        neededDraw2D = true;
+        int count = cast(int)luaL_checkinteger(L, 4);
+        debugWriteln(backgroundTextures[count]);
+        backgroundTextures[count].height = backgroundTextures[count].texture.height;
+        backgroundTextures[count].width = backgroundTextures[count].texture.width;
+        backgroundTextures[count].x = luaL_checknumber(L, 1);
+        backgroundTextures[count].y = luaL_checknumber(L, 2);
+        backgroundTextures[count].scale = cast(int)luaL_checknumber(L, 3);
+        backgroundTextures[count].drawTexture = true;
+        debugWriteln(backgroundTextures[count]);
     }
     catch (Exception e)
     {
@@ -133,8 +135,8 @@ extern (C) nothrow int luaL_stopDraw2Dbackground(lua_State* L)
 {
     try
     {
-        backgroundTexture = Texture2D();
-        neededDraw2D = true;
+        int count = cast(int)luaL_checkinteger(L, 1);
+        backgroundTextures[count].drawTexture = false;
     }
     catch (Exception e)
     {
@@ -145,7 +147,8 @@ extern (C) nothrow int luaL_stopDraw2Dbackground(lua_State* L)
 
 extern (C) nothrow int luaL_unload2Dbackground(lua_State* L)
 {
-    UnloadTexture(backgrounds[cast(int) luaL_checkinteger(L, 1)]);
+    int count = cast(int)luaL_checkinteger(L, 1);
+    UnloadTexture(backgroundTextures[count].texture);
     return 0;
 }
 
@@ -407,9 +410,9 @@ extern (C) nothrow int luaL_loadScript(lua_State* L)
     {
         UnloadTexture(characterTextures[i].texture);
     }
-    for (int i = cast(int)backgrounds.length; i < backgrounds.length; i++)
+    for (int i = cast(int)backgroundTextures.length; i < backgroundTextures.length; i++)
     {
-        UnloadTexture(backgrounds[i]);
+        UnloadTexture(backgroundTextures[i].texture);
     }
     try
     {

+ 2 - 2
source/system/cleanup.d

@@ -21,9 +21,9 @@ void unloadResourcesLogic()
     {
         UnloadTexture(characterTextures[i].texture);
     }
-    for (int i = cast(int) backgrounds.length; i < backgrounds.length; i++)
+    for (int i = cast(int) backgroundTextures.length; i < backgroundTextures.length; i++)
     {
-        UnloadTexture(backgrounds[i]);
+        UnloadTexture(backgroundTextures[i].texture);
     }
     CloseAudioDevice();
     CloseWindow();

+ 5 - 6
source/variables.d

@@ -12,7 +12,7 @@ void resetAllScriptValues() {
     debugWriteln("Resetting all values!");
     selectedChoice = 0;
     characterTextures = [];
-    backgrounds = [];
+    backgroundTextures = [];
 }
 
 /* system */
@@ -27,7 +27,7 @@ struct SystemSettings {
     char opmenu_button;
 }
 
-struct CharacterTexture {
+struct TextureEngine {
     bool drawTexture;
     float width;
     float height;
@@ -59,7 +59,9 @@ enum EngineExitCodes {
 
 Camera2D camera;
 
-CharacterTexture[] characterTextures;
+TextureEngine[] characterTextures;
+
+TextureEngine[] backgroundTextures;
 
 SystemSettings systemSettings;
 
@@ -133,11 +135,8 @@ float typingSpeed = 0.6f;
 
 /* textures */
 
-Texture2D[] backgrounds;
-
 Texture2D[] framesUI;
 
-Texture2D backgroundTexture;
 
 /* integer values */