From fa7b265618f8794a010d700e3d6ce433066c4d76 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 18 Mar 2019 16:52:27 +0100 Subject: [PATCH] modules/assets: add a basic texture object --- gamecore/modules/assets/init.lua | 6 ++- gamecore/modules/assets/texture.lua | 61 +++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 gamecore/modules/assets/texture.lua diff --git a/gamecore/modules/assets/init.lua b/gamecore/modules/assets/init.lua index 50812e3..5fe379f 100644 --- a/gamecore/modules/assets/init.lua +++ b/gamecore/modules/assets/init.lua @@ -26,6 +26,8 @@ local Assets = Object:extend() local cwd = (...):gsub('%.init$', '') .. "." +local Texture = require(cwd .. "texture") + local Sprite = require(cwd .. "sprites") local Font = require(cwd .. "fonts") local ImageFont = require(cwd .. "imagefonts") @@ -123,11 +125,11 @@ end -- Background -- function Assets:addImage(name, filename) - self.images[name] = love.graphics.newImage(filename) + self.images[name] = Texture(filename) end function Assets:drawImage(name, x, y, r, sx, sy, ox, oy, kx, ky) - love.graphics.draw(self.images[name], x, y, r, sx, sy, ox, oy, kx, ky) + self.images[name]:draw(x, y, r, sx, sy, ox, oy, kx, ky) end -- Images -- diff --git a/gamecore/modules/assets/texture.lua b/gamecore/modules/assets/texture.lua new file mode 100644 index 0000000..96e868b --- /dev/null +++ b/gamecore/modules/assets/texture.lua @@ -0,0 +1,61 @@ +-- assets/texture :: the texture object, essentially used to be able to draw easily +-- the mask of the texture (used for stuff like flashing sprite, etc) + +--[[ + Copyright © 2019 Kazhnuz + + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files (the "Software"), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +]] + + +local Texture = Object:extend() + +local function getMask(x, y, r, g, b, a) + -- template for defining your own pixel mapping function + -- perform computations giving the new values for r, g, b and a + -- ... + return 1, 1, 1, a +end + +function Texture:new(filename) + self.imageData = love.image.newImageData(filename) + + local maskData = self.imageData:clone() + maskData:mapPixel( getMask ) + + self.image = love.graphics.newImage( self.imageData ) + self.mask = love.graphics.newImage( maskData ) +end + +function Texture:draw(x, y, r, sx, sy, ox, oy, kx, ky) + love.graphics.draw(self.image, x, y, r, sx, sy, ox, oy, kx, ky) +end + +function Texture:drawQuad(quad, x, y, r, sx, sy, ox, oy, kx, ky) + love.graphics.draw(self.image, quad, x, y, r, sx, sy, ox, oy, kx, ky) +end + +function Texture:drawMask(x, y, r, sx, sy, ox, oy, kx, ky) + love.graphics.draw(self.mask, x, y, r, sx, sy, ox, oy, kx, ky) +end + +function Texture:drawMaskQuad(quad, x, y, r, sx, sy, ox, oy, kx, ky) + love.graphics.draw(self.mask, quad, x, y, r, sx, sy, ox, oy, kx, ky) +end + +return Texture