DxDrawLine: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 27: Line 27:
Example code for drawing crosshair for Tank.
Example code for drawing crosshair for Tank.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local rootElement = getRootElement()
local width, height = guiGetScreenSize ( )    -- Get player's screen resolution (width and height).
local x,y = guiGetScreenSize() -- Get players resolution.
local centerX, centerY = ( x / 2 ), ( y / 2 )  -- Get x and y co-ordinates of the center of the screen.
 
local colGreen = tocolor ( 0, 255, 0, 255 )   -- Get green color.


function create2DLine ( )
function create2DLine ( )
dxDrawLine ( x/2, y/2-200, x/2, y/2-170, tocolor ( 0, 255, 0, 255 ), 1 )       -- Draw vertical crosshair line.
  dxDrawLine ( centerX, centerY - 200, centerX, centerY - 170, colGreen, 1 )             -- Draw vertical crosshair line
dxDrawLine ( x/2-20, y/2-185, x/2+20, y/2-185, tocolor ( 0, 255, 0, 255 ), 1 ) -- Draw horizontal crosshair line.
  dxDrawLine ( centerY - 20, centerY - 185, centerX + 20, centerY - 185, colGreen, 1 )   -- Draw horizontal crosshair line
end
end




function HandleTheRendering()
function HandleTheRendering ( )
addEventHandler("onClientRender",rootElement, create2DLine)  -- Keep the line visible with onClientRender.
  addEventHandler ( "onClientRender", root, create2DLine)  -- Keep the line visible with onClientRender event.
end
end
addEventHandler("onClientResourceStart",resourceRoot, HandleTheRendering)
 
addEventHandler ( "onClientResourceStart", resourceRoot, HandleTheRendering )
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 19:28, 26 December 2011

This function draws a 2D line across the screen - rendered for one frame. This should be used in conjunction with onClientRender in order to display continuously.

Syntax

bool dxDrawLine ( int startX, int startY, int endX, int endY, int color, [float width=1, bool postGUI=false] )

Required Arguments

  • startX: An integer representing the absolute start X position of the line, represented by pixels on the screen.
  • startY: An integer representing the absolute start Y position of the line, represented by pixels on the screen.
  • endX: An integer representing the absolute end X position of the line, represented by pixels on the screen.
  • endY: An integer representing the absolute end Y position of the line, represented by pixels on the screen.
  • color: An integer of the hex color, produced using tocolor or 0xAARRGGBB (AA = alpha, RR = red, GG = green, BB = blue).

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • width: The width/thickness of the line
  • postGUI: A bool representing whether the line should be drawn on top of or behind any ingame GUI (rendered by CEGUI).

Returns

Returns a true if the operation was successful, false otherwise.

Example

Click to collapse [-]
Client

Example code for drawing crosshair for Tank.

local width, height = guiGetScreenSize ( )     -- Get player's screen resolution (width and height).
local centerX, centerY = ( x / 2 ), ( y / 2 )  -- Get x and y co-ordinates of the center of the screen.
local colGreen = tocolor ( 0, 255, 0, 255 )    -- Get green color.

function create2DLine ( )
   dxDrawLine ( centerX, centerY - 200, centerX, centerY - 170, colGreen, 1 )              -- Draw vertical crosshair line
   dxDrawLine ( centerY - 20, centerY - 185, centerX + 20, centerY - 185, colGreen, 1 )    -- Draw horizontal crosshair line
end  


function HandleTheRendering ( )
   addEventHandler ( "onClientRender", root, create2DLine)  -- Keep the line visible with onClientRender event.
end

addEventHandler ( "onClientResourceStart", resourceRoot, HandleTheRendering )

See Also