Skip to content

Instantly share code, notes, and snippets.

View r-lyeh-archived's full-sized avatar

r-lyeh-archived

View GitHub Profile
@rorydriscoll
rorydriscoll / FeatherUI.cpp
Created January 8, 2012 21:56
Mikko Mononen's UI 'feathering' technique
void CalculateEdgeNormal(float& nx, float& ny, float x0, float y0, float x1, float y1)
{
const float x01 = x1 - x0;
const float y01 = y1 - y0;
const float length = Sqrt(x01 * x01 + y01 * y01);
const float dx = x01 / length;
const float dy = y01 / length;
@albeva
albeva / gist:2975962
Created June 23, 2012 00:25
C++11 variadic template based tuple sort
#include <iostream>
#include <utility>
#include <tuple>
using namespace std;
//
// forward declare
//
template<int N1, int N2, int LAST, typename T>
@Farfarer
Farfarer / characterMotor.js
Created October 15, 2012 09:22
Solve simple IK for character legs on differing ground height for Unity3D.
function LateUpdate () {
if ( collision.grounded ) {
IKOn = true;
}
else {
IKOn = false;
}
ik.solveLegIK ();
}
@ssylvan
ssylvan / rh_hash_table.hpp
Last active January 12, 2023 04:52
Quick'n'dirty Robin Hood hash table implementation. Note, I have implemented this algorithm before, with tons of tests etc. But *this* code was written specifically for the blog post at http://sebastiansylvan.com/post/robin-hood-hashing-should-be-your-default-hash-table-implementation/, it has not been extensively tested so there may be bugs (an…
#define USE_ROBIN_HOOD_HASH 1
#define USE_SEPARATE_HASH_ARRAY 1
template<class Key, class Value>
class hash_table
{
static const int INITIAL_SIZE = 256;
static const int LOAD_FACTOR_PERCENT = 90;
struct elem
@jedy
jedy / compress_pvr.py
Created July 8, 2013 02:34
convert pvr to pvr.ccz
import zlib
import struct
import sys
pvr = sys.argv[1]
ccz = pvr + ".ccz"
pvr = open(pvr).read()
ccz = open(ccz, "wb")
@panzi
panzi / portable_endian.h
Last active July 10, 2024 08:11
This provides the endian conversion functions form endian.h on Windows, Linux, *BSD, Mac OS X, and QNX. You still need to use -std=gnu99 instead of -std=c99 for gcc. The functions might actually be macros. Functions: htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh. License: I hereby put …
// "License": Public Domain
// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
// an example on how to get the endian conversion functions on different platforms.
#ifndef PORTABLE_ENDIAN_H__
#define PORTABLE_ENDIAN_H__
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
@r-lyeh-archived
r-lyeh-archived / compo.cpp
Last active December 9, 2019 06:28
Component-entity system in 16 lines of C++11. extract from kult engine (https://github.com/r-lyeh/kult)
#include <map> // Component-entity system in 16 lines of C++11. 2013 rlyeh, MIT licensed
#include <set> // Code fragment from kult engine - https://github.com/r-lyeh/kult
enum {JOIN,MERGE,EXCLUDE};using set=std::set<unsigned>;template<typename T> set&system(){
static set entities;return entities;}template<typename T,int MODE>set subsystem(const set
&B){set newset;const set&A=system<T>();if(MODE==MERGE){newset=B;for(auto&id:A)newset.ins\
ert(id);}else if(MODE==EXCLUDE){newset=B;for(auto&id:A)newset.erase(id);}else if(A.size()
<B.size()){for(auto&id:A)if(B.find(id)!=B.end())newset.insert(id);}else{for(auto&id:B)if(
A.find(id)!=A.end())newset.insert(id);}return newset;}template<typename T>std::map<unsig\
ned,T>&components(){static std::map<unsigned,T>objects;return objects;}template<typename\
T>bool has(unsigned id){return components<T>().find(id)!=components<T>().end();}templat\
@Fiona-J-W
Fiona-J-W / main.cpp
Last active December 31, 2015 23:59
#include <cmath>
#include <cstddef>
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <vector>
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns                     on recent CPU
L2 cache reference ........................... 7 ns                     14x L1 cache
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns                     20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs 4X memory

@fmela
fmela / GNUmakefile
Created February 17, 2014 23:04
C program makefile boilerplate
SOURCES = $(wildcard *.c)
HEADERS = $(wildcard *.h)
OBJECTS = $(SOURCES:%.c=%.o)
PROGRAM = $(shell basename `pwd`)
CC := $(shell which clang || which gcc)
CFLAGS = -Wall -W -O
LIBS =
LDFLAGS = $(LIBS:%=-l%)