From 5898a0fac0c5d34c0f02d5115e169540cb9e12b4 Mon Sep 17 00:00:00 2001 From: tangent Date: Thu, 29 Oct 2026 17:29:57 -0600 Subject: [PATCH] loading tilesheets should be possible This system doesn't properly expose/account for the user wanting to scale the tiles. You'd have to know what size it is already to scale things. Not good. Will figure out how to properly adjust that later. --- lib/spritesheet.lua | 8 -------- lib/tilesheet.lua | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) delete mode 100644 lib/spritesheet.lua create mode 100644 lib/tilesheet.lua diff --git a/lib/spritesheet.lua b/lib/spritesheet.lua deleted file mode 100644 index 29dba89..0000000 --- a/lib/spritesheet.lua +++ /dev/null @@ -1,8 +0,0 @@ -local load_image = function(file_path) - local file = io.open(file_path, "rb") - local raw_data = file:read("*all") - file:close() - local love_file_data = love.filesystem.newFileData(raw_data, file_path) - local love_image = love.graphics.newImage(love_file_data) - return love_image -end diff --git a/lib/tilesheet.lua b/lib/tilesheet.lua new file mode 100644 index 0000000..f06186a --- /dev/null +++ b/lib/tilesheet.lua @@ -0,0 +1,39 @@ +local load_image = function(file_path) + local file = io.open(file_path, "rb") + local raw_data = file:read("*all") + file:close() + + local love_file_data = love.filesystem.newFileData(raw_data, file_path) + local love_image = love.graphics.newImage(love_file_data) + return love_image +end + +local tilesheet = {} + +local tile_metatable = { + __index = { + draw = function(self, ...) + love.graphics.draw(self.image, self.quad, ...) + end, + } +} + +local tilesheet.load = function(data_or_path) + if type(data_or_path) == "table" then + local tile_image = load_image(data_or_path.image_path) + local tiles = {} + + for name, data in pairs(data_or_path.tiles) do + tiles[name] = setmetatable({ + image = tile_image, + quad = love.graphics.newQuad( (data.x - 1) * data_or_path.tile_size, (data.y - 1) * data_or_path.tile_size, data_or_path.tile_size, data_or_path.tile_size, tile_image ), + }, tile_metatable) + end + + return tiles + end +end + +return setmetatable(tilesheet, { + __call = function(self, ...) return tilesheet.load(...) end +})