Page 8 of 13

Re: Small Useful Functions

Posted: Tue Oct 14, 2014 6:52 pm
by undef
Robin wrote:Yeah, it's just fun and games (hopefully no-one will lose an eye). ;)

Also: ooh, that one is better. Here's one without select:

Code: Select all

local function requirei(d, m, ...)
    if m then
        return require(d .. m), requirei(d, ...)
    end
end
Ah, beautiful :)
Select was quite the nuisance to me when I edited that code earlier, but I couldn't find a way to avoid it.
Now, as usual, this code looks like an obvious approach to me :)

Re: Small Useful Functions

Posted: Wed Oct 15, 2014 7:47 am
by Roland_Yonaba
Robin wrote:

Code: Select all

local function requirei(d, m, ...)
    if m then
        return require(d .. m), requirei(d, ...)
    end
end
Clean, Robin, clean. I was just about to suggest that. I remember I once shared similar code (not for require though) an I got an awesome response from Bartbes. :3

Re: Small extra functions

Posted: Mon Nov 10, 2014 10:39 pm
by SpotlightKid
HugoBDesigner wrote:A code I wrote to convert frames to quads (a new image in quad format)!
Neat idea! Here's an IMHO cleaner and more efficient version. Instead of copying each pixel, use ImageData:paste() instead.

Still, this is something that should probably better be done using a general purpose graphics package, instead of LÖVE. The restrictions of love.filesystem mean you have to put the script in a love package and name it main.lua, copy the image files in a subdirectory, run it and then get the output file from the save directory somewhere buried in ~/.local/share or similar. But it does work ;)

Code: Select all

love.filesystem.setIdentity("maketilestrip")
local folder = arg[2]
local width = 0
local height = 0
local images = {}
local files = love.filesystem.getDirectoryItems(folder)
table.sort(files)

for _, file in ipairs(files) do
    if string.sub(file, -4, -1) == ".png" then
        local image = love.image.newImageData(folder .. "/" .. file)
        local w, h = image:getDimensions()
        images[#images+1] = {image, width, w, h}
        width = width + w
        height = math.max(height, h)
    end
end

if #images ~= 0 then
    local newimage = love.image.newImageData(width, height)

    for _, image in ipairs(images) do
        image, x, w, h = unpack(image)
        newimage:paste(image, x, height - h, 0, 0, w, h)
    end

    print(("Writing output to '%s/%s.png'..."):format(
        love.filesystem.getSaveDirectory():gsub('//', '/'), folder))
    newimage:encode(folder .. ".png")
end

love.event.quit()

Re: Small Useful Functions

Posted: Tue Nov 25, 2014 2:22 pm
by HugoBDesigner
For my physics tutorial program, I decided to do something a little and have dashed lines. So here are two functions to have dashed outlines - in rectangles and in lines:

Code: Select all

function pointyline(x1, y1, x2, y2, pointyness) --Pointyness should be a word, it sounds so funny :P
	local pointyness = pointyness or 10
	local a = math.atan2(y2-y1, x2-x1)
	
	local size = dist(x1, y1, x2, y2)
	
	for i = 1, math.ceil(size/pointyness) do
		local p1x, p1y = math.cos(a)*(i-1)*pointyness, math.sin(a)*(i-1)*pointyness
		local p2x, p2y = math.cos(a)*i*pointyness, math.sin(a)*i*pointyness
		
		if i == math.ceil(size/pointyness) then
			p2x = x2-x1
			p2y = y2-y1
		end
		
		if math.mod(i, 2) ~= 0 then
			love.graphics.line(p1x+x1, p1y+y1, p2x+x1, p2y+y1)
		end
	end
end

function pointyrectangle(x, y, w, h, pointyness)
	pointyline(x, y, x + w, y, pointyness)
	pointyline(x, y, x, y + h, pointyness)
	pointyline(x + w, y, x + w, y + h, pointyness)
	pointyline(x, y + h, x + w, y + h, pointyness)
end
Image

Re: Small Useful Functions

Posted: Thu Dec 04, 2014 9:20 pm
by s-ol
A bunch of functional-programming-esque utility functions:

Code: Select all

function count(...) -- seems useless but can add some syntactic sugar
	return #{...}
end

Code: Select all

local STATE_IDLE, STATE_RUNNING, STATE_JUMPING, STATE_DEAD = enum( 7 )

function enum( n )
	local l = {}
	for i=1,n do
		l[i] = i
	end
	return unpack(l)
end

Code: Select all

local nested = { {a=3, b=4}, {a=5, b=6}, {a=7, b=8} }
local res = eachIndex( nested, 'b' ) -- => { 4, 6, 8 }

function eachIndex( all, id )
	local l = {}
	for i,v in ipairs( all ) do
		l[i] = v[id]
	end
	return unpack(l)
end

Code: Select all

local unnamed = { 1, 2, 3, 4, 5 }
rename( {"one", "two", "three", "four", "five"}, unnamed ) -- => { one=1, two=2, three=3, four=4, five=5 }

function rename( names, ... )
	local l = {}
	local arg = {...}
	for i,v in ipairs( arg ) do
		l[names[i]] = v
	end
	return l
end

Code: Select all

local a, b = all( 0 ) -- all are 0 now
local a, l, o, t, of, v, ar, i, ab, le, s = all( 5, 11 ) -- all 11 are 5 now

function all( val, num ) 
	local r = {} 
	for i=1,(num or 10) do
		r[i] = val
	end
	return unpack(r)
end

Re: Small Useful Functions

Posted: Thu Dec 04, 2014 9:44 pm
by bartbes
I assume the comment on the last snippet is meant to say "all 11 are 5 now".

Re: Small Useful Functions

Posted: Thu Dec 04, 2014 11:10 pm
by s-ol
bartbes wrote:I assume the comment on the last snippet is meant to say "all 11 are 5 now".
Whoops, you are right.

Re: Small Useful Functions

Posted: Fri Dec 05, 2014 3:20 pm
by Robin
count(...) is the same as select('#', ...), though.

Re: Small Useful Functions

Posted: Fri Dec 05, 2014 8:09 pm
by s-ol
Robin wrote:count(...) is the same as select('#', ...), though.
Its the same as #{...} too. It just looks a little cleaner.

Re: Small Useful Functions

Posted: Mon Dec 08, 2014 5:46 am
by HugoBDesigner
A code that I made for a friend of mine to fix the space that the characters in some fonts leave. It works great, but there are some problems:
1. I only made it work for a single font, even though adapting it for several fonts shouldn't be so hard...
2. I also made it only add/remove space *before* the character, so if the extra/missing space is *after* the character, it can't be fixed.

If anyone decides to use this but needs any of the things above fixed, you can let me know. I only made this quickly, so it isn't optimized either (this friend suggested me to make it a recursive function, but I didn't want to, since it'd take some time to figure out).

Code: Select all

spaceTable = {["f"] = -6, ["b"] = 5} --You can place this on love.load (it is better, actually)

function love.graphics.newPrint(t, x, y)
	local font = love.graphics.getFont()
	local last = {size = 0, off = false, t = ""}
	for i = 1, string.len(t) do
		local v = string.sub(t, i, i)
		local off = last.off or 0
		
		if spaceTable[v] then
			love.graphics.print(last.t, x + last.size + off, y)
			last.t = v
			if not last.off then
				last.off = spaceTable[v]
			else
				last.off = last.off + spaceTable[v]
			end
			last.size = font:getWidth(string.sub(t, 1, i-1))
		else
			last.t = last.t .. v
		end
		
		if i == string.len(t) then
			off = last.off or 0
			love.graphics.print(last.t, x + last.size + off, y)
		end
	end
end
Image