Skip to content

♻ScrW ScrH#18

Open
Ty4a925 wants to merge 1 commit intoPika-Software:mainfrom
Ty4a925:main
Open

♻ScrW ScrH#18
Ty4a925 wants to merge 1 commit intoPika-Software:mainfrom
Ty4a925:main

Conversation

@Ty4a925
Copy link
Copy Markdown
Contributor

@Ty4a925 Ty4a925 commented Dec 6, 2025

1000000 iters
Before: 25.63
After: 0.27

1000000 iters
Before: 25.63
After: 0.27
@unknown-gd
Copy link
Copy Markdown
Member

unknown-gd commented Dec 13, 2025

Hello, this optimization previously existed in patches, but it caused problems with workshop addons. It is not mentioned on the wiki, but ScrW() and ScrH() are contextual. You can verify this using the code I have attached below.

---@type CamData
local camera = {
    type = "3D",
    x = 128,
    y = 128,
    w = 128,
    h = 128
}

hook.Add( "HUDPaint", "test1", function()
    print( ScrW(), ScrH() ) -- 1920	1080

    cam.Start( camera )
        cam.Start2D()
            print( ScrW(), ScrH() ) -- 128	128
        cam.End2D()
    cam.End()
end )

If you think this should be added, we can run new tests and re-check; perhaps it no longer causes problems.

@Ty4a925
Copy link
Copy Markdown
Contributor Author

Ty4a925 commented Dec 14, 2025

🤔

@Astralcircle
Copy link
Copy Markdown

i've been using this optimization for almost 2 years on my sandbox server without any problems

@unknown-gd
Copy link
Copy Markdown
Member

as I remember this addon stops working properly with this optimization

@CherrieTheShifter
Copy link
Copy Markdown

Hello, this optimization previously existed in patches, but it caused problems with workshop addons. It is not mentioned on the wiki, but ScrW() and ScrH() are contextual. You can verify this using the code I have attached below.

---@type CamData
local camera = {
    type = "3D",
    x = 128,
    y = 128,
    w = 128,
    h = 128
}

hook.Add( "HUDPaint", "test1", function()
    print( ScrW(), ScrH() ) -- 1920	1080

    cam.Start( camera )
        cam.Start2D()
            print( ScrW(), ScrH() ) -- 128	128
        cam.End2D()
    cam.End()
end )

If you think this should be added, we can run new tests and re-check; perhaps it no longer causes problems.

Okay, yes, and also:

well i feel like things are better now
so this optimization could be readded, may be fine now
could maybe even be improved or something yeah

@CherrieTheShifter CherrieTheShifter mentioned this pull request Mar 28, 2026
@CherrieTheShifter
Copy link
Copy Markdown

CherrieTheShifter commented Mar 29, 2026

Hello, this optimization previously existed in patches, but it caused problems with workshop addons. It is not mentioned on the wiki, but ScrW() and ScrH() are contextual. You can verify this using the code I have attached below.

---@type CamData
local camera = {
    type = "3D",
    x = 128,
    y = 128,
    w = 128,
    h = 128
}

hook.Add( "HUDPaint", "test1", function()
    print( ScrW(), ScrH() ) -- 1920	1080

    cam.Start( camera )
        cam.Start2D()
            print( ScrW(), ScrH() ) -- 128	128
        cam.End2D()
    cam.End()
end )

If you think this should be added, we can run new tests and re-check; perhaps it no longer causes problems.

Okay, yes, and also:

well i feel like things are better now so this optimization could be readded, may be fine now could maybe even be improved or something yeah

---@type CamData
local camera = {
type = "3D",
x = 128,
y = 128,
w = 128,
h = 128
}

do

local scr_w_cache, scr_h_cache = ScrW(), ScrH()

local _640 = 1 / 640.0
local _480 = 1 / 480.0

local scr_w_640, scr_h_480 = scr_w_cache * _640, scr_h_cache * _480

-- Store original functions BEFORE overriding
local _ScrW = ScrW
local _ScrH = ScrH

-- Camera depth counter (handles nesting safely)
local camera_depth = 0

ScrW = function()
    if camera_depth > 0 then return _ScrW() end
    return scr_w_cache
end

ScrH = function()
    if camera_depth > 0 then return _ScrH() end
    return scr_h_cache
end

ScreenScale = function(a)
    if camera_depth > 0 then return a * (_ScrW() * _640) end
    return a * scr_w_640
end

ScreenScaleH = function(a)
    if camera_depth > 0 then return a * (_ScrH() * _480) end
    return a * scr_h_480
end

hook.Add("OnScreenSizeChanged", addon_name .. " - ScreenSizes", function(_, _, w, h)
    scr_w_cache, scr_h_cache = w, h
    scr_w_640, scr_h_480 = w * _640, h * _480
end, PRE_HOOK)

-- Keep original cam functions
local old_cam_Start = cam.Start
local old_cam_End = cam.End

cam.Start = function(data)
    camera_depth = camera_depth + 1
    return old_cam_Start(data)
end

cam.End = function(...)
    camera_depth = math.max(camera_depth - 1, 0)
    return old_cam_End(...)
end

end

hook.Add("HUDPaint", "test1", function()
print(ScrW(), ScrH()) -- 1920 1080

cam.Start(camera)
    cam.Start2D()
        print(ScrW(), ScrH()) -- 128 128
    cam.End2D()
cam.End()

end)

probaly could make it better
make it not break mods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants