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.
This commit is contained in:
1 parent
309b5abef5
commit
5898a0fac0
2 files changed
+39
-8
No files matched your search
@@ -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
|
||||
@@ -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
|
||||
})
|
||||
Reference in new issue
Block a user