-
Notifications
You must be signed in to change notification settings - Fork 30
Copy constructors assignment operators and graphic API objects
There are several classes which represent an easy interface to handle graphic API objects and content on the graphics card. These classes are Shader, ShaderStage, Mesh, Textures (Texture2D, ...), Framebuffer, etc. Now you might ask what happens when you use copy constructors or assignment operators on these classes. To put it simply: Everything is being copied. When I was developing these methods I asked myself when I wanted to use a copy constructor or an assignment operator. My answer to that question was that you only wanted to use these methods when you want a copy of the object. Or the other way around: When you don't want a copy you can always use a reference. Let's have an example:
// One common example would be that we want to pass a mesh as a function parameter.
// This is what you shouldn't do if you don't want to create a copy of the mesh:
void DoSomeStuffWithAMesh(Mesh mesh) {
}
// Use a reference instead to make sure the object isn't being duplicated:
void DoSomeStuffWithAMesh(Mesh &mesh) {
}
// Let's go a step further:
void AddStuffToScene(Scene* scene, Mesh mesh) {
for (int32_t i = 0; i < 100; i++)
scene->Add(new StaticMeshActor(mesh));
// Store the mesh somewhere to keep it in memory.
}
// Is using a copy constructor
auto mesh = Mesh();
// The following is really bad because the scene uses the pointer to the
// meshes to sort them properly. Because we're creating a new mesh for every
// call there are now 100 meshes in the scene
for (int32_t i = 0; i < 100; i++)
AddStuffToScene(&scene, mesh);
// Let's try it again:
void AddStuffToScene(Scene* scene, Mesh& mesh) {
for (int32_t i = 0; i < 100; i++)
scene->Add(new StaticMeshActor(mesh));
// Store the mesh somewhere to keep it in memory.
}
// Even better would be Mesh mesh(args);
auto& mesh = Mesh();
for (int32_t i = 0; i < 100; i++)
AddStuffToScene(&scene, mesh);
Please note that classes like the actors contain a pointer to the classes presented above. When using an assignment operator on an actor the mesh isn't copied, but the pointer to that mesh is.