For creating a new header file go to menu Project->Add->New item (or right click on the project in the solution explorer and select the same option). A window will pop up, then select Header file in the window and set the name to game.h:
We're going to place in this file some code that will be used from main.cpp, so in main.cpp we need to include it:
#include "game.h" // use "" for files found in the project folder, contrary to <stdio.h> or <vector>
Now we can place some of the code into the file. We're going to move all the #define lines, the #include for <vector> and the structure declarations to game.h. We're also going to add the following lines at the end of game.h:
// -- these game functions need to be included at main.cpp in order to use them
void setupMap( SGame* gameObject ); // initialize map layers
void setupPlayer( SGame* gameObject ); // initialize player
void setupEnemies( SGame* gameObject ); // initialize enemy list
void updateMap( SGame* gameObject, float fTimeElapsed ); // update tiles
void updatePlayer( SGame* gameObject, float fTimeElapsed ); // update player
void updateEnemies( SGame* gameObject, float fTimeElapsed ); // update enemy AI and refresh enemy layer
void drawASCIIMap( const SGame* gameObject ); // take the map data and print it on the console
void drawASCIIGameInfo( const SGame* gameObject ); // print the player and misc infoWe're also going to be adding files that will contain a timer object that we will be using for querying the time elapsed between game frames. We're going to need this in the next tutorials to improve animation, AI and more. These files will be called timer.h and timer.cpp and will define a structure called STimer that we will be able to use in any of our projects. It will also contain the following member variables and methods:
// members
float LastTime; // the time calculated by the last Frame() call.
// methods
void Reset(); // Reset timer
void Frame(); // Calculate time elapsed since the last Frame() or Reset() call.We're going to need this file included in our main.cpp in order to use the STimer object in our main loop, in which we're going to declare an instance of the timer and query Frame() on every loop iteration so we can send the time delta to the update() function:
int main( void ) // start application from here
{
SGame gameInstance;
STimer timer; // declare our timer instance
setup( &gameInstance ); // call setup() and send the address of our data as parameters
int frameCounter=0; // declare a variable to keep track of the frame number
while( true ) // execute block {} while what's inside () is true
{
timer.Frame(); // Query time elapsed since last Frame() call
printf("Current frame number: %i\n"
"Last frame time: %f\n", frameCounter, timer.LastTime );
update( &gameInstance, timer.LastTime ); // send time elapsed to update() call
draw( &gameInstance ); // render game dataWe're also be modifying our functions at main.cpp to work with the declared functions at game.h and we're going to move all the remaining code to a new game.cpp file where the game functions will be defined. So our setup() function will now look somewhat like this:
// Use this function to setup our game data
void setup( SGame* gameObject ) // Accepts an address pointing to an SGame instance
{
printf("- setup() called.\n");
// call setup game functions
setupMap(gameObject); // initialize map setupPlayer(gameObject); // initialize player setupEnemies(gameObject); // initialize enemy list
};
We're also be updating our update() and draw() functions the same way and moving all the game code into our game.cpp functions, which will now look similar to the following:
// Use this function to update the map tiles
void updateMap( SGame* gameObject, float fLastFrameTime )
{
}
// Use this function to update the player
void updatePlayer( SGame* gameObject, float fLastFrameTime )
{
if(GetAsyncKeyState(VK_UP))
gameObject->Player.z -= 1; // decrease by 1
if(GetAsyncKeyState(VK_DOWN))
gameObject->Player.z += 1; // increase by 1
if(GetAsyncKeyState(VK_RIGHT))
gameObject->Player.x ++; // increase by 1
if(GetAsyncKeyState(VK_LEFT))
gameObject->Player.x --; // decrease by 1 }We should end up with a file structure similar to the one shown below and it should still build and run as it previously did:
The complete files for this tutorial can be found in the repository I made for the tutorials at google code. After the code is better organized we can start looking at improving our game systems and making a proper game in which you can do something more interesting than just run and get your health lowered up to negative values.
In the next tutorial we will be improving the way our characters move and enable a configurable speed for each of them. See you there!


No hay comentarios:
Publicar un comentario