Small Useful Functions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Small Useful Functions

Post 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 :)
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Small Useful Functions

Post 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
User avatar
SpotlightKid
Prole
Posts: 6
Joined: Mon Nov 10, 2014 7:57 am
Location: Cologne, Germany

Re: Small extra functions

Post 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()
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Small Useful Functions

Post 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
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Small Useful Functions

Post 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
Last edited by s-ol on Thu Dec 04, 2014 11:10 pm, edited 1 time in total.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Small Useful Functions

Post by bartbes »

I assume the comment on the last snippet is meant to say "all 11 are 5 now".
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Small Useful Functions

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Small Useful Functions

Post by Robin »

count(...) is the same as select('#', ...), though.
Help us help you: attach a .love.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Small Useful Functions

Post by s-ol »

Robin wrote:count(...) is the same as select('#', ...), though.
Its the same as #{...} too. It just looks a little cleaner.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Small Useful Functions

Post 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
@HugoBDesigner - Twitter
HugoBDesigner - Blog
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 74 guests