Legacy-LuaSTG-Engine / LuaSTG-Sub

LuaSTG Sub, a modern version of the LuaSTG engine. LuaSTG Sub,现代化的 LuaSTG 引擎分支。
MIT License
35 stars 7 forks source link

Using rendertargets as texture in fullscreen, then changing focus, causes weird behavior #6

Closed Tru-Dev closed 2 years ago

Tru-Dev commented 2 years ago

I've been experimenting with using rendertargets as textures, and one thing that's bothered me is that if you click on another window in fullscreen (I use two monitors) it breaks depending on how it's rendered.

If using lstg.RenderTexture: Sampler state set in lstg.SetTextureSamplerState breaks. If using lstg.LoadImage on the rendertarget, then rendering that image: The image freezes.

If you could fix this that would be amazing, I'd like to use this concept to better organize how stuff is rendered in game, and this is practically the only thing that would prevent me from doing so.

Demonese commented 2 years ago

This is undefined behavior. The rendertarget texture is recreated every time the window size changes, but LoadImage refers to the old texture. Try using a fixed size rendertarget?

Demonese commented 2 years ago

If using lstg.LoadImage on the rendertarget, then rendering that image: The image freezes.

this commit fixed 52fe360beb53f2952617e07f236b723b1d1f9b34

Demonese commented 2 years ago

If using lstg.RenderTexture: Sampler state set in lstg.SetTextureSamplerState breaks.

According to my tests it works fine

Tru-Dev commented 2 years ago

Ah thanks, I'll try it now

Tru-Dev commented 2 years ago

As far as I know it's not working (although the lstg.SetTextureSamplerState issue is easy to get around)... Try using this main.lua:

local timer = 0
-- local window = { width = 1280, height = 720, }
local window = { width = 1920, height = 1080, }
local Keyboard = lstg.Input.Keyboard
local task = require("task")
local global_tasks = task.Manager()
local global_color = lstg.Color(255, 0, 0, 0)
local function set_camera()
    lstg.SetViewport(0, window.width, 0, window.height)
    lstg.SetScissorRect(0, window.width, 0, window.height)
    lstg.SetOrtho(0, window.width, 0, window.height)
    lstg.SetImageScale(1)
    lstg.SetFog()
end
function GameInit()
    lstg.ChangeVideoMode(window.width, window.height, false, false)
    set_camera()
    lstg.LoadTexture("white", "res/white.png", false)
    lstg.LoadImage("white", "white", 0, 0, 16, 16)
    if not lstg.LoadTTF("Sans", "C:/Windows/Fonts/msyh.ttc", 48, 48) then
        lstg.LoadTTF("Sans", "C:/Windows/Fonts/msyh.ttf", 48, 48) -- Windows 7
    end
    lstg.CreateRenderTarget("mainRdrTgt")
    lstg.LoadImage("mainRdrTgt", "mainRdrTgt", 0, 0, window.width, window.height)
    -- lstg.SetTextureSamplerState("mainRdrTgt", "linear+wrap")
    global_tasks:add(function()
        while true do
            for i = 1, 60 do
                global_color.r = (i / 60) * 128
                global_color.b = (i / 60) * 128
                task.wait(1)
            end
            for i = 59, 0, -1 do
                global_color.r = (i / 60) * 128
                global_color.b = (i / 60) * 128
                task.wait(1)
            end
            global_tasks:add(function()
                for i = 1, 15 do
                    global_color.g = (i / 15) * 192
                    task.wait(1)
                end
                for i = 14, 0, -1 do
                    global_color.g = (i / 15) * 192
                    task.wait(1)
                end
            end)
        end
    end)
end
function GameExit()
end
function FrameFunc()
    global_tasks:remove_dead()
    global_tasks:resume_all()
    lstg.ObjFrame()
    lstg.BoundCheck()
    lstg.CollisionCheck(1, 2) -- group 1 and group 2
    lstg.UpdateXY()
    lstg.AfterFrame()
    timer = timer + 1
    if Keyboard.GetKeyState(Keyboard.Escape) then
        return true -- exit
    end
    return false
end
function RenderFunc()
    lstg.BeginScene()
    lstg.PushRenderTarget("mainRdrTgt")
    lstg.RenderClear(lstg.Color(255, 255, 255, 255))
    set_camera()
    lstg.ObjRender()
    local cx, cy = window.width / 2, window.height / 2
    lstg.RenderTTF("Sans", "海内存知己\n天涯若比邻\n欢迎来到 LuaSTG Sub", cx, cx, cy, cy, 1 + 4, lstg.Color(255, 0, 0, 0), 2)
    lstg.SetImageState("white", "", global_color)
    local circle_r = 300
    for i = 0, (360 - 1), (360 / 60) do
        local angle = i + timer * 0.17
        local cosv, sinv = math.cos(math.rad(angle)), math.sin(math.rad(angle))
        local hscale = 16 + 15 * math.sin(math.rad(i * 11 + timer * 0.11))
        local cr = circle_r + hscale * 8
        lstg.Render("white", cx + cr * cosv, cy + cr * sinv, angle, hscale, 1)
    end
    lstg.PopRenderTarget()
    lstg.RenderClear(lstg.Color(255, 255, 255, 255))
    set_camera()
    -- Alternate rendering method
    -- lstg.RenderTexture(
    --     "mainRdrTgt", "",
    --     {0, window.height, 0, timer, timer, lstg.Color(0xFFFFFFFF)},
    --     {window.width, window.height, 0, window.width + timer, timer, lstg.Color(0xFFFFFFFF)},
    --     {window.width, 0, 0, window.width + timer, window.height + timer, lstg.Color(0xFFFFFFFF)},
    --     {0, 0, 0, timer, window.height + timer, lstg.Color(0xFFFFFFFF)}
    -- )
    lstg.RenderRect("mainRdrTgt", 0, window.width, 0, window.height)
    lstg.EndScene()
end
Tru-Dev commented 2 years ago

Also, what did you mean by fixed-size rendertarget? I thought I couldn't resize them at all, that they had to be screen-sized...

Demonese commented 2 years ago

finally f07a4cb91452de9ee779abd03135278f0e55464e it works There is a test in data/test/sampler Press F1 and F2 to trigger window resize

Tru-Dev commented 2 years ago

Fixed, thanks so much!