我正在尝试为我的love2d项目实现一个库,它应该能够使用九种补丁方法来扩展图像。 (如果你还没有听说过: http://radleymarx.com/blog/simple-guide-to-9-patch/ 。)我认为在love2d中最简单的方法是使用spriteBatches,我在这里为图像的各个部分创建四边形应该/不应该缩放。无论如何,我真的无法让它发挥作用。
第一个问题:如何在创建四边形之后设置它们的缩放比例?
第二个问题:我希望能够重复四边形而不是根据需要缩放它们,但我不知道这是如何工作的。但这是可选的,首先我想让缩放本身起作用。
到目前为止,这是我的代码:
function sign(x) --math function to sign a value.
return x>0 and 1 or x<0 and -1 or 0
end
function convert(image, borderWidth, borderHeight) --convert a normal image into a spritebatch with generated quads.
local rW = image:getWidth() --reference width
local rH = image:getHeight() --reference height
local bW = borderWidth --border width
local bH = borderHeight --border height
local cW = rW - 2 * bW --content width
local cH = rH - 2 * bH --content height
local spriteBatch = love.graphics.newSpriteBatch(image, 9)
local id = {}
--generate the 9 quads with some neat calculations
for i = 1, 3 do
for j = 1, 3 do
local x = sign(i - 1) * bW + math.floor(i / 3) * cW
local y = sign(j - 1) * bH + math.floor(j / 3) * cH
local w = math.abs(i - 2) * bW + math.abs(math.abs(i - 2) - 1) * cW
local h = math.abs(j - 2) * bH + math.abs(math.abs(j - 2) - 1) * cH
id[j + (i * 3) - 3] = spriteBatch:add(love.graphics.newQuad(x, y, w, h, rW, rH), x, y)
end
end
--draw the spriteBatch with applied scaling
--[[
name = spriteBatch object,
width = width of final image,
height = height of final image,
scale = boolean value to switch between scaling and repeating the quads
]]
spriteBatch.draw = function(name, width, height, scale)
--How do I change the scaling of the already existing quads to achieve the wanted width/height?
--(How could I let the quads which would be scaled repeat themselves instead of scaling if needed?)
end
return spriteBatch
end
我知道我可以使用存在的“斑驳”库( https://github.com/excessive/patchy ),但我仍然在学习,我认为创建这样一个对我来说更有意义我自己习惯于spriteBatches并进行一些练习。