Skip to content

Instantly share code, notes, and snippets.

View mattiasgustavsson's full-sized avatar
💾

Mattias Gustavsson mattiasgustavsson

💾
View GitHub Profile
@mattiasgustavsson
mattiasgustavsson / testfw.h
Last active July 15, 2025 08:34
Very basic "test framework", meant for when I don't want to use my full `testfw.h` lib (usage syntax is the same for both)
char const* g_testfw_desc = NULL; int g_testfw_line = 0, g_testfw_current_test_failed = 0, g_testfw_tests_count = 0, g_testfw_asserts_count = 0, g_testfw_tests_failed = 0, g_testfw_asserts_failed = 0;
#define TESTFW_INIT() g_testfw_desc = NULL; g_testfw_line = 0; g_testfw_current_test_failed = 0; g_testfw_tests_count = 0; g_testfw_asserts_count = 0; g_testfw_tests_failed = 0; g_testfw_asserts_failed = 0;
#define TESTFW_SUMMARY() \
( ( g_testfw_tests_failed == 0 ) ? printf( "\n===============================================================================\nAll tests passed (%d assertions in %d test cases)\n", g_testfw_asserts_count, g_testfw_tests_count ) : \
( printf( "\n===============================================================================\ntest cases: %4d | %4d passed | %4d failed\n", g_testfw_tests_count, g_testfw_tests_count - g_testfw_tests_failed, g_testfw_tests_failed ),\
printf( "assertions: %4d | %4d passed | %4d failed\n", g_testfw_asserts_count, g_testfw_asserts_count - g_testfw
@mattiasgustavsson
mattiasgustavsson / assert.h
Last active April 2, 2025 17:02
Custom assert macro for windows
//------------------------------------------ assert.h ----------------------------------------------------
#ifdef _MSC_VER
#ifdef NDEBUG
#define ASSERT( expression, message )
#else
int display_assert_message( char const* expression, char const* message, char const* file, int line,
char const* function );
#define ASSERT( expression, message ) \
do { \
// In C, a void* can be implicitly cast to any other kind of pointer, while in C++ you need an explicit cast. In most
// cases, the explicit cast works for both C and C++, but if we consider the case where we have nested structs, then
// the way you refer to them differs between C and C++ (in C++, `parent_type::nested_type`, in C just `nested_type`).
// In addition, with the automatic cast in C, it is possible to use unnamed nested structs and still dynamically
// allocate arrays of that type - this would be desirable when the code is compiled from C++ as well.
// This VOID_CAST macro allows for automatic cast from void* in C++. In C, it does nothing, but for C++ it uses a
// simple template function to define a cast-to-anything operator.
// Use like this:
// struct {
// struct {
// Rounds up to the next nearest power-of-two value
static inline unsigned int pow2_ceil( unsigned int x ) {
#if defined( __clang__ ) || defined( __GNUC__ )
return 1 + ( ( x >= 2 ) * ( ( ( 1u << 31 ) >> ( __builtin_clz( ( x - 1 ) | 1 ) - 1 ) ) - 1 ) );
#elif defined( _MSC_VER ) && ( defined( _M_IX86 ) || defined( _M_X64 ) )
return ( 1u << 31 ) >> ( __lzcnt( ( x - 1 ) | ( x == 0 ) ) - 1 );
#elif defined( _MSC_VER ) && ( defined( _M_ARM ) || defined( _M_ARM64 ) )
return ( 1u << 31 ) >> ( __clz( ( x - 1 ) | ( x == 0 ) ) - 1 );
#else
--x; // if x is already pow2, we don't want the next pow2
// color1 and color2 are R4G4B4 12bit RGB color values, alpha is 0-255
uint16_t blend_12bit( uint16_t color1, uint16_t color2, uint8_t alpha ) {
uint64_t c1 = (uint64_t) color1;
uint64_t c2 = (uint64_t) color2;
uint64_t a = (uint64_t)( alpha >> 4 );
// bit magic to alpha blend R G B with single mul
c1 = ( c1 | ( c1 << 12 ) ) & 0x0f0f0f;
c2 = ( c2 | ( c2 << 12 ) ) & 0x0f0f0f;
uint32_t o = ( ( ( ( c2 - c1 ) * a ) >> 4 ) + c1 ) & 0x0f0f0f;
@mattiasgustavsson
mattiasgustavsson / bass_and_treble.h
Created January 30, 2025 13:06
Audio filters for a stereo widening effect and for simple bass/treble eq
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
bass_and_treble.h - v1.0 - Simple audio filter to boost bass and treble.
Do this:
#define BASS_AND_TREBLE_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
@mattiasgustavsson
mattiasgustavsson / lowpass.h
Last active March 28, 2025 16:32
low pass filter
typedef struct filter_t {
double a0, a1, a2; // Feedforward coefficients
double b1, b2; // Feedback coefficients
double z1, z2; // Filter state (history)
} filter_t;
void filter_init(filter_t *filter, double cutoff, double sample_rate) {
double const PI = 3.14159265358979323846;
double omega = 2.0 * PI * cutoff / sample_rate;
@mattiasgustavsson
mattiasgustavsson / main.c
Created March 5, 2024 19:18
Minimal code example for creating a window to plot pixels to
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <windows.h>
#pragma comment( lib, "user32.lib" )
#pragma comment( lib, "gdi32.lib" )
#define SCRW 640
#define SCRH 480
@mattiasgustavsson
mattiasgustavsson / strpool.c
Last active March 30, 2025 11:00
Simple string allocation system, allowing for freeing all allocated strings in one go
#define STR_NEW( pool, str ) ( pool = memcpy( (char*) memcpy( malloc( ( str ? strlen( str ) : 0 ) + 1 + \
sizeof( char* ) ), &pool, sizeof( char* ) ) + sizeof( char*), str ? str : "", ( str ? strlen( str ) : 0 ) + 1 ) )
#define STR_FREE( pool ) while( pool ) { char* STR_FREE_TMP = ( pool - sizeof( char* ) ); \
pool = ( *(char**)STR_FREE_TMP ); free( STR_FREE_TMP ); }
//---------------------------
char* strpool = NULL;