Creating an Application and World

From C4 Engine Wiki

Jump to: navigation, search

Creating the Application

First step is to create a new project. Follow the tutorial to create a basic Hello World. You’ll need everything in it except for the lines in the MyGame constructor. You can change the name of the filename and Game class as you need to, but for this tutorial, we’ll be using RTS as the project name and class name.

<RTS.h>

#ifndef RTS_H
#define RTS_H
 
#include "C4Application.h" 
#include "C4Engine.h"
 
using namespace C4;
 
class RTS : public Singleton <RTS>, public Application
{
    private:
 
    public:
 
        RTS(void);
        ~RTS(void);
};
 
extern "C"
{
   module_export Application *ConstructApplication(void);
}
 
#endif

<RTS.cpp>

#include "C4Engine.h"
#include "C4Interface.h"
#include "RTS.h"
 
using namespace C4;
 
// Global pointer to our game object
RTS *TheGame = nullptr;
 
Application *ConstructApplication(void)
{
    return (new RTS);
}
 
RTS::RTS(void) : Singleton<RTS>(TheGame)    
{ 
}
 
RTS::~RTS(void) 
{
}

Creating the World

In Windows Explorer, create an RTS subfolder in the Data folder and also in the Import folder where the C4 executable and RTS.dll reside. This will be where we put all of our game assets. Start up C4 with the RTS game dll, and hit Ctrl + N to create a new world. Create a large enough box primitive to serve as the ground plane for the units (10x10x1 is fine). Make sure the box surface is positioned so that it is somewhat flush to the XY plane. Also add a Point Light in the center of the playfield, slightly above it, and give it a decent radius by switching to Scale Nodes mode and dragging a sphere from the Point Light. Turn on Toggle Lighting to see the effect of the point light. Save your new world as RTS/rts.wld. This saves the new world asset into the Data folder. The RTS subfolder is where your game project will find all the assets for this tutorial.


Image:playfield.jpg


Now that we have our base application created and a world to work with, let’s put in some world code so that we can run our game and check out the world in-game. We’ll create a class called CombatWorld to represent our world or level in the game. All it has right now is overridden Preprocess and Render functions that we’ll be adding to later.

<CombatWorld.h>

#ifndef COMBAT_WORLD_H
#define COMBAT_WORLD_H
 
#include "C4Engine.h"
#include "C4World.h"
 
namespace C4
{
	class CombatWorld : public World
	{
		public:
			CombatWorld(const char *name);
			~CombatWorld();
 
			WorldResult Preprocess(void);
			void Render(void);
	};
}
 
 
#endif

<CombatWorld.cpp>

#include "CombatWorld.h"
 
using namespace C4;
 
CombatWorld::CombatWorld(const char *name) :
		World(name)
{
}
 
CombatWorld::~CombatWorld()
{
}
 
WorldResult CombatWorld::Preprocess(void)
{
	WorldResult result = World::Preprocess();
	if (result != kWorldOkay) return (result);
 
	return (kWorldOkay);
}
 
void CombatWorld::Render(void)
{
	World::Render();
}

The application now needs a way to create the world that we made above. To do this, we add a static ConstructWorld function here. (Majority of this code is directly from the SimpleBall demo game)

<RTS.h>

#ifndef RTS_H
#define RTS_H
 
#include "C4Application.h" 
#include "C4Engine.h"
#include "C4World.h"	// <-- Add
#include "CombatWorld.h"	// <-- Add
 
using namespace C4;
 
class RTS : public Singleton <RTS>, public Application
{
    private:
    public:
 
        RTS(void);
        ~RTS(void);
 
	static World *ConstructWorld(const char *name, void *data);	// <-- Add
};
 
extern "C"
{
   module Application *ConstructApplication(void);
}
 
#endif

<RTS.cpp>

RTS::RTS(void) : Singleton<RTS>(TheGame),
{
	TheWorldMgr->SetWorldConstructor(&ConstructWorld);	// <-- Add
}
 
RTS::~RTS(void) 
{
	TheWorldMgr->UnloadWorld();	// <-- Add
	TheWorldMgr->SetWorldConstructor(nullptr);	// <-- Add
}
 
// New function
World *RTS::ConstructWorld(const char *name, void *data)
{
	return (new CombatWorld(name));
}

The ConstructWorld function simply creates a new instance of a CombatWorld in our application. At this point, you can try loading your world by typing “load rts” in the console window, but there is actually nothing to see at this point because we do not have any cameras set up in the world. You’ll end up seeing something like this:


Image:blankworld.jpg

Next Step

All right, the application is set up, and the world is created. Next step is to set up a camera. For this, continue on to Creating a Basic Navigation Camera

Personal tools