3 Ways Leadwerks Game Engine 5 Makes Game Programming Easier
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
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.
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.
- 4
4 Comments
Recommended Comments