Noobzo

Friday, July 31, 2009

A Nifty Macro Trick that allows us to get Compile-Time Constants with Binary Representation…





A Nifty Macro Trick that allows us to get Compile-Time Constants

with Binary Representation…

Here’s a very handy Macro trick that uses the (##) macro

concatenation operator to generate constants from binary

representation at compile time.


#define HEX_DIGIT_0000 0
#define HEX_DIGIT_0001 1
#define HEX_DIGIT_0010 2
#define HEX_DIGIT_0011 3
#define HEX_DIGIT_0100 4
#define HEX_DIGIT_0101 5
#define HEX_DIGIT_0110 6
#define HEX_DIGIT_0111 7
#define HEX_DIGIT_1000 8
#define HEX_DIGIT_1001 9
#define HEX_DIGIT_1010 A
#define HEX_DIGIT_1011 B
#define HEX_DIGIT_1100 C
#define HEX_DIGIT_1101 D
#define HEX_DIGIT_1110 E
#define HEX_DIGIT_1111 F

#define HEX_DIGIT(a) HEX_DIGIT_ ## a

#define BINARY1H(a) (0x ## a)

#define BINARY1I(a) BINARY1H(a)

#define BINARY1(a) BINARY1I(HEX_DIGIT(a))


#define BINARY2H(a,b) (0x ## a ## b)

#define BINARY2I(a,b) BINARY2H(a,b)

#define BINARY2(a,b) BINARY2I(HEX_DIGIT(a), HEX_DIGIT(b))

#define BINARY8H(a,b,c,d,e,f,g,h) (0x##a##b##c##d##e##f##g##h)

#define BINARY8I(a,b,c,d,e,f,g,h) BINARY8H(a,b,c,d,e,f,g,h)

#define BINARY8(a,b,c,d,e,f,g,h) BINARY8I(HEX_DIGIT(a), HEX_DIGIT(b), \
HEX_DIGIT(c), HEX_DIGIT(d), HEX_DIGIT(e), HEX_DIGIT(f), \
HEX_DIGIT(g), HEX_DIGIT(h))


int main ()

{

unsigned int nibble = 0;//a nibble is a half of a byte and contains 4 bits
nibble = BINARY1(0011);
nibble = BINARY1(1010);

unsigned int Byte = 0;
Byte = BINARY2(1111, 1111);
Byte = BINARY2(0010 , 1010);

unsigned int DoubleWord = 0;
DoubleWord = BINARY8(1111, 1111, 1111, 1111, 1111, 1111, 1111, 1111);
DoubleWord = BINARY8(0000, 1111, 1010, 0001, 1111, 1111, 0100, 1000);
}

Resources:
Game Programming Gems 3.

Enjoy :)

Tuesday, July 28, 2009

An Excellent Random Number Generator…


An Excellent Random Number Generator…

There are many good algorithms for generating random numbers. Most programmers will soon discover that the ANSI rand() function is completely inadequate because it can only generate a single stream of random numbers. And sometimes, especially in games programming, a multiple discrete streams of random numbers are required.

Here's a cool little class to keep track of your random numbers. You'll want to make sure you save this code and stuff it into your own toolbox. The RNG core is called a Mersenne Twister pseudorandom number generator and it was originally developed by Takuji Nishimura and Makoto Matsumoto:

#include <iostream>

#include <time.h>

using namespace std;


/* Period parameters */

#define CMATH_N 624

#define CMATH_M 397

#define CMATH_MATRIX_A 0x9908b0df /* constant vector a */

#define CMATH_UPPER_MASK 0x80000000 /* most significant w-r bits */

#define CMATH_LOWER_MASK 0x7fffffff /* least significant r bits */


/* Tempering parameters */

#define CMATH_TEMPERING_MASK_B 0x9d2c5680

#define CMATH_TEMPERING_MASK_C 0xefc60000

#define CMATH_TEMPERING_SHIFT_U(y) (y >> 11)

#define CMATH_TEMPERING_SHIFT_S(y) (y << 7)

#define CMATH_TEMPERING_SHIFT_T(y) (y << 15)

#define CMATH_TEMPERING_SHIFT_L(y) (y >> 18)


class CRandom

{

// DATA

unsigned int rseed;

unsigned long mt[CMATH_N]; /* the array for the state vector */

int mti; /* mti==N+1 means mt[N] is not initialized */


// FUNCTIONS

public:

CRandom(void);

unsigned int Random( unsigned int n );

void SetRandomSeed(unsigned int n);

unsigned int GetRandomSeed(void);

void Randomize(void);

};


CRandom::CRandom(void)

{

rseed = 1;

mti=CMATH_N+1;

}


// Returns a number from 0 to n (excluding n)

unsigned int CRandom::Random( unsigned int n )

{

unsigned long y;

static unsigned long mag01[2]={0x0, CMATH_MATRIX_A};


if(n==0)

return(0);


/* mag01[x] = x * MATRIX_A for x=0,1 */


if (mti >= CMATH_N) { /* generate N words at one time */

int kk;


if (mti == CMATH_N+1) /* if sgenrand() has not been called, */

SetRandomSeed(4357); /* a default initial seed is used */


for (kk=0;kk<CMATH_N-CMATH_M;kk++) {

y = (mt[kk]&CMATH_UPPER_MASK)|(mt[kk+1]&CMATH_LOWER_MASK);

mt[kk] = mt[kk+CMATH_M] ^ (y >> 1) ^ mag01[y & 0x1];

}

for (;kk<CMATH_N-1;kk++) {

y = (mt[kk]&CMATH_UPPER_MASK)|(mt[kk+1]&CMATH_LOWER_MASK);

mt[kk] = mt[kk+(CMATH_M-CMATH_N)] ^ (y >> 1) ^ mag01[y & 0x1];

}

y = (mt[CMATH_N-1]&CMATH_UPPER_MASK)|(mt[0]&CMATH_LOWER_MASK);

mt[CMATH_N-1] = mt[CMATH_M-1] ^ (y >> 1) ^ mag01[y & 0x1];


mti = 0;

}


y = mt[mti++];

y ^= CMATH_TEMPERING_SHIFT_U(y);

y ^= CMATH_TEMPERING_SHIFT_S(y) & CMATH_TEMPERING_MASK_B;

y ^= CMATH_TEMPERING_SHIFT_T(y) & CMATH_TEMPERING_MASK_C;

y ^= CMATH_TEMPERING_SHIFT_L(y);


return (y%n);


}


void CRandom::SetRandomSeed(unsigned int n)

{

/* setting initial seeds to mt[N] using */

/* the generator Line 25 of Table 1 in */

/* [KNUTH 1981, The Art of Computer Programming */

/* Vol. 2 (2nd Ed.), pp102] */

mt[0]= n & 0xffffffff;

for (mti=1; mti<CMATH_N; mti++)

mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;


rseed = n;

}

unsigned int CRandom::GetRandomSeed(void)

{

return(rseed);

}


void CRandom::Randomize(void)

{

SetRandomSeed(time(NULL));

}


int main()

{


CRandom r;

r.Randomize();

unsigned int num = r.Random(100);

.

.

.

}



The right way to use this class is to create different objects for each case you want.
Enjoy :)

Resources:
Game Coding Complete
by Mike McShaffry

A Neat visual C++ debugging trick...




Its almost impossible nowadays to write programs without a debugger, and placing a breakpoint is considered to be the most basic thing to do when debugging an application, sometimes though, we don’t have the luxury of simply placing those nice little red circles directly where you want to break, for example, I have worked on a multithreaded applications that never stopped at breakpoints, so I had to look for an alternative.

One neat trick is to use the (__asm) directive to write assembly code inside your C/C++ code, so it would be like this:

int X = 5;//this is C++
X += 3; //this is C++
__asm int 3// this is Assembly
X--;//this is C++

Now this statement (__asm int 3) is an assembly instruction which means interrupt 3, the inline assembler in MS Visual C++ takes that and places it in the final binary, and once the debugger hits this statement it breaks ALWAYS as if it’s a regular breakpoint.

One thing to keep in mind though is that this only works when the debugger is on and you’re actually trying to debug the application.

Enjoy ;)

Thank God for long build times..


Working as a programmer means that during work hours you are kept BUSY, there always will be tasks, bugs and stuff to read and learn, this though made me kinda jealous of other workers at my work place, like quality assurance people, secretaries, or even management lol.

Thank God though for big project and long build times haha, usually when I work on a big project with a long build time ( we r talking about like 2 to 3 minutes, that’s with optimized build mechanisms) I take my revenge ;)

Personally, I like to use this stolen time for recreation lol, sometimes I would read a couple of web comics, or listen to a song, or just check my emails.

A very amazing and funny web comic site (personally my favorite) is http://www.explosm.net

And I think you might like it too ;)
Enjoy.

Monday, July 27, 2009

Grim Fandango, Best game ever made.



Join Manny, the undead travel agent, and uncover a conspiracy to keep new additions to the underworld from buying a safe passage through purgatory. Grim Fandango combines a unique story line and complicated puzzles to create an adventure different from any you have experienced before. Follow Manny through four years of mystery on his quest for true love and eternal salvation.
The game opens to find Manny in search of the perfect client, one with the means to place them both on the fast track out of purgatory and into eternal paradise. Enter Mercedes Colomar, the client who has it all--beauty, brains, and enough money to buy them each tickets on the exclusive No. 9 train. Following the film-noir formula, Mercedes promptly vanishes, leaving Manny to solve the mystery behind her disappearance and her connection with the Department of Death.
With fantastic graphics--stylishly rendered in the film-noir style--and art from the Mayan, Aztec, and Mexican traditions, Grim Fandango is imaginative and appealing. The challenging puzzles call for attentive play and serious exploration of the Land of the Dead--not an unappealing job when surrounded by such beautiful animation. Include the original story line and humorous characters and you won't want to stop playing--we didn't!


Resources:
http://www.amazon.com/Grim-Fandango-Pc/dp/B00000DMAD

Followers