Skip to content

Instantly share code, notes, and snippets.

@jstimpfle
jstimpfle / win32_window_resize_repaint.c
Created December 1, 2022 23:33
win32_window_resize_repaint
// Example how screen updates can be done even when DefaultWindowProc is
// resizing or moving the window. We catch WM_ENTERMENULOOP and WM_ENTERSIZEMOVE
// to start a timer to notify us every so often. This way we regain some of the
// control that DefaultWindowProc takes from us in these modal loops.
//
// When resizing or moving the window, you should see a glimmering red
// background that turns from dark to bright and back again in 180 screen
// updates. That cycle should take a few seconds - it's hard to say how long
// exactly since we're not explicitly synchronizing with the monitor and I'm not
// sure how often we can receive WM_PAINT. The 4ms from the SetTimer() call are
@jstimpfle
jstimpfle / win32_proc_wait.cpp
Last active November 30, 2022 13:54
win32_proc_wait.cpp - Get notified when a Windows process terminates
/* win32_proc_wait.cpp - Get notified when a Windows process terminates */
#include <Windows.h>
typedef void Proc_Terminated_Callback(void *data);
/* Proc_Wait - Waiting for a process to finish on Win32.
Zero out a Proc_Wait structure before use.
Call proc_wait_open() to register an asynchronous wait operation for a process handle.
@jstimpfle
jstimpfle / refgui.c
Last active September 14, 2021 18:32
refgui
// Simple reference for what I think is a sane approach to structuring GUIs.
// Platform code is cleanly separated from the GUI code.
// Win32 backend (the OpenGL code is unused)
// Easy to port to other platforms - X11, SDL, OpenGL...
#include <Windows.h>
#include <windowsx.h> // GET_X_LPARAM(), GET_Y_LPARAM()
#include <mshtmcid.h> // IDM_CUT, IDM_COPY etc
#include <GL/gl.h>
@jstimpfle
jstimpfle / lisp.c
Created February 17, 2021 22:17
Toy Lisp (work in progress)
#include <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#define NORETURN __declspec(noreturn)
@jstimpfle
jstimpfle / bytelang.c
Created January 26, 2021 18:19
A little stack machine, done in a few hours
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#define NORETURN __attribute__((noreturn))
#define ARRAY_LENGTH(a) ((int) (sizeof (a) / sizeof (a)[0]))
//
// Baseline code (compiles on Linux) for sending UDP packets at a relatively
// high rate. With some modifications, this will be suited as a starting point
// to experiment with UDP-based transfer protocols.
//
// Achieves ~350 MiB/s (350K packets/s) on my laptop from 2011 (Lenovo x220,
// Sandy Bridge i5-2520M).
//
// Compile this as a binary "test" and run two instances in parallel (in two
// terminals).
// Experiment: Intrusively linked list, implemented using C++ templates
// 2019, Jens Stimpfle
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
struct ListLink {
ListLink *next;
ListLink *prev;
@jstimpfle
jstimpfle / list.h
Last active January 19, 2021 10:12
// Intrusively doubly linked lists boilerplate. Similar to Linux list.h
// 2020, Jens Stimpfle
typedef struct _ListNode ListNode;
typedef struct _List List;
struct _ListNode {
ListNode *next;
ListNode *prev;
};
/*
Ringbuffers are surprisingly hard to code because wraparound
can split the readable / writeable region in two. This is the
cutest implementation that I've come up with so far.
NOT ACTUALLY TESTED
- jstimpfle, 2020-09
*/
/*
Dynamically resizable array in constant virtual address space.
This will require a 64-bit system (or >32-bit system) for most
purposes. The idea is to reserve a huge chunk of address
space at program startup, but request actual memory backing
only when that address space is actually needed.
The big advantage of this approach is that we get stable
pointers.