XFactorDev.net Article   XFactorDev.net Article

 

Prt-2- The coding begins

 

You've been dreading it...but where going to put some code down.  Now don't get scared, the code for a GameEngine is really simple, especially when you start from scratch...as it progresses, this monster will grow and grow...hopefully to a point of pure perfection :)  A gameengine that you'll be able to change to all your gaming needs.

 

As this is sort of a tutorial/on-going project, where always reading for feedback and further ideas...its an open source GameEngine, so you can use it and change it as much as you want...as long as you recognise its authors and creators :)

 

 

So our first part of code is the main.cpp/.h...all this does is point out or starting position!...this is where it begins and ends.

 

/*                                                                     *

  *                                                                   *

   *                                                               *

     *                                                           *

       *                                                       *

         *                                                   *

           *                  ENTRY POINT                  *

             *                                           *

               *                                       *

                 *                                   *

                   *                               *

                     *                           *

                       *                       *

                         *                   *

                           *               *

                             *           *

                               *       *

                                 *   *

                                   */

/***************************************************************************/

/*                                                                         */

/*  main.h                                                                 */

/*                                                                         */

/***************************************************************************/

 

#ifndef _XF_MAIN_H

#define _XF_MAIN_H

 

 

#include "game.h"

 

 

void main();

 

 

#endif // _XF_MAIN_H

 

 

What on earth are those things... "#ifndef _XF_MAIN_H"... well you could leave them out...and you might get away with it...but there used for the pre-compiler...as when you compile a file, it checks all the #include's so it will go to game.h, and game.cpp, and main.h etc putting it all together piece by piece....but you could have put #include 'game.h' twice, and the pre-compiler gets comfused very easily...telling you an error of already defined in such a file.....to prevent this... we add the #ifndef etc...telling the precompiler its already defined if not include it.....hope I explained that okay :-/

 

And for the function code..

 

/*                                                                     *

  *                                                                   *

   *                                                               *

     *                                                           *

       *                                                       *

         *                                                   *

           *                  ENTRY POINT                  *

             *                                           *

               *                                       *

                 *                                   *

                   *                               *

                     *                           *

                       *                       *

                         *                   *

                           *               *

                             *           *

                               *       *

                                 *   *

                                   */

/***************************************************************************/

/*                                                                         */

/*  main.cpp                                                               */

/*                                                                         */

/***************************************************************************/

 

 

#include "main.h"

 

void main()

{

     

            /*

            -1- Initilize Any Main Functions

           

            -2- Go InTo GameLoop

 

            -3- Clean Up Anything And Say Goodbye :)

            */

}

 

 

 

 

Its not really much...just the starting point of our code....as you might have guessed I'm a keen fan in adding snazzy ascii comments to my code where I can....makes the code a little easier to follow I think.

 

Now this is where the we take a special road... Usually you'd put your Game in a class, so that when you initialize an instance of the CGame class or CApp class the constructor is called ...then when you've finished the destructor is called and everything is tidied up...all automatically you might say.  In between of course we would call our GameLoop code.

 

There's a lot of newbie programmers out there....still fighting with Classes and Structures and Functions and how to use them....so how should we go with this?

 

 

CGame class was my final decision.......so we'll have a very very simple class....!...it will contain only 3 member functions and will hold the meat and potatos of our game.

 

 

class CGame

{

public:

      // Initilisation called only once in the constructor

      CGame();

 

      // GameLoop, gets called repeatedly while it returns 1;

      bool GameLoop();

 

      // Cleanup process, all done in our destructor

      ~CGame();

}

 

 

Thats it...its not complicated...and we put it in our main() entry point, like this:

 

main()

{

CGame* gg = new CGame();  // constructor would get called at creation time

while( gg.GameLoop() ); // gameloop called repeatedly

delete gg; // destructor when we delete our Game Object.

}

 

 

This prevents us having any globals now........we can keep any valuable game variables in the CGame class.

 

 

But before putting the game.h/game.cpp code in...we need a way of debugging our code... a way of testing for errors etc...a way to determine if values are correct...sometimes its not always necessary to step through a game line after line with the debugger, so I deviced another way, simple but effective way.

 

Let me just take a drink of my coffee ... *SSsllluurrrpPPpppp* ...I feel a lot better now...all this coding is hard work you know..hehehe...   Lets create a really simple debug.h/debug.cpp set of code, that we could use anytime we want....and it will be invaluable to us at times.... as I usually find I leave out error checking at points so that the code is easier to follow, only to discover a typo error later on.

 

 

/***************************************************************************/

/*                                                                         */

/*  File: debug.h                                                          */

/*                                                                         */

/*  Details: Debugging Code                                                */

/*                                                                         */

/*  Auth:    x-factor development (http://www.xfactordev.net)              */

/*           Ben_3D@xfactordev.net or contact@xfactordev.net               */

/*                                                                         */

/***************************************************************************/

 

#ifndef _XF_DEBUG_H

#define _XF_DEBUG_H

 

 

#include <stdio.h>  // used for fopen, fprintf and fclose

 

 

void debug(char *str);

 

 

#endif // _XF_DEBUG_H

 

 

Code body....

 

/***************************************************************************/

/*                                                                         */

/*  File:    debug.cpp                                                     */

/*                                                                         */

/*  Details: Debugging Code                                                */

/*                                                                         */

/*  Auth:    x-factor development (http://www.xfactordev.net)              */

/*           Ben_3D@xfactordev.net or contact@xfactordev.net               */

/*                                                                         */

/***************************************************************************/

 

#include "debug.h"

 

// Notice the xbox debug information file...in this case it will be put to

// the same location as the xbe, when you put it to cdrw or dvdr later on,

// be sure to change this!

 

#define DEBUG_FILE "D:\\DebugInfo.txt"

 

void debug(char *str)

{

      // Used during debugging of the program

     

      FILE *fp;

      fp=fopen(DEBUG_FILE, "a+");

      fprintf(fp, "%s\n", str);

      fclose(fp);

     

}

 

// Don't forget now, if you want to exclude your debug information, just

// comment out the code inside the debug function using /* and */

 

 

I don't want to start writing millions of lines of code and loads of files without seeing something!  We've got a game.h/cpp file and a debug.h/cpp file....now lets do some coding!....lets see that it all compiles and works shall we.

 

 

...

#include "main.h"

 

// Lets include our debug!

#include "debug.h"

 

void main()

{

     

            debug("Entered Main...");

            /*

            -1- Initilize Any Main Functions

            */

 

            debug("Going to do a GameLoop");

            /*

            -2- Go InTo GameLoop

            */

 

            debug("Washing and doing the dishes (Cleanup)..");

            /*

            -3- Clean Up Anything And Say Goodbye :)

            */

}

..

 

 

 

With these changes, you compile, generate your xbe, patch it and run it....and tada!  Your xbox just sort of crashes :(  But reboot it, connect agian using your FTP program, and go to the directory your xbe is located...and what do you find in there?  "DebugInfo.txt".  My god...so whats in there your wondering...lets open it and have a look...and this is what you should find:

 

Entered Main...
Going to do a GameLoop
Washing and doing the dishes (Cleanup)..

 

See, now we can create a debug file which we can use as we progress through our GameEngine's birth...monitoring it as it goes along...taking its temperature and making sure its growing correctly....to see it grow into a full grown up game engine.

 

 

 

 

 

 

 

 

 

 

  XFactorDev.net Article



      
webmaster@xfactordev.net