Skip to content

Commit

Permalink
Fixed MAJORITY OF THE lags caused by DrawColorString
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin452 committed Mar 23, 2024
1 parent d456755 commit 0b9e461
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 54 deletions.
2 changes: 1 addition & 1 deletion main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ end
function love.draw()
_JPROFILER.push("frame")
_JPROFILER.push("MainDraw")
setFont()
SetFont()
LuaCraftCurrentGameState:draw()
if HudMessage ~= nil then
local width, height = Lovegraphics.getDimensions()
Expand Down
4 changes: 2 additions & 2 deletions src/client/models/blocks/blockrendering.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end
-- Adds a face to the model based on direction and transparency
local function addFaceToModel(model, x, y, z, otx, oty, BlockModelScale, gettype)
_JPROFILER.push("addFaceToModel")
local tx, ty, tx2, ty2 = calculationotxoty(otx, oty)
local tx, ty, tx2, ty2 = Calculationotxoty(otx, oty)
local x_plus_scale = x + BlockModelScale
local y_plus_scale = y + BlockModelScale
local z_plus_scale = z + BlockModelScale
Expand Down Expand Up @@ -93,7 +93,7 @@ local function addFace(gettype, direction, y_offset, light_offset, thisLight, mo
local textureIndex = math.min(2 + y_offset, #TileTextures(direction))
local texture = (gettype == "getTop" or gettype == "getBottom") and TileTextures(direction)[textureIndex]
or TileTextures(direction)[1]
local otx, oty = getTextureCoordinatesAndLight(texture, math.max(thisLight - light_offset, 0))
local otx, oty = GetTextureCoordinatesAndLight(texture, math.max(thisLight - light_offset, 0))
addFaceToModel(model, x, y + y_offset * scale, z, otx, oty, scale, gettype)
end
_JPROFILER.pop("addFace_blockrendering")
Expand Down
4 changes: 2 additions & 2 deletions src/client/models/blocks/tilerendering.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ local function createTileModel(tileID, thisLight, BlockModelScale)

-- Retrieve texture and light coordinates for the tile
local texture = TileTextures(tileID)[1]
local otx, oty = getTextureCoordinatesAndLight(texture, thisLight)
local tx, ty, tx2, ty2 = calculationotxoty(otx, oty)
local otx, oty = GetTextureCoordinatesAndLight(texture, thisLight)
local tx, ty, tx2, ty2 = Calculationotxoty(otx, oty)

-- Define vertices for the 2D tile model
local diagLong = 0.7071 * BlockModelScale * 0.5 + 0.5
Expand Down
10 changes: 5 additions & 5 deletions src/utils/gamestateshandling/GameStateGamePausing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ function GamestateGamePausing2:draw()

local posX = w * 0.4
local posY = h * 0.4
local lineHeight = getSelectedFont():getHeight("X")
drawColorString(_GamePlayingPauseMenu.title, posX, posY)
local lineHeight = GetSelectedFont():getHeight("X")
DrawColorString(_GamePlayingPauseMenu.title, posX, posY)
posY = posY + lineHeight
for n = 1, #_GamePlayingPauseMenu.choice do
if _GamePlayingPauseMenu.selection == n then
marque = "%1*%0 "
else
marque = " "
end
drawColorString(marque .. "" .. _GamePlayingPauseMenu.choice[n], posX, posY)
DrawColorString(marque .. "" .. _GamePlayingPauseMenu.choice[n], posX, posY)
posY = posY + lineHeight
end
end
Expand Down Expand Up @@ -66,10 +66,10 @@ function GamestateGamePausing2:mousepressed(x, y, b)
local w, h = Lovegraphics.getDimensions()
local posX = w * 0.4
local posY = h * 0.4
local lineHeight = getSelectedFont():getHeight("X")
local lineHeight = GetSelectedFont():getHeight("X")
local menuWidth = 0
for _, choice in ipairs(_GamePlayingPauseMenu.choice) do
local choiceWidth = getSelectedFont():getWidth(choice)
local choiceWidth = GetSelectedFont():getWidth(choice)
if choiceWidth > menuWidth then
menuWidth = choiceWidth
end
Expand Down
10 changes: 5 additions & 5 deletions src/utils/gamestateshandling/GameStateMainMenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ function GamestateMainMenu2:draw()
Lovegraphics.draw(MainMenuBackground, 0, 0, 0, scaleX, scaleY)
local posX = w * 0.4
local posY = h * 0.4
local lineHeight = getSelectedFont():getHeight("X")
drawColorString(_Mainmenu.title, posX, posY)
local lineHeight = GetSelectedFont():getHeight("X")
DrawColorString(_Mainmenu.title, posX, posY)
posY = posY + lineHeight
for n = 1, #_Mainmenu.choice do
if _Mainmenu.selection == n then
marque = "%1*%0 "
else
marque = " "
end
drawColorString(marque .. "" .. _Mainmenu.choice[n], posX, posY)
DrawColorString(marque .. "" .. _Mainmenu.choice[n], posX, posY)
posY = posY + lineHeight
end
end
Expand All @@ -44,10 +44,10 @@ function GamestateMainMenu2:mousepressed(x, y, b)
local w, h = Lovegraphics.getDimensions()
local posX = w * 0.4
local posY = h * 0.4
local lineHeight = getSelectedFont():getHeight("X")
local lineHeight = GetSelectedFont():getHeight("X")
local menuWidth = 0
for _, choice in ipairs(_Mainmenu.choice) do
local choiceWidth = getSelectedFont():getWidth(choice)
local choiceWidth = GetSelectedFont():getWidth(choice)
if choiceWidth > menuWidth then
menuWidth = choiceWidth
end
Expand Down
12 changes: 6 additions & 6 deletions src/utils/gamestateshandling/GameStateWorldCreationMenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function GamestateWorldCreationMenu2:draw()
Lovegraphics.draw(WorldCreationBackground, 0, 0, 0, scaleX, scaleY)
local posX = w * 0.4
local posY = h * 0.4
local lineHeight = getSelectedFont():getHeight("X")
drawColorString(_WorldCreationMenu.title, posX, posY)
local lineHeight = GetSelectedFont():getHeight("X")
DrawColorString(_WorldCreationMenu.title, posX, posY)
posY = posY + lineHeight

for n = 1, #_WorldCreationMenu.choice do
Expand All @@ -31,9 +31,9 @@ function GamestateWorldCreationMenu2:draw()
if n == 2 then
local worldType = WorldTypeMap[GlobalWorldType]
local worldTypeName = worldType.name
drawColorString(marque .. choiceText .. " (" .. worldTypeName .. ")", posX, posY)
DrawColorString(marque .. choiceText .. " (" .. worldTypeName .. ")", posX, posY)
else
drawColorString(marque .. "" .. choiceText, posX, posY)
DrawColorString(marque .. "" .. choiceText, posX, posY)
end
posY = posY + lineHeight
end
Expand All @@ -60,10 +60,10 @@ function GamestateWorldCreationMenu2:mousepressed(x, y, b)
local w, h = Lovegraphics.getDimensions()
local posX = w * 0.4
local posY = h * 0.4
local lineHeight = getSelectedFont():getHeight("X")
local lineHeight = GetSelectedFont():getHeight("X")
local menuWidth = 0
for _, choice in ipairs(_WorldCreationMenu.choice) do
local choiceWidth = getSelectedFont():getWidth(choice)
local choiceWidth = GetSelectedFont():getWidth(choice)
if choiceWidth > menuWidth then
menuWidth = choiceWidth
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ function SharedKeybindingSettingsDraw()
Lovegraphics.draw(KeybindingSettingsBackground, 0, 0, 0, scaleX, scaleY)
local posX = w * 0.4
local posY = h * 0.4
local lineHeight = getSelectedFont():getHeight("X")
drawColorString(_KeybindingMenuSettings.title, posX, posY)
local lineHeight = GetSelectedFont():getHeight("X")
DrawColorString(_KeybindingMenuSettings.title, posX, posY)
posY = posY + lineHeight
local file_content, error_message = customReadFile(Luacraftconfig)
if file_content then
Expand Down Expand Up @@ -41,7 +41,7 @@ function SharedKeybindingSettingsDraw()
if n == 4 and Settings["rightmovementkey"] then
choiceText = choiceText .. string.rep(" ", numberOfSpaces) .. Settings["rightmovementkey"]
end
drawColorString(marque .. "" .. choiceText, posX, posY)
DrawColorString(marque .. "" .. choiceText, posX, posY)
posY = posY + lineHeight
end
else
Expand Down Expand Up @@ -76,10 +76,10 @@ function SharedKeybindingSettingsMousePressed(x, y, b)
local w, h = Lovegraphics.getDimensions()
local posX = w * 0.4
local posY = h * 0.4
local lineHeight = getSelectedFont():getHeight("X")
local lineHeight = GetSelectedFont():getHeight("X")
local menuWidth = 0
for _, choice in ipairs(_KeybindingMenuSettings.choice) do
local choiceWidth = getSelectedFont():getWidth(choice)
local choiceWidth = GetSelectedFont():getWidth(choice)
if choiceWidth > menuWidth then
menuWidth = choiceWidth
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function SharedSettingsDraw()
Lovegraphics.draw(MainMenuSettingsBackground, 0, 0, 0, scaleX, scaleY)
local posX = w * 0.4
local posY = h * 0.4
local lineHeight = getSelectedFont():getHeight("X")
drawColorString(_MainMenuSettings.title, posX, posY)
local lineHeight = GetSelectedFont():getHeight("X")
DrawColorString(_MainMenuSettings.title, posX, posY)
posY = posY + lineHeight
local file_content, error_message = customReadFile(Luacraftconfig)
if file_content then
Expand Down Expand Up @@ -49,7 +49,7 @@ function SharedSettingsDraw()
local numberOfSpaces = 1
choiceText = choiceText .. string.rep(" ", numberOfSpaces) .. Settings["renderdistance"]
end
drawColorString(marque .. "" .. choiceText, posX, posY)
DrawColorString(marque .. "" .. choiceText, posX, posY)

posY = posY + lineHeight
end
Expand Down Expand Up @@ -98,10 +98,10 @@ function SharedSettingsMousePressed(x, y, b)
local w, h = Lovegraphics.getDimensions()
local posX = w * 0.4
local posY = h * 0.4
local lineHeight = getSelectedFont():getHeight("X")
local lineHeight = GetSelectedFont():getHeight("X")
local menuWidth = 0
for _, choice in ipairs(_MainMenuSettings.choice) do
local choiceWidth = getSelectedFont():getWidth(choice)
local choiceWidth = GetSelectedFont():getWidth(choice)
if choiceWidth > menuWidth then
menuWidth = choiceWidth
end
Expand Down
9 changes: 4 additions & 5 deletions src/utils/threads/modeling/blockmodellingThreadCode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ end
return TilesTextureList[n]
end
local function getTextureCoordinatesAndLight(texture, lightOffset)
local function GetTextureCoordinatesAndLight(texture, lightOffset)
local textureIndex = texture
local otx = ((textureIndex / ADJUSTMENT_FACTOR_TEXTURE_COORDINATES) % LightValues + 16 * lightOffset)
local oty = math.floor(textureIndex / (ADJUSTMENT_FACTOR_TEXTURE_COORDINATES * LightValues))
return otx, oty
end
local function calculationotxoty(otx, oty)
local function Calculationotxoty(otx, oty)
local tx = otx * TileWidth / LightValues
local ty = oty * TileHeight
local tx2 = (otx + ADJUSTMENT_FACTOR_OTX_OTY) * TileWidth / LightValues
Expand All @@ -54,7 +54,7 @@ end
-- Adds a face to the model based on direction and transparency
local function addFaceToModel(model, x, y, z, otx, oty, BlockModelScale, gettype)
local tx, ty, tx2, ty2 = calculationotxoty(otx, oty)
local tx, ty, tx2, ty2 = Calculationotxoty(otx, oty)
local x_plus_scale = x + BlockModelScale
local y_plus_scale = y + BlockModelScale
local z_plus_scale = z + BlockModelScale
Expand Down Expand Up @@ -115,7 +115,7 @@ local function addFace(gettype, direction, y_offset, light_offset, thisLight, mo
local textureIndex = math.min(2 + y_offset, #TileTextures(direction))
local texture = (gettype == "getTop" or gettype == "getBottom") and TileTextures(direction)[textureIndex]
or TileTextures(direction)[1]
local otx, oty = getTextureCoordinatesAndLight(texture, math.max(thisLight - light_offset, 0))
local otx, oty = GetTextureCoordinatesAndLight(texture, math.max(thisLight - light_offset, 0))
addFaceToModel(model, x, y + y_offset * scale, z, otx, oty, scale, gettype)
end
end
Expand Down Expand Up @@ -213,6 +213,5 @@ end
while true do
local data = BlockModellingChannel:demand()
if data then

end
end
40 changes: 22 additions & 18 deletions src/utils/usefull.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
local ADJUSTMENT_FACTOR_OTX_OTY = 256 / FinalAtlasSize
local ADJUSTMENT_FACTOR_TEXTURE_COORDINATES = FinalAtlasSize / 256
local colorMap = {
["0"] = { 255, 255, 255 }, -- white
["1"] = { 255, 0, 0 }, -- red
["2"] = { 0, 255, 0 }, -- green
["3"] = { 0, 255, 255 }, -- blue
}
local selectedFont = nil

function getSelectedFont()
function GetSelectedFont()
return selectedFont
end

function drawColorString(Pstring, Px, Py)
_JPROFILER.push("drawColorString")
function DrawColorString(Pstring, Px, Py)
_JPROFILER.push("DrawColorString")
local rx, ry = Px, Py
local defaultColor = { 255, 255, 255 }
local currentColor = defaultColor
Expand All @@ -23,32 +22,37 @@ function drawColorString(Pstring, Px, Py)

local i = 1
local len = #Pstring
local buffer = ""

while i <= len do
local c = string.sub(Pstring, i, i)

if c == "%" then
if buffer ~= "" then
Lovegraphics.print(buffer, rx, ry)
rx = rx + GetSelectedFont():getWidth(buffer)
buffer = ""
end
local colorDigit = string.sub(Pstring, i + 1, i + 1)
currentColor = colorMap[tostring(colorDigit)] or defaultColor
Lovegraphics.setColor(currentColor)
i = i + 2 -- skip both '%' and the color digit
else
Lovegraphics.print(c, rx, ry)
selectedFont = getSelectedFont()
if selectedFont then
local fontWidth = selectedFont:getWidth(c)
rx = rx + fontWidth
end
buffer = buffer .. c
i = i + 1
end
end
if buffer ~= "" then
Lovegraphics.print(buffer, rx, ry)
end
Lovegraphics.setColor(defaultColor)
_JPROFILER.pop("drawColorString")
_JPROFILER.pop("DrawColorString")
end


local previousGamestate = nil

function setFont()
function SetFont()
_JPROFILER.push("setFont")
if LuaCraftCurrentGameState ~= previousGamestate then
selectedFont = LuaCraftCurrentGameState:setFont()
Expand All @@ -59,22 +63,22 @@ function setFont()
end

-- Calculates texture coordinates for given offsets
function calculationotxoty(otx, oty)
_JPROFILER.push("calculationotxoty")
function Calculationotxoty(otx, oty)
_JPROFILER.push("Calculationotxoty")
local tx = otx * TileWidth / LightValues
local ty = oty * TileHeight
local tx2 = (otx + ADJUSTMENT_FACTOR_OTX_OTY) * TileWidth / LightValues
local ty2 = (oty + ADJUSTMENT_FACTOR_OTX_OTY) * TileHeight
_JPROFILER.pop("calculationotxoty")
_JPROFILER.pop("Calculationotxoty")
return tx, ty, tx2, ty2
end

-- Retrieves texture coordinates and light information
function getTextureCoordinatesAndLight(texture, lightOffset)
_JPROFILER.push("getTextureCoordinatesAndLight")
function GetTextureCoordinatesAndLight(texture, lightOffset)
_JPROFILER.push("GetTextureCoordinatesAndLight")
local textureIndex = texture
local otx = ((textureIndex / ADJUSTMENT_FACTOR_TEXTURE_COORDINATES) % LightValues + 16 * lightOffset)
local oty = math.floor(textureIndex / (ADJUSTMENT_FACTOR_TEXTURE_COORDINATES * LightValues))
_JPROFILER.pop("getTextureCoordinatesAndLight")
_JPROFILER.pop("GetTextureCoordinatesAndLight")
return otx, oty
end

0 comments on commit 0b9e461

Please sign in to comment.