Jump to content

How to use properly the dxCreateRenderTarget?


Recommended Posts

Hi guys, I'm wondering how to use this dxCreateRenderTarget in a proper way. So i have this script:
I'm trying to make a fade animation, when the window comes up and down, but nothing appears. Anyone can help me how to use this?

local window = {}

window.windowWidth = 600
window.headerHeight = 600
window.renderTarget.mainHeader = dxCreateRenderTarget(window.windowWidth, window.headerHeight, true)

function window.drawHeader()
  	dxSetRenderTarget(window.renderTarget.mainHeader, true)
  
	dxDrawRectangle(0, 0, screenWidth, screenHeight, tocolor(10, 10, 10, math_min(window.colors.alpha, 100)))

    dxDrawImage(window.headerX, window.headerY, window.windowWidth, window.headerHeight, textures.header_bg, 0, 0, 0, tocolor(30, 30, 30, math.min(window.colors.alpha, 250)), false)
  
	window.drawMatchScore()
  
  	dxSetRenderTarget()
end

function window.drawHeaderInfos()
	-- ofc there is more data, but its not relevant 
	dxDrawText(vsText, vsTextX, vsTextY, vsTextX, vsTextY, tocolor(230, 230, 230, window.colors.alpha), window.fonts.size, window.fonts.fontSize)
end

local function updateScoreboard()
    local now = getTickCount()
    local elapsedTime = now - window.fadeStartTime
    local progress = math_min(elapsedTime / window.fadeDuration, 1)
    
    window.currentAlpha = interpolateBetween(
        window.fadeStartAlpha, 0, 0,
        window.targetAlpha, 0, 0,
        progress, "Linear")
    
    window.colors.alpha = math_floor(window.currentAlpha)
	
	if window.renderTarget.mainHeader then
		dxDrawImage(window.headerX, window.headerY, window.windowWidth, window.headerHeight, window.renderTarget.mainHeader)
	end
	
    if progress == 1 and not window.isVisible and window.currentAlpha == 0 then
        removeEventHandler("onClientRender", root, updateScoreboard)
		window.isHandlerAdded = false 
    end
end

local function toggleScoreboard(state)
    window.isVisible = state
    window.targetAlpha = state and 255 or 0
    window.fadeStartTime = getTickCount()
    window.fadeStartAlpha = window.currentAlpha
	
	if not window.isHandlerAdded then
        addEventHandler("onClientRender", root, updateScoreboard)
        window.isHandlerAdded = true 
    end
end

bindKey("H", "down", function()
	window.drawHeader()
    toggleScoreboard(true)
end)

bindKey("H", "up", function()
    toggleScoreboard(false)
end)

 

Link to comment

In dxDrawRectangle you are using screenWidth and screenHeight, which are probably the dimensions of the screen, but your render target has a size defined by window.windowWidth and window.headerHeight. You are using math_min(window.colors.alpha, 100) and math.min(window.colors.alpha, 250) in different parts of your rendering. you are trying to render the window header to a dxRenderTarget. When calling dxSetRenderTarget you are projecting at the correct size and when a projected window appears, it is being called within onClientRender correctly. Don't forget to call dxSetRenderTarget() at the end of the window.drawHeader() function. Fix your code.

 

janela = {} 

janela.windowWidth = 600 
janela.headerHeight = 600 
janela.renderTarget = { mainHeader = dxCreateRenderTarget(janela.windowWidth, janela.headerHeight, true) }
janela.cores = { alfa = 0 }
janela.currentAlpha = 0
janela.fadeDuration = 1000
janela.isVisible = false
janela.isHandlerAdded = false

function lxfunction1() 
    dxSetRenderTarget(janela.renderTarget.mainHeader, true) 
  
    dxDrawRectangle(0, 0, janela.windowWidth, janela.headerHeight, tocolor(10, 10, 10, math.min(janela.cores.alfa, 100)))
    dxDrawImage(0, 0, janela.windowWidth, janela.headerHeight, "path_to_texture.png", 0, 0, 0, tocolor(30, 30, 30, math.min(janela.cores.alfa, 250)))

    dxSetRenderTarget()
end

function lxfunction2() 
    local now = getTickCount() 
    local elapsedTime = now - janela.fadeStartTime
    local progress = math.min(elapsedTime / janela.fadeDuration, 1)  

    janela.currentAlpha = interpolateBetween(janela.fadeStartAlpha, 0, 0, janela.targetAlpha, 0, 0, progress, "Linear")
    janela.cores.alfa = math.floor(janela.currentAlpha)

    if janela.renderTarget.mainHeader then 
        dxDrawImage(janela.headerX, janela.headerY, janela.windowWidth, janela.headerHeight, janela.renderTarget.mainHeader)
    end  

    if progress == 1 and not janela.isVisible and janela.currentAlpha == 0 then 
        removeEventHandler("onClientRender", root, lxfunction2) 
        janela.isHandlerAdded = false
    end
end    

function lxfunction3(state) 
    janela.isVisible = state  
    janela.targetAlpha = state and 255 or 0 
    janela.fadeStartTime = getTickCount() 
    janela.fadeStartAlpha = janela.currentAlpha    

    if not janela.isHandlerAdded then 
        addEventHandler("onClientRender", root, lxfunction2) 
        janela.isHandlerAdded = true
    end
end    

bindKey("H", "down", function() 
    lxfunction1() 
    lxfunction3(true)
end)

bindKey("H", "up", function() 
    lxfunction3(false)
end)

 

Edited by Laxante101
  • Like 1
Link to comment

Thank your for your reply. By the way, dxDrawRectangle should cover the full size of the screen. The other part, including dxDrawImage and everything inside window.drawHeaderInfos(), should be drawn using dxSetRenderTarget. I want the entire header to use dxSetRenderTarget.

That's why I asked what the proper way to use it. If there are multiple hud elements (functions) one after the other, for example

and pls don't rename "window" to "janela" bcs its hard to read that way :D

Edited by Hiding
Link to comment
2 hours ago, Hiding said:

Obrigado pela sua resposta. A propósito, dxDrawRectangle deve cobrir o tamanho total da tela. A outra parte, incluindo dxDrawImage e tudo dentro de window.drawHeaderInfos(), deve ser desenhada usando dxSetRenderTarget. Quero que o cabeçalho inteiro use dxSetRenderTarget.

É por isso que perguntei qual a maneira correta de usá-lo. Se houver vários elementos hud (funções) um após o outro, por exemplo,

e por favor, não renomeie "window" para "janela" porque é difícil de ler dessa forma:D

Sorry man, my browser's translator automatically translates and sends it wrong.🤣

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...