Tuesday, 29 November 2011

The Siege

My First Game using a proper 2D game engine-----> The Siege

Here's the download link :

Developed by Game Mechanics
Members : 1. Ishkaran Singh
                 2. Shanker Sharma
Here are some snapshots from the actual Gameplay :

Meleeing an Enenmy down and backup coming from behind.

Near the end of first level.................see the pile of dead bodies

Second Level....what the hell are those creatures.....!!

Here's some technical details :

1.Used Visual C++ 2005
2.Game Engine : Haaf's Game Engine
3. irrKlang Sound library
4.Implemented State Design pattern for the architecture of the game.
5.Tilemaaping for placing different objects in the levels.
6.AI implemented through Finite State machine
7.Approximately more than 15000 lines of Code

Note : 1366*768 resolution has been hard coded into it,if your monitors native resolution is not 1366*768, then please run the windowed version.
JUMP is through "b".

Hope you the enjoy the game and don't forget to give feedback

Basics of Pointers C++

A small introduction to POINTERS.....!!

Pointers are generally the most feared part in a programming language. The only concept which many programmers doesnt seem to get. Well just get over the fact that pointers are somrthing terrifying build only to make prgrammers like us have horrifying nighmares....!! Heres the truth about pointers....!!
but the twist here is that they are a special kind of variables that stores special kind of info and that special info is the ADDRESS of another variable.
See just like an int variable can store only int values and a float can store only float values, same is the case with the pointers. An int pointer stores the address of an integer variable only and not a float variable.
So in 1 line a pointer is :

defining a pointer is a little different than defining variables.
int* x; /*we just declared an integer pointer which means that it vil only store addresses of integer variables.*/
dont be confused with the star (*)...u can also do lyk
int *x or int * x...its all d same....!!
Similarly to create a float or a char pointer just change the data type
float* x;
char* x;
now store something in the pointer.
REMEMBER : The variable that the pointer is pointing to is called a "POINTEE".
int pointee;
int* pointer;
pointer = &pointee; //Now wat the hell is "&" for.. >:)
this is known as "address of" operator it takes a variable and returns its address.
for eg: &pointee will give you the address of pointee, remember that pointers only store addresses so if we want pointer to point to pointee we will give the address of pointee
So pointer is pointing to pointee now so wat.....??
Now we can access pointee through a dereference operater(*)...
int pointee = 10;
int* pointer;
like cout<<*pointer;
vil give 10.
Suppose address of pointee is 1000 and value is 10.
So pointer vil store 1000 as its value and when *pointer is done, it will go to the address conatined by pointer which is 1000 as of now and vill access d value storesd at that address.
So *pointer is now the same as pointee.
doing *pointer = 20;
or pointee = 20 //is the same thing
Suppose there is another int pointer 
int* another_pointer ;
now we want this new pointer to point to the same location that pointer is pointing to.
so we vil do lyk:
another_pointer = pointer; //another pointer vil now also point to pointee;
So now the final programme to illustrate the concepts:
int main()
int pointee = 10;
int *pointer1, *pointer2; //u cant do lyk int* pointer1,pointer2,here pointer2 vil be pointer1 = &pointee; //a simple int
cout<<pointer1; //address of pointee
cout<<*pointer1; //value of pointee i.e 10
pointer2 = pointer1; //pointer2 now points also points to pointee 
*pointer2 = 20; //value of pointee chngd to 20
cout<<pointer2; //address of pointee
cout<<pointer1; //same as above...address of pointee
cout<<*pointer2; //value of pointee which is now 20
cout<<*pointer1; //value os pointee which is now 20

I hope this vil settle all the dust about baiscs of POINTERS and now we can start understandin the intricate details about pointers and their GOTCAHS....!!
This is just a very breif intro to POINTERS and Theres still a lot to learn about pointers so keep writing code and experimenting....!!


What is a macro ?
A macro is a piece of code that was labeled with a name. Whenever the preprocessor encounters it, the label is replaced by the associated code. Basically there are two kinds of macros : object-like macros (resemble data objects when used) and function-like macros (resemble function calls).

Example for object-like macros:

#include <stdio.h>
#define HELLO "Hello World Macro!"
int main(){
return 0;
In the above example the label is HELLO and the associated data is “Hello World Macro!“ . Before compile-time the preprocessor will replace the label with the associated code. I think the results are pretty obvious.

Example for function-like macros:

#include <stdio.h>

#define MAX(a, b) ((a) > (b)) ? (a) : (b)

int main(){
printf("%dn", MAX(1,3));
return 0;
In the case above MAX works exactly as a function that receives two arguments a, b and returns the maximum of the two . Note that the arguments are generic and appear to be typeless, the preprocessor is not performing any validation on types (that’s the compiler job) – this an advantage and also a disadvantage.

Inheritance + Virtual Functions = Polymorphism

Hope u guys hav read dat if not den plz read dat before reding dis....!!
Inheritance is powerful but still u vil think dat v can do without it. Obviously yes .... v can, v have been doing without it coz v still use C language.
But introucing d concept of POLYMORPHISM with inheritance takes d power to the next level.
We introduce d concept of polymorphism by introducing VIRTUAL FUNCTIONS.
So u should first understand d concept of Virtual functions.
For eg: We create an ENEMY class and we have 20 different types of ENEMIES
class enemy
void Attack();

So dis becomes our base class for all enemies now if we have like 20 diff. enemies in our game and all of dem have different attacks. So without using Virtual functions
we make 20 classes for diff enemies and all of dem inherit from base class enemy.
den enemy obj = new badEnemy();
enemy obj1 = new BadAssEnemy();
enemy obj2 = new RealBadAssEnemy();

but when we call attack() of all three objects it will call d attack() of base class.....:( so bad na same kind of attack for every enemy makes ur game a dull game,
UNTIL we make dat attack() function "VIRTUAL". Now each enemy class vil have different implementation for their attack() which vil actually make dem different.
like badenemy attack() simply fires a pistol at you while BadAssEnemy's attack() fires a machine gun at you and RealBadAssEnemy's attack() fires rocket launcher at u..!!
Hope ur following d point..it gives us d power to implement d function differently for every class.

C++ Inheritance Explained


inheritance is a feature which allows us to easily create new class based on already created classes. By deriving from dem and adding more functionality.
suppose we just finished creating our class for ENEMY which takes care of enemies animations, health and ai. we use dese enemies in d whole level and wen d ending of d level comes v hav to create a boss for d level. so instead of creating a new class boss we inherit d ENEMY class and add some more functionality because even the boss vil hv animations ,health and ai. So we dnt hv to write d code again for dese thngs.

Inheritance is a very powerful tool but u shud use it wisely....

Rules wether to inherit or not.

Rule 1. should be "Is a" relationship and not "Has a" relationship
derived class should follow IS A model...like BOSS is an ENEMY so it can inherit enemy class.
"has a" relationship should be followed when conatinment is to be applied.
For eg: we have a class for weapons. So should weapon class inherit enemy class?
NO..coz a weapon is not an enemy we can also hav a weapon.
But an ENEMY has a WEAPON so it follows HAS A relationship.
hence enemy class vil hav an object of WEAPON class inside it as its member.

Rule 2. There should be beahvioral change and not only data change
For eg: our boss class satisfies d 1st rule but suppose our boss is dumb and acts and looks xactly d same with only difference being its health.
suppose normal enemy int health = 100;
Boss health = 200;
so dere is only chnge in data bt nt in d beahvior of boss so it fails d inheritance eligibility.
But suppose our BOSS is very powerful and can also attack us from air where as normal enemy can only attack from land. Then there is also behavioral change.Then we should inherit d enemy class.

while designing architectures inheritance shud be used carefully it can either simplify ur design or make ur nights sleepless.
These are d 2 thumb rules that u shud always consider while deciding wether to inherit or not.

Variadic Functions

‎#include<stdarg.h> is a header file which enables us to pass multiple arguments to a function.You can pass multiple arguments by using "..." after d last variable.
for eg: avg(int num,...);
so this function avg(int num,...) vil take variable no of arguments and 1 integer arguments for num.
it has 1 type : va_list to hold info abt variable arguments
and 3 functions
1. va_start
2. va_arg
3. va_end

va_start is used to start iterating by passing it 2 arguments : an object of type va_list
and second parameter is d last named parameter of d function

for eg: avg(int num,...)
den va_list l;
va_start(l,num); //dis initializes d list of variable arguments

now to use individual arguments we use va_arg()
it accepts va_list as first argument and type of d argument in the list.
i.e va_arg(l,int); it returns d arguments in d same order in which dey were passed to the function...!!

when we r done we use va_end to free d memory used by our list.
In c it is mandatory to give 1 normal parameter but in c++ its nt mandatory....!!

Basics of Virtual Functions


The address of all the functions are known at compile time with the exception of virtual functions. There address is not known at until run time, this is an example of late binding.
One of d factors deciding the size of an executable (.exe) depends on this binding if a lot of early binding is done d size tends to increase and with late binding the size tends to decrease.
In the world of VIRTUAL functions there are "VTABLES" and "VPTRS"
VTABLES: They are an array of function pointers which holds the addresses of all the virtual functions.
Only 1 VTABLE is generated for a single class
VPTRS: It is a pointer which points at a location inside the VTABLE.Each object will have a VPTR which points to the correct location in a VTABLE.

SO what happens when we call a virtual function

1.VPTR of that object is fetched
2.Offset in the VTABLE is calculated
3. At that offset is d function pointer which is pointing to the virtual function which is then fetched
4. finally the function call is made.

Though the actual memory footprint of a virtual function is very complex but dis is d crux of what happens behind and should give u a basic understanding.

NOTE: Compilers are smart enough to optimize VIRTUAL function calls.
For eg:
class A
virtual void func();
class B : public A
virtual void func();
int main()
A* obj = new B():
B* obj1 = new B():

obj.func(); \\Virtual function overhead, VTABLE lookup occurs here
obj1.func(); \\No overhead here simple member function call passing a "this" pointer.
return 0;

Hidden parameter : this Pointer to member function

Implicit this pointer

Whenever we create a class
There are data members and member functions...
for eg:
class A
int x;
void display();

A obj,obj1,obj2;

Now each obj,obj1 and obj2 vil have there own local copy of x.
But there vil be only 1 copy of Display() in memory not THREE copies.
So how does the Display() know which objects'x X to show....??

This is solved by the compiler by implicitly passing a "this" pointer to display function.
Read the next line carefully
"A this pointer is a pointer which points to the calling object."
so obj.display();
In this case it vil be of type A and it vil point to address of obj.
so this call obj.display() will internally become A_display(&obj).
So &obj gives display() function the address of the calling object i.e this pointe.So it knows which object to access.
And REMEMBER this pointer is passed implicitly by the compiler i.e without the knowledge of the programmer.
But now u knw...lol

Structure/Class Padding

Structure Padding:

Whenever we create classes and structures we should pay special attention to the way we r defining variables insdie, because the size of the struct or class depends on it.
Also frequently used variables shoild be kept on top in order to avoid cache misses.
A class or a struct is always a multiple of the largest sized datatype inside it.
for eg:
struct A
double a;
char ch;

now sizeof(A) == 16 and not 8 + 1 = 9
even if the struct is rewritten like
struct A
double a;
char ch;
char ch1;
char ch2;
char ch3;

even then the size vil be 16

All variables defined inside a struct or a class are alligned in memory according to their size. Like an int can only be stored at addresses 4,8 and 12 that are multiples of 4.
6 is an invalid address for an int.Similarly for double it can be strored only at multiples of 8.
NOTE: A char can be stored at any byte coz its nly of 1 byte.
for eg:
struct A
char ch;
int x;
char ch1;
double d;

1 + 4 + 1 + 8 = 14 and considering double
final size must be 16 but actually

sizeof(A) == 24 .............WHY???

Now lets see WHY, hypothetical starting address 0;
0-1 1 byte for ch
now next valid address for int is 4, hence
1-2 padding
2-3 padding
3-4 padding
4-8 4 bytes for int
8-9 1 byte for ch1
Now next valid address for double vil be 16, hence
9-16 7 bytes for padding
16-24 8 bytes for d

So we wasted 10 bytes in padding dats actually a quite if you use dis struct for creating a 2D array of size 100*100
total objects of struct = 10000
bytes wasted on padding = 10000* 10 bytes
Approx 98 kB is being wasted on just padding.
Lets rearrange d structrure now
struct A
double d;
int x;
char ch;
char ch1;

0-8 8 bytes for double
8-12 4 bytes for int
12-13 1 byte for ch
13-14 1 byte for ch1
since d structure needs to be a multiple of 8 so final size would be 16
Only 2 bytes required for padding
So for 100*100 array only 19kB is wasted on padding
Simply shuffling d variables inside saved us 78kB
So always keep this thing in mind while creating classes and structure in the future.

Two faces of STATIC

By default all functions are implicitly declared as extern, which means they're visible across translation units. But when we use static it restricts visibility of the function to the translation unit in which it's defined. So we can say
Functions that are visible only to other functions in the same file are known as static functions.

Let use try out some code about static functions.


#include "header.h"

int main()
return 0;


#include "header.h"

void int hello()
printf("HELLO WORLD\n");

view plainprint?
#include <stdio.h>

static int hello(); 
If we compile above code it fails as shown below
view plainprint?
$gcc main.c func.c
header.h:4: warning: "hello" used but never defined
/tmp/ccaHx5Ic.o: In function `main':
main.c:(.text+0x12): undefined reference to `hello'
collect2: ld returned 1 exit status
It fails in Linking since function hello() is declared as static and its definition is accessible only within func.c file but not for main.c file. All the functions within func.c can access hello() function but not by functions outside func.c file.

Using this concept we can restrict others from accessing the internal functions which we want to hide from outside world. Now we don't need to create private header files for internal functions.

For some reason, static has different meanings in in different contexts.

1. When specified on a function declaration, it makes the function local to the file.
2. When specified with a variable inside a function, it allows the variable to retain its value between calls to the function. See static variables.

It seems a little strange that the same keyword has such different meanings...

How and When are Temporaries generated

Understanding Temporaries

Temporaries are 1 of the main bottlenecks in creating high speed and efficient software.
so their understanding is very important.

A temporary as the name suggests is a hidden object that the compiler creates itself in order to achive something.Its done implicitly by the compiler without the knowledge of the programmer.
Main reasons for compilers such behavior is

1.Type mismatch
for eg:
class A
int var;
A(int x)
var = x;
void func(A);

Now here is a constructor for A which takes an integer argument...!!
So now dese r d ways in which we can create objects for A
A a(10); \\Normal nthng unusual
A a = 10; \\Hey wats happening here
a.func(10); \\Hey wats happening here
So wats happening in these two lines...??
actually the compiler was expecting an object of A as function argument and on RHS..
but instead it got an "int"....But wait these lines still runs and the compiler doesnt complain. This shows that compiler is very generous and is helping us underneath.
Actually the compiler knows that an object of can be created if it has an integer argument, SO
it creates a TEMPORARY.....!!
A temp(10); //Constructor called
a = temp; //Copy constructor called
~temp; //Destructor called
So u see just to create a TEMPORARY
2 constructors and 1 destructor is called...u will say so what
when we do
int = int + int...no 1 complained for this so far
but listen guys temporaries are not significant for primitive data types but for user defined object of size 100 bytes IT IS SIGNIFICANT.
So A = A + A; vil also create a temporary if '+' operator is overloaded.
a way to overcome dis is use += unary operator.
A obj,obj1,obj2;
for obj = obj1 + obj2; \\Temporary here
but obj = obj1 \\No Temporary
obj += obj2 \\No Temporary here as well

Also Remember passing by value and returning by value also creates a TEMPOARAY. As a local copy is always created.
So always try to pass pointers or better References.

And these constructors are called "Converting Constructors"
To prevent this we should use "Explicit constructors"
explicit A(int)
now it wont create a temporary :)

File Handling in C++(Basics)

File Handling in C++(Basics)
File handling is done so dat u can store any result or output to your Hard disk and read dat later on.For eg:
you write a programme to accept the marks of students of your class and store dem in some variables.But as soon d programme is over all those variables are erased from the memory thus all your data is gone.Hence you need to store them in some file from where u can read dem later.This is achieved by FILE HANDLING.
First of all u need "fstream" header file.So include it
after dat u need to open a file either in READ(ifstream) mode or in WRITE(ofstream) mode or in READ-WRITE(fstream) mode.For eg:
ifstream read;
There are different modes in which you can open files.
Deafult modes
Ifstream ios::in
ofstream ios::out
fstream ios::in | ios::out

All modes
ios::app If FileName is a new file, data is written to it.
If FileName already exists and contains data, then it is opened, the compiler 
goes to the end of the file and adds the new data to it.

ios::ate If FileName is a new file, data is written to it and subsequently added to the 
end of the file. If FileName already exists and contains data, then it is opened and data is 
written in the current position.

ios::in If FileName is a new file, then it gets created fine as an empty file.
If FileName already exists, then it is opened and its content is made available 
for processing.

ios::out If FileName is a new file, then it gets created fine as an empty file. Once/Since it gets created empty, you can write data to it.
If FileName already exists, then it is opened, its content is destroyed, and the file becomes as new.
Therefore you can create new data to write to it. Then,if you save the file, which is the main purpose of this mode, the new content 
is saved it.This operation is typically used when you want to save a file

ios::trunc If FileName already exists, its content is destroyed and the file becomes as new.

ios::nocreate If FileName is a new file, the operation fails because it cannot create a new file.
If FileName already exists, then it is opened and its content is made available for processing.

ios::noreplace If FileName is a new file, then it gets created fine.
If FileName already exists and you try to open it, this operation would fail because it cannot create a file of the same name in the same location.

Now lets try to open a file, write to it, and save it to hard disk

int main()
ofstream write_file("c:/ishkaran.txt"); //file opened in write mode

write_file << "hello!";

write_file.close(); //file closed
return 0;
It will create a txt file in your c:\ drive of name ishkaran.txt then it vil write to dat file the string provided and den it vil close d file.
You can also check if the file has been properly opened before readin or writing to it by
//Do something with the file

Now lets read the file we just wrote to.
int main()
{char ch[256];
ifstream read_file("c:/ishkaran.txt"); //file opened in write mode

read_file >> ch;
read_file.close(); //file closed
return 0;
this will print hello but deres a problem here it will read until a space is encountered. So to overcome that uoy should use
read_file.getline( char*,int count,char delimiter);
it reads the number of characters specified in count and passes it to 1st argument either this or it will read until delimiter character is encountered whichever happems first.
u can read like this
//Read here

this loop will work until whole of the file is read. eof() returns true if End of File is encountered.

I have explained only the basics, reading and writing simple data types
Theres still a lot more to file handling ....Will explain more in further posts....!!



From bookish point of view...an array is a collection of homogeneous data, by homogeneous we mean dat the data vil be of same data type,may it be int or char or float.
int arr[10]..is an array of data type integer with 10 variables occupying size of 40 bytes.
Dis vil be same as defining int a0,a1,a.....a9
But the difference is dat v can now access dem easily through 1 name.
for eg: if v want to access 2nd element we will do like arr[1] ,5th element arr[4] and so on.
Remember: array-name[index]
array-name can be anythng and index starts from 0 and not 1.
an array of 5 elements: int arr[5]
defining simple variables : arr1 arr2 arr3 arr4 arr5
using array arr[0] arr[1] arr[2] arr[3] arr[4]
notice the indices
Hope u get it..!!
You can treat them just like normal variables but accessing them through array style.
Remember: Arrays are allocated in memory contiguously.
for eg: in ram arr[0],arr[1],arr[2] will be adjuacent to each other
like address of arr[0] is 1000,address[1] is 1004 and so on...!!
Now internally arrays are nothing more than "CONSTANT POINTERS".
For eg: u declare int arr[10];
then arr is only the address of arr[0] and it vil always point to that location only
For eg : int arr[5]; lets assume arr is pointing to 1000
So what happens when u require arr[3].
arr + index*sizeof(datatype) this formula is used
1000 + 3*4 //sizeof(int) is 4
which gives 1012 this vil be the address of arr[3].
Remember when i told u that arrays are allocated contigously in memory.This is the main importance . if they are not allocated contiguously then this scheme would fail.
Since the compiler just multiplies d index with the size of data type and adds it to base address...u vil be surprised to knw that u can also use array like
3[arr]..this vil be same as arr[3].

and remember that arrays are like constant pointers internally
you cannot assign an array to another variable once its been defined.
and due to the way that arrays are implemented internally they hav O(c) access time..
which means constant access time.
for further experience actually code the arrays for first hand experience.

Few things about MAIN() ( C/C++)

Few things about MAIN()

We all are aware of tis function,right? We all love this function coz its the entry point of our software.Actually before main() is entered preprocessor is run which processes everything defined/declared before entering main(),for eg: including header files,processing macros, etc.
By ISO standard main() should always return int.
You should always declare int main(), you can also use void but remember the standard is using "int main()".
It should return 0 on successful completion and 1 on unsuccessful completion.
Remember you can also return any other value like return 10, return 23 anything but remeber what the standard is.

Passing Arguments to main()---->

int main( int argc, char* argv[] )
this is the syntax for main(),expecting 2 arguments ARGC( ARGUMENT COUNT ) of type int and ARGV( ARGUMENT VECTOR ) of type char**.It can also be denoted as char*[].It means that it points to an array of char*.You can also declare like
int main( int argc, char** argv )
There are at least two arguments to main: argc and argv. The first of these is a count of the arguments supplied to the program and the second is an array of pointers to the strings which are those arguments—its type is (almost) ‘array of pointer to char’.
Now u run the programme and say "hey, Wait id dint paased any arguments"
REMEMBER: that u pass arguments through command line i.e dos in other wrds command prompt(CMD).For any1 who hasnt seen cmd type cmd in run(win + r).

When a program starts, the arguments to main will have been initialized to meet the following conditions:
1. argc is greater than zero.
2. argv[argc] is a null pointer.
3. argv[0] will be a string containing the program's name or a null string if that is not available. Remaining elements of argv represent the arguments supplied to the program. In cases where there is only support for single-case characters, the contents of these strings will be supplied to the program in lower-case.
Now wat u actually do with these arguments inside your programme depends on you.
The most imp thing to remember is that :
ARGC(ARGUMENT COUNT) tells us how many arguments are passed to your programme which are stored in ARGV( ARGUMENT VECTOR ).
Default value of ARGC is 1 because the first argument inside ARGV is the path of the exe file itself(m talkng abt windows, dnt knw bt linux :( ).

A sample programme:
#include <stdio.h>

int main(int argc, char **argv)
printf("%s\n", *argv++);
return 0;

But on visual studio 2005 the implementation is a little diff.
for eg: if you pass 2 arguments to the programme,then argv[2] will be the name of the programme and not argv[0].
rest is same.
Write code to actually get the first hand experience.