Skip to content

Instantly share code, notes, and snippets.

View ikskuh's full-sized avatar
☺️
Hacking on Dunstwolke

Felix Queißner ikskuh

☺️
Hacking on Dunstwolke
View GitHub Profile
@ikskuh
ikskuh / soundwaves-ogg.cpp
Last active December 8, 2020 13:15
Getting started with SDL2 sound generation: Simple sine wave synth with moving the freq up and down
#include <SDL.h>
#include "stb_vorbis.h"
short audiodata[4096];
volatile bool needsData = true; // poor mans semaphore
void callback(void* userdata, Uint8* stream, int len)
{
printf("%p,%d\n", stream, len);
while(needsData); // yay
@ikskuh
ikskuh / modern-c++.md
Last active December 8, 2018 14:26
C++ Cheat Sheet & Brain Markers
  • std::byte for "plain byte data" instead of char, unsigned char or uint8_t
  • use std::optional<T> for optional return values (kinda obvious, but you have to know)
  • std::variant<T1,T2,...> is a C++ version of union
  • use std::bitset<N> for bitfields
  • structured bindings are useful for multiple return values:
    std::tuple<std::string, int>> get_name_and_age();
    auto [ name, age ] = get_name_and_age();
@ikskuh
ikskuh / tagged_int.hpp
Created April 26, 2019 14:13
Tagged, configurable integer type which allows only "self-interaction"
#include <type_traits>
template<
typename Tag, // Discriminator
typename UnderlyingType = std::size_t, // What integer are we defining
bool EnableOrdering = true, // Allow relational operators
bool EnableBitwise = false, // Allow bitwise operators
bool EnableArithmetic = false // Allow arithmetic operators
>
struct tagged_int
@ikskuh
ikskuh / allocator.cpp
Last active September 18, 2019 13:06
C++ simple block allocator
#include <cstdint>
#include <cstddef>
#include <bitset>
// WARNING: Code untested!
/// @tparam LowAddress Inclusive lower address of allocation size
/// @tparam HighAddress Inclusive upper bound of allocation size
/// @tparam BlockSize Size of minimal allocation size
template<uintptr_t LowAddress, uintptr_t HighAddress, size_t BlockSize>
@ikskuh
ikskuh / coroutine.zig
Last active October 1, 2019 10:34
Abusing async/await to create fake threads
const std = @import("std");
/// stores some state for each coroutine
/// as well as a pointer to the frame to be resumed
const CoroutineState = struct {
pub const Self = @This();
frame: anyframe = undefined,
quit: bool = false,
active: bool = false,
@ikskuh
ikskuh / zig-update.sh
Last active October 17, 2019 07:36
Zig updater script
#!/bin/bash
#
# allows installing and/or updating your local
# zig installation.
#
# adjust INSTALLDIR, ZIG_OS, ZIG_TARGET to
# configure your installation.
#
# prerequisites:
# - jq (https://stedolan.github.io/jq/)
@ikskuh
ikskuh / main.zig
Created December 2, 2019 07:39
Zig SDL2 Template
const std = @import("std");
// import the C headers here:
const c = @cImport({
@cInclude("SDL.h");
@cInclude("SDL_image.h");
});
/// game entry point which may return an error.
/// if the error is error.SdlError, SDL_GetError() will be called
@ikskuh
ikskuh / closures.zig
Created December 8, 2019 13:19
How to emulate "downward" closures with Zig
const std = @import("std");
fn printSliceWithFilter(comptime T: type, values: []const T, context: var, filter: fn (T, @typeOf(context)) bool) void {
for (values) |val, i| {
if (!filter(val, context))
continue;
std.debug.warn("[{}] = {}\n", i, val);
}
}
const fixed_22_10 = arithmetic(i32) {
const Self = @This();
fn add(lhs: Self, rhs: Self) Self {
return @as(Self, @value(lhs) + @value(rhs));
}
fn mul(lhs: Self, rhs: Self) Self {
return @as(Self, (@as(i64, @value(lhs)) * @as(i64, @value(rhs))) / 1024);
}
@ikskuh
ikskuh / Generator.cs
Created January 28, 2020 22:25
HDRP Mask Map Generator
/**
* Licence: MIT
* Author: Felix Queißner, 2020
*
* Compile with:
* csc /out:Generator.cs /r:System.Drawing.dll Generator.cs
*
* Usage:
* Execute with a single file argument, this tool will generate a mask map for
* the Unity HDRP shader set. It assumes that the required files exist with