Skip to content

Instantly share code, notes, and snippets.

@mmozeiko
mmozeiko / upng.h
Last active October 8, 2024 03:47
uncompressed png writer & reader
#pragma once
// uncompressed png writer & reader
// supports only 8-bit and 16-bit formats
// Performance comparison for 8192x8192 BGRA8 image (256MB)
// Compiled with "clang -O2", AVX2 requires extra "-mavx2" or "/arch:AVX2" argument
//
// For libpng (compressed) uses default libpng/zlib compression settings
// For libpng (uncompressed) case following two functions are used:
@d7samurai
d7samurai / .readme.md
Last active October 8, 2024 23:35
Minimal D3D11 sprite renderer

Minimal D3D11 sprite renderer

Minimal D3D11 sprite renderer: basic back-to-front sprite rendering reference code with example sprite sheet animation logic. As usual: Complete, runnable single-function app. No modern C++ / OOP / obscuring cruft.

adventurer

Swap out the sprite sheet with a font atlas for a lightweight GUI / text renderer. Clip individual sprites and glyphs by offsetting screenPos and atlasPos to top left corner of visible area and adjusting size accordingly (all values in pixels):

sprite.screenPos.x += 17;
sprite.screenPos.y += 10;
@MysteriousJ
MysteriousJ / minimal_wasapi.cpp
Created January 31, 2023 04:30
Minimal WASAPI implementation that plays a sine wave
// Compile: cl minimal_wasapi.cpp /link ole32.lib
#include <windows.h>
#include <inttypes.h>
#include <math.h>
#include <mmdeviceapi.h>
#include <audioclient.h>
#define BYTES_PER_CHANNEL sizeof(float)
#define CHANNELS_PER_SAMPLE 1
#define SAMPLES_PER_SECOND 44100
@MysteriousJ
MysteriousJ / music_box.cpp
Last active January 31, 2023 14:38
Sturdy WASAPI example
// Compile: cl music_box.cpp /link ole32.lib
// Usage: music_box.exe "Hello World"
#include <windows.h>
#include <inttypes.h>
#include <math.h>
#include <mmdeviceapi.h>
#include <audioclient.h>
#define BYTES_PER_CHANNEL sizeof(float)
#define CHANNELS_PER_SAMPLE 1
@MysteriousJ
MysteriousJ / DirectoryWatch.cpp
Last active January 6, 2023 08:18
File change notifications on Windows
#include <windows.h>
#include <stdio.h>
struct DirectoryWatch
{
char filePath[1024];
char changeBuffer[65536];
HANDLE directoryHandle;
FILE_NOTIFY_INFORMATION* nextChange;
};
@valinet
valinet / toast2.c
Last active September 22, 2024 08:38
Send a toast notification in Windows 10/11 using plain C including COM activator
#include <initguid.h>
#include <Windows.h>
#include <roapi.h>
#include <Windows.ui.notifications.h>
#include <notificationactivationcallback.h>
#include <tchar.h>
#include <stdio.h>
#pragma comment(lib, "runtimeobject.lib")
DWORD dwMainThreadId = 0;
@mmozeiko
mmozeiko / !README.md
Last active November 17, 2024 22:58
Download MSVC compiler/linker & Windows SDK without installing full Visual Studio

This downloads standalone MSVC compiler, linker & other tools, also headers/libraries from Windows SDK into portable folder, without installing Visual Studio. Has bare minimum components - no UWP/Store/WindowsRT stuff, just files & tools for native desktop app development.

Run py.exe portable-msvc.py and it will download output into msvc folder. By default it will download latest available MSVC & Windows SDK - currently v14.40.33807 and v10.0.26100.0.

You can list available versions with py.exe portable-msvc.py --show-versions and then pass versions you want with --msvc-version and --sdk-version arguments.

To use cl.exe/link.exe first run setup_TARGET.bat - after that PATH/INCLUDE/LIB env variables will be updated to use all the tools as usual. You can also use clang-cl.exe with these includes & libraries.

To use clang-cl.exe without running setup.bat, pass extra /winsysroot msvc argument (msvc is folder name where output is stored).

// This can grow a Robin Hood linear probing hash table near word-at-a-time memcpy speeds. If you're confused why I use 'keys'
// to describe the hash values, it's because my favorite perspective on Robin Hood (which I learned from Paul Khuong)
// is that it's just a sorted gap array which is MSB bucketed and insertion sorted per chain:
// https://pvk.ca/Blog/2019/09/29/a-couple-of-probabilistic-worst-case-bounds-for-robin-hood-linear-probing/
// The more widely known "max displacement" picture of Robin Hood hashing also has strengths since the max displacement
// can be stored very compactly. You can see a micro-optimized example of that here for small tables where the max displacement
// can fit in 4 bits: Sub-nanosecond Searches Using Vector Instructions, https://www.youtube.com/watch?v=paxIkKBzqBU
void grow(Table *table) {
u64 exp = 64 - table->shift;
// We grow the table downward in place by a factor of 2 (not counting the overflow area at table->end).
@pervognsen
pervognsen / shift_dfa.md
Last active November 1, 2024 23:53
Shift-based DFAs

A traditional table-based DFA implementation looks like this:

uint8_t table[NUM_STATES][256]

uint8_t run(const uint8_t *start, const uint8_t *end, uint8_t state) {
    for (const uint8_t *s = start; s != end; s++)
        state = table[state][*s];
    return state;
}
/*
NOTE this doesn't really check if a specifier is entirely valid in order to highlight
for example if you have %lls we highlight it
we simply check each subspecifier in order to see if it seems valid and move on and don't have logic to see if
the subspecifiers are comptaible or not
we don't highlight if any of the subspecifier checks fail so if you do %.qd we don't highlight it
Also I commented out some checks that I wasn't sure if they were invalid in c++ but valid in C or ones that I just never use
Instructions
In 4coder_draw.cpp there a function draw_cpp_token_colors() that decides what color to draw tokens and draws them.