Skip to content

Instantly share code, notes, and snippets.

View mmozeiko's full-sized avatar

Mārtiņš Možeiko mmozeiko

View GitHub Profile
@mmozeiko
mmozeiko / FlushFileCache.c
Last active October 13, 2024 00:51
flush file cache on Windows
// clang.exe -Os -nostdlib -fuse-ld=lld -Wl,-fixed,-subsystem:console FlushFileCache.c -o FlushFileCache.exe
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma comment (lib, "kernel32.lib")
#pragma comment (lib, "advapi32.lib")
#define error(str) do { DWORD written; WriteFile(GetStdHandle(STD_ERROR_HANDLE), str, sizeof(str)-1, &written, NULL); } while (0)
@mmozeiko
mmozeiko / images2ico.py
Created January 10, 2022 02:24
packs multiple images into ico file
#!/usr/bin/env python3
# packs multiple images (bmp/png/...) into ico file
# width and height of images must be <= 256
# pixel format of images must be 32-bit RGBA
import argparse
import struct
import os
from PIL import Image # https://python-pillow.org/
@mmozeiko
mmozeiko / tls_client.c
Last active March 13, 2025 07:28
simple example of TLS socket client using win32 schannel api
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#define SECURITY_WIN32
#include <security.h>
#include <schannel.h>
#include <shlwapi.h>
#include <assert.h>
#include <stdio.h>
@mmozeiko
mmozeiko / win32_opengl_multi.c
Last active November 7, 2024 20:23
setting up modern OpenGL 4.5 context for drawing to multiple windows
// example how to set up OpenGL core context on Windows for rendering to multiple windows
#define WINDOW_COUNT 4 // how many windows we'll be opening?
// important extension functionality used here:
// (4.3) KHR_debug: https://www.khronos.org/registry/OpenGL/extensions/KHR/KHR_debug.txt
// (4.5) ARB_direct_state_access: https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_direct_state_access.txt
// (4.1) ARB_separate_shader_objects: https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_separate_shader_objects.txt
// (4.2) ARB_shading_language_420pack: https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_shading_language_420pack.txt
// (4.3) ARB_explicit_uniform_location: https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_explicit_uniform_location.txt
@mmozeiko
mmozeiko / q1.c
Created August 23, 2021 00:35
Quake 1 animated model rendering
// download glfw3 from https://www.glfw.org/download and then compile:
// cl.exe -O2 q1.c -Iinclude -link opengl32.lib glu32.lib lib-vc2019\glfw3dll.lib
#define _CRT_SECURE_NO_DEPRECATE
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
@mmozeiko
mmozeiko / pcg32.h
Last active March 5, 2025 22:06
simple standalone C headers for PCG random number generator
#pragma once
#include <stdint.h>
// pcg32_next() implementation matches pcg_engines::oneseq_xsh_rr_64_32
// NOTE: use this only for compatibility with 32-bit architectures
// otherwise prefer using pcg64.h implementation with 128-bit state
typedef struct {
@mmozeiko
mmozeiko / shader.hlsl
Last active November 7, 2024 20:23
compute shader for rendering monospaced glyphs in grid
//
struct TerminalCell
{
// cell index into GlyphTexture, should be two 16-bit (x,y) values packed: "x | (y << 16)"
uint GlyphIndex;
// 0xAABBGGRR encoded colors, nonzero alpha for Foreground indicates to render colored-glyph
// which means use RGB values from GlyphTexture directly as output, not as ClearType blending weights
uint Foreground;
@mmozeiko
mmozeiko / image2clipboard.c
Last active October 13, 2024 00:49
example how to put RGBA image in clipboard
#define STB_IMAGE_STATIC
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" // get from https://raw.githubusercontent.com/nothings/stb/master/stb_image.h
#include <windows.h>
int main(int argc, char* argv[])
{
int w, h;
stbi_uc* data = stbi_load(argv[1], &w, &h, NULL, 4);
@mmozeiko
mmozeiko / x11_opengl.c
Last active July 19, 2023 06:02
setting up and using modern OpenGL 4.5 core context on X11 with EGL
// example how to set up OpenGL core context on X11 with EGL
// and use basic functionality of OpenGL 4.5 version
// to compile on Ubuntu first install following packages: build-essential libx11-dev libgl-dev libegl-dev, then run:
// gcc x11_opengl.c -o x11_opengl -lm -lX11 -lEGL
// important extension functionality used here:
// (4.3) KHR_debug: https://www.khronos.org/registry/OpenGL/extensions/KHR/KHR_debug.txt
// (4.5) ARB_direct_state_access: https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_direct_state_access.txt
// (4.1) ARB_separate_shader_objects: https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_separate_shader_objects.txt
@mmozeiko
mmozeiko / sdl2_pixels.c
Last active January 18, 2025 14:34
drawing pixels with SDL
// on Windows compile with:
// cl.exe sdl2_pixels.c -Zi -Iinclude -link -incremental:no -subsystem:windows SDL2.lib SDL2main.lib shell32.lib
#include <SDL.h>
#include <math.h>
int main(int argc, char* argv[])
{
int width = 1280;
int height = 720;