Skip to content

Commit

Permalink
Made In Memory Atlas instead of using a PNG
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin452 committed Mar 7, 2024
1 parent 5fb7f4f commit 807b5f2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
68 changes: 42 additions & 26 deletions src/init/assetsinit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ function InitializeAssets()
[Tiles.STONE_BRICK_Block] = stone_brickTexture,
[Tiles.GLOWSTONE_Block] = glowstoneTexture,
}
createTextureAtlas()
TileTexture = lovegraphics.newImage("Atlass/Atlas.png")
createTextureAtlas("PNG")
-- TileTexture = lovegraphics.newImage("Atlass/Atlas.png")
atlasInRAM, TilesTextureList = createTextureAtlas("RAM")
atlasImage = lovegraphics.newImage(atlasInRAM)
TilesTextureList = {
-- textures are in format: FRONT UP DOWN
-- at least one texture must be present
Expand All @@ -107,7 +109,7 @@ function InitializeAssets()
end
for blockType, _ in pairs(TilesTextureAtlasList) do
if not blockTypeExists(blockType) then
TilesTextureList[blockType] = { unpack(textureAtlassCoordinates[blockType]) }
TilesTextureList[blockType] = { unpack(textureAtlasCoordinates[blockType]) }
else
print("This BlockType " .. blockType .. " Has been already registered in TilesTextureList")
end
Expand All @@ -117,22 +119,26 @@ end
--TODO ADD EASY texture size changer (for now only support 16x16)
finalAtlasSize = 256

function createTextureAtlas()
--TODO MADE INRAM ATLAS instead of using the png Atlas.png or probably i will not remove the Atlas.png creation(for debug) and making its creation into an another thread, but will use the INRAM ATLAS
function createTextureAtlas(mode)
local totalTimeStart = os.clock()

if finalAtlasSize < 256 or finalAtlasSize % 256 ~= 0 then
error("finalAtlasSize must be a multiple of 256 and not less than 256")
end

local function initializeAtlas(atlasSize)
local atlas = loveimage.newImageData(atlasSize, atlasSize)
local x, y = 0, 0
textureAtlassCoordinates = {}
textureAtlasCoordinates = {}

local needResize = false

repeat
local loopStartTime = os.clock()

for blockType, texturePaths in pairs(TilesTextureAtlasList) do
local textureList = (mode == "PNG") and TilesTextureAtlasList or TilesTextureAtlasList

for blockType, texturePaths in pairs(textureList) do
if type(texturePaths) ~= "table" then
texturePaths = { texturePaths }
end
Expand All @@ -157,7 +163,7 @@ function createTextureAtlas()
finalAtlasSize = atlasSize
atlas = loveimage.newImageData(atlasSize, atlasSize)
x, y = 0, 0
textureAtlassCoordinates = {}
textureAtlasCoordinates = {}
needResize = true
break
else
Expand All @@ -169,7 +175,7 @@ function createTextureAtlas()
local tileWidth, tileHeight = 16, 16
local index = x / tileWidth + y / tileHeight * (finalAtlasSize / tileHeight)

textureAtlassCoordinates[blockType] = { index }
textureAtlasCoordinates[blockType] = { index }

x = x + width
else
Expand All @@ -184,33 +190,43 @@ function createTextureAtlas()

local loopEndTime = os.clock()
local loopElapsedTime = loopEndTime - loopStartTime
print("Atlas Time taken in loop: " .. loopElapsedTime .. " seconds")
print(mode .. " Atlas Time taken in loop: " .. loopElapsedTime .. " seconds")
until not needResize

local totalTimeEnd = os.clock()
local totalTimeElapsed = totalTimeEnd - totalTimeStart
print("Atlas Total time taken: " .. totalTimeElapsed .. " seconds")
print(mode .. " Atlas Total time taken: " .. totalTimeElapsed .. " seconds")

return atlas
end

local atlasSize = finalAtlasSize

local atlas = initializeAtlas(atlasSize)

local atlasDirectory = "Atlass"
lovefilesystem.createDirectory(atlasDirectory)

local saveStartTime = os.clock()
local atlasImagePath = atlasDirectory .. "/Atlas.png"
local pngData = atlas:encode("png")
lovefilesystem.write(atlasImagePath, pngData)
local saveEndTime = os.clock()

local saveElapsedTime = saveEndTime - saveStartTime
print("Atlas Time taken for saving: " .. saveElapsedTime .. " seconds")

print("Created Atlas.png at " .. lovefilesystem.getSaveDirectory() .. "/Atlass , with size " .. atlasSize)
if mode == "PNG" then
local atlasDirectory = "Atlas"
lovefilesystem.createDirectory(atlasDirectory)

local saveStartTime = os.clock()
local atlasImagePath = atlasDirectory .. "/Atlas.png"
local pngData = atlas:encode("png")
lovefilesystem.write(atlasImagePath, pngData)
local saveEndTime = os.clock()

local saveElapsedTime = saveEndTime - saveStartTime
print("PNG Atlas Time taken for saving: " .. saveElapsedTime .. " seconds")

print(
"PNG Created "
.. mode
.. ".png at "
.. lovefilesystem.getSaveDirectory()
.. "/"
.. atlasDirectory
.. " with size "
.. atlasSize
)
end

return atlas, TilesTextureList
return atlas, textureAtlasCoordinates
end
5 changes: 3 additions & 2 deletions src/init/canvasinit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ function InitializeHUDTileCanvas()
if not TileCanvas[index] then
TileCanvas[index] = lovegraphics.newCanvas(tileSize, tileSize)
lovegraphics.setCanvas(TileCanvas[index])
lovegraphics.draw(TileTexture, -(i - 1) * tileSize, -(j - 1) * tileSize)
lovegraphics.draw(atlasImage, -(i - 1) * tileSize, -(j - 1) * tileSize)
lovegraphics.setCanvas()
end
end
end
end

function InitializeGameTileCanvas()
-- create lighting value textures on LightingTexture canvas
LightValues = 16
Expand All @@ -26,7 +27,7 @@ function InitializeGameTileCanvas()
for i = LightValues, 1, -1 do
local xx = (i - 1) * finalAtlasSize
lovegraphics.setColor(mult, mult, mult)
lovegraphics.draw(TileTexture, xx, 0)
lovegraphics.draw(atlasImage, xx, 0)
mult = mult * 0.8
end
lovegraphics.setColor(1, 1, 1)
Expand Down

0 comments on commit 807b5f2

Please sign in to comment.