Skip to content

Instantly share code, notes, and snippets.

@SpexGuy
SpexGuy / DrawCircleQuads.c
Created September 4, 2023 13:49
DrawCircle with quads, saving previous corner position
rlBegin(RL_QUADS);
float lastCornerX = center.x + cosf(DEG2RAD*angle)*radius;
float lastCornerY = center.y + sinf(DEG2RAD*angle)*radius;
// NOTE: Every QUAD actually represents two segments
for (int i = 0; i < segments/2; i++)
{
float nextAngle = angle + stepLength*2.0f;
float nextCornerX = center.x + cosf(DEG2RAD*(nextAngle))*radius;
const std = @import("std");
const Channel = struct {
value: u32,
frame: anyframe,
};
fn generate(channel: *Channel) void {
suspend { channel.frame = @frame(); }
var i: u32 = 2;
/// This function issues a compile error with a helpful message if there
/// is a problem with the provided context type. A context must have the following
/// member functions:
/// - hash(self, PseudoKey) Hash
/// - eql(self, PseudoKey, Key) bool
/// If you are passing a context to a *Adapted function, PseudoKey is the type
/// of the key parameter. Otherwise, when creating a HashMap or HashMapUnmanaged
/// type, PseudoKey = Key = K.
pub fn verifyContext(comptime RawContext: type, comptime PseudoKey: type, comptime Key: type, comptime Hash: type) void {
comptime {
var tok = std.mem.tokenize(line, "- :");
var min = try std.fmt.parseInt(u32, tok.next().?, 10);
var max = try std.fmt.parseInt(u32, tok.next().?, 10);
var char = tok.next().?[0];
var pass = tok.next().?;
assert(tok.next() == null);
@SpexGuy
SpexGuy / day01_vectorized.zig
Created December 1, 2020 23:35
Advent of code 2020 day 1, in zig, vectorized.
const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const print = std.debug.print;
const Timer = std.time.Timer;
const data = @embedFile("data/day01.txt");
const EntriesList = std.ArrayList(i16);
@SpexGuy
SpexGuy / net.zig
Created October 30, 2020 19:32
Is this correct nonblocking use?
const std = @import("std");
pub const raw = @import("zig-network");
const heap_allocator = std.heap.c_allocator;
// config vars
pub var serverConnectionPort: u16 = 0x4d3;
pub var addressFamily: raw.AddressFamily = .ipv4;
pub var maxConnectionsPerFrame: usize = 4;
@SpexGuy
SpexGuy / stack_alloc.zig
Created October 30, 2020 18:33
A Buffer Stack Allocator for Zig
const std = @import("std");
const testing = std.testing;
const Allocator = std.mem.Allocator;
const Error = Allocator.Error;
const assert = std.debug.assert;
const mem = std.mem;
const math = std.math;
pub fn StackAllocator(comptime buffer_size: comptime_int, comptime max_alignment: comptime_int) type {
@SpexGuy
SpexGuy / select_frames.zig
Created June 15, 2020 18:42
A weird method for frame selection
const SelectFrames = struct {
frames: []@Frame(runInternal),
next_get_result: usize,
next_put_result: usize,
result_frames: []@Frame(awaitResultInternal);
result_values: []Result;
pub fn init(self: *@This(), in_frames: []anyframe->Result) !void {
const wrapped_frames = try allocator.alloc(@Frame(runInternal), in_frames.len);
errdefer allocator.free(wrapped_frames);
@SpexGuy
SpexGuy / erase.zig
Last active June 3, 2020 18:47
Type Erasure in Zig, untested.
pub const erased_runtime_safety = false;
pub const erased_debug_names = false;
/// Returns a struct containing a field named "unique_global".
/// There is exactly one such unique_global for every passed type.
/// The address of `unique_global` is guaranteed to be unique for each type.
/// If `erased_debug_names` is true, unique_global is of type [N:0]u8
/// and its value is the name of the passed type T. Otherwise unique_global
/// is of type `u8` and its value is meaningless.

The current implementation of async is modeled on state machines. Every async function has a Frame struct, which looks something like this:

const Frame = struct {
    ptrToFunction: usize,
    state: usize,
    parent_frame: usize,
    ptr_to_return_value: *ReturnType,
    locals: struct { ... },
};