Skip to content

Instantly share code, notes, and snippets.

@vurtun
vurtun / lay.c
Last active December 15, 2021 22:37
// gui_lay_row(ctx, 3, (int[]) { 30, -90, -1 }, 0);
/* Layout */
enum gui_lay_dir {
GUI_ROW,
GUI_COL,
};
struct gui_lay_sol {
int fix_siz;
int fix_cnt;
@vurtun
vurtun / ui.c
Last active July 26, 2021 19:55
struct gui_bnd {int min, mid, max, ext;};
struct gui_box {
struct gui_bnd x;
struct gui_bnd y;
};
#define gui_box(x,y,w,h) (struct gui_box){{x,x+(w>>1),x+w,w},{y,y+(h>>1),y+h,h}}
#define gui_min_max(a,b) (struct gui_bnd){a,a+((b-a)>>1),b,b-a}
#define gui_min_ext(m,e) (struct gui_bnd){m,m+(e>>1),m+e,e}
#define gui_max_ext(m,e) (struct gui_bnd){m-e,m-(e>>1),m,e}
#define gui_mid_min(c,m) (struct gui_bnd){m,c,m+c-m,(m-c)<<1}
@vurtun
vurtun / prng.c
Last active March 24, 2020 19:50
static unsigned
wang_hash(unsigned u)
{
u = ((u ^ 61) ^ (u >> 16)) * 9;
u = (u ^ (u >> 4)) * 0x27d4eb2d;
return u ^ (u >> 15);
}
static unsigned
rand_lcg(unsigned *rng)
{
@vurtun
vurtun / utf.c
Last active December 26, 2023 11:58
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define UTF_INVALID 0xFFFD
static const char*
utf_dec(unsigned *dst, const char *p, const char *e)
{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
static int
str_fzy(const char *str, const char *ptn)
{
const char *pat = ptn;
@vurtun
vurtun / zoom.c
Last active January 7, 2020 14:39
Zoom to point
static float
scalbnf(float x, int n)
{
union {float f; unsigned i;} u;
float y = x;
if (n > 127) {
y *= 0x1p127f;
n -= 127;
if (n > 127) {
y *= 0x1p127f;
@vurtun
vurtun / x11_clipboard.c
Last active June 13, 2023 21:28
X11 clipboard
static char*
sys_clip_get(struct sys *s, Atom selection, Atom target)
{
assert(s);
struct sys_x11 *x11 = s->platform;
/* blocking wait for clipboard data */
XEvent notify;
XConvertSelection(x11->dpy, selection, target, selection, x11->helper, CurrentTime);
while (!XCheckTypedWindowEvent(x11->dpy, x11->helper, SelectionNotify, &notify)) {
@vurtun
vurtun / math.c
Last active July 17, 2020 12:19
Linear Algebra
#include <math.h>
#define op(r,e,a,p,b,i,s) ((r) e (a) p ((b) i (s)))
#define lerp(r,a,b,t) ((r)=(a)+(t)*((b)-(a)))
#define rad(x) ((x)*3.141592653f/180.0f)
#define op3(r,e,a,p,b,i,s)\
do{op((r)[0],e,(a)[0],p,(b)[0],i,s), op((r)[1],e,(a)[1],p,(b)[1],i,s),\
op((r)[2],e,(a)[2],p,(b)[2],i,s);} while(0)
#define op3s(r,e,a,p,s)\
do{op((r)[0],e,(0),+,(a)[0],p,s), op((r)[1],e,(0),+,(a)[1],p,s),\
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#define cast(t,p) ((t)(p))
#define szof(a) ((int)sizeof(a))
@vurtun
vurtun / lzw.c
Last active August 2, 2020 12:04
struct lzw {
int bitcnt;
int bits, off;
};
static int
lzw_compressed_size(int size)
{
return size*2;
}
static unsigned char*