Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View jaames's full-sized avatar
🐳
~

James jaames

🐳
~
  • UK, '97
  • 12:29 (UTC +01:00)
View GitHub Profile
@jaames
jaames / shapeTool.ts
Created November 27, 2022 19:49
projective shape tool demo
export const dist = (x1: number, y1: number, x2: number, y2: number) => Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2));
export class LineSegment {
x0: number = 0;
y0: number = 0;
x1: number = 0;
y1: number = 0;
constructor (x0: number, y0: number, x1: number, y1: number) {
this.x0 = x0;
@jaames
jaames / crc32.lua
Last active September 17, 2022 14:32
crc32 function for playdate lua. only works on strings.
local CRC32_LUT = nil
local function crc32_init()
if not CRC32_LUT then
CRC32_LUT = table.create(255, 0)
local rem
for i = 0, 255 do
rem = i
for j = 1, 8 do
if (rem & 1 == 1) then
@jaames
jaames / playdate-prism-highlight.js
Created August 5, 2022 20:15
Prism.js syntax highlighting settings for Playdate-flavour Lua
Prism.languages.lua = {
'comment': /^#!.+|--(?:\[(=*)\[[\s\S]*?\]\1\]|.*)/m,
// \z may be used to skip the following space
'string': {
pattern: /(["'])(?:(?!\1)[^\\\r\n]|\\z(?:\r\n|\s)|\\(?:\r\n|[^z]))*\1|\[(=*)\[[\s\S]*?\]\2\]/,
greedy: true
},
'number': /\b0x[a-f\d]+(?:\.[a-f\d]*)?(?:p[+-]?\d+)?\b|\b\d+(?:\.\B|(?:\.\d*)?(?:e[+-]?\d+)?\b)|\B\.\d+(?:e[+-]?\d+)?\b/i,
'keyword': /\b(?:and|break|do|else|elseif|end|false|for|function|goto|if|in|local|nil|not|or|repeat|return|then|true|until|while)\b/,
'function': /(?!\d)\w+(?=\s*(?:[({]))/,
@jaames
jaames / PdvParser.ts
Last active July 6, 2022 20:33
Basic Playdate .PDV video format parser in Typescript (doesn't handle frame type 3 yet)
import { unzlibSync } from 'fflate';
function assert(condition: boolean, errMsg: string = 'Assert failed'): asserts condition {
if (!condition) {
console.trace(errMsg);
throw new Error(errMsg);
}
}
function readChars(data: DataView, ptr: number, size?: number) {
// run in node.js, version 16.0 or later
// assumes sound effects will be in a folder called 'pda', located next to the script (replace all instances of './pda' to change this)
const fs = require('fs');
function assert(condition, errMsg = 'Assert failed') {
if (!condition) {
console.trace(errMsg);
throw new Error(errMsg);
}
@jaames
jaames / playdate_season_1.md
Created May 23, 2022 18:11
Playdate Season 1 game bundle IDs. Enjoy :)
com.panic.b360
com.tpmcosoft.battleship
com.a-m.beats-bleeps-boogie
com.radstronomical.CasualBirder
com.uvula.crankin
com.crookedpark.demonquest85
com.samanthazero.echoicmemory
com.davemakes.execgolf
com.serenityforge.elevator
@jaames
jaames / playdate_perf.md
Last active August 30, 2022 22:44
James' Playdate performance tips

General tips

  • For repeated expensive drawing options, such as drawing a rotated sprite or a large amount of text, it's a good idea to cache the result to an image once and draw that repeatedly instead.
  • Audio formats in order of decode speed, fastest to slowest: 16-bit pcm wav, 8-bit pcm wav, adpcm, mp3.
  • The Playdate's audio sample rate is 44100, but if you're experiencing lag when playing audio at that rate, you can try halving the rate to 22050.

C tips

  • The Playdate has a weak CPU but a comparatively generous amount of memory. Favour memory-based optimisation techniques, such as lookup tables.
  • Where possible, move branch statements outside of loops, even if it means duplicating the code for the loop. (probably applies to Lua too?)
  • If you need to parse a binary file, use playdate->file->seek to jump to the parts you need and playdate->file->read to only read the data you want. Try to avoid memcpy-ing the whole file to memory in one go.
  • If you're blitting to the frame buffer and need to ap
@jaames
jaames / playdate-curve.lua
Last active June 10, 2022 10:08
Simple bezier curve drawing functions for the Playdate Lua SDK
-- bezier curve drawing functions for playdate lua
-- these are based on de Casteljau's algorithm
-- this site has a nice interactive demo to compare both types of curve: https://pomax.github.io/bezierinfo/#flattening
-- draws a curve starting at x1,y1, ending at x3,y3, with x2,y2 being a control point that "pulls" the curve towards it
-- steps is the number of line segments to use, lower is better for performance, higher makes your curve look smoother
-- the playdate is kinda slow, so it's recommended to find a relatively low step number that looks passable enough!
function drawQuadraticBezier(x1, y1, x2, y2, x3, y3, steps)
steps = steps or 8
local d = 1 / steps
@jaames
jaames / playdate_adpcm.c
Last active April 2, 2022 23:07
Example for decoding and playing an ADPCM audio buffer on the Playdate in C, only supports mono IMA-ADPCM with no blocks!
// adpcm.h
#define CLAMP(n, l, h) \
if (n < l) n = l; \
if (n > h) n = h;
#define min(a,b) (((a) < (b)) ? (a) : (b))
static const int8_t indexTable[8] =
{ -1, -1, -1, -1, 2, 4, 6, 8 };
@jaames
jaames / pd_bitmap_notes.c
Created March 8, 2022 11:00
playdate->graphics->getBitmapData pixel format notes
int width = 0;
int height = 0;
int stride = 0;
int hasMask = 0;
u8* bitmapBuffer;
pd->graphics->getBitmapData(bitmap, &width, &height, &stride, &hasMask, &bitmapBuffer);
// bitmap data is comprised of two maps for each channel, one after the other
int mapSize = height * stride;