TSnake41/raylib-lua

How is Texture2D handled

Closed this issue · 2 comments

-- you'll see I moved the images into the folder with the file and with luaJit.exe and the libaries
Here's the original C source
https://github.com/raysan5/raylib/blob/master/examples/textures/textures_background_scrolling.c
Where background, scrollingMid, scrollingFore are there's Texture2D statement ahead of them
but how do we do that in Lua?
All I get is crazy flashing window.

------- textures_background_scrolling.lua

local rl = require("raylib")
-- Initialization
local screenWidth = 800
local screenHeight = 450

rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - background scrolling")
rl.SetTargetFPS(60) -- Set our game to run at 60 frames-per-second

 -- NOTE: Be careful, background width must be equal or bigger than screen width
 -- if not, texture should be draw more than two times for scrolling effect
  background = rl.LoadTexture("cyberpunk_street_background.png")
  midground = rl.LoadTexture("cyberpunk_street_midground.png")
  foreground = rl.LoadTexture("cyberpunk_street_foreground.png")

 scrollingBack = 0.0
 scrollingMid = 0.0
 scrollingFore = 0.0


while not rl.WindowShouldClose() do   -- Detect window close button or ESC key
 -- Update
    ------------------------------------------------------------------------------------
    scrollingBack = scrollingBack- 0.1
    scrollingMid = scrollingMid- 0.5
    scrollingFore = scrollingFore- 1.0
  
    -- NOTE: Texture is scaled twice its size, so it sould be considered on scrolling
    if (scrollingBack <= -background.width*2) then scrollingBack = 0 end
    if (scrollingMid <= -midground.width*2) then scrollingMid = 0 end
    if (scrollingFore <= -foreground.width*2) then scrollingFore = 0 end
    -- Draw
    -----------------------------------------------------------------------------------
    rl.BeginDrawing()   
    
    
     --ClearBackground(GetColor(0x052c46ff))
      rl.ClearBackground(rl.RAYWHITE)
        -- Draw background image twice
        -- NOTE: Texture is scaled twice its size
      rl.DrawTextureEx(background, (Vector2){ scrollingBack, 20 }, 0.0, 2.0, WHITE)
      rl.DrawTextureEx(background, (Vector2){ background.width*2 + scrollingBack, 20 }, 0.0, 2.0,  rl.WHITE)

        --Draw midground image twice
      rl.DrawTextureEx(midground, (Vector2){ scrollingMid, 20 }, 0.0, 2.0, WHITE)
       rl.DrawTextureEx(midground, (Vector2){ midground.width*2 + scrollingMid, 20 }, 0.0, 2.0,  rl.WHITE)

        -- Draw foreground image twice
      rl.DrawTextureEx(foreground, (Vector2){ scrollingFore, 70 }, 0.0, 2.0, WHITE)
       rl.DrawTextureEx(foreground, (Vector2){ foreground.width*2 + scrollingFore, 70 }, 0.0, 2.0, rl.WHITE)

        rl.DrawText("BACKGROUND SCROLLING & PARALLAX", 10, 10, 20, RED)
        rl.DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, RAYWHITE)

    EndDrawing()


rl.DrawText("Congrats! You created your first window!", 190, 200, 20, rl.LIGHTGRAY)

rl.EndDrawing()
end

-- De-Initialization
----------------------------------------------------------------------------------------
rl.UnloadTexture(background)  -- Unload background texture
rl.UnloadTexture(midground)   -- Unload midground texture
rl.UnloadTexture(foreground)  -- Unload foreground texture

rl.CloseWindow()              -- Close window and OpenGL context
----------------------------------------------------------------------------------------

You may not be using raylib-lua but possibly another lua binding such as https://github.com/Rabios/raylua.

Anyway, the code you provided had some issues with Lua syntax, like (Vector2){ ... } which is invalid (it's either Vector2(...) or rl.new("Vector2", ...) or rl.Vector2(...) depending of the binding used.

A fixed code, that works with https://github.com/TSnake41/raylib-lua

local screenWidth = 800
local screenHeight = 450

rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - background scrolling")
rl.SetTargetFPS(60) -- Set our game to run at 60 frames-per-second

 -- NOTE: Be careful, background width must be equal or bigger than screen width
 -- if not, texture should be draw more than two times for scrolling effect
  background = rl.LoadTexture("cyberpunk_street_background.png")
  midground = rl.LoadTexture("cyberpunk_street_midground.png")
  foreground = rl.LoadTexture("cyberpunk_street_foreground.png")

 scrollingBack = 0.0
 scrollingMid = 0.0
 scrollingFore = 0.0


while not rl.WindowShouldClose() do   -- Detect window close button or ESC key
 -- Update
    ------------------------------------------------------------------------------------
    scrollingBack = scrollingBack- 0.1
    scrollingMid = scrollingMid- 0.5
    scrollingFore = scrollingFore- 1.0
  
    -- NOTE: Texture is scaled twice its size, so it sould be considered on scrolling
    if (scrollingBack <= -background.width*2) then scrollingBack = 0 end
    if (scrollingMid <= -midground.width*2) then scrollingMid = 0 end
    if (scrollingFore <= -foreground.width*2) then scrollingFore = 0 end
    -- Draw
    -----------------------------------------------------------------------------------
    rl.BeginDrawing()   
    
    
     --ClearBackground(GetColor(0x052c46ff))
      rl.ClearBackground(rl.RAYWHITE)
        -- Draw background image twice
        -- NOTE: Texture is scaled twice its size
      rl.DrawTextureEx(background, { scrollingBack, 20 }, 0.0, 2.0, rl.WHITE)
      rl.DrawTextureEx(background, { background.width*2 + scrollingBack, 20 }, 0.0, 2.0,  rl.WHITE)

        --Draw midground image twice
      rl.DrawTextureEx(midground, { scrollingMid, 20 }, 0.0, 2.0, rl.WHITE)
       rl.DrawTextureEx(midground, { midground.width*2 + scrollingMid, 20 }, 0.0, 2.0,  rl.WHITE)

        -- Draw foreground image twice
      rl.DrawTextureEx(foreground, { scrollingFore, 70 }, 0.0, 2.0, rl.WHITE)
       rl.DrawTextureEx(foreground, { foreground.width*2 + scrollingFore, 70 }, 0.0, 2.0, rl.WHITE)

        rl.DrawText("BACKGROUND SCROLLING & PARALLAX", 10, 10, 20, rl.RED)
        rl.DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, rl.RAYWHITE)

    rl.EndDrawing()
end

-- De-Initialization
----------------------------------------------------------------------------------------
rl.UnloadTexture(background)  -- Unload background texture
rl.UnloadTexture(midground)   -- Unload midground texture
rl.UnloadTexture(foreground)  -- Unload foreground texture

rl.CloseWindow()              -- Close window and OpenGL context
----------------------------------------------------------------------------------------

In my case, I don't see any issues with background, maybe the problem is elsewhere ?

Great, That Works! yeah I was afraid the bindings might get mixed up, but thought yours was the only LuaJit one(?) Things got mixed up when trying to use ZeroBrane, It couldn't find a lot of what it was looking for until I set the project directory, so I had just dumped every rayLib Lua on earth into a folder hoping something would work. I better do a clean install of everything so things aren't a mess. Thanks for the response!