Game Development Reference
In-Depth Information
#elif PLATFORM_IOS
#include "iOSEnemy.h"
using Enemy = iOSEnemy;
#endif
}
EnemyBase* CreateEnemy(EnemyType enemyType, const uint32_t serializableId)
{
Enemy* pEnemy = nullptr;
switch (enemyType)
{
case EnemyType::Dragon:
pEnemy = new Enemy(EnemyType::Dragon, serializableId);
break;
case EnemyType::Orc:
pEnemy = new Enemy(EnemyType::Orc, serializableId);
break;
default:
assert(false); // Unknown enemy type
break;
}
return pEnemy;
}
The CreateEnemy function itself has only changed in one way. Its return type is now EnemyBase rather
than Enemy . This is the case because I've used a type alias to have the Enemy keyword map to the
correct platform-specific Enemy version. You can see this in action in the unnamed namespace before
the function. I check each platform definition, include the appropriate header, and finally add using
Enemy = to set the type alias to the correct type.
The Factory pattern is the perfect method to use when you need to implement platform-specific
versions of classes. The Factory allows you to hide the implementation details of the creation of
objects from the rest of your program. This leads to code that is easier to maintain and reduces the
number of places in your codebase where you have to change things around to add new platforms.
Reducing the time to port to a new platform could be a lucrative business opportunity and open up
new potential revenue streams for your company.
Summary
This brings us to the end of your introduction to game programming in C++. This topic has walked
you through the main programming paradigms in C++ from procedural programming to object-
oriented programming through to generic programming. You've also seen how the STL can provide
you with ready-made data structures as well as algorithms to work on and with those structures.
The last part of this topic has introduced you to some topics that will be important for you to
learn as you go through your game development career. Mastering managing memory, concurrent
programming, and higher level topics such as design patterns will ensure that you and your
programs can adapt to whatever the future brings.
 
Search WWH ::




Custom Search