Engine initialization - Linderdaum Engine

Engine initialization

From Linderdaum Engine

Jump to: navigation, search

After you have finished setting up Visual Studio you want to initialize the engine. Creating applications that don't use scene sorting is very simple.

Initialization code

First, let us create a new Environment for the engine and run the application loop:

#include "Linderdaum.h"
sEnvironment* Env = NULL;
   Env = new sEnvironment();
   Env->DeployDefaultEnvironment( NULL, "../../CommonMedia" );
   Env->RunApplication( DEFAULT_CONSOLE_AUTOEXEC );

Now you have a black window 800x600 with a FPS counter in top right corner.

Engine functionality is accessible through sEnvironment structure (see API reference http://www.linderdaum.com/Docs/HTML/index.html). Let's use it to render a simple cube. We need some rendering code:

LMatrix4              Projection;
clVirtualTrackball    Trackball;
clGameCamera*         Camera = NULL;
clScene*              Scene  = NULL;
void DrawOverlay(LEvent Event, const LEventArgs& Args)
   // update camera transform & projection
   Scene->SetCameraTransform( Trackball.GetRotationMatrix() * Camera->GetCamera().GetModelViewMatrix() );
   Scene->SetCameraProjection( Projection );
   // render scene
   Scene->SetUseOffscreenBuffer( false, false );
   // update trackball
   bool MousePressedL = Env->Console->IsKeyPressed( LK_LBUTTON );
   Env->Viewport->UpdateTrackball( &Trackball, 10.0f, MousePressedL );

and we need to initialize Projection, ModelView and Mesh before entering application loop:

// set projection
Projection = Math::Perspective( 45.0f, Env->Viewport->GetAspectRatio(), 0.4f, 2000.0f );
// receive events from the Engine
Camera = Env->Linker->Instantiate( "clGameCamera" );
Camera->GetCamera().SetPosition( LVector3(0,-10,10) );
CONNECTOBJ( L_EVENT_TIMER, &clGameCamera::Event_TIMER, Camera );
Env->Connect( L_EVENT_DRAWOVERLAY, Utils::Bind( &DrawOverlay ) );
Env->Connect( L_EVENT_TIMER,       Utils::Bind( &Update      ) );
// create scene
Scene = Env->Linker->Instantiate("clScene");
// add geom object to the scene
int ID = Scene->AddGeom( Env->Resources->CreateIcosahedron( 3.0f, LVector3(0) ) );
// set material
clMaterial* Mtl = Env->Resources->CreateMaterial();
Mtl->SetPropertyValue( "DiffuseColor", "1.0 0.0 0.0 0" );
Mtl->SetPropertyValue( "CastShadow",   "false" );
Scene->SetMtl( ID, Mtl );
// set position of the object
Scene->SetLocalTransform( ID, LMatrix4::GetTranslateMatrix( LVector3(  0.0f, 0.0f, 0.0f ) ) );

Default material is the very basic component of engine's material system allowing you to instantly add simple shading to the model.

You have to tell the engine to use DrawOverlay() as an event callback. Place the following code before and after main loop:

Env->Connect( L_EVENT_DRAWOVERLAY, Utils::Bind( &DrawOverlay ) );

That's it.

P.S. If you want to use the engine like an OpenGL wrapper you can place your OpenGL commands in DrawOverlay() function.

Window configuration

Run the application and press TAB. You should see engine's console with a red warning message in the first line "StdConfig\System.cfg" not found. The engine initialization is data-driven. To specify parameters of the window you have to create a system configuration file: Data\StdConfig\System.cfg and place the following lines there:

  Set Viewport.Width   1600
  Set Viewport.Height  1200

If you run the application again your window will be 1600x1200 and that warning will be gone. There is a similar warning complaining about Data\StdConfig\Console.cfg file. To remove that warning just create an empty Data\StdConfig\Console.cfg

The difference between System.cfg and Console.cfg is that the first one is executed within clDeployer::DeployDefaultEnvironment() to initialize basic parameters of the engine and the second is used in clDeployer::RunApplication() to configure user parameters. You cann't use any commands except Set in System.cfg because engine's subsystems are not yet initialized.