Jump to content

dxCreateRenderTarget


Et-win

Recommended Posts

I was wondering which of this 2 code's is good to use correctly:

1:

sWidth, sHeight = guiGetScreenSize() 
sW, sH = 105, 32 
cFont = dxCreateFont("files/fonts/SegoeWPN_0.ttf", 32, false) 
gTarget = dxCreateRenderTarget(sW, sH, true) 
  
function testRender() 
    if (gTarget ~= false) then 
        dxSetRenderTarget(gTarget) 
        dxDrawText("CWS:Home", 0, 0, sW, sH, tocolor(0, 0, 255), 0.5, cFont, "center", "center", true) 
        dxSetRenderTarget() 
        dxDrawImage((sWidth/2)-(sW/2), (sHeight/2)-(sH/2), sW, sH, gTarget) 
    end 
end 
  
addEventHandler("onClientRender", getRootElement(), testRender) 

Or 2:

sWidth, sHeight = guiGetScreenSize() 
sW, sH = 105, 32 
cFont = dxCreateFont("files/fonts/SegoeWPN_0.ttf", 32, false) 
  
function testRender() 
    if (gTarget ~= false) then 
        gTarget = dxCreateRenderTarget(sW, sH, true) 
        dxSetRenderTarget(gTarget) 
        dxDrawText("CWS:Home", 0, 0, sW, sH, tocolor(0, 0, 255), 0.5, cFont, "center", "center", true) 
        dxSetRenderTarget() 
        dxDrawImage((sWidth/2)-(sW/2), (sHeight/2)-(sH/2), sW, sH, gTarget) 
    end 
end 
  
addEventHandler("onClientRender", getRootElement(), testRender) 

It's about the dxCreateRenderTarget code I am using. I'm not sure which one is correct to use. Does it have to be in or out the render function? (I don't know (yet) or this depends on it: But I'm going to move the text in dxCreateRenderTarget)

Link to comment

First one definitely, second one creates render target again and again (it will not cause bugs, but it will be unnoticeably slower). I'm sure the wiki has the examples with render target created outside the onClientRender.

Link to comment

Both are wrong, but second one is closest to right.

Render targets are textures and they can be created outside onClientRender. They should be created once, and when you want to update it you should use dxSetRenderTarget(gTarget) just like you are doing (this can be called in onClientRender or outside) and then draw stuff on it just like you are doing right now and then call dxSetRenderTarget() which you are doing right.

But since dxCreateRenderTarget creates a texture like dxCreateTexture does and is a dx function, you have to draw it each frame (in onClientRender), so dxDrawImage with gTarget has to be done always in onClientRender if you want it to be visible.

Important notes: when the mta loses focus (alt-tab is used, etc) then render targets are cleared so they are empty, and you have to redraw everything in the render target. onClientRestore is called when mta is back in focus again and you can draw on the render target again there. onClientRestore has a parameter "didClearRenderTargets" but dont check if it's true, instead always redraw to render target when onClientRestore is called.

Text may have bad quality in render targets, so you should experiment with dxSetBlendMode (look it up on mta wiki for example) for better visual quality.

Another important thing to remember is that when you are drawing in render targets you cant enable postGUI (for example in dxDrawText) since then the text wont be visible in the render target, but you can draw the render target with postGUI enabled.

Here is an example using your example as base:

sWidth, sHeight = guiGetScreenSize() 
sW, sH = 105, 32 
cFont = dxCreateFont("files/fonts/SegoeWPN_0.ttf", 32, false) 
gTarget = dxCreateRenderTarget(sW, sH, true) 
  
function updateRenderTarget() 
    -- Set second argument to true to clear the render target 
    -- before drawing on it. 
    dxSetRenderTarget(gTarget, true) 
    -- Clipping the text here using same width and height 
    -- as the render target is not necessary, since the render target does that automatically. 
    dxDrawText("CWS:Home", 0, 0, sW, sH, tocolor(0, 0, 255), 0.5, cFont, "center", "center") 
    dxSetRenderTarget() 
end 
updateRenderTarget() 
addEventHandler("onClientRestore", root, updateRenderTarget) 
  
function testRender() 
    dxDrawImage((sWidth/2)-(sW/2), (sHeight/2)-(sH/2), sW, sH, gTarget) 
end 
addEventHandler("onClientRender", getRootElement(), testRender) 

  • Like 1
Link to comment

Thanks for the reply everyone, it made me wiser.

function updateRenderTarget() 
    -- Set second argument to true to clear the render target 
    -- before drawing on it. 
    dxSetRenderTarget(gTarget, true) 
    -- Clipping the text here using same width and height 
    -- as the render target is not necessary, since the render target does that automatically. 
    dxDrawText("CWS:Home", 0, 0, sW, sH, tocolor(0, 0, 255), 0.5, cFont, "center", "center") 
    dxSetRenderTarget() 
end 
updateRenderTarget() 
addEventHandler("onClientRestore", root, updateRenderTarget) 

Actually if I understand (And tested it) right, this only gets called when MTA loses focus. I want to make the text inside this render target moving, which means I have to re-draw the text on every frame. (As soon as the resource starts, the text is moving permanently) So that would mean, can I also put a render on this function? Because I don't see another way to keep editing the positions of the text without render?

EDIT:

I now have this:

sWidth, sHeight = guiGetScreenSize() 
sW, sH = 105, 32 
cFont = dxCreateFont("files/fonts/SegoeWPN_0.ttf", 32, false) 
gTarget = dxCreateRenderTarget(sW, sH, true) 
  
function updateRenderTarget() 
    dxSetRenderTarget(gTarget, true) 
    dxSetBlendMode("modulate_add") 
    dxDrawText("CWS:Home", 0, 0, sW, sH, tocolor(0, 0, 255), 0.5, cFont, "center", "center") 
    dxSetBlendMode("blend") 
    dxSetRenderTarget() 
    dxSetBlendMode("add") 
    dxDrawImage((sWidth/2)-(sW/2), (sHeight/2)-(sH/2), sW, sH, gTarget) 
    dxSetBlendMode("blend") 
end 
  
addEventHandler("onClientRender", root, updateRenderTarget) 

It works fine, but just want to be sure or it isn't going to cause any problems( later). (I do understand how this works now, but still want to be sure, because it's permanently running. :roll: )

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...