Re: Small Useful Functions
Posted: Mon Jun 08, 2015 5:47 pm
I don't think this is usable because of the license because it is strait out of garry's mod client...
I don't think this is usable because of the license because it is strait out of garry's mod client...
Code: Select all
local function error_wrap(co, err, ...)
if err == false then
io.stderr:write(debug.traceback(co))
error(...)
else
return ...
end
end
function COWRAP(start)
local co = coroutine.create(start)
return function(...)
return error_wrap(co, coroutine.resume(co, ...))
end
end
Code: Select all
_translate = love.graphics.translate
_translateX, _translateY = {0}, {0}
_translateCounter = {0}
_pop = love.graphics.pop
_push = love.graphics.push
_origin = love.graphics.origin
_scissor = love.graphics.setScissor
function love.graphics.pop()
for i = 1, _translateCounter[#_translateCounter] do
table.remove(_translateX)
table.remove(_translateY)
end
table.remove(_translateCounter)
_pop()
end
function love.graphics.push()
table.insert(_translateCounter, 0)
_push()
end
function love.graphics.translate(x, y)
table.insert(_translateX, x)
table.insert(_translateY, y)
_translateCounter[#_translateCounter] = _translateCounter[#_translateCounter] + 1
_translate(x, y)
end
function love.graphics.getTranslation()
return love.graphics.getTranslationX(), love.graphics.getTranslationY()
end
function love.graphics.getTranslationX()
local x = 0
for i, v in ipairs(_translateX) do
x = x + v
end
return x
end
function love.graphics.getTranslationY()
local y = 0
for i, v in ipairs(_translateY) do
y = y + v
end
return y
end
function love.graphics.origin()
for i = 1, _translateCounter[#_translateCounter] do
_translateX[#_translateX - (i-1)] = 0
_translateY[#_translateY - (i-1)] = 0
end
_origin()
end
function love.graphics.setScissor(x, y, w, h)
if x and type(x) == "number" then
x = x + love.graphics.getTranslationX()
end
if y and type(y) == "number" then
y = y + love.graphics.getTranslationY()
end
_scissor(x, y, w, h)
end
Code: Select all
function table.map(f, t)
local function apply(values)
local results = {}
for index,value in pairs(values) do
results[index] = f(value)
end
return results
end
if t then
return apply(t)
else
return apply
end
end
Code: Select all
function scrollText( text, limit ) -- retain only most recent lines (limit determines number)
local limit, numlines, location = limit or 20, 0, {}
location[0] = 0
while true do
location[ numlines+1 ] = string.find( text, "\n", location[numlines] + 1 )
if location[ numlines+1 ] == nil then
break
else
numlines = numlines + 1
end
end
if numlines > limit then
text = string.sub( text, location[ numlines - limit ], #text )
end
return text
end
Code: Select all
text = ' line 7\n line 6\n line 5\n line 4\n line 3\n line 2\n line 1\n'
text = scrollText( text, 5 )
print( text )
line 5
line 4
line 3
line 2
line 1
You can use font:getWrap() to find how mans lines text will take up when drawn and wrapped at a certain width (using love.graphics.printf).Ref wrote:Was playing around with the Box2D tut and found that it is difficult to read the text which was being printed to the screen. The text string is erased when a fixed number of characters are in the string.
-snip-
[/code]
Code: Select all
function intersect(x1, y1, x2, y2, x3, y3, x4, y4)
local x21, x43 = x2 - x1, x4 - x3
local y21, y43 = y2 - y1, y4 - y3
local d = x21 * y43 - y21 * x43
if d == 0 then return false end
local xy34 = x3 * y4 - y3 * x4
local xy12 = x1 * y2 - y1 * x2
local a = xy34 * x21 - xy12 * x43
local b = xy34 * y21 - xy12 * y43
return a / d, b / d
end
Code: Select all
function isBetween(n, min, max)
if min > max then
min, max = max, min
end
return (n >= min) and (n <= max)
end
function intersect(x1, y1, x2, y2, x3, y3, x4, y4)
local x21, x43 = x2 - x1, x4 - x3
local y21, y43 = y2 - y1, y4 - y3
local d = x21 * y43 - y21 * x43
if d == 0 then return false end
local xy34 = x3 * y4 - y3 * x4
local xy12 = x1 * y2 - y1 * x2
local a = xy34 * x21 - xy12 * x43
local b = xy34 * y21 - xy12 * y43
return a / d, b / d
end
function intersectL(x1, y1, x2, y2, x3, y3, x4, y4)
local xi, yi = intersect(x1, y1, x2, y2, x3, y3, x4, y4)
if isBetween(xi, x1, x2) and isBetween(yi, y1, y2) and isBetween(xi, x3, x4) and isBetween(yi, y3, y4) then
return xi, yi
end
return false
end