Skip to content

Commit

Permalink
fix draw.SimpleText not working due to fonts not resolving + try impl…
Browse files Browse the repository at this point in the history
…ementing min/mag filtering (failing)
  • Loading branch information
luttje committed Aug 12, 2024
1 parent 217547d commit 19b9b75
Show file tree
Hide file tree
Showing 28 changed files with 240 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,10 @@ CT_DOWNTRODDEN = 1
CT_REFUGEE = 2
CT_REBEL = 3
CT_UNIQUE = 4

TEXFILTER = {
NONE = 0,
POINT = 1,
LINEAR = 2,
ANISOTROPIC = 3,
}
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,11 @@ else
}

render.SetModelLighting = render.SetAmbientLightCube
render.ResetModelLighting = render.ResetAmbientLightCube
render.ResetModelLighting = render.ResetAmbientLightCube
render.PushFilterMin = render.PushFilterMinification
render.PopFilterMin = render.PopFilterMinification
render.PushFilterMag = render.PushFilterMagnification
render.PopFilterMag = render.PopFilterMagnification

function render.Clear(r, g, b, a, clearDepth, clearStencil)
render.ClearBuffers(true, clearDepth or false, clearStencil or false)
Expand Down Expand Up @@ -981,11 +985,22 @@ else
surface.DrawRect = surface.DrawFilledRect
surface.DrawTexturedRectUV = surface.DrawTexturedSubRect
surface.GetTextPos = surface.DrawGetTextPos
surface.SetFont = surface.DrawSetTextFont
surface.SetTextPos = surface.DrawSetTextPos
surface.SetTextColor = surface.DrawSetTextColor
surface.DrawText = surface.DrawPrintText
surface.SetTexture = surface.DrawSetTexture
surface.SetTexture = surface.DrawSetTexture

local currentFont
surface.SetFont = function(font)
currentFont = font
surface.DrawSetTextFont(font)
end

local oldTextSize = Surface.GetTextSize

surface.GetTextSize = function(text)
return oldTextSize(currentFont, text)
end

surface.SetMaterial = function(material)
local name = material:GetString("$basetexture")
Expand Down
2 changes: 1 addition & 1 deletion game/experiment/bin/game_shader_dx9.dll
Git LFS file not shown
2 changes: 1 addition & 1 deletion game/experiment/bin/game_shader_dx9.pdb
Git LFS file not shown
2 changes: 1 addition & 1 deletion game/experiment/bin/libpng.dll
Git LFS file not shown
2 changes: 1 addition & 1 deletion game/experiment/bin/libpng.pdb
Git LFS file not shown
2 changes: 1 addition & 1 deletion game/experiment/bin/lua54.dll
Git LFS file not shown
2 changes: 1 addition & 1 deletion game/experiment/bin/lua54.pdb
Git LFS file not shown
2 changes: 1 addition & 1 deletion game/experiment/bin/luasocket.dll
Git LFS file not shown
2 changes: 1 addition & 1 deletion game/experiment/bin/luasocket.pdb
Git LFS file not shown
2 changes: 1 addition & 1 deletion game/experiment/lua/bin/sv_helloworld_win32.dll
Git LFS file not shown
2 changes: 1 addition & 1 deletion game/experiment/lua/bin/sv_helloworld_win32.pdb
Git LFS file not shown
123 changes: 48 additions & 75 deletions game/experiment/scripts/lua/autoload/client/font.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,10 @@ function Util.ComputeStringWidth(font, str)
end
end

function _R.IScheme.GetFontName(font)
local type = type(font)
if (type ~= "FontHandle" and type ~= "FontHandleContainer") then
error("bad argument #1 to 'GetFontName' (font or fontcontainer expected, got " .. type .. ")", 2)
end
if (type == "FontHandle") then
return GetFontName(font)
elseif (type == "FontHandleContainer") then
return GetFontName(font.font)
end
function _R.IScheme.GetFontName(scheme, font)
local resolvedFont = Surface.ResolveFont(font, scheme)

return GetFontName(scheme, resolvedFont)
end

function Surface.CreateFont()
Expand All @@ -125,95 +119,74 @@ function Surface.CreateFont()
return fontcontainer
end

function Surface.DrawSetTextFont(font)
function Surface.ResolveFont(font, scheme)
local type = type(font)
if (type ~= "FontHandle" and type ~= "FontHandleContainer") then
error("bad argument #1 to 'DrawSetTextFont' (font or fontcontainer expected, got " .. type .. ")", 2)
end
if (type == "FontHandle") then
return DrawSetTextFont(font)

if (type ~= "string" and type ~= "FontHandle" and type ~= "FontHandleContainer") then
error("bad argument #1 to 'ResolveFont' (string, font or fontcontainer expected, got " .. type .. ")", 2)
end

if (type == "string") then
local hScheme = not scheme and Scheme.GetScheme("ClientScheme") or nil
scheme = scheme or Scheme.GetIScheme(hScheme)

return scheme:GetFont(font)
elseif (type == "FontHandle") then
return font
elseif (type == "FontHandleContainer") then
return DrawSetTextFont(font.font)
return font.font
end
end

function Surface.DrawSetTextFont(font)
local resolvedFont = Surface.ResolveFont(font)

return DrawSetTextFont(resolvedFont)
end

function Surface.GetCharABCwide(font, ch)
local type = type(font)
if (type ~= "FontHandle" and type ~= "FontHandleContainer") then
error("bad argument #1 to 'GetCharABCwide' (font or fontcontainer expected, got " .. type .. ")", 2)
end
if (type == "FontHandle") then
return GetCharABCwide(font, ch)
elseif (type == "FontHandleContainer") then
return GetCharABCwide(font.font, ch)
end
local resolvedFont = Surface.ResolveFont(font)

return GetCharABCwide(resolvedFont, ch)
end

function Surface.GetCharacterWidth(font, ch)
local type = type(font)
if (type ~= "FontHandle" and type ~= "FontHandleContainer") then
error("bad argument #1 to 'GetCharacterWidth' (font or fontcontainer expected, got " .. type .. ")", 2)
end
if (type == "FontHandle") then
return GetCharacterWidth(font, ch)
elseif (type == "FontHandleContainer") then
return GetCharacterWidth(font.font, ch)
end
local resolvedFont = Surface.ResolveFont(font)

return GetCharacterWidth(resolvedFont, ch)
end

function Surface.GetFontAscent(font, ch)
local type = type(font)
if (type ~= "FontHandle" and type ~= "FontHandleContainer") then
error("bad argument #1 to 'GetFontAscent' (font or fontcontainer expected, got " .. type .. ")", 2)
end
if (type == "FontHandle") then
return GetFontAscent(font, ch)
elseif (type == "FontHandleContainer") then
return GetFontAscent(font.font, ch)
end
local resolvedFont = Surface.ResolveFont(font)

return GetFontAscent(resolvedFont, ch)
end

function Surface.GetFontTall(font)
local type = type(font)
if (type ~= "FontHandle" and type ~= "FontHandleContainer") then
error("bad argument #1 to 'GetFontTall' (font or fontcontainer expected, got " .. type .. ")", 2)
end
if (type == "FontHandle") then
return GetFontTall(font)
elseif (type == "FontHandleContainer") then
return GetFontTall(font.font)
end
local resolvedFont = Surface.ResolveFont(font)

return GetFontTall(resolvedFont)
end

function Surface.GetTextSize(font, text)
local type = type(font)
if (type ~= "FontHandle" and type ~= "FontHandleContainer") then
error("bad argument #1 to 'GetTextSize' (font or fontcontainer expected, got " .. type .. ")", 2)
end
if (type == "FontHandle") then
return GetTextSize(font, text)
elseif (type == "FontHandleContainer") then
return GetTextSize(font.font, text)
end
local resolvedFont = Surface.ResolveFont(font)

return GetTextSize(resolvedFont, text)
end

function Surface.IsFontAdditive(font)
local type = type(font)
if (type ~= "FontHandle" and type ~= "FontHandleContainer") then
error("bad argument #1 to 'IsFontAdditive' (font or fontcontainer expected, got " .. type .. ")", 2)
end
if (type == "FontHandle") then
return IsFontAdditive(font)
elseif (type == "FontHandleContainer") then
return IsFontAdditive(font.font)
end
local resolvedFont = Surface.ResolveFont(font)

return IsFontAdditive(resolvedFont)
end

function Surface.SetFontGlyphSet(font, windowsFontName, tall, weight, blur, scanlines, flags, nRangeMin, nRangeMax)
local type = type(font)
if (type ~= "FontHandle" and type ~= "FontHandleContainer") then
error("bad argument #1 to 'SetFontGlyphSet' (font or fontcontainer expected, got " .. type .. ")", 2)
end
local type = type(font)

if (type ~= "FontHandle" and type ~= "FontHandleContainer") then
error("bad argument #1 to 'SetFontGlyphSet' (font or fontcontainer expected, got " .. type .. ")", 2)
end

if (type == "FontHandle") then
return SetFontGlyphSet(font, windowsFontName, tall, weight, blur, scanlines, flags, nRangeMin, nRangeMax)
elseif (type == "FontHandleContainer") then
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--[[
Source: https://wiki.facepunch.com/gmod/render_min_mag_filters#usageexample
lua_openscript_cl utilities/test/gmod_texture_filters.lua
--]]

-- Calling Material() every frame is quite expensive
-- So we call it once, outside of any hooks, and cache the result in a local variable
local ourMat = Material("silkicons/anchor.png")
local sizeBig = 500
local sizeSmall = 100
local pos = 100

local filters = {}
filters[TEXFILTER.NONE] = "NONE"
filters[TEXFILTER.POINT] = "POINT"
filters[TEXFILTER.LINEAR] = "LINEAR"
filters[TEXFILTER.ANISOTROPIC] = "ANISOTROPIC"

hook.Add("HUDPaint", "PutAUniqueHookNameHere", function()
surface.SetDrawColor(255, 255, 255, 255) -- Set the drawing color
surface.SetMaterial(ourMat) -- Use our cached material

-- Show each filter for 1 second each
-- (TEXFILTER enums correspond to integer numbers)
local filter = math.floor(CurTime() % 4)

render.PushFilterMag(filter)
render.PushFilterMin(filter)

-- Actually draw the rectangle with a magnification filter
surface.DrawTexturedRect(pos, pos, sizeBig, sizeBig)

-- Actually draw the rectangle with a minification filter
surface.DrawTexturedRect(pos + sizeBig + 1, pos, sizeSmall, sizeSmall)

render.PopFilterMin()
render.PopFilterMag()

draw.SimpleText(filters[filter], "ChatFont", pos + 1, pos + 1, color_white)
end)
6 changes: 5 additions & 1 deletion src/game/client/cdll_client_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
#include "hltvcamera.h"
#include "mapload_background.h"
#include "gameinfostore.h"
#include <shaderapi/ishaderapi.h>
#if defined( REPLAY_ENABLED )
#include "replay/replaycamera.h"
#include "replay/replay_ragdoll.h"
Expand Down Expand Up @@ -169,7 +170,6 @@ extern vgui::IInputInternal *g_InputInternal;
#include "luamanager.h"
#include "mountaddons.h"
#include "mountsteamcontent.h"
#include <lrender.h>
#include <litexture.h>
#include <cpng.h>
#endif
Expand Down Expand Up @@ -217,6 +217,7 @@ IXboxSystem *xboxsystem = NULL; // Xbox 360 only
IMatchmaking *matchmaking = NULL;
IUploadGameStats *gamestatsuploader = NULL;
IClientReplayContext *g_pClientReplayContext = NULL;
IShaderAPI *g_pShaderApi = NULL;
#if defined( REPLAY_ENABLED )
IReplayManager *g_pReplayManager = NULL;
IReplayMovieManager *g_pReplayMovieManager = NULL;
Expand Down Expand Up @@ -1026,6 +1027,9 @@ int CHLClient::Init( CreateInterfaceFn appSystemFactory,
INTERFACEVERSION_UPLOADGAMESTATS, NULL ) ) == NULL )
return false;
#endif
if ( ( g_pShaderApi = ( IShaderAPI * )appSystemFactory(
SHADERAPI_INTERFACE_VERSION, NULL ) ) == NULL )
return false;

#if defined( REPLAY_ENABLED )
if ( IsPC() && ( g_pEngineReplay = ( IEngineReplay * )appSystemFactory(
Expand Down
2 changes: 2 additions & 0 deletions src/game/client/cdll_client_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class IMatchmaking;
class IVideoServices;
class CSteamAPIContext;
class IClientReplayContext;
class IShaderAPI;
class IReplayManager;
class IEngineReplay;
class IEngineClientReplay;
Expand Down Expand Up @@ -106,6 +107,7 @@ extern IUploadGameStats *gamestatsuploader;
extern CSteamAPIContext *steamapicontext;
extern IReplaySystem *g_pReplay;
extern IClientReplayContext *g_pClientReplayContext;
extern IShaderAPI *g_pShaderApi;
extern IReplayManager *g_pReplayManager;
extern IReplayScreenshotManager *g_pReplayScreenshotManager;
extern IEngineReplay *g_pEngineReplay;
Expand Down
Loading

0 comments on commit 19b9b75

Please sign in to comment.