Programming with C/C++

From Leadwerks Developer Wiki

Jump to: navigation, search

Contents

Introduction

Image:Helloworld.jpg

A "hello world" program has become the traditional first program that many people learn.

Hello World! Example - C

The classical Hello World! example is used to show the minimum effort to display a text saying "Hello World!" on the screen. In Leadwerks Engine 2 with C, it looks like this:

#include "engine.h"
int main(int argc, char** argv)
{
	Initialize();
	SetAppTitle("Hello World (window title)");
	Graphics(640,480);
	CreateWorld();
 
	while(!KeyHit(KEY_ESCAPE) && !AppTerminate())
	{
		DrawText(32,32,"Hello World!");
 
		UpdateWorld();
		Flip();
	}
	return Terminate();
}

Hello World! Example - C++

The classical Hello World! example is used to show the minimum effort to display a text saying "Hello World!" on the screen. In Leadwerks Engine 2 with C++ (LEO), it looks like this:

#include "leo.h"
using namespace LEO;
 
int main()
{
	Engine		engine;
	World		world;
	Camera		camera;
 
	engine.Create("Hello World (window title)",640,480);
	world.Create();
	camera.Create();
	while(!Keyboard::IsHit(KEY_ESCAPE))
	{
		world.Update();
		world.Render();
 
		Draw::Text(32,32,"Hello World!");
		Engine::Flip();
	}
	return 0;
}

Some more LEO examples

These are the same examples as those found in Programming with BlitzMax.

These are outdated (like: there's no more Window class in LEO, and other things changed since they were made),
if someone has time, please update them and remove this.

It's recommended to look at the video tutorials too, because they also cover all these areas, but more detailed.

Making a rotating 3D Cube

Now we can add a few lines more to display a rotating 3D cube on the screen. Note the lines with MoveEntity, they take as 1st parameter the entity, which is at the same time created, and as 2nd parameter the 3D vector which tells MoveEntity where to move the entity.

#include "leo.h"
using namespace LEO;
 
int main(int argc, char** argv)
{
	Engine    engine( "Rotating Cube Sample" ,640,480);
	World     world( CREATENOW ) ;
 
	Camera    camera( CREATENOW ) ;
	camera.Move( 0 ,0 ,-10 );
 
	Cube    cube( CREATENOW ) ;
	Buffer  buffer( 640,480 ) ;
 
	PointLight	light( CREATENOW ) ;
	light.Move(0,0,-3);
 
	while(!Keyboard::IsHit())
	{
		cube.Turn(1,1,2);
 
		buffer.Set();
		world .Render();
		buffer.Set(Engine::GetBackBuffer());
		world .RenderLights(buffer);
 
		Draw::Text(32,32,"Hello World!");
		Engine::Flip();
	}
    return 0;
}

Loading a 3D World

Now we could add 3 more lines to have our cube rotating inside a 3D map. Simply add:

Model model;
model .Set3DWLoaderScale();
model .Load( "abstract::tutorialroom1.3dw" );

You will also need to create this map using 3D World Studio, and put in under the Models directory in your Leadwerks Engine 2 directory. Any textures or materials you used in the map, can be put under a Materials directory (you can use any name you want, it will be automatically found). After a few more lines, we have already a decently looking 3D scene:

#include "leo.h"
using namespace LEO;
 
int main(int argc, char** argv)
{
	Engine		engine		;
	Window		window		;
	Camera		camera		;
	Keyboard	keyboard	;
	World		world		;
	Draw		draw		;
	PointLight	light		;
	Cube		cube		;
	Buffer		buffer		;
	Model		model		;
	Material	mat		;
 
	window.Create(1024,768);
	world .Create();
	buffer.Create(1024,768);
	camera.Create();
	light .Create(5);
	cube  .Create();
 
	model .Set3DWLoaderScale();
	model .Load( "abstract::tutorialroom1.3dw" );
	mat   .Load( "abstract::wood01.dds" );
	cube  .Paint( mat );
 
	camera.Move  (0,2,-1);
	camera.Turn  (45,0,0);
	light .Move  (0,1,-2);
 
	while(!keyboard.IsHit(KEY_ESCAPE))
	{
		cube  .Turn(1,1,2);
		buffer.Set();
		world .Render();
		buffer.Set(window.GetBackBuffer());
		world .RenderLights(buffer);
		draw  .Text(0,0,"Hello World!");
		world .Flip();
	}
	return 0;
}

Image:Tutorial3.png

Looking around

Before we continue with more hardcoded positions, lets get rid of the previously hardcoded positions and make sure that everything in future will be fully parametrizable from 3DWS maps. Our 3DWS map has 3 entities which act as position markers for our next example. The map has an point light entity, which has in it's properties set type=pointlight, a custom entity called playerstart, which has in it's properties type=playerstart, and a custom entity called playerlookat which has in it's properties type=playerlookat. Now we can read those values directly from the loaded 3DWS map. Finally we also read the mouse position to turn the camera around.

#include "leo.h"
using namespace LEO;
using namespace std;
 
int main(int argc, char** argv)
{
	Engine		engine		;
	Window		window		;
	Camera		camera		;
	Keyboard	keyboard	;
	World		world		;
	Draw		draw		;
	Cube		cube		;
	Buffer		buffer		;
	Scene		scene		;
	Material	mat		;
	Mouse		mouse		;
	Pivot		playerpos, playerlookat		   ;
	int		sx=1024, sy=768, cx=sx/2, cy=sy/2  ;
	flt		mx=0, my=0  	;
	TVec3		camrotation 	;
 
	window.Create(sx,sy);
	world .Create();
	buffer.Create(sx,sy);
	camera.Create();
	cube  .Create();
	playerpos   .Create();
	playerlookat.Create();
 
	world .Set3DWLoaderScale();
	scene .Load( "abstract::tutorialscene1.sbx" );
	int n = scene.GetCountChildren();
	string s;
	Entity e;
	PointLight pointlight;
	for(int i=1; i<=n; i++)
	{
		e = scene.GetChild(i);
		s = e.GetKey( "classname", "nothing" );
		if ( s == "light_point" )
		{
			pointlight.Create();
			pointlight.SetPosition( e.GetPosition() );
			e.Hide();
		} else
		if ( s == "player_start" )
		{
			playerpos.SetPosition( e.GetPosition() );
			e.Hide();
		} else
		if ( s == "player_lookat" )
		{
			playerlookat.SetPosition( e.GetPosition() );
			e.Hide();
		}
	}
 
	mat   .Load( "abstract::wood01.dds" );
	cube  .Paint( mat );
	cube  .Scale(Vec3(.1f));
 
	camera.SetPosition( playerpos.GetPosition() );
	camera.Point( playerlookat );
 
	mouse.Hide();
	mouse.Move(cx,cy);
 
	engine.SetFilters();
 
	while(!keyboard.IsHit(KEY_ESCAPE))
	{
		cube  .Turn(1,1,2);
 
		mx = Curve(mouse.GetX()-cx,mx,4.0f);
		my = Curve(mouse.GetY()-cy,my,4.0f);
		mouse.Move(cx,cy);
 
		camrotation = camera.GetRotation();
		camrotation.X += my*0.25f;
		camrotation.Y -= mx*0.25f;
		camera.SetRotation(camrotation);
 
		buffer.Set();
		world .Render();
		buffer.Set(window.GetBackBuffer());
		world .RenderLights(buffer);
		draw  .Text(0,0,"Hello World!");
		world .Flip();
	}
	return 0;
}

Moving around

Our final step in the Getting Started section of the Beginner tutorials is to make a player entity which can move and look around. This is done by adding a physics body, so the player moves realistically using acceleration, inertia, damping and collisions.

#include "leo.h"
using namespace LEO;
using namespace std;
 
int main(int argc, char** argv)
{
	Engine		engine		;
	Window		window		;
	Camera		camera		;
	Keyboard	keyboard	;
	World		world		;
	Draw		draw		;
	Cube		cubemesh	;
	Buffer		buffer		;
	Scene		scene		;
	Material	mat		;
	Mouse		mouse		;
	Pivot		playerpos, playerlookat		   ;
	int		sx=1024, sy=768, cx=sx/2, cy=sy/2  ;
	flt		mx=0, my=0  	;
	TVec3		camrotation , playerforce	   ;
	BodySphere	player	    	;
	BodyBox		cube	    	;
 
	window.Create(sx,sy);
	world .Create();
	buffer.Create(sx,sy);
	camera.Create();
	playerpos   .Create();
	playerlookat.Create();
 
	world .Set3DWLoaderScale();
	scene .Load( "abstract::tutorialscene1.sbx" );
	int n = scene.GetCountChildren();
	string s;
	Entity e;
	PointLight pointlight;
	for(int i=1; i<=n; i++)
	{
		e = scene.GetChild(i);
		s = e.GetKey( "classname", "nothing" );
		if ( s == "light_point" )
		{
			pointlight.Create();
			pointlight.SetPosition( e.GetPosition() );
			e.Hide();
		} else
		if ( s == "player_start" )
		{
			playerpos.SetPosition( e.GetPosition() );
			e.Hide();
		} else
		if ( s == "player_lookat" )
		{
			playerlookat.SetPosition( e.GetPosition() );
			e.Hide();
		} else
		if ( s == "nothing" ) // everything else should be static models
		{
			e.SetType(1);
		}
	}
 
	camera.SetPosition( playerpos.GetPosition() );
	camera.Point( playerlookat );
 
	player.Create(0.3);				// our player is a ball
	player.SetPosition(camera.GetPosition());	// put the player where the camera is
	player.SetGravity(0);				// zero gravity
	player.SetMass(1);				// give player some essence
 
	cube	.Create(.1,.1,.1);
	cubemesh.Create(cube);
	mat     .Load( "abstract::wood01.dds" );
	cubemesh.Paint( mat );
	cubemesh.Scale(Vec3(.1f));
 
	mouse.Hide();
	mouse.Move(cx,cy);
 
	player.SetType(1);
	cube  .SetType(1);				// our cube wants also to collide
	world .SetCollisions(1,1,SlidingCollision);	// make type 1 entities collide with other type 1 entities
 
	engine.SetFilters();
 
	while(!keyboard.IsHit(KEY_ESCAPE))
	{
		cube  .Turn(1,1,2);		// we can use Vec3 or 3 floats
 
		// Looking around
		mx = Curve(mouse.GetX()-cx,mx,4.0f);
		my = Curve(mouse.GetY()-cy,my,4.0f);
		mouse.Move(cx,cy);
 
		camrotation = camera.GetRotation();
		camrotation.X += my*0.25f;
		camrotation.Y -= mx*0.25f;
		camera.SetRotation(camrotation);
 
		// Moving around
		playerforce = Vec3( (keyboard.IsDown(KEY_D)-keyboard.IsDown(KEY_A)*2.0),
				    0,
				    (keyboard.IsDown(KEY_W)-keyboard.IsDown(KEY_S))*2.0);
		TFormVector( &playerforce, camera, NULL);
		player.AddForce( playerforce );
		camera.SetPosition(Vec3(
					 Curve(player.GetPosition().X, camera.GetPosition().X, 8.0),
					 Curve(player.GetPosition().Y, camera.GetPosition().Y, 8.0),
					 Curve(player.GetPosition().Z, camera.GetPosition().Z, 8.0)));
 
		world .Update();	 // physics bodies need to be updated
		buffer.Set();
		world .Render();
		buffer.Set(window.GetBackBuffer());
		world .RenderLights(buffer);
 
		draw  .Text(0,0,"Hello World!");
		world .Flip();
	}
	return 0;
}
Personal tools