HUD that fits all resolutions?

I’m completely lost. I can’t seem to figure out how to make the HUD stay in a constant position, no matter what resolution the client is running. I have tried multiple different ways to go about this, but none of them seem to work.


local SCREEN_W, SCREEN_H = 1600, 900;
local _w, _h = ScrW( ), ScrH( );
local _wMod, _hMod = _w / SCREEN_W, _h / SCREEN_H;

The above code is located under HUD ConVars. SCREEN_W and SCREEN_H are equal to the the resolution I used to build the HUD.


surface.SetMaterial(dzghud)
	surface.SetDrawColor(256,256,256,255)
	surface.DrawTexturedRect(_wMod*-30,_hMod*520,575,575)
	draw.RoundedBox(5, _wMod*188, _hMod*845, 170, 20, Color(25,25,25,190))
	draw.RoundedBox(1, _wMod*191, _hMod*848, health, 14, Color(255,0,0,190))
	draw.RoundedBox(5, _wMod*363, _hMod*845, 170, 20, Color(25,25,25,190))
	draw.RoundedBox(1, _wMod*366, _hMod*848, armor, 14, Color(0,198,237,190))
	surface.SetMaterial(job)
	surface.SetDrawColor( 0, 150, 120, 255 )
	surface.DrawTexturedRect(_wMod*187,_hMod*820,20,20)
	surface.SetMaterial(wallet)
	surface.SetDrawColor( 240, 240, 0, 255 )
	surface.DrawTexturedRect(_wMod*187,_hMod*795,20,20)
	surface.SetMaterial(salary)
	surface.SetDrawColor( 240, 240, 0, 255 )
	surface.DrawTexturedRect(_wMod*284,_hMod*795,20,20)
	draw.SimpleText("- $" .. LocalPlayer().DarkRPVars.salary,"TargetID", 180 + 30,ScrH() - 102, Color(255,255,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
	draw.SimpleText("- $" .. LocalPlayer().DarkRPVars.money,"TargetID", 276 + 30,ScrH() - 102, Color(255,255,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
	draw.SimpleText("- " .. LocalPlayer().DarkRPVars.job,"TargetID", 180 + 30,ScrH() - 76, Color(255,255,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)

This is the code I am using to draw the HUD. It is located under local function DrawHUD(). Is there any way to make the HUD draw at a constant position on the screen for players using different resolutions? Any help would be appreciated, thank you!

HUD by default draws at a constant position.

I would also not recommend doing it the way you do, but your problem is that you are not using your “_wMod” variables in draw.SimpleText calls.

Well when I uploaded it to my server, people were telling me it looked all types of fucked. --> http://i.imgur.com/UGUOM43.png This is what the HUD looks like to other players, for me, it looks completely fine… And that was when I tried hardcoding the position vectors into the script because I also figured it would draw at a constant position by default… And I will put the “_wMod” and “_hMod” in the draw.SimpleText’s right now.

yeah the hud looks fucked for me! i use 16:9 1920x1080

Use these tags, ScrH() / [number] ScrW / [number]

I’ve already tried that, it just barely moves the .png down, and nothing else… weird.

You must either use SrcW() and ScrH() in ALL your positions, or use “_wMod” and “_HMod” in ALL your calculations.

May I add you on Steam?

the way I do it is functions like these:[lua]local sx = function(i) return i * (ScrW() / 1920) end
local sy = function(j) return j * (ScrH() / 1080) end[/lua]where 1920x1080 is the resolution I use
the problem with this though is that aspect ratio isn’t maintained, and someone on a different aspect ratio’s hud will be stretched out/squished

What does i and j represent? Sorry, I’m a noob ahha

I think it’s just a custom implementation of http://wiki.garrysmod.com/page/Global/ScreenScale