Jump to content

Reducing dxDrawImageOnElement's CPU usage


Lergen

Recommended Posts

Pardon if this is a dumb question,

I'm using dxDrawImageOnElement to draw a simple image over a moving element. While it works as intended and there are no issues with it, because it draws the image every frame the CPU usage is rather high; roughly in the range of about 10-11% for a single image. Is there any way to cut back on this, or suggestions for an alternate approach? I'd really appreciate any tips.

Link to comment
19 minutes ago, Tekken said:

May we see an example of how you  use it?

Sure. I experimented a bit more and the example on the wiki page actually works just fine with negligible CPU usage (~0.40%). My mistake in assuming it was the function itself, I jumped to the conclusion since I know dxDraws are generally pretty CPU heavy if they're not using a RenderTarget.

function dxDrawImageOnElement(TheElement,Image,distance,height,width,R,G,B,alpha)
	local x, y, z = getElementPosition(TheElement)
	local x2, y2, z2 = getElementPosition(localPlayer)
	local distance = distance or 20
	local height = height or 1
	local width = width or 1
	local checkBuildings = checkBuildings or true
	local checkVehicles = checkVehicles or false
	local checkPeds = checkPeds or false
	local checkObjects = checkObjects or true
	local checkDummies = checkDummies or true
	local seeThroughStuff = seeThroughStuff or false
	local ignoreSomeObjectsForCamera = ignoreSomeObjectsForCamera or false
	local ignoredElement = ignoredElement or nil
	if (isLineOfSightClear(x, y, z, x2, y2, z2, checkBuildings, checkVehicles, checkPeds , checkObjects,checkDummies,seeThroughStuff,ignoreSomeObjectsForCamera,ignoredElement)) then
		local sx, sy = getScreenFromWorldPosition(x, y, z+height)
		if(sx) and (sy) then
			local distanceBetweenPoints = getDistanceBetweenPoints3D(x, y, z, x2, y2, z2)
			if(distanceBetweenPoints < distance) then
			dxDrawMaterialLine3D(x, y, z+1+height-(distanceBetweenPoints/distance*width)-1+width, x, y, z+height, Image, width-(distanceBetweenPoints/distance*width), tocolor(R or 255, G or 255, B or 255, alpha or 255))			end
		end
	end
end

testimage = dxCreateTexture("images/test.png")
                                                
 local function getData()
		clientElement = getElementData(root,"serverElement")
end
addEventHandler('onClientResourceStart', resourceRoot, getData)
                                                
addEventHandler("onClientPreRender", root,
function()
dxDrawImageOnElement(clientElement,testimage, 300, -1, 1.8)
end)

Is it from trying to draw the image over an element synced from the server? Removing the onClientPreRender event and it's attached function completely negates the huge CPU usage it otherwise has, so it's definitely an issue somewhere from trying to draw the image.

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