Skip to content

Instantly share code, notes, and snippets.

View pmttavara's full-sized avatar

Phillip Trudeau pmttavara

View GitHub Profile
@pmttavara
pmttavara / good_assert.h
Last active May 18, 2023 12:33
Better assert() for Windows - public domain
#ifndef good_assert /* Better assert() for Windows - public domain */
#ifdef _WIN32
#ifdef __cplusplus
extern "C" {
#endif
#ifndef _WINDOWS_
__declspec(dllimport) int __stdcall ExitProcess(unsigned int a);
__declspec(dllimport) int __stdcall MessageBoxA(void *a, const char *b,
const char *c, unsigned int d);
#endif /* _WINDOWS_ */
@pmttavara
pmttavara / defer.hpp
Created March 25, 2018 07:27
The definitive defer implementation for C++. Syntax: defer { statements; };
#ifndef defer
struct defer_dummy {};
template <class F> struct deferrer { F f; ~deferrer() { f(); } };
template <class F> deferrer<F> operator*(defer_dummy, F f) { return {f}; }
#define DEFER_(LINE) zz_defer##LINE
#define DEFER(LINE) DEFER_(LINE)
#define defer auto DEFER(__LINE__) = defer_dummy{} *[&]()
#endif // defer
@pmttavara
pmttavara / readme.txt
Last active December 7, 2018 10:27
eddems (PuzzleScript Script)
Play this game by pasting the script in http://www.puzzlescript.net/editor.html
@pmttavara
pmttavara / dead_simple_debug_allocator.c
Created March 31, 2019 07:33
Debug allocator that catches leaks, use-after-free, double-free, buffer overrun, invalid/mismatched free. Windows x64. Simple but slow (no page reuse).
// libs: Kernel32.lib User32.lib DbgHelp.lib msvcrt.lib (unless you have your own printf)
// includes: Windows.h (LEAN_AND_MEAN is ok) WinUser.h DbgHelp.h stdio.h
// call SymInitialize() at startup and debug_check_leaks at shutdown
#ifndef NDEBUG
static void print_stack_trace(void **stack, u64 stack_count, const char *indent) {
struct {
SYMBOL_INFO si;
@pmttavara
pmttavara / hello_world.c
Created April 10, 2019 06:53
Simple C Win32 Hello World program without preprocessor directives or library imports. Build with `cl *.c`.
__pragma(comment(linker, "/entry:main"));
__pragma(comment(linker, "/subsystem:console"));
void *(*GetProcAddress)(void *module, const char *name);
void *(*LoadLibraryA)(const char *name);
void (*ExitProcess)(unsigned int return_code);
int (*MessageBoxA)(void *hWnd, const char *lpText, const char *lpCaption, unsigned int uType);
static inline void load_proc(void *lib, void *fn, char *name) {
*(void **)fn = GetProcAddress(lib, name);
@pmttavara
pmttavara / X_macro_call_by_name.cpp
Last active June 27, 2019 10:09
(for Ryan Fleury's stream) Variant of the X macro that doesn't require undefinition or redefinition of macros.
// Whatever, just example instructions.
#define INSTRUCTION_TYPE_LIST(instruction_type) \
instruction_type(push, sizeof(u32)) \
instruction_type(pop, sizeof(u32)) \
instruction_type(move, sizeof(u32 *) + sizeof(u32 *))
// Examples of usage (I didn't quite catch what the real usage was):
#define instruction_enum_name(name, size) Instruction_Type_ ## name,
#define instr_size(name, size) (size_t)size,
@pmttavara
pmttavara / virtual_array.cpp
Last active December 29, 2019 01:26
Virtual allocated array template
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <windows.h>
template <class T> struct Virtual_Array {
@pmttavara
pmttavara / ogl_load.c
Created January 2, 2020 08:37
OpenGL 1.0 loader in Win32
static const char *const GL_funcnames[] = {
"wglDeleteContext",
"wglUseFontBitmapsA",
"wglSwapIntervalEXT",
"glGetError",
"glBlendFunc",
"glEnable",
"glDisable",
"glViewport",
"glVertex3f",
@pmttavara
pmttavara / enum_strings.cpp
Last active January 6, 2024 12:42
Using "functional X macro" for metaprogramming-like enum name extraction
#include <string.h> // for strcmp
#define Enum_Member(Name, Value) Name = Value,
#define Enum_To_String(Name, Value) if (e == Name) return # Name;
#define Enum_From_String(Name, Value) if (strcmp(str, # Name) == 0) return Name;
#define Define_Enum(EnumName) \
\
@pmttavara
pmttavara / allocator.h
Created February 27, 2021 06:04
Allocator abstraction that works for most use cases, according to Andrei Alexandrescu (+ Jon Blow)
#ifdef __cplusplus
extern "C" {
#endif
typedef struct Memory_Block {
uint8_t *data;
size_t num_bytes;
} Memory_Block;
typedef enum Allocator_Mode {