Skip to content

Getting started

Isaac edited this page Sep 16, 2018 · 24 revisions

This guide will help you set up your first file and get you introduced to some basic Game Engine API. This guide works best for developers intending to create test cases to test out portions of code that they have been working on. If you have not yet cloned the repository please see Cloning the repository. That guide will get you set up so you can start adding your features to the game engine.

There is no entry point written in the Game Engine as it is just an Engine. There should be a definite line between the Game Engine Code and your own Game code. To get started you must write this entry point and pass the control off to the Game Engine. To do this create a new C++ class, both the header and source code files (.h and .cpp), in this example we will be calling it StartApp. In the header file, you must include the Application class in the Game Engine code and add the header guards.

#include "Application.h"

Then publicly extend your class with the Application class. class StartApp: public Application

The Application class has four methods, three of which are virtual and must be defined by you.

        void initialize();
	void run();
	void shutdown();

The function is called to so you can initialize the Game Engine and any other subsystems, resources and such are needed for your game. The run() function is primarily to start the main loop inside the Game Engine, essentially starting of the rendering and updating the game. Finally, the shutdown method will be called once the game is finished, in this function you will be doing some basic cleanup, delete pointers and shut down the game engine.

The last piece we need is to define the entry point or main method. Inside the main method, you will create an instance of the class you have just written up and call the launch(); function. This will hand over the process off to the game engine which in turn will call the initialize, run and shutdown methods.

Your final code should resemble something like this SartApp.h file

#ifndef START_APP_H
#define START_APP_H

#include "Application.h"
class StartApp :
	public Application
{
public:
	StartApp();
	~StartApp();

	void run();
	void initialize();
	void shutdown();
};

#endif

And this StartApp.cpp file (I have included memory management code in here)

#include "StartApp.h"
#include "World.h"
#include "Logger.h"

// Memory detection
#ifdef _MSC_VER
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define VS_MEM_CHECK _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF)
#endif

/*
	This constructor is for your own personal use

	WARNING WARNING WARNING WARNING
	Do not initialize subsystems here
	
*/
StartApp::StartApp()
{
}

/*
	Basic cleanup here, just make sure you don't have any memory leaks
	We can't do everything for you ;)
	That being said we do most of the heavy lifting for you.
*/
StartApp::~StartApp()
{
}

/*
	This function is designed to start up and initialize everything
	There is a very specific order that the subsystems must be initialized in
	This function is designed to reduce the errors and manage the most 
        important initialization for you

	First, initialize the Game Engine and all the subsystems you will need

	Second, initialize any other subsystems that are not initialized by the game
        These are game specific subsystems

	Third initialize the world and GUI manager etc. You can also load some resources, 
        basic ones just for the GUI

	Fourth add GUI elements and load some resources. You can separate the loading 
        of resources. You can either do it all here and if you have done the Game Engine 
        initialization properly. You will have a loading screen, or you can create your 
        own custom screen and load somewhere else. Must load all essential resources here

	Fifth register process that need to take place, eg. final resource loading
*/
void StartApp::initialize() {}

/*
	This passes the process to the Game Engine to run the main loop, it is called 
        after the initialization.
	Or you can directly call all the rendering calls in the World and in the Renderer
	Have fun with that one hahaha
*/
void StartApp::run() {}

void StartApp::shutdown() {}

/*
	This is the startup template script, this just creates a new StartApp instance
	And then hands over the process to the application.
	This function will then handle background process and calls a few functions
	These call back functions can be used to start up and set up your game
*/
int main(int argc, char** argv) {

	VS_MEM_CHECK;
	
	StartApp app;
	app.launch();

	return 0;
}

Next step is to get something to show up. The most important parts are the GameEngine, World and Camera classes. All of these are required to display something on the screen.

We first need to set up the system to handle all the input management, rendering fonts etc. This is done by making a Game Engine class and initializing it. The GameEngine class is already included in the Application.h header. Make a new GameEngine, World and Camera var in your StartApp.h.

GameEngine engine;
World* world;
Camera* camera;

In your StartApp.cpp class in the initialize(); function initialize the GameEngine add all subsystems, the world and create your camera.

engine.initialize(GameEngine::GAME_ENGINE_ALL_SUBSYSTEMS);
world = new World();
world.initialize();
camera = new Camera();

Next, add the camera to the world and add the world to the GameEngine.

world->setActiveCamera(camera);
engine.setWorld(world);

In the void run(); function run the game engine main loop by calling engine.run(); Next, don't forget to shut down the engine and world properly when the game is done.

engine.shutdown();
world->shutdown();

That will get everything up and going, you can add anything to the world as an entity and it will show up in the world. And just to spice it up a bit lets add a nice background to our world. To do this we can use the AssetManager to load a cube map and set the worlds cube map. To do this we just use the following code.

world->setCubeMap(AssetManager::LoadCubeMap("Texture\\cubemap\\morning"));

Now your StartApp.h and StartApp.cpp files should look something like this.

#ifndef START_APP_H
#define START_APP_H

#include "Application.h"
class StartApp :
	public Application
{
public:
	StartApp();
	~StartApp();

	void run();
	void initialize();
	void shutdown();

	GameEngine engine;
	World* world;
	Camera* camera;
};

#endif

This doesn't have any of the comments.

#include "StartApp.h"
//#include "World.h"

// Memory detection
#ifdef _MSC_VER
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define VS_MEM_CHECK _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF)
#endif

StartApp::StartApp()
{
}

StartApp::~StartApp()
{
	delete camera, world;
}

void StartApp::initialize() {
	engine.initialize(GameEngine::GAME_ENGINE_ALL_SUBSYSTEMS);

	world = new World(); // Create a new world instance
	world->initialize(); // Initialize the world
	camera = new Camera();
        // Set the active camera, TODO create more controls to change camera etc.
	world->setActiveCamera(camera); 
	world->setCubeMap(AssetManager::LoadCubeMap("Texture\\cubemap\\morning")); 
        // Set the current world in Game Engine, 
        // will recieve updates, events, rendering calls
	engine.setWorld(world);
}

void StartApp::run() {
	engine.run();
}

void StartApp::shutdown() {
	world->shutdown();
	engine.shudown();
}

int main(int argc, char** argv) {

	VS_MEM_CHECK;
	
	StartApp app;
	app.launch();

	return 0;
}

You should also be aware that if you had trouble with this the complete files can be found in the 'Start Files' directory of the project. Just COPY them into your 'GameEngine' folder with all the other files and they should work. Make sure you understand how they work though, since they are how you start your own project.

Clone this wiki locally