Skip to content

Instantly share code, notes, and snippets.

/* We need this to properly get reported a mouse button release outside a window when there was a drag. */
static unsigned win32_mousebuttonMask; /* a mask of 1<<button for each button that is down. */
static int win32_isMouseCaptured;
static void win32_process_mouse_event(HWND hwnd, UINT msg)
{
/* We capture the mouse using SetCapture() and release the mouse
using ReleaseCapture() when the mouse state changes between having
any or no depressed mouse buttons.
This makes sure that we get notified when the mouse is released
@jstimpfle
jstimpfle / stringpool.c
Last active June 6, 2019 00:30
Simple string pool allocator
#include <string.h> // memset()
#include <stdlib.h> // malloc(), free()
enum {
STRINGPOOLCHUNKSIZE_16,
STRINGPOOLCHUNKSIZE_32,
STRINGPOOLCHUNKSIZE_64,
STRINGPOOLCHUNKSIZE_128,
STRINGPOOLCHUNKSIZE_256,
STRINGPOOLCHUNKSIZE_512,
/*
Dynamic array with an independently allocated count.
This is nice for relational and SOA (structure of arrays) programming.
We can share a count value across multiple dynamic arrays.
This is how it should be! (Except, it's still abstract, and quite a bit of boilerplate)
*/
#include <cassert>
#include <cstdio>
#include <cstdlib>
@jstimpfle
jstimpfle / contest-blurb.c
Last active July 11, 2018 01:32
contest-blurb.c
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define UNUSED __attribute__((unused))
#define NORETURN __attribute__((noreturn))
#define DIE(fmt, ...) _die("FATAL ERROR at line %d: " fmt "\n", __LINE__, ##__VA_ARGS__)
#define REALLOC(ptr, nels) _reallocfail((void**)&(ptr), nels, sizeof *(ptr), __FILE__, __LINE__)
@jstimpfle
jstimpfle / buf.c
Created May 24, 2018 23:25
C code starting point for getting most of what std::vector offers
static void _buf_init(void **buf, int *cap, int elsize, const char *file, const int line)
{
fprintf(stderr, "_buf_init(%p, %s, %d)\n", buf, file, line);
*buf = NULL;
*cap = 0;
}
static void _buf_reserve(void **buf, int *cap, int cnt, int elsize, const char *file, const int line)
{
fprintf(stderr, "_buf_reserve(%p, %s, %d)\n", buf, file, line);
@jstimpfle
jstimpfle / example.txt
Created April 29, 2018 13:26
Example lexer: possible starting point for language experiments
f
Got KIND_NAME token
f f
Got KIND_NAME token
Got KIND_NAME token
f f"ab c" x
Got KIND_NAME token
Got KIND_NAME token
Got KIND_STRING token
Got KIND_NAME token
@jstimpfle
jstimpfle / stackmachine.c
Last active April 28, 2018 15:00
simple stackmachine example (no parser)
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define LENGTH(a) (sizeof (a) / sizeof (a)[0])
enum {
OP_NAME,
OP_VARDECL,
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PROGRAM_NAME "testwidths"
#define MAKE_TEST(type, name) \
int name(int n, int v) \
{ \
@jstimpfle
jstimpfle / sudoku
Created September 3, 2017 09:53
sudoku solver
values = [[None for _ in range(9)] for _ in range(9)]
taken = [[set() for _ in range(9)] for _ in range(9)]
free = set((i, j) for i in range(9) for j in range(9))
def set_val(i, j, val):
assert(val not in taken[i][j])
values[i][j] = val
free.remove((i,j))
for k in range(9):
taken[i][k].add(val)
@jstimpfle
jstimpfle / test.ps
Created June 3, 2017 16:27
dabbling in postscript
1 setlinewidth
% move somewhere near the center of an A4/letter page
100 500 moveto
% string -> (Write the string)
/logstring
{
(%stdout) (w) file
exch writestring
} def