Jump to content

3 Ways Leadwerks Game Engine 5 Makes Game Programming Easier

Josh

1,118 views

Leadwerks Game Engine 5 moves Leadwerks forward into the future with massive performance increases and more advanced features, it also makes game development easier than ever before with three great new programming features.

Shared Pointers

I could write a whole article about the benefits of shared pointers.  Shared pointers are basically a simple form of garbage collection that relieves you from the need to manually delete objects, but doesn't suffer from the slow speed of full garbage collection.like C# uses.

In Leadwerks 4 we need to keep track of object reference counts by using the Release command when we are done with them:

Texture *texture = Texture::Load("Materials/Bricks/brick01.tex");
Material *material = Material::Create()
material->SetTexture(texture);
Model *model = Model::Box();
box->SetMaterial(material);
material->Release();
if (texture) texture->Release();

In Leadwerks 5 we never have to worry about calling Release() or (less commonly used) AddRef() because reference counts of shared pointers are tracked automatically.  The code below would cause a memory leak in Leadwerks 4 but is perfectly fine to use in Leadwerks 5:

auto material = Material::Create()
material->SetTexture(Texture::Load("Materials/Bricks/brick01.tex"));
auto model = Model::Box();
box->SetMaterial(material);

This even works with entities.  As soon as a variable goes out of scope the object is deleted:

if (true)
{
	auto model = Model::Box();
}// automatic deletion occurs here

1303-and-itsgone.jpg.cb17acc011c706142b7d8cf55eecd733.jpg

We can prevent deletion of an object by either keeping the variable in our code, or keeping a container that holds it.  In the case of map loading, the Map:Load() will return a structure that contains all the entity handles so they stay in memory.  Want to delete a camera?  It's easy:

camera = NULL;// poof!

There are still some details to work out but so far this approach is working great.

Another great benefit of shared pointers is that you can completely eliminate the need for big complicated destructors like this one.  All those objects will be automatically deleted when they go out of scope:

OpenGLCamera::~OpenGLCamera()
{
	for (int i=0; i<8; ++i)
	{
		if (shader_ambient[i])
		{
			shader_ambient[i]->Release();
			shader_ambient[i]=NULL;
		}
		for (int n = 0; n < 2; ++n)
		{
			for (int q = 0; q < 2; ++q)
			{
				for (int p = 0; p < 2; ++p)
				{
					if (shader_directional[i][n][q][p])
					{
						shader_directional[i][n][q][p]->Release();
						shader_directional[i][n][q][p] = NULL;
					}
					if (shader_directional_volume[i][n][q][p])
					{
						shader_directional_volume[i][n][q][p]->Release();
						shader_directional_volume[i][n][q][p] = NULL;
					}
					if (shader_point[i][n][q][p])
					{
						shader_point[i][n][q][p]->Release();
						shader_point[i][n][q][p] = NULL;
					}
					if (shader_point_volume[i][n][q][p])
					{
						shader_point_volume[i][n][q][p]->Release();
						shader_point_volume[i][n][q][p] = NULL;
					}
					if (shader_spot_volume[i][n][q][p])
					{
						shader_spot_volume[i][n][q][p]->Release();
						shader_spot_volume[i][n][q][p] = NULL;
					}
					if (shader_environment[i][n][q][p])
					{
						shader_environment[i][n][q][p]->Release();
						shader_environment[i][n][q][p] = NULL;
					}
					for (int usedecal = 0; usedecal < 2; usedecal++)
					{
						if (shader_spot[i][n][q][usedecal][p])
						{
							shader_spot[i][n][q][usedecal][p]->Release();
							shader_spot[i][n][q][usedecal][p] = NULL;
						}
					}
				}
			}
		}
	}
}

That entire function has been commented out in Leadwerks Game Engine 5. :D

Finally, shared pointers eliminate a problem that is the bane of all programmers' existence.  When used correctly, you can you can say goodbye to invalid and uninitialized pointers forever!  Yet shared pointers, unlike garbage collection, are fast to use and don't slow your game down.

Automatic Typing

The use of C++11 in Leadwerks Game Engine 5 gives us automatic typing of variables with the auto keyword.  We can simply something like this:

shared_ptr<Model> model = Model::Box();

To just this:

auto model = Model::Box();

If you have long complicated type names this makes life a million times easier:

for (std::list<shared_ptr<Entity> >::iterator it = list.begin(); it!= list.end(); it++)
{
	shared_ptr<Entity> entity = *it;
	entity->SetPosition(1,2,3);
}

Here's the same code simplified with auto (and range-based loops thanks to @Roland and @thehankinator.  Look how much more readable it is:

for (auto entity : list)
{
	entity->SetPosition(1,2,3);
}

This isn't completely new, but Leadwerks 5 is the first version built exclusively for C++11 features.

More Explicit API

Leadwerks 4 uses several bound states to store a current world and context.  These are global variables that are mostly invisible to the user.  This causes problems with multithreaded programming because two threads might try to change or access the bound state at the same time.

World *world1 = World::Create();
Model *model1 = Model::Box();

World *world2 = World::Create();
Model *model2 = Model::Box();
World::SetCurrent(world1);

Model *model3 = Model::Box();

Quick, which world does "model3" belong to?  Leadwerks 5 gets rid of the concept of bound states and requires you to explicitly specify the object you want to use.  Here's how the above code would look in Leadwerks 5:

auto world1 = World::Create();
auto model1 = Model::Box(world1);

auto world2 = World::Create();
auto model2 = Model::Box(world2);

auto model3 = Model::Box(world1);

Not only is the second example easier to understand, it's also one line shorter.

In a similar manner, the idea of a "current" context will be eliminated.  When you render a world you will need to explicitly specify which context to render to:

world->Render(context)

These programming features will make it easier than ever to code games with Leadwerks.

  • Upvote 5


4 Comments


Recommended Comments

Looks great 

This one could be even more simple

for each( auto entity in list )
{
    entity->SetPosition(1,2,3);
}

instead of 

for (auto it = list.begin(); it!= list.end(); it++)
{
    auto entity = *it;
    entity->SetPosition(1,2,3);
}

 

Share this comment


Link to comment
21 minutes ago, Roland said:

Looks great 

This one could be even more simple


for each( auto entity in list )
{
    entity->SetPosition(1,2,3);
}

 

Is that a Visual Studio only feature?

Share this comment


Link to comment
27 minutes ago, Josh said:

Is that a Visual Studio only feature?

Ranged based for loops were added to the C++ standard in c++11, not a MSVC only thing. I've not seen the exact syntax that Roland used but this is how I do it in MSVC 2015  and GCC.

for (auto&& entity : list)
{
	entity->SetPosition(1,2,3);
}

( auto&& ensures you get a reference and no copies are made)

http://en.cppreference.com/w/cpp/language/range-for

https://stackoverflow.com/questions/26991393/what-does-auto-e-do-in-range-based-for-loops

  • Upvote 2

Share this comment


Link to comment
2 hours ago, thehankinator said:

Ranged based for loops were added to the C++ standard in c++11, not a MSVC only thing. I've not seen the exact syntax that Roland used but this is how I do it in MSVC 2015  and GCC.


for (auto&& entity : list)
{
	entity->SetPosition(1,2,3);
}

( auto&& ensures you get a reference and no copies are made)

http://en.cppreference.com/w/cpp/language/range-for

https://stackoverflow.com/questions/26991393/what-does-auto-e-do-in-range-based-for-loops

Yes that is even better. Will change my loops to that

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Create Your Account

Sign in

Already have an account? Sign in here.

Sign In Now
×