Jump to content

DxDraw Lag


'~DaLesTe^'

Recommended Posts

Posted (edited)
Spoiler

 local drawDistance = 100.0
g_StreamedInPlayers = {}

function drawHPBar( x, y, v, d)
    if(v < 0.0) then
        v = 0.0
    elseif(v > 100.0) then
        v = 100.0
    end
    dxDrawRectangle(x - 36, y + 10, 84, 7, tocolor ( 0, 0, 0, 255-d ))
    dxDrawRectangle(x - 35, y + 11, v/1.21 , 5.5, tocolor ( (200-v) * 2.55, (v * 5.55), 0, 255-d ))
end

function drawArmourBar( x, y, v, d)
    if(v < 0.0) then
        v = 0.0
    elseif(v > 100.0) then
        v = 100.0
    end
    dxDrawRectangle(x - 36, y + 8, 84, 7, tocolor ( 0, 0, 0, 255-d ))
    dxDrawRectangle(x - 35, y + 9, v/1.21 , 5.5, tocolor ( 200, 255, 250, 255-d ))
end

function drawHud()
    -- > 573 = baixo

    healthColor = tocolor (0,0,0,255)
    healthbgColor = tocolor (255,151,0,127)
    healthfgColor = tocolor (255,151,0,185)
    sx,sy = guiGetScreenSize ()
    healthx = sx/800*683
    healthy = sy/600*89
    healthxoverlay = sx/800*685
    healthyoverlay = sy/600*91

    vehiclehealthx = sx/800*619
    vehiclehealthy = sy/600*169
    vehiclehealthxoverlay = sx/800*621
    vehiclehealthyoverlay = sy/600*171

    if (not normalhealthbar) then

        local health = getElementHealth(getLocalPlayer())
        local armour = getPedArmor(getLocalPlayer())
        local rate =  500 / getPedStat(getLocalPlayer(),24)
        if (getElementHealth(getLocalPlayer()) == 0) then
            if (getTickCount() - visibleTick < 500) then
                local healthRelative = health*rate/100
                local v = health*rate
                dxDrawRectangle (healthx, healthy, 76, 12, healthColor, false)
                dxDrawRectangle (healthxoverlay, healthyoverlay, 62, 8, tocolor((100-v) * 2.55, (v * 2.55), 0, 127), false)
                dxDrawRectangle (healthxoverlay, healthyoverlay, 62*healthRelative, 8, tocolor((100-v) * 2.55, (v * 2.55), 0, 185), false)
            else
                if(getTickCount() - visibleTick >= 1000) then
                    visibleTick = getTickCount()
                end
            end
        else
            local healthRelative = health*rate/100
            local v = health*rate
            dxDrawRectangle (healthx, healthy, 76, 12, healthColor, false)
            dxDrawRectangle (healthxoverlay, healthyoverlay, 72, 8, tocolor((100-v) * 2.55, (v * 2.55), 0, 127), false)
            dxDrawRectangle (healthxoverlay, healthyoverlay, 72*healthRelative, 8, tocolor((100-v) * 2.55, (v * 2.55), 0, 185), false)
        end
    end
end

function un:O(text)
    return string.gsub(text, "(#%x%x%x%x%x%x)", function(colorString) return "" end)
end

function onClientRender( )
    --drawHud()
    local cx, cy, cz, lx, ly, lz = getCameraMatrix()
    for k, player in pairs(g_StreamedInPlayers) do
        if isElement(player) and isElementStreamedIn(player) then
            local vx, vy, vz = getPedBonePosition(player, 8)
            local dist = getDistanceBetweenPoints3D(cx, cy, cz, vx, vy, vz )
            if dist < drawDistance then
                if( isLineOfSightClear(cx, cy, cz, vx, vy, vz, true, false, false) ) then
                    local x, y = getScreenFromWorldPosition (vx, vy, vz + 0.3)
                    if(x and y) then
                        local name = getPlayerName(player)
                        --local w = dxGetTextWidth(name, 1, "default-bold")
                        --local h = dxGetFontHeight(1, "default-bold")
                        if getPlayerTeam(player) then
                            local pR, pG, pB = getTeamColor(getPlayerTeam(player))
                            local playertag = getElementData(player,"gang.tag") or ""
                            local finaltag = rgb2hex(pR,pG,pB) .. "" .. playertag
                            local w = dxGetTextWidth(rgb2hex(pR,pG,pB) .. "" .. playertag .. "" .. name .. " (ID:" .. getPlayerID(player) .. ")", 1.5, "default-bold")
                            local h = dxGetFontHeight(1.5, "default-bold")
                            local tw = dxGetTextWidth(getTeamName(getPlayerTeam(player)), 1, "default-bold")
                            local th = dxGetFontHeight(1, "default-bold")
                            --dxDrawText(finaltag .. "" .. un:O(name) .. " (ID:" .. getPlayerID(player) .. ")", x + 1 - 30 - w / 6,y - h - 6 + 1, w, h, tocolor(0,0,0), 1, "default-bold", true, true, true, true)
                            dxDrawColoredText(finaltag .. "" .. name .. " (ID:" .. getPlayerID(player) .. ")", x - 30 - w / 6,y - h - 6, w, h, tocolor(getPlayerNametagColor(player)),1.5, "default-bold")
                            dxDrawColoredText(rgb2hex(pR,pG,pB) .."" .. getTeamName(getPlayerTeam(player)), x - 30 - tw / 6,y - th - 30, tw, th, tocolor(getPlayerNametagColor(player)),1, "default-bold")
                        else
                            local w = dxGetTextWidth(name .. "(ID:" .. getPlayerID(player) .. ")", 1, "default-bold")
                            local h = dxGetFontHeight(1, "default-bold")
                            --dxDrawText(un:O(name) .. "(ID:" .. getPlayerID(player) .. ")", x + 1 - 30 - w / 6,y - h - 6 + 1, w, h, tocolor(0,0,0), 1, "default-bold")
                            dxDrawColoredText(name .. "(ID:" .. getPlayerID(player) .. ")", x - 30 - w / 6,y - h - 6, w, h, tocolor(getPlayerNametagColor(player)), 1, "default-bold")            
                        end
                        local health = getElementHealth ( player )
                        local armour = getPedArmor ( player )
                        if(health > 0.0) then
                            local rate =  500 / getPedStat(player,24)
                            drawHPBar(x, y-6.0, health*rate, dist)
                        if(armour > 0.0) then
                            drawArmourBar(x, y-12.0, armour, dist)
                            end
                        end
                    end
                end
            end
        else
            table.remove(g_StreamedInPlayers, k)
        end
    end
end
addEventHandler("onClientRender", root, onClientRender)

function onClientElementStreamIn()
    if getElementType(source) == "player"  then
        if source ~= getLocalPlayer() then
            setPlayerNametagShowing(source, false)
            table.insert(g_StreamedInPlayers, source)
        end
    end
end
addEventHandler("onClientElementStreamIn", root, onClientElementStreamIn)

function onClientResourceStart(startedResource)

    visibleTick = getTickCount()
    counter = 0
    normalhealthbar = false

    local players = getElementsByType("player")
    for k, v in pairs(players) do
        if isElementStreamedIn(v) then
            if v ~= getLocalPlayer() then
                setPlayerNametagShowing(v, false)
                table.insert(g_StreamedInPlayers, v)
            end
        end
    end
end
addEventHandler("onClientResourceStart", resourceRoot, onClientResourceStart )

local screen_width, screen_height = guiGetScreenSize()

function dxDrawColoredText(text, left, top, right, bottom, color, scale, font, alignX, alignY, clip, wordBreak, postGUI)
    -- Making them optional.
    right = right or screen_width
    bottom = bottom or screen_height
    while(left < 0) do
        left = screen_width - math.abs(left)
    end
    while(top < 0) do
        top = screen_height - math.abs(top)
    end
    while(right < 0) do
        right = screen_width - math.abs(right)
    end
    while(bottom < 0) do
        bottom = screen_height - math.abs(bottom)
    end
    if(right > screen_width)then right = screen_width end
    if(bottom > screen_height)then bottom = screen_height end
    color = color or tocolor(255, 255, 255, 200)
    scale = scale or 1
    font = font or "default"
    alignX = alignX or "left"
    alignY = alignY or "top"
    clip = clip or false
    wordBreak = wordBreak or false
    postGUI = postGUI or true
    local alpha = intToAlpha(color)
    local text_width = 0
    local offset = 0
    local text_height = dxGetFontHeight(scale, font)
    local TEXT_WIDTH = dxGetTextWidth(text:gsub("#%x%x%x%x%x%x", ""), scale, font)
    local off = -TEXT_WIDTH
    local width = 0
    local height = 0
    if(alignX == "center")then
        left = left + width/2 - TEXT_WIDTH/2
    end
    if(alignX == "right")then
        left = left + width - TEXT_WIDTH
    end
    if(alignY == "center")then
        top = top + height/2 - text_height/2
    end
    if(alignY == "bottom")then
        top = top + height - text_height
    end
    alignX = "left"
    alignY = "top"
    
    -- 0 index ?
    local col1, col2 = string.find(text, "#%x%x%x%x%x%x")
    if col1 ~= nil then col1 = col1-1 end
    
    -- draw text with the color we sent until we find hexadecimal code.
    for i = 1, col1 or string.len(text) do
        text_width = dxGetTextWidth(string.sub(text, i, i), scale, font)
        dxDrawText(string.sub(text, i, i), left + offset, top, right-off+offset, bottom, color, scale, font, alignX, alignY, clip, wordBreak, postGUI)
        offset = offset + text_width
    end
    while(string.find(text, "#%x%x%x%x%x%x", i))do
        local hex1, hex2 = string.find(text, "#%x%x%x%x%x%x")
        local r, g, b, a = getColorFromString(string.sub(text, hex1, hex2))
        text = string.sub(text, hex2 + 1)
        hex1, hex2 = string.find(text, "#%x%x%x%x%x%x")
        if hex1 ~= nil then hex1 = hex1-1 end
        for i = 1, hex1 or string.len(text) do
            text_width = dxGetTextWidth(string.sub(text, i, i), scale, font)
            dxDrawText(string.sub(text, i, i), left + offset, top, right-off+offset, bottom, tocolor(r, g, b, alpha), scale, font, alignX, alignY, clip, wordBreak, postGUI)
            offset = offset + text_width
        end
    end
end

function intToAlpha(color)
    local a = 16777216
    local red, green, blue, alpha = 0, 0, 0, 0
    local ap = 1
    
    if(color < 0)then
        color = 2147483648 + (-color)
        alpha = 383
        ap = -ap
    end
    
    while(color >= a)do
        alpha = alpha + ap
        color = color - a
    end
    return alpha
end


function getPlayerFromID(id)
return call(getResourceFromName("game_id"), "getPlayerFromID", tonumber(id))
end
 
function getPlayerID(player)
return  getElementData(player,"id")
end

function rgb2hex(r,g,b)
  return string.format("#%02X%02X%02X", r,g,b)
end

 

 

When a player change the skin several times this script causes lag. Could you tell me what causes this?

Edited by '~DaLesTe^'
Posted

Oh good heavens... Oh well, first for all try and narrow it down a bit so you know what function causes the lagging. Also are you sure this is the right script? I can't seem to find anything obvious to do with skin changing - but rather mostly about drawing HUD elements?

Posted
21 minutes ago, Dealman said:

Oh good heavens... Oh well, first for all try and narrow it down a bit so you know what function causes the lagging. Also are you sure this is the right script? I can't seem to find anything obvious to do with skin changing - but rather mostly about drawing HUD elements?

It is how to create several DrawRectangle when changing skin.

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