Last active
October 30, 2023 16:31
-
-
Save NPException/9e23ae67e78f78698a4ac37fd85ca58e to your computer and use it in GitHub Desktop.
Playdate code snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Tiny snippet to bring a sort-of `require` functionality, | |
-- where return values of a module (or pdz files in this case) | |
-- will be cached, and the file only be run once. | |
-- After `import`ing this file in your main file, you can use `require` similar to how you would in basic Lua. | |
-- This also adds convenience wrappers around `playdate.file.load()` and `playdate.file.run()`, | |
-- to pose as `loadfile()` and `dofile()` respectively. | |
local pd <const> = playdate | |
local function loadfile(pdzFile, ...) | |
return pd.file.load(pdzFile, ...) | |
end | |
_G.loadfile = loadfile | |
local assert <const> = assert | |
local function dofile (filename) | |
local f = assert(loadfile(filename)) | |
return f() | |
end | |
_G.dofile = dofile | |
local seen <const> = {} | |
local cache <const> = {} | |
local function require(pdzFile) | |
print("Require: " .. pdzFile) | |
if seen[pdzFile] then | |
return cache[pdzFile] | |
end | |
local value = dofile(pdzFile) | |
seen[pdzFile] = true | |
cache[pdzFile] = value | |
return value | |
end | |
_G.require = require |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Fennel macro module which can be imported via `(import-macros ...)` | |
; creates a local that is tagged as a constant and can't be reassigned (which brings a bit more performance) | |
(fn const [name x] | |
`(lua ,(.. "local " (tostring name) " <const> = " (tostring x)))) | |
; Wrap a body of code in `gfx.pushContext(img) ... gfx.popContext()`. | |
; (Assumes that `playdate.graphics` is assigned to a local const named `gfx`) | |
(fn with-context [img ...] | |
(let [tbl [`(gfx.pushContext ,img) ...]] | |
(table.insert tbl `(gfx.popContext)) | |
`(do ,(unpack tbl)))) | |
; Sample a body of code via `CoreLibs/utilities/sampler` | |
; (remember to import that library if you want to use this) | |
(fn sampling [sample-name ...] | |
`(sample ,sample-name | |
(fn [] ,...))) | |
{: const | |
: with-context | |
: sampling} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment