From 2cd21d3608b949780c7e12eea6077f9facaff188 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 22 Oct 2023 13:08:52 +0200 Subject: [PATCH] set_shader_texture_size --- spritesheet.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spritesheet.lua b/spritesheet.lua index 3fc7ad6..2d35e1a 100644 --- a/spritesheet.lua +++ b/spritesheet.lua @@ -72,11 +72,21 @@ function Sprite.new (quad, imagesheet) return setmetatable({ quad = quad, imagesheet = imagesheet, is_sprite = true }, Sprite) end +local function set_shader_texture_size(texture) + local currently_active_shader = love.graphics.getShader() + if currently_active_shader ~= nil and currently_active_shader:hasUniform('spritesheet_inverse_width') then + local width, height = texture:getDimensions() + currently_active_shader:send('spritesheet_inverse_width', {1/width, 0}) + currently_active_shader:send('spritesheet_inverse_height', { 0, 1/height}) + end +end + if define_love then function Sprite:generateImage () local imagesheet, quad = self.imagesheet, self.quad self.func = function (x, y) + set_shader_texture_size(imagesheet.image) love.graphics.draw(imagesheet.image, quad, math.floor(x), math.floor(y), 0, 1, 1, imagesheet.origin_x, imagesheet.origin_y) end end @@ -138,6 +148,7 @@ function Animation:generateImage () if self.wrap then t = t % self.duration end local quad = get_quad_based_on_time(self, t) if not quad then error_internal('Could not determine quad when drawing animation. Time was %f.', t) end + set_shader_texture_size(self.imagesheet.image) love.graphics.draw(self.imagesheet.image, quad, x, y, 0, 1, 1, self.imagesheet.origin_x, self.imagesheet.origin_y) end end