Skip to content

Instantly share code, notes, and snippets.

@baines
baines / tag.h
Created June 8, 2019 14:44
C integer constant variable length string tag macros (up to 8 chars), for enums etc
#include <stdint.h> // uint64_t
#define TSH(a,b) ((uint64_t)(a) << (b * 8))
#define TAG_1(a) (TSH(a,0))
#define TAG_2(b,a) (TSH(b,1)|TSH(a,0))
#define TAG_3(c,b,a) (TSH(c,2)|TSH(b,1)|TSH(a,0))
#define TAG_4(d,c,b,a) (TSH(d,3)|TSH(c,2)|TSH(b,1)|TSH(a,0))
#define TAG_5(e,d,c,b,a) (TSH(e,4)|TSH(d,3)|TSH(c,2)|TSH(b,1)|TSH(a,0))
#define TAG_6(f,e,d,c,b,a) (TSH(f,5)|TSH(e,4)|TSH(d,3)|TSH(c,2)|TSH(b,1)|TSH(a,0))
@baines
baines / inso_json.h
Last active March 14, 2023 19:51
json tokenizer
#define _GNU_SOURCE
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
// streaming json tokenizer that operates on fixed sized memory chunks
// no dynamic memory allocation necessary
enum ij_type {
@baines
baines / asmsort.s
Created January 17, 2017 01:27
x86_64 linux sort program, 640 bytes
#!/bin/bash
# example: ./asmsort 3 2 1 -> prints 1 2 3
// 2>/dev/null; as -g $0 -o $0.o && ld -s $0.o -o asmsort; exit
.text
.global _start
quicksort:
dec %rsi # base case
jle qs_end
@baines
baines / quicksort.s
Last active January 16, 2017 17:32
x86_64 asm quicksort
#!/bin/bash
# (gdb) r
# -> SIGTRAP
# (gdb) x/12g $rax
# -> stuff is sorted
// 2>/dev/null; as -g $0 -o $0.o && ld $0.o -o $0.bin && gdb $0.bin; exit
.data
stuff: # numbers to sort
.quad 74, 3, 53, 11, 52, 94, 16, 12, 48, 88, 19, 82
@baines
baines / freetype-atlas.c
Created July 15, 2016 13:32
minimal freetype texture atlas example
#include <stdio.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#define NUM_GLYPHS 128
struct glyph_info {
int x0, y0, x1, y1; // coords of glyph in the texture atlas
@baines
baines / xkeyexample.c
Last active September 19, 2023 20:53
simple xlib key example
#include <stdio.h>
#include <locale.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
int main(void){
setlocale(LC_ALL, "");
Display* dpy = XOpenDisplay(NULL);
@baines
baines / script.txt
Last active April 18, 2016 21:03 — forked from anonymous/readme.txt
Lawn-Mowing Robot [PuzzleScript Game]
title Lawn-Mowing Robot
author Alex Baines
homepage abaines.me.uk
again_interval 0.5
background_color #1B1825
(
debug
verbose_logging
)
========
@baines
baines / twitch-notify.sh
Last active November 9, 2019 12:08
Twitch.tv desktop notification bash script
#!/bin/bash
# twitch-notify.sh by Alex Baines
# Desktop notifications about twitch.tv streams.
# dependencies:
# curl
# underscore-cli
# notify-send
# md5sum
@baines
baines / oggextract.cpp
Last active August 29, 2015 14:06
Extract OGGs from stuff
#include <cstdio>
#include <cstdlib>
#include <numeric>
#include <unistd.h>
bool test_header(FILE* f, int magic = 2){
bool r = false;
char buf[5];
fread(buf, 1, 5, f);
@baines
baines / hls-multi-atomic.cpp
Last active September 14, 2016 22:23
3 C++11 threaded HLS downloader / player thingies
#if 0
g++ $0 -std=c++11 -pthread -lcurl -o hls-dl
exit
#endif
#include <thread>
#include <mutex>
#include <algorithm>
#include <vector>
#include <array>
#include <string>