This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string CFGetType( long typeId ) | |
{ | |
static map<long,string> types = { | |
{CFArrayGetTypeID(),"CFArray"}, | |
{CFBooleanGetTypeID(),"CFBoolean"}, | |
{CFDataGetTypeID(),"CFData"}, | |
{CFDateGetTypeID(),"CFDate"}, | |
{CFDictionaryGetTypeID(),"CFDictionary"}, | |
{CFNumberGetTypeID(),"CFNumber"}, | |
{CFStringGetTypeID(),"CFString"}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename T> | |
struct CCFArray | |
{ | |
CFArrayRef array; | |
CCFArray() | |
{ | |
array = CFArrayCreate( kCFAllocatorDefault, 0, 0, 0 ); | |
} | |
CCFArray(CFArrayRef cf):array(cf) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <stdio.h> | |
#include <stdlib.h> | |
using namespace std ; | |
int randInt( int a, int b ) | |
{ | |
return a + rand()%(b-a) ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |