Skip to content

Instantly share code, notes, and snippets.

@RandyGaul
RandyGaul / serialize.h
Last active December 15, 2025 20:58
SV - Save Version - Saving versioned binary data
//--------------------------------------------------------------------------------------------------
// Hacky compat layer (fill in with your own definitions if you want).
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
typedef struct v2 { float x, y; } v2;
#define V2(x,y) ((v2){ x, y })
#define dyna
#define sdyna
@RandyGaul
RandyGaul / Notes.txt
Created October 10, 2025 19:41
CF renderer blog post sketch
renderer
- sprite as pod type
- Online atlas compiler
- SDF shapes
- Local aa
- Local rounding
- Alpha discard cutouts and shrink wrap OBB quads
- Polylines with SDF compatible triangulation
- On demand glyph rasterization
- No need to declare utf8 codes up-front, extremely efficient storage for blur/bold/italic, multiple languages, multiple fonts, etc
@RandyGaul
RandyGaul / ecs.h
Created September 25, 2025 21:23
ECS API in C
#define PICO_ECS_IMPLEMENTATION
#include <thirdparty/pico_ecs.h>
ecs_t* g_ecs;
//--------------------------------------------------------------------------------------------------
// Components.
// Adds a component to the ECS.
// ...You must define T_ctor and T_dtor for your component.
@RandyGaul
RandyGaul / input.h
Created September 23, 2025 22:11
Experimental highish level CF input binding layer
//--------------------------------------------------------------------------------------------------
// Basic input polling.
// Original implementation by Noel Berry, taken from his Blah code on GitHub.
#define joy_press cf_joypad_button_just_pressed
#define joy_release cf_joypad_button_just_released
#define joy_axis cf_joypad_axis
#define joy_axis_prev cf_joypad_axis_prev
#define joy_down cf_joypad_button_down
#define joy_up !cf_joypad_button_down
@RandyGaul
RandyGaul / array.c
Created September 3, 2025 18:55
dynamic array in C (stretchy buffer technique by stb)
#include <stdio.h>
#include <stdlib.h>
//--------------------------------------------------------------------------------------------------
// Dynamic array public API.
#define array_count(a) ((a) ? ARRAY_HEADER(a)->len : 0)
#define array_cap(a) ((a) ? ARRAY_HEADER(a)->cap : 0)
#define array_free(a) ((a) ? (free(ARRAY_HEADER(a)), (a)=NULL, 0) : 0)
#define array_add(a, val) (array__maybegrow(a, 1), (a)[ARRAY_HEADER(a)->len++] = (val))
@RandyGaul
RandyGaul / climb.lua
Last active July 28, 2025 23:52
Climbing example of 2d platformer coroutine use case
function Player:climb()
self.climbing = true
self:set_facing(self.climbable_wall_facing)
-- Initially zero out all y-vel to "latch" onto the wall.
if not self.climb_sliding then
self.vy = 0
end
self.vx = 0
self.climbing_needs_reset = true
@RandyGaul
RandyGaul / vmath.h
Last active June 17, 2025 05:18
Common 3D math
#pragma once
#ifndef VMATH_INLINE
# define VMATH_INLINE inline
#endif
VMATH_INLINE float clamp(float x, float minv, float maxv) { return fmaxf(minv, fminf(maxv, x)); }
VMATH_INLINE float lerp(float a, float b, float t) { return a * (1.0f - t) + b * t; }
struct V3
@RandyGaul
RandyGaul / coroutine.h
Created October 20, 2024 02:21
C Coroutine (macro based)
// A portable "coroutine"-like thing for implementing FSM and behavior-cycles.
//
// Original implementation by Noel Berry.
// See: https://gist.github.com/NoelFB/7a5fa66fc29dd7ed1c11042c30f1b00e
//
// Routine is a set of macros useful to implement Finite State Machine type of
// behaviors. It works really well for simpler behaviors with loops or cycles,
// and works especially well for anything resembling a cutscene.
//
// The macros here are wrappers around a switch statement. All of the `rt_***`
@RandyGaul
RandyGaul / spatial_hash.c
Last active October 16, 2024 04:18
2d spatial hashing
/**
* 2D spatial hash implementation.
*
* The spatial hash is really good at representing grids in games. It only stores elements
* of the grid where items are inserted into the spacial hash, making it a memory-efficient
* option for grid games with lots of empty space between tiles/entities.
*
* Spatial hash queries are extremely fast for small queries covering low surface areas.
*
* Spatial hashes breakdown when queries cover large surface areas. This is because each
@RandyGaul
RandyGaul / prio_q.lua
Created August 3, 2024 16:37
priority queue implementation in lua
-- Piorirty queue, used for implementing other algorithms such as
-- prioritized message queues, or the A* algorithm.
--
-- Create a new priority q like so:
--
-- q = prio_q()
--
-- Add elements to the queue, each has a cost associated. THe cost is
-- used to sort the elements, where the lowest cost is considered the
-- highest priority (first out when pop() is called).