Páginas

domingo, 8 de maio de 2016

Games in C++ can't be that hard, right? - Code refactoring already.

It's been some time since last post. I've been tying pieces of code together as I programmed a way of rendering 3d meshes with Open GL. One awful point of the “learning-as-you-go” way of thinking is that, even if each small part is simpler to learn and understand, binding everything together can become a real problem as the communication between classes wasn't properly analyzed and decided upon.

So, you get pieces of code like this:

auto entity = entityPtr.lock();
entity->AddComponent(std::make_shared<Engine::ComponentReferenceStoringFactory<Engine::Transform>>(compFactories->GetFactory<Engine::Transform>()->Instantiate(), Engine::Global->GetComponentTypeMap()->GetTypeId<Engine::Transform>()));
entity->AddComponent(std::make_shared<Engine::ComponentReferenceStoringFactory<Engine::MeshFilter>>(compFactories->GetFactory<Engine::MeshFilter>()->Instantiate(), Engine::Global->GetComponentTypeMap()->GetTypeId<Engine::MeshFilter>()));
entity->AddComponent(std::make_shared<Engine::ComponentReferenceStoringFactory<Engine::MeshRenderer>>(compFactories->GetFactory<Engine::MeshRenderer>()->Instantiate(), Engine::Global->GetComponentTypeMap()->GetTypeId<Engine::MeshRenderer>()));
entity->AddComponent(std::make_shared<Engine::ComponentReferenceStoringFactory<Engine::SimpleControlBehavior>>(compFactories->GetFactory<Engine::SimpleControlBehavior>()->Instantiate(), Engine::Global->GetComponentTypeMap()->GetTypeId<Engine::SimpleControlBehavior>()));

A lot of time has been used to tie things together, as the snippet above can show. And another good deal of time will be needed to refactor and create utility classes and functions so that the system itself does not become painful.


Lesson learned: plan (more) before you code. C++ can become quite verbose.