Appendix A.) Basis C/C++ and ARSA Framework for Game Programming

Chapters

          This chapter introduce basic of C/C++ and ARSA Framework. Whether you are expert experience user or you are novice, We very suggestion read this before start other section. This chapter will cover all the basis path to make your game, all with plenty of examples.

            1. Include Header and Namespace

// include arsa.h to this source code

// write on top of source code

#include<arsa.h>

 

// tell compiler pull using irr name's space

// ARSA Framework have main name space to following

// irr is basis variable and function.

// io is input/output operator.

// gui is function relate graphic user interface.

// video is video function.

// scene is scenenode function.

usingnamespace irr;

            2. Variable Type

char          // character

int                  // integer

long          // long integer

float         // 32 bit floating point

double        // 64 bit floating point

irr::c8              // 8 bit character

irr::s8              // 8 bit signed

irr::u8              // 8 bit unsigned

irr::u16      // 16 bit unsigned

irr::s16      // 16 bit signed

irr::u32      // 32 bit unsigned

irr::s32      // 32 bit signed

irr::u64      // 64 bit unsigned

irr::s64      // 64 bit signed

irr::f32      // 32 bit floating point

irr::f64      // 64 bit floating point

irr::core::stringc   // string c8

irr::core::stringw   // string wchar_t

irr::core::array<datatype> // dynamic array

arsa_xml_doc  // arsa xml document

arsa_xml_node // arsa xml node

ArrayXmlNode  // arsa xml node array

 

// ARSA Framework global pointer vaiable ready to use

// don't duplicate name

g_device      // system device

g_smgr        // scene manager

g_fs          // file system

g_driver      // driver manager

g_gui         // gui namager

g_log         // log file

g_rand        // random number

g_psd         // psd operator

g_camera      // default camera

g_input              // input handle

g_snd         // sound manager

 

            3. Dynamic Arrays and Data Structure

3.1 Add data to array

// declare int array as IntList

irr::core::array<int> IntList;

 

// add data to array

IntList.push_back(10);

IntList.push_back(20);

IntList.push_back(30);

 

// print data in IntList

printf("%d ", IntList[0]); // show 10

printf("%d ", IntList[1]); // show 20

printf("%d ", IntList[2]); // show 30

3.2 Read data from array using for loop

// IntList.size() is get current array size

for (irr::u32 i = 0; i < IntList.size(); ++i)

{

       printf("%d ", IntList[i]); // show value in each slot

}

3.3 Remove data from array

// specify slot to delete

// becareful if deleted any data in array then all memory will reallocate

IntList.earse(0);

 

// remove all data in array

IntList.clear();

3.4 Advanced remove data from array by for loop

// often in game update need delete some data in array list by for loop

// this section shown you how to delete them

for (irr::u32 i = 0; i < IntList.size(); ++i)

{

       IntList.earse(i); // delete data

       // after this line array slot i can't use anymore

       --i; // must shift back index    

}

3.5 Advanced allocate array

IntList.set_used( 10 ); // allocate int 10 slot

IntList[0] = 1;

IntList[1] = 2;

...

...

...

IntList[9] = 10;

3.6 Basis copy array

// copy array IntList to IntList2

irr::core::array<int> IntList;

irr::core::array<int> IntList2;

 

IntList.push_back(10);

IntList.push_back(20);

IntList.push_back(30);

 

// result IntList2 equal IntList

IntList2 = IntList;

3.7 Advanced copy array

// allocate destination slot

IntList2.set_used(IntList.size());

 

// use memcpy copy data from IntList to IntList2

// IntList2.pointer() return first address in array

// IntList.const_pointer() same pointer() but is const

// use when user make sure don't change any data in this.

// sizeof(int)*IntList.size() tell memcpy how many copy count.

//            sizeof(int) is data size in IntList int = 4 byte

//            IntList.size() is array element count = 3, 3*4 = 12 byte copies.

memcpy(IntList2.pointer(),IntList.const_pointer(),sizeof(int)*IntList.size());

3.8 Use as queue

// pop function

IntList[0]; // get data in first slot

 

// after used data user can delete or not (depend on task)

IntList.earse(0); // remove data in begin

 

// push back/front

IntList.push_back(10); // add data to end list

IntList.push_front(10); // add data to start list

3.9 Custom structure

struct SData // start structure with name

{

       SData() // constructor

       {

              Id = -1; // default start value

       }

       int Id;

       irr::core::stringc Name;

}; // end

3.10 Advanced custom structure

struct SData

{

       // default constant id = -1, Name = empty string

       SData(int id=-1,const irr::core::stringc& name=irr::core::stringc())

       {

              Id = id;

              Name = name;

       }

       // equal operator for search

       booloperator==(const SData& other) const

       {

              if (Id == other.Id)

                     returntrue;

              returnfalse;

       }

       // less than operator for sort

       booloperator<(const SData& other) const

       {

              // Id < other.Id is Ascendant

              // Id > other.Id is Descendant

              return Id < other.Id;

       }

       int Id;

       irr::core::stringc Name;

};

 

// declare array

irr::core::array<SData> DataList;

 

// add data, choose one from below

// add type 1

DataList.push_back(SData(0,"Winner"));

 

// add type 2

SData dat(2, "Fly");

DataList.push_back(dat);

 

// add type 3

SData dat2;

dat.Id = 3;

dat.Name = "Slide";

DataList.push_back(dat2);

3.11 Search in array

// search

irr::s32 id = DataList.linear_search(SData(1));

if (id != -1)

{

       // found data, use id as slot for get data

       printf( "%d: %s", DataList[id].Id, DataList[id].Name.c_str() );

}

3.12 Sort in array

// sort

DataList.set_sorted(false);

DataList.sort();

            4. Control

4.1 if, else and switch

// if else

// > greater

// < less than

// >= greater or equal

// <= less than or equal

// == equal

// != not equal

// && and condition

// || or condition

int i = 10;

if (i > 10)

       printf("i > 10");

elseif (i > 10)

       printf("i < 10");

else

       printf("i = 10");

 

// check if as range

if (i>0 && i<10)

       printf("i between 1-10");

 

// float equal compare must use equal() function, don't use ==

// because 1.567f maybe compiler understand is 1.569999999f

// when user compare 1.567f == 1.569999999f computer understand this not equal

// if some case in game need accurately compare e.g. trigger at animation time line

float speed = 1.567f;

if (irr::core::equal(speed, 1.567f))

       printf("speed is %.f", speed);

 

// switch can check equal value only, often case is compare integer

switch (i)

{

       case 0:

              printf("i = 0");

       break;

       // if have { } user can declare variable in case

       case 1:

       {

              int j = 10;

              printf("i = 1, j = %d", j );

       }

       break;

       default: // if check all case are false

              printf("don't know value!");

       break;

} // end switch

4.2 for, while and do

// every loop in c will repeat until condition is false

// basic for

for (int i = 0;i < 10;++i)

{

       printf("%d\n",i)

}

 

// advanced for

// multiple variable increase in loop

int j = 0;

for (int i=0;i<10;++i,j+=2)

{

       printf("i=%d j=%d\n",i,j);

}

 

// multiple declare variable in loop

int j = 0;

for (int i=0,k=0;i<10&&k<5;++i,j+=2, k++)

{

       printf("i=%d j=%d k=%d\n",i,j,k);

}

 

// infinite for

for (;;)

{

}

 

// break and continue

int i=0;

for (;;)

{

       if (i > 10)

              break; // exit this loop.

       if (i > 5)

       {

              i+=2;

              continue; // goto start position, don't do below function.

       }

       i++;

}

 

 

// basic while

while (i<10)

{

       printf("%d\n", i)

       i++;

}

 

// infinite while

while (1)

{

}

 

// basic do

 

do

{

       printf("%d\n", i)

       i++;

}while (i < 10);

 

// infinite do

do

{

}while (1)

            5. Pointer and Memory

Soon!

            6. Functions

Soon!

            7. File

Soon!

            8. XML

Soon!

 

Good Tutorial C/C++

http://www.learncpp.com/

Tags: