From 3b8aed464d7137c5ad23eb5700b1cff9ed193b42 Mon Sep 17 00:00:00 2001 From: Vanic Date: Mon, 20 Oct 2025 18:18:26 +0300 Subject: [PATCH] TileSheet.new: You can now specify the resolution of the tile that is on the canvas --- mods/CORE/TileSheet/src/TileSheet.lua | 80 +++++++++++++++++++++++---- mods/GAME/ITEMS/Rocks/init.lua | 2 +- mods/GAME/ITEMS/Soils/init.lua | 2 +- 3 files changed, 71 insertions(+), 13 deletions(-) diff --git a/mods/CORE/TileSheet/src/TileSheet.lua b/mods/CORE/TileSheet/src/TileSheet.lua index a362aa3..de9553c 100644 --- a/mods/CORE/TileSheet/src/TileSheet.lua +++ b/mods/CORE/TileSheet/src/TileSheet.lua @@ -1,23 +1,81 @@ +local mathRound = math.round + ---@class TileSheet ---@field image string Name of texture ---@field width number Number of tiles in the table horizontally ---@field height number Number of tiles in the table vertically local TileSheet = {} ----@param image string Name of texture ----@param width number Number of tiles in the table horizontally ----@param height number Number of tiles in the table vertically ----@return TileSheet -function TileSheet:new(image, width, height) +---@param imagePath string +---@return number, number +local function getSizePng(imagePath) + local file = io.open(imagePath, 'rb') + if not file then + error('Cannot open file: ' .. imagePath) + end + + -- PNG signature check (8 bytes) + local signature = file:read(8) + if not signature or signature ~= '\137PNG\r\n\26\n' then + file:close() + error('Not a valid PNG file: ' .. imagePath) + end + + -- Reading the first chunk (must be IHDR) + local length_data = file:read(4) + local chunk_type = file:read(4) + if not length_data or not chunk_type or chunk_type ~= 'IHDR' then + file:close() + error('Invalid PNG structure in file: ' .. imagePath) + end + + -- Reading IHDR data (width and height) + local width_data = file:read(4) + local height_data = file:read(4) + if not width_data or not height_data then + file:close() + error('Incomplete PNG header in file:' .. imagePath) + end + + file:close() + + -- Converting big-endian bytes to numbers + local width = (string.byte(width_data, 1) * 16777216) + + (string.byte(width_data, 2) * 65536) + + (string.byte(width_data, 3) * 256) + + string.byte(width_data, 4) + + local height = (string.byte(height_data, 1) * 16777216) + + (string.byte(height_data, 2) * 65536) + + (string.byte(height_data, 3) * 256) + + string.byte(height_data, 4) + + return width, height +end + +---Returns an instance of the TileSheet class by canvas name and single tile resolution. +--- +---Warning: only `.png` images +---@param image string Name of texture. +---@param tileWidth number Width of one tile +---@param tileHeight number Height of one tile +---@return TileSheet +function TileSheet:new(image, tileWidth, tileHeight) + local modPath = core.get_modpath(core.get_current_modname()) + -- The canvas image must be in `/textures`, otherwise it will not be retrieved. + local width, height = getSizePng(modPath .. '/textures/' .. image) + + if width % tileWidth ~= 0 or height % tileHeight ~= 0 then + error('Tilesheet does not have an integer number of tiles with resolution ' .. tostring(tileWidth) ..' * '.. tostring(tileHeight)) + end + ---@type TileSheet local instance = setmetatable({ - image = image, - width = width, - height = height, + image = image, + width = mathRound(width / tileWidth), + height = mathRound(height / tileHeight), }, {__index = self}) - -- TODO возможно будет лучше сделать автоматическое определение высоты и ширины по разрешению текстуры - return instance end @@ -26,7 +84,7 @@ end ---@param y number ---@return string function TileSheet:getTextureName(x, y) - local textureName = self.image .. "^[sheet:".. self.width .. "x" .. self.height .. ":" .. x .. "," .. y + local textureName = self.image .. '^[sheet:'.. self.width .. 'x' .. self.height .. ':' .. x .. ',' .. y return textureName end diff --git a/mods/GAME/ITEMS/Rocks/init.lua b/mods/GAME/ITEMS/Rocks/init.lua index 5713dda..00bb84e 100644 --- a/mods/GAME/ITEMS/Rocks/init.lua +++ b/mods/GAME/ITEMS/Rocks/init.lua @@ -16,7 +16,7 @@ local defaultRock = Core.Node.Class:getExtended(defaultRockDef) local rockFactory = defaultRock:getFactory() ---@type TileSheet -local ts = Core.TileSheet:getModClassInstance("rocks_sheet.png", 7, 15) +local ts = Core.TileSheet:getModClassInstance("rocks_sheet.png", 16, 16) rockFactory:registerNodesByShortDef({ {"sylite", "Sylite", nil, {ts:t(0, 0)}}, diff --git a/mods/GAME/ITEMS/Soils/init.lua b/mods/GAME/ITEMS/Soils/init.lua index 0cea263..cc52029 100644 --- a/mods/GAME/ITEMS/Soils/init.lua +++ b/mods/GAME/ITEMS/Soils/init.lua @@ -17,7 +17,7 @@ local defaultSoil = Core.Node.Class:getExtended(defaultSoilDef) local soilFactory = defaultSoil:getFactory() ---@type TileSheet -local ts = Core.TileSheet:getModClassInstance("soils_sheet.png", 7, 1) +local ts = Core.TileSheet:getModClassInstance("soils_sheet.png", 16, 16) local clayTiles = { ts:t(0,0)