How to approach porting a game engine to another platform?
I've run into this problem several times before and wanted to hear what other's experience and advice is. Assume you have a working and stable but relatively small game engine that works on only one platform, and you want to port it to another platform.
The first step is obvious: you take the code, link it to the platforms libraries instead of the old ones, make necessary changes to the project or target build settings and then hit build. About five to twenty thousand errors appear. Of course there are a lot of duplicates but it immediately raises the question what the next steps should be?
How do you approach porting a game engine to another platform, or any platform-specific code that can't just be compiled on the other platform due to inherent changes in system and API design? How do you wade through all these error? How do you identify the parts that sho开发者_开发技巧uld be approached first?
In general: how should i approach porting existing source code?
I'm looking for general advice on how to approach a source code port. Assume the programming language and compiler are the same on both platforms, so it's mostly API changes.
A textbook answer (perhaps) might be wrapping all of your platform-specific calls and using these wrapper functions throughout your code instead of the platform-specific. This way, if you can match the methods you use one-to-one on both platforms (easier said than done, maybe) then you can switch out which platform-specific function is called with your wrapper functions, and not change the rest of the logic in your code.
Say you have:
void RenderBadGuy(int badGuyID)
{
// Windows-specific bad-guy rendering code here
}
For now you can just write ("G_" is for generic)
void G_RenderBadGuy(int badGuyID)
{
RenderBadGuy(badGuyID);
}
This adds a small amount of overhead to your engine, but this shouldn't break things as you have them (just an extra function call).
Now, every place you use RenderBadGuy, just use G_RenderBadGuy. Rinse and repeat for all of your methods, and now later you can switch out your code to something like
void G_RenderBadGuy(int badGuyID)
{
// now we're rendering on a Mac
RenderBadGuy_Mac(badGuyID);
}
and then your main engine wouldn't change.
You probably can do this a lot nicer and more generically than this (pointers to functions, I don't know) but that's the idea.
Or, you could do a Java-like thing and have your library talk with a module that in turn knows the specifics of your platform. Just develop different modules (like VMs) for each platform, and you only develop your library once.
精彩评论