Small Useful Functions
Posted: Mon Mar 31, 2014 2:21 pm
(Just renamed the thread to make it more "accurate")
So, I was going to ask help with a small project that uses rectangles with rounded corners. I fixed them, and now I want to share it with you guys. To make it so I haven't made a whole new thread just for it, I'm going to post also a function to print a text in multiple colors:
Rounded rectangles:
love.graphics.roundrectangle([wiki]DrawMode[/wiki], x, y, width, height, radius, segments)
Multi-printing texts:
love.graphics.multiprint(textsTable, colorsTable, x, y, r, sx, sy, ox, oy, kx, ky)
Should I post more of these? May I make this a thread where you can post these kind of useful, small functions? Is there already a thread for it (not projects, but small, useful functions)? Thank you!
Oh, and, also, a small function to make LÖVE 0.8.0 and LÖVE 0.9.0 compatible in love.graphics.draw:
So, I was going to ask help with a small project that uses rectangles with rounded corners. I fixed them, and now I want to share it with you guys. To make it so I haven't made a whole new thread just for it, I'm going to post also a function to print a text in multiple colors:
Rounded rectangles:
love.graphics.roundrectangle([wiki]DrawMode[/wiki], x, y, width, height, radius, segments)
Code: Select all
function love.graphics.roundrectangle(mode, x, y, w, h, rd, s)
local r, g, b, a = love.graphics.getColor()
local rd = rd or math.min(w, h)/4
local s = s or 32
local l = love.graphics.getLineWidth()
local corner = 1
local function mystencil()
love.graphics.setColor(255, 255, 255, 255)
if corner == 1 then
love.graphics.rectangle("fill", x-l, y-l, rd+l, rd+l)
elseif corner == 2 then
love.graphics.rectangle("fill", x+w-rd+l, y-l, rd+l, rd+l)
elseif corner == 3 then
love.graphics.rectangle("fill", x-l, y+h-rd+l, rd+l, rd+l)
elseif corner == 4 then
love.graphics.rectangle("fill", x+w-rd+l, y+h-rd+l, rd+l, rd+l)
elseif corner == 0 then
love.graphics.rectangle("fill", x+rd, y-l, w-2*rd+l, h+2*l)
love.graphics.rectangle("fill", x-l, y+rd, w+2*l, h-2*rd+l)
end
end
love.graphics.setStencil(mystencil)
love.graphics.setColor(r, g, b, a)
love.graphics.circle(mode, x+rd, y+rd, rd, s)
love.graphics.setStencil()
corner = 2
love.graphics.setStencil(mystencil)
love.graphics.setColor(r, g, b, a)
love.graphics.circle(mode, x+w-rd, y+rd, rd, s)
love.graphics.setStencil()
corner = 3
love.graphics.setStencil(mystencil)
love.graphics.setColor(r, g, b, a)
love.graphics.circle(mode, x+rd, y+h-rd, rd, s)
love.graphics.setStencil()
corner = 4
love.graphics.setStencil(mystencil)
love.graphics.setColor(r, g, b, a)
love.graphics.circle(mode, x+w-rd, y+h-rd, rd, s)
love.graphics.setStencil()
corner = 0
love.graphics.setStencil(mystencil)
love.graphics.setColor(r, g, b, a)
love.graphics.rectangle(mode, x, y, w, h)
love.graphics.setStencil()
end
love.graphics.multiprint(textsTable, colorsTable, x, y, r, sx, sy, ox, oy, kx, ky)
Code: Select all
function love.graphics.multiprint( ... )
local r, g, b, a = love.graphics.getColor()
local args = { ... }
local temp = { ... }
table.remove(args, 1)
local texts = temp[1]
local colors = temp[2]
local textdist = ""
for i = 1, #texts do
love.graphics.setColor(unpack(colors[i]))
args[1] = texts[i]
args[2] = temp[3]+love.graphics.getFont():getWidth(textdist)
love.graphics.print(unpack(args))
textdist = textdist .. texts[i]
end
love.graphics.setColor(r, g, b, a)
end
Oh, and, also, a small function to make LÖVE 0.8.0 and LÖVE 0.9.0 compatible in love.graphics.draw:
Code: Select all
love.graphics.rdraw = love.graphics.draw
love.graphics.drawq = love.graphics.drawq or love.graphics.rdraw
function love.graphics.draw( ... )
local args = { ... }
if type(args[2]) == "userdata" then
love.graphics.drawq(unpack(args))
else
love.graphics.rdraw(unpack(args))
end
end