Skip to content

Instantly share code, notes, and snippets.

@pfirsich
pfirsich / physfs_io.cpp
Created May 17, 2023 17:41
An example of a C++ wrapper for `PHYSFS_Io`
#include <cstdio>
#include <fmt/format.h>
#include <physfs.h>
template <typename Derived>
struct PhysfsIo : PHYSFS_Io {
protected:
PhysfsIo()
: PHYSFS_Io {
#pragma once
#include <string>
#include <spdlog/spdlog.h>
template <typename Derived>
struct DebugLogging {
DebugLogging() { spdlog::info("{} constructed", debugId()); }
@pfirsich
pfirsich / mpsc_queue.cpp
Last active April 26, 2022 10:59
Dmitry Vyukov's thread-safe multiple producer single consumer queue with wait free production ported to C++. I read the source code carefully and added lots of comments explaining what it does (it took me a while to figure it all out).
// g++ -Wall -Wextra -O0 -g -o log_test
#include <atomic>
#include <optional>
// Vyukov MPSC (wait-free multiple producers, single consumer) queue
// https://www.1024cores.net/home/lock-free-algorithms/queues/intrusive-mpsc-node-based-queue
template <typename T>
class MpscQueue {
public:
@pfirsich
pfirsich / convertfont.py
Created March 22, 2020 20:28
BMFont XML to Text for löve
#!/usr/bin/python3
import argparse
import sys
import xml.etree.ElementTree as ET
# In general the attributes I copy are not the ones that BMFont allows, but the ones that löve will read:
# https://github.com/love2d/love/blob/fc4847c69d6c9ad7ed84501197427b23400ae1b4/src/modules/font/BMFontRasterizer.cpp
# That means in general this script might generate files that might not be fully compliant BMFont files
# but they should work with löve
@pfirsich
pfirsich / setColor.lua
Created October 17, 2018 13:23
love.graphics.setColor compatibility monkeypatch for migrating from love 0.10.2 to 11+
do
local oldSetColor = love.graphics.setColor
function love.graphics.setColor(r, g, b, a)
if type(r) == "table" then
assert(#r == 3 or #r == 4)
assert(g == nil and b == nil and a == nil)
r, g, b, a = unpack(r)
end
assert(type(r) == "number" and type(g) == "number" and type(b) == "number")
@pfirsich
pfirsich / static_var_default.cpp
Last active October 1, 2018 01:42
A function `getVar` that gets the value of a static variable inside a class (template argument) and returns a default value, if that static variable is not present.
#include <iostream>
using VarType = int;
const VarType DEFAULT_VAR = 0;
struct doesHaveVar {
static const VarType var = 1;
};
struct doesNotHaveVar {
@pfirsich
pfirsich / bitmask.lua
Created August 26, 2018 18:04
A LuaJIT bitmask class (like std::bitset)
local bit = require("bit")
local band, bor, bxor, bnot, lshift = bit.band, bit.bor, bit.bxor, bit.bnot, bit.lshift
local function pot(n) -- power of two
return lshift(1, n)
end
local BitMask = setmetatable({}, {__call = function(t, ...)
local self = setmetatable({}, t)
self:initialize(...)
@pfirsich
pfirsich / pot_bench.lua
Created August 25, 2018 10:42
Power of Two function benchmark for luajit (löve used for timing)
local potLut = {}
potLut[0] = 1
for i = 1, 32 do
potLut[i] = potLut[i-1] * 2
end
-- compare this with lshift(2, n)
local function potA(n) -- power of two
return potLut[n]
end
@pfirsich
pfirsich / _hybrid.lua
Last active August 23, 2018 21:39
Sketches for high-level library for kaun (chu)
-- resources (kaun)
local paddleMesh = kaun.newMesh("paddle.obj")
local paddleTexture = kaun.newTexture("paddle.png")
local ballMesh = kaun.newSphereMesh(1.0, 12, 12)
local ballTexture = kaun.newTexture("ball.png")
local level = kaun.newMesh("level.obj")
local skyboxMesh = kaun.newBoxMesh(1, 1, 1)
@pfirsich
pfirsich / controlconfigscripts.md
Last active July 16, 2018 10:02
Documentation for Controll Config Scripts in Spectacular Shatter Buddies (http://shatterbuddies.net/)

Control Config Scripts (*.sbcs)

Notes

For syntax highlighting you can usually use JavaScript or C syntax highlighting.

The configs you find in the save directory in control_configs will be overwritten every time you restart the game. If you want to make a custom control script, you need to create a copy with a different name.

Sadly if there is a syntax error in these scripts, the script will be parsed up until that error without notifying you, so that you will have only partially working controls!

General Syntax