Skip to content

Instantly share code, notes, and snippets.

@ForeverZer0
ForeverZer0 / extconfig.rb
Created October 3, 2018 18:42
Automatically include/compile all Ruby C extension source files (including subdirectories) using mkmf
require "mkmf"
ext_name = 'my_extension'
excluded = ['x86', 'x64']
dir_config(ext_name)
$srcs = Dir.glob("#{$srcdir}/**/*.c").map { |path| File.basename(path) }
Dir.glob("#{$srcdir}/*/") do |path|
dir = File.basename(path)
@ForeverZer0
ForeverZer0 / EndianSwapExtensions.cs
Created August 24, 2021 08:25
Swap endian of numeric types using grouped bit-shifts instead of the much slower Array.Reverse.
using System;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
namespace ChangeThisNamespace
{
/// <summary>
/// Contains extension methods dealing with endianness of numeric types.
/// </summary>
[PublicAPI]
@ForeverZer0
ForeverZer0 / Adler32.cs
Created August 24, 2021 07:03
C# ZLib stream implementation
using System;
using System.Runtime.CompilerServices;
namespace ZLib
{
/// <summary>
/// An Adler-32 checksum implementation for ZLib streams.
/// </summary>
/// <seealso href=""/>
public sealed class Adler32
@ForeverZer0
ForeverZer0 / snake_case.rb
Created November 29, 2020 03:56
Convert from camelCase/PascalCase to snake_case in Ruby
def snake_case
return downcase if match(/\A[A-Z]+\z/)
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
gsub(/([a-z])([A-Z])/, '\1_\2').
downcase
end
"FooBar".snake_case #=> "foo_bar"
"HeadlineCNNNews".snake_case #=> "headline_cnn_fake_news"
"CNN".snake_case #=> "cnn"
@ForeverZer0
ForeverZer0 / GL.Delegates.cs
Created May 10, 2020 09:03
Generated C# bindings for OpenGL 3.3 (Core Profile)
using System;
using System.Runtime.InteropServices;
using System.Security;
// ReSharper disable InconsistentNaming
// ReSharper disable IdentifierTypo
namespace OpenGL
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
@ForeverZer0
ForeverZer0 / MeshLoader.cs
Created May 4, 2020 03:03
Simplified loader for creating a mesh from Wavefront OBJ (*.obj) format.
/*
* MIT License
*
* Copyright (c) 2020 Eric Freed
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@ForeverZer0
ForeverZer0 / SphereMesh.cs
Last active May 3, 2020 22:17
Various mesh generation techniques for spheres.
/*
* MIT License
*
* Copyright (c) 2020 Eric Freed
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@ForeverZer0
ForeverZer0 / RectPacker.cs
Created April 4, 2020 01:03
High-efficiency bin-packer implementation for 2D rectangles.
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
namespace MyNamespace
{
/// <summary>
/// Represents a rectangular box shape.
/// </summary>
#!/usr/bin/env ruby
require 'tty-prompt'
require 'fileutils'
@prompt = TTY::Prompt.new
def find_product(name)
paths = []
Dir.entries(Dir.home).each do |entry|
@ForeverZer0
ForeverZer0 / hue-shift.frag
Created March 1, 2020 21:29
Simple and efficient hue-shift function for a GLSL fragment shader.
/**
* @brief Applies a hue-shift in a GLSL fragment shader.
*
* @param color The color of the fragment to shift.
* @param hue The amount of hue-shift to apply, in radians. Use GLSL radians function to convert from degrees if needed.
*
* @return The hue-shifted fragment color.
*/
vec3 hueShift(vec3 color, float hue) {
const vec3 k = vec3(0.57735, 0.57735, 0.57735);