Open source game engine

02 Hello World

Contents

[ hide ]

  1. 1 Hello world

Tutorial 02

Hello world

This Section Describe how to add a new File to Solution and Initialize the KGE and Use KGE Scene Manager,also show the simple Source Code that Add a Camera and Animated Mesh Node.

First we need to add a source code to our solution,so in the <Solution Explorer> dialog , Right click on <Source Files> Directory and select <Add> and then select <New Item…>,in the Add new Item Window select <C++ Files(.cpp)> and in the <Name> text box choose a name for it like “main.cpp” , location is our project path and don’t change it.

Click <Add> Button.

After we have set up the Compiler, the compiler will know where to find the KGE Engine header files so we can include it now in our code.

#include <kge.h>

In the KGE Engine, everything can be found in the namespace ‘kge’. So if you want to use a class of the engine, you have to write kge:: before the name of the class.

There are 9 sub namespaces in the KGE Engine , Let’s take a look at them

kge::sn -> use for everything in Scene for example Static Mesh , Animated Mesh , Camera , Light, Sound and …

kge::gfx -> use for Graphics for example Renderer, Material, Shader,Color and …

kge::efx -> use for Effect for example Refraction , Mirror Plane , Shadow Plane and …

kge::core -> this name space have Timer and Functor Class…

kge::io -> use for Input/Output Classes for example Keyboard , Mouse , File , Joystick and …

kge::ph -> use for Physic feature of Engine for example Actor , Joint , Material , and …

kge::av -> use for Audio/Video Features.

kge::gui -> use for Graphic User Inteface for example Text Box , Button , Image and …

kge::math – >use for Mathematics for example Matrix , Vector and …

To get rid of the kge:: in front of the name of every class, we tell the compiler that we use that namespace from now on, and we will not have to write kge:: anymore.

using namespace kge;

To be able to use KGE.dll we need setup Compiler to link the KGE.lib , for this purpose we use a pragma comment lib for Visual Studio:

#pragma comment (lib, "kge.lib")

for our example source code we need use pointers to some class,first need a pointer to Device class of KGE.with this Pointer we can initialize the device and this class is most used class for get another class pointers.

Device*                pDevice;

another most used class is Scene Manager and Renderer , then create pointers for those:

sn::SceneManager*    pSceneManager;
gfx::Renderer*        pRenderer;

we declare new Pointer to sn::Camera in the our source code to control camera:

sn::Camera*            pCamera;

we want add an Animated Mesh in our scene then create a sn::AnimatedMesh pointer for it:

sn::AnimatedMesh*    pAnimatedMesh;

Always the start point of executing any source code in C and C++ programs is main() function:

int main()
{

now we initialize the Device and get Pointer of it:

pDevice = new Device();

If Engine can’t initialize the Device pointer the init function return false , otherwise return true.

if( !pDevice->Init() )
return 1;

In one of init function declaration it have 8 parameter and another it have 9 parameter , we use default parameters and don’t pass any parameter for call it.

Next step we can get the SceneManager and Renderer pointer from Device Class:

pSceneManager = pDevice->GetSceneManager();
pRenderer = pDevice->GetRenderer();

now we can add the camera to Scene , for this purpose write below source line to code:

pCamera = pSceneManager->AddCameraNode( math::Vector( 0.0 , 50.0 , -50.0 ) , math::Vector( 0.0 , 0.0 , 0.0) , math::Vector( 0.0 , 1.0 , 0.0 ) );

As you can see AddCameraNode take 3 parameter from pointer to math::Vector Class,First parameter is original position of the Camera,second parameter is where the camera look at , and third parameter show the up direction of the Camera. As I said before for adding anything in the scene we must take help from the SceneManager,the camera is the scene elements so we use the SceneManager for add it to scene.

kge::Vector class is very usefull , Vectors for three-dimensional and three-dimensional position in the scene are used,math::vector function parameter input values, respectively are length, width and depth or the X,Y and Z.

In the next step we Add an AnimatedMesh to the scene,it’s too easy:

pAnimatedMesh = pSceneManager->AddAnimatedMeshNode( "..\media\ninja\ninja.ms3d" );

AddAnimatedMeshNode function only take one parameter,namely is file name of 3d model.(in Downloadable sample of site you can found the media directory,copy this directory to current project path or in the function parameter use the location of 3d model files.) the Animated/Static Mesh is the scene elements so we must use the SceneManager for add it to scene.

now we can render the scene and show the result of our code:

while( pDevice->run() )
{
pSceneManager->RenderAll();
pRenderer->EndRendering();
}

In line 19 we check if the device is running and let the device to handle the window events.
In line 21 anything in the scene begin rendered and line 22 is end point of render progress.

return 0;
}
This page is a Wiki! Log in or register an account to edit.

Leave a comment for: "02 Hello World"