Skip to content

Instantly share code, notes, and snippets.

View superwills's full-sized avatar

William superwills

View GitHub Profile
@superwills
superwills / info.h
Last active August 29, 2015 14:26
Info,warning,error functions
#include <stdio.h>
#include <stdarg.h>
// bitfield because some error messages can
// be categorized as Info | Warning | Error
enum ErrorLevel{
Info = 1 << 0, Warning = 1 << 1, Error = 1 << 2
};
const char* ErrorLevelName[] = {
"None", //0
@superwills
superwills / MersenneTwister.hpp
Last active August 29, 2015 14:26
C++ Class For Mersenne Twister
#ifndef MERSENNETWISTER_H
#define MERSENNETWISTER_H
/*
A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.
Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
@superwills
superwills / contains.hpp
Last active August 29, 2015 14:13
contains
// for a vector
template <typename T> bool contains( const vector<T> &vvector, const T& item )
{
return vvector.find( item ) != vvector.end() ;
}
// for a set
template <typename T> bool contains( const set<T> &sset, const T& item )
{
return sset.find( item ) != sset.end() ;
@superwills
superwills / 2dmatrix
Created January 16, 2015 03:12
2D matrix
// For allocating a 2D vector
template <typename T> vector< vector<T> > twoD( size_t rows, size_t cols )
{
vector< vector<T> > vv( rows );
for( int i = 0; i < vv.size(); i++ )
vv[i].resize( cols );
return vv;
}
map<IOReturn,string> ioMakeErrorMap()
{
map<IOReturn,string> map;
map[ kIOReturnSuccess ] = "kIOReturnSuccess KERN_SUCCESS // OK";
map[ kIOReturnError ] = " // general error";
map[ kIOReturnNoMemory ] = " // can't allocate memory ";
map[ kIOReturnNoResources ] = "resource shortage ";
map[ kIOReturnIPCError ] = "error during IPC ";
map[ kIOReturnNoDevice ] = "no such device ";
map[ kIOReturnNotPrivileged ] = "privilege violation ";
string CFGetType( long typeId )
{
static map<long,string> types = {
{CFArrayGetTypeID(),"CFArray"},
{CFBooleanGetTypeID(),"CFBoolean"},
{CFDataGetTypeID(),"CFData"},
{CFDateGetTypeID(),"CFDate"},
{CFDictionaryGetTypeID(),"CFDictionary"},
{CFNumberGetTypeID(),"CFNumber"},
{CFStringGetTypeID(),"CFString"},
@superwills
superwills / CCF.hpp
Created April 19, 2014 09:51
CCFSet and CCFArray template wrappers
template <typename T>
struct CCFArray
{
CFArrayRef array;
CCFArray()
{
array = CFArrayCreate( kCFAllocatorDefault, 0, 0, 0 );
}
CCFArray(CFArrayRef cf):array(cf)
{
@superwills
superwills / stringtrim.hpp
Last active December 29, 2015 20:19
std::string::trimExtensions for std::string to trim() whitespace.
// Extensions for std::string to trim whitespace
string& trimR( string& str )
{
if( !str.size() ) return str;
// find the last whitespace chr, then delete from there to end.
string::iterator iter = --str.end() ;
while( isspace( *iter ) ) --iter;
str.erase( ++iter, str.end() ) ;
return str ;
}
@superwills
superwills / fisheryates.cpp
Created November 29, 2013 01:08
Fisher-Yates shuffle
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std ;
int randInt( int a, int b )
{
return a + rand()%(b-a) ;
}
@superwills
superwills / chances.txt
Last active December 28, 2015 16:19
Chance table
If you want an event in a game that runs at 60FPS to occur ON AVERAGE (once every 30 seconds),
then the probability interval you should use is 0.000555555556. So pick a range,
say [0, 0.000555555556].
Call randFloat(0,1) once every frame, and if the random generated is on
[0,0.000555555556] then make the event happen.
This table is to choose a probability interval (on [0,1]).
If you want something to occur on average ONCE per second for a 60 FPS game,
then the probability interval for that event should be exactly 1/60 chance