Skip to content

Instantly share code, notes, and snippets.

View maxattack's full-sized avatar

Max Kaufmann maxattack

View GitHub Profile
template<typename T, int N>
class AnonymousPool {
private:
uint32_t mCount;
T mRecords[N];
public:
AnonymousPool() : mCount(0) {
}
@maxattack
maxattack / GameObjectPool.cs
Last active December 30, 2015 18:29
No-frills GameObject pooling in Unity. KISS
using UnityEngine;
// CustomBehaviour is just a trivial subclass of MonoBehaviour with a bunch
// of static finger-friendly helper-functions :P
public class PooledObject : CustomBehaviour {
// subclass this to treat a behaviour as the basis
// of a poolable object. Stores simple metadata
// related to pooling
float u = clamp(player.deathCountdown/PLAYER_DEATH_SECONDS);
u = 1.f - easeOut4(1.f - u);
setTransform(translationMatrix(vec(
8.f * sinf(7.f * M_TAU * u),
16.f* cosf(13.f * M_TAU * u)
)));
@maxattack
maxattack / gist:9005161
Created February 14, 2014 17:24
C++11 Unrestricted Union "Slot"
#include <utility>
// Is this mad? The idea is to make a "slot" for an object but have manual initialization/finalization,
// rather than being constrained to C++'s semantics. Useful, e.g., in Object Pools. Is there a simpler
// way to get this effect?
template<typename T>
union Slot {
T inst;
@maxattack
maxattack / envelopes.py
Created March 9, 2014 07:15
Two Envelopes
#!/usr/bin/python
import random
nonSwitcherMoney = 0
switcherMoney = 0
def random_amount_of_money(): return 1 + 999 * random.random()
def coin_flip(): return random.random() > 0.5
# do a million trials
@maxattack
maxattack / Mixin.h
Last active August 29, 2015 13:57
C++11 Me Am Go Too Far!
#pragma once
#include "GameBase.h"
// Event Book Pool - Apply this to any pool that implements the alloc/release contract
// to have it automatically call enable/disable methods just before the first slot is allocated
// and just after the last slot is released.
template<typename T>
class EventHookPool : public T {
public:
@maxattack
maxattack / PoolingExample.cs
Last active August 29, 2015 13:57
Object Pooling Example for Unity
using UnityEngine;
using System.Collections;
using Dbg = System.Diagnostics.Debug;
public class PoolingExample : MonoBehaviour {
PoolingExample next;
PoolingExample prefab;
@maxattack
maxattack / Shader-Lerp.shader
Created April 13, 2014 00:09
Unity Sprite "Lerp" Shader
Shader "Little Polygon/Sprites (Lerp)"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
}
SubShader
@maxattack
maxattack / has_member.cpp
Created August 26, 2014 16:24
Has Member Template
#include <type_traits>
#include <iostream>
#include <iomanip>
#define DECLARE_HAS_MEMBER(member) \
\
template < class T > \
class HasMember_##member \
{ \
private: \
@maxattack
maxattack / gist:17bff2f6b20df58b4a2b
Last active April 27, 2024 20:02
C++ Getting a Compile-Time Unique Numeric ID for a Type
#include <iostream>
template<typename T>
size_t numeric_handle() {
static int x;
return (size_t)(&x);
}
int main(int argc, char* argv[]) {
using namespace std;