Skip to content

Instantly share code, notes, and snippets.

View jonvaldes's full-sized avatar

Jon Valdés jonvaldes

View GitHub Profile
@jonvaldes
jonvaldes / fonts.lua
Created November 2, 2021 20:23
Neovide font switching snippet
local fonts
if (jit.os == 'Windows') then
fonts = {
'DejaVuSansMono Nerd Font Mono',
'CaskaydiaCove NF',
'Hack NF',
'Monofur NF',
'VictorMono NF', -- Doesn't work well with telescope
'Hurmit NF', -- Doesn't work well with telescope
@jonvaldes
jonvaldes / test.rs
Last active February 16, 2021 09:08
Testing how to generate a tga image with Rust
use std::io;
use std::fs::File;
use std::io::Write;
use std::mem;
use std::slice;
#[derive(Clone)]
struct Color(u8, u8, u8);
struct Image {
@jonvaldes
jonvaldes / cpp_lexer.cpp
Last active August 9, 2020 10:00
Simple OpenGL function wrappers by way of metaprogramming
// Parser for C(++) code, aimed at generating
// code instead of using C macros
typedef int i32;
typedef struct {
const char *Code;
i32 CodeLength;
i32 CurrentLine;
} cpp_lexer;
@jonvaldes
jonvaldes / test.cpp
Last active October 21, 2019 11:14
Array-literal iteration using a custom initializer_list implementation
// So, I discovered you can actually iterate over array literals in C++, which is kinda nice,
// but only if you include <initializer_list>. As that will drag in >9K LOC from the standard
// libraries, I tried implementing my own by trimming the MSVC one, and hey, it works. And
// in just 30 LOC (and an stddef.h include I couldn't avoid (for size_t))
// Here it goes, with an example at the end
// ---------------------------------->8----------------------------------
#include <stddef.h>
namespace std {
template <class _Elem>
@jonvaldes
jonvaldes / deinterleave.c
Last active March 13, 2023 17:42
In-place sequence de-interleaving
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// In response to watching Casey Muratori think about in-place de-interleaving of audio samples
// in Handmade Hero ep 138, here's a solution for sequences with power-of-two length.
//
// Algorithm swaps the 2 internal elements in blocks of 4 elems, then grows block size to 8, 16, etc
// For example, for a sequence of length 8:
// a,1,b,2,c,3,d,4 --> a,b,1,2,c,d,3,4 --> a,b,c,d,1,2,3,4
@jonvaldes
jonvaldes / concat.cpp
Last active December 26, 2015 08:49
Compile-time text concatenation via templates and constexpr. Not exactly clean, but perfectly functional. Intended use is automatic JNI type string construction. Technique is a derivation of this: http://stackoverflow.com/a/16720106/375486
#include <cstdio>
using namespace std;
template <size_t dim> struct c_array {
char value[dim];
};
// helper; construct a sequence of non-type template arguments
template <size_t... tt_i > struct seq {};
@jonvaldes
jonvaldes / Output
Last active December 22, 2015 16:29
Simple helper class that, using range-based for loops, allows for integer range loops of the "for(int i=0;i<20;++i)" kind with a cleaner syntax: for(int a : range(0, 10))
a:0
a:1
a:2
a:3
a:4
a:5
a:6
a:7
a:8
a:9
@jonvaldes
jonvaldes / Discrete Spiral
Last active May 2, 2016 01:29
Function that generates a discrete spiral from consecutive numbers.
Needed a function would spiral out to cover a tiled world, then remembered these posts by John Carmack:
* https://twitter.com/ID_AA_Carmack/status/308678512099852288
* https://twitter.com/ID_AA_Carmack/status/308676302079160320
Derived the solution with an initial implementation that uses several arrays to calculate positions,
then removed those array values for a more compact implementation. There's probably still room for
improvement, though.
It uses uniform initialisation from c++11, so you'll have to enable compiler support for it if you want
@jonvaldes
jonvaldes / templateLoop.cpp
Created March 7, 2013 13:08
Coworker asked if I knew how to do a "for" with C macros, to avoid manually instantiating lots of helper functions. Here's a template metaprogramming-based solution, wich creates an array-like struct that contains 500 pointers to function you can then call. Woot! Note that the maximum T allowed by clang is 512, while gcc sets the maximum at 500 …
#include <cstdio>
template <int T> void func(){printf("Number %d\n", T);}
typedef void (*functype)();
template <int T> struct funcs
{
funcs<T-1> rest ;
functype f ;