DxCreateRenderTarget: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Fixed example, now its works fine)
(Update example to actually make sense and use this feature properly)
Line 33: Line 33:
==Example==
==Example==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local myRenderTarget = dxCreateRenderTarget(500, 500, true)
local myRenderTarget
 
addEventHandler("onClientResourceStart", resourceRoot,
addEventHandler("onClientResourceStart", resourceRoot,
     function()
     function()
         myRenderTarget = dxCreateRenderTarget( 80, 100 )           -- Create a render target texture which is 80 x 100 pixels
         myRenderTarget = dxCreateRenderTarget(250, 100, true)     -- Create a render target
 
        if (myRenderTarget) then
            updateRenderTarget()
        end
     end
     end
)
)
Line 43: Line 48:
     function()
     function()
         if myRenderTarget then
         if myRenderTarget then
             dxSetRenderTarget( myRenderTarget )                    -- Start drawing on myRenderTarget
             -- Draw the render target lots of times in different positions on the screen
            dxDrawText ( "Hello", 10, 20 )                          -- Draw a message
             dxDrawImage(350, 50, 250, 100, myRenderTarget)
            dxSetRenderTarget()                                    -- Stop drawing on myRenderTarget
             dxDrawImage(450, 380, 250, 100, myRenderTarget)
 
             dxDrawImage(550, 250, 250, 100, myRenderTarget)
             dxDrawImage( 50, 50, 100, 100, myRenderTarget )       -- Now use myRenderTarget as a material and draw it lots of times
             dxDrawImage(650, 70, 250, 100, myRenderTarget)
             dxDrawImage( 150, 350, 150, 100, myRenderTarget )
             dxDrawImage( 250, 250, 100, 150, myRenderTarget )
             dxDrawImage( 350, 30, 150, 150, myRenderTarget )
         end
         end
     end
     end
Line 62: Line 64:
     dxSetBlendMode("modulate_add")  -- Set 'modulate_add' when drawing stuff on the render target
     dxSetBlendMode("modulate_add")  -- Set 'modulate_add' when drawing stuff on the render target


     dxDrawText("Testing "..getTickCount(), 0, 0, 0, 0, tocolor(255, 255, 255, 255), 2, "clear")
     dxDrawText("Hello " .. getTickCount(), 10, 10, 0, 0, tocolor(255, 255, 255, 255), 2, "clear")   -- Draw a message
    dxDrawRectangle(10, 50, 40, 40, tocolor(math.random(255), math.random(255), math.random(255)))  -- Draw a square with random color


     dxSetBlendMode("blend")        -- Restore default blending
     dxSetBlendMode("blend")        -- Restore default blending
     dxSetRenderTarget()            -- Restore default render target
     dxSetRenderTarget()            -- Restore default render target
end
end
bindKey("r", "down", updateRenderTarget )
bindKey("r", "down", updateRenderTarget)


</syntaxhighlight>
</syntaxhighlight>

Revision as of 16:36, 21 August 2021

This function creates a render target element, which is a special type of texture that can be drawn on with the dx functions. Successful render target creation is not guaranteed, and may fail due to hardware or memory limitations.

To see if creation is likely to fail, use dxGetStatus. (When VideoMemoryFreeForMTA is zero, failure is guaranteed.)

[[{{{image}}}|link=|]] Tip: Use dxSetBlendMode to get better quality
[[{{{image}}}|link=|]] Tip: It is highly recommended that dxSetTestMode is used when writing and testing scripts using dxCreateRenderTarget.
[[{{{image}}}|link=|]] Note: Render targets are usually cleared when the player minimizes MTA (i.e. alt-tab). See onClientRestore for details on when to restore any fixed content.

Syntax

element dxCreateRenderTarget ( int width, int height [, bool withAlpha = false ] )

OOP Syntax Help! I don't understand this!

Method: DxRenderTarget(...)


Required Arguments

  • width : The width of the texture in pixels.
  • height : The height of the texture in pixels.
  • withAlpha: The render target will be created with an alpha channel. 'false' will turn images' alpha channels to black color

Returns

Returns a texture element if successful, false if the system is unable to create a render target.

You should always check to see if this function has returned false.

Explanation

What is a rendertarget? A rendertarget is like a big, white paper(or if you set alpha to true, it will be a transparent paper) that you can draw on, after you drawn on it, you can draw it as many times as you like, without impacting performance. It could be used for dashboard, where a few hundred dxDraw* functions are called, so, instead of calling these every frame, you can just draw it on a render target, and draw the render target, instead of every information one by one.


Example

local myRenderTarget

addEventHandler("onClientResourceStart", resourceRoot,
    function()
        myRenderTarget = dxCreateRenderTarget(250, 100, true)     -- Create a render target

        if (myRenderTarget) then 
            updateRenderTarget()
        end
    end
)

addEventHandler( "onClientRender", root,
    function()
        if myRenderTarget then
            -- Draw the render target lots of times in different positions on the screen
            dxDrawImage(350, 50, 250, 100, myRenderTarget)
            dxDrawImage(450, 380, 250, 100, myRenderTarget)
            dxDrawImage(550, 250, 250, 100, myRenderTarget)
            dxDrawImage(650, 70, 250, 100, myRenderTarget)
        end
    end
)

--
-- Function to draw text to our render target with '''modulate_add''' blend mode when the 'r' key is pressed
--
function updateRenderTarget()
    dxSetRenderTarget(myRenderTarget, true)
    dxSetBlendMode("modulate_add")  -- Set 'modulate_add' when drawing stuff on the render target

    dxDrawText("Hello " .. getTickCount(), 10, 10, 0, 0, tocolor(255, 255, 255, 255), 2, "clear")    -- Draw a message
    dxDrawRectangle(10, 50, 40, 40, tocolor(math.random(255), math.random(255), math.random(255)))   -- Draw a square with random color

    dxSetBlendMode("blend")         -- Restore default blending
    dxSetRenderTarget()             -- Restore default render target
end
bindKey("r", "down", updateRenderTarget)

See Also