Skip to content

Instantly share code, notes, and snippets.

View 1bardesign's full-sized avatar

Max Cahill 1bardesign

View GitHub Profile
--sort core
--
-- stable sorting routines for lua
--
--based on MIT licensed code from Dirk Laurie and Steve Fisher
--license as follows:
--[[
Copyright © 2013 Dirk Laurie and Steve Fisher.
--[[
"semi-manual" garbage collection
specify a time budget and a memory ceiling per call.
called once per frame, this will spread any big collections
over a couple of frames, and "catch up" when there is. This
keeps GC burden much more predictable.
The memory ceiling provides some level of "relief valve" - if
@1bardesign
1bardesign / typewriter_demo.lua
Created October 5, 2018 07:03
a simple line by line "typewriter" text demo for love2d
--[[
typewriter class
]]
local typewriter = {}
--(metatable used for instances, don't worry about it too much
-- but do read up on metatables at some point)
typewriter._mt = {__index = typewriter}
--create a new typewriter
@1bardesign
1bardesign / main.lua
Created November 13, 2018 00:10
recolour.lua
--[[
example use
]]
local recolour = require("recolour")
--we want to recolour this asset image
local to_recolour = love.image.newImageData("path/to/image.png")
--using this palette image
local palette = love.image.newImageData("path/to/palette.png")
@1bardesign
1bardesign / audio.lua
Last active November 14, 2018 02:01
will not work out of the box, as it relies on my class() impl and table.foreach, but should be fairly readable as a jumping off point!
--[[
audio manager
]]
--sound source wrapper
local sound = class()
--
function sound:new(source, volume, set)
return self:init({
@1bardesign
1bardesign / oo.lua
Last active February 6, 2019 04:41
state management code - fsm and a push/pop "game state manager" - see state_machine.lua for the main interesting bit
--[[
oop basics
]]
function class(inherits)
local c = {}
c.__mt = {__index = c}
--handle single inheritence
if type(inherits) == "table" and inherits.__mt then
setmetatable(c, inherits.__mt)
@1bardesign
1bardesign / stable_sort.lua
Last active July 7, 2022 00:03
stable sorting routines for lua
-- stable sorting routines for lua
--
-- modifies the global table namespace so you don't have
-- to re-require it everywhere.
--
-- table.stable_sort
-- a fast stable sort
-- table.unstable_sort
-- alias for the builtin unstable table.sort
-- table.insertion_sort
@1bardesign
1bardesign / main.lua
Created May 27, 2019 23:49
state machine example code - to help understand how to define states, and how to use them to keep related logic all in one place
--require the state machine module
local state_machine = require("state_machine")
--variable for the example state machine - set up in love.load
local example_machine
--(simple "last 10" history of what states we've been to)
local state_history = {}
function add_history(s)
table.insert(state_history, s)
@1bardesign
1bardesign / dialogue.lua
Created June 12, 2019 02:32
example dialogue system for love2d based on coroutines - runnable love file prepared here: https://www.dropbox.com/s/0n6vs634jzlk266/dialogue_co.love
--dialogue type
local dialogue = {}
dialogue._mt = {
__index = dialogue,
}
function dialogue:new(f)
return setmetatable({
@1bardesign
1bardesign / loop_benchmark.lua
Last active April 10, 2022 04:27
a microbenchmark comparing different loop types, and different work size per-iteration
--config
local replicas = 3
local test_duration = 0.5
local single_test_size = 1e3
--per-iteration operation
function test_operation(a, b, work_size)
for _ = 1, work_size do
local s = tostring(a + b)