How to design a game engine in an object-oriented language? [closed]

Whenever I try and write a game in any object-oriented language, the first problem I always face (after thinking about what kind of game to write) is how to design the engine. Even if I'm using existing libraries or frameworks like SDL, I still find myself having to make certain decisions for every game, like whether to use a state machine to manage menus, what kind of class to use for resource loading, etc.

What is a good design and how would it be implemented? What are some tradeoffs that have to be made and their pros/cons?

• What's wrong with going on the impulse, and refactoring from there rather than suffering analysis paralysis? – The Communist Duck Feb 13 '11 at 9:54
• @TheCommunistDuck Because going on impulse is the approach I've taken in all of my previous projects-- and every single one hits a wall after a few months when I find that any new feature requires monumental effort and complexity to add. Right now I spend more time rewriting my engines than I do writing the game itself, so I'm hoping that by with a little forethough and planning I'll save myself time in the long run. – extropic-engine Feb 13 '11 at 9:59
• @chuzzum, good point. One thing I would recommend then is to check out the C4 engine's architecture, that is; terathon.com/c4engine/images/architecture.png It may be a lot higher-level than you need, but might give you some ideas ;-) – The Communist Duck Feb 13 '11 at 10:01
• i.imgur.com/81zIY.png – The Communist Duck Feb 13 '11 at 10:05
• Also this question is kind of vague. Perhaps take one of your examples and make that a deeper question or two. – Tetrad Feb 13 '11 at 10:48

I doubt somebody is going to be able to say 'You have to do this and that and this and this slots with that using pattern X'.

However, some useful resources:
Enginuity - a series of engine building articles on Gamedev.net.
Game Coding Complete - I own this book, and it goes over every (well, almost) aspect of game programming well. It also does have an engine built throughout the book.
Game Engine Architecture - This is another great book for engine design.
C4 Engine Layout - Taken from my comment, but this shows a high-level way of fitting each part of the engine together.

These may be a little too much for what you need, but you can't know too much about something, and I'm sure you'll get a good plan from them.

EDIT: I forgot the Gamedev articles have been archived since the new site, fixed :)

• The Enginuity link was broken, and googling the articles seemed to show that they are no longer on the web. (?) The books look like good resources though, and I'm always keeping an eye out for more programming books ;) – extropic-engine Feb 13 '11 at 10:19
• Also, in regards to your first comment, I'm not expecting anyone to come up with a master plan that fits every game. I just noticed, over the course of developing a few games, that common patterns do tend to come up a lot, so I wondered what other people used in their games. – extropic-engine Feb 13 '11 at 10:20
• Fixed the link. – The Communist Duck Feb 13 '11 at 10:23
• +1 for enginuity. @chuzzum Just examine a couple of game engines, let them inspire you and derive the optimal architecture for yourself. Plus: It is often better to make your game engine component based than hierarchical, see cowboyprogramming.com/2007/01/05/evolve-your-heirachy – Dave O. Feb 13 '11 at 15:02
• I wouldn't say that it's the engine that needs to be aggregated, more the entity framework part. – The Communist Duck Feb 13 '11 at 15:12

As an example, here's how my current roguelike project is structured (in Java). It is using a 2D graphics engine so a lot of the rendering code was already taken care of for me. Criticism is welcomed.

class Game
This class sets up the state machine that manages the current state of the game. (in a menu vs. starting a new game vs. playing a saved game)

interface State
Each State class contains two loops: a loop for updating the logic and a loop for rendering. They also contain code for calling the Game class and requesting a change to a different state.

class ResourceManager
A singleton that is initialized by the Game class that loads all the needed resources and allows access to them. I don't like this design because it makes it hard to load/unload resources on different levels, for example. I would probably design this differently if I were starting over.

class Map
A map contains an array of tiles and a list of all the creatures and items on the map. It's a pretty basic class.

class Creature
Creatures contain information about themselves including movement calculations (requiring them to know which Map they're in, and and to be able to query it to find out about obstacles). Deciding whether to do this, or have some kind of manager class take care of it for all the creatures is something I struggle with.

interface AITask
Creatures can have a list of AITasks, which are executed every time the creature's logic loop is run. The AITask has its own logic loop that issues commands to the creature, and a termination condition that determines if the task was completed successfully or not.

interface UIElement
I implemented my own UI for this engine. Each UIElement has a rendering loop and a logic loop. They also have a loop for processing keyboard/mouse input. All elements can have a number of child elements, which are rendered after their parents, and take over the keyboard/mouse input. This lets you have menus with submenus, for example.

• What exactly is going wrong with this? It seems perfectly fine to me. – The Communist Duck Feb 13 '11 at 10:02
• @TheCommunistDuck It doesn't really come through in the examples I picked, but I do have a lot of problems with this code. The ResourceManager class is one of them, but I have with trouble with states too-- I end up with a huge proliferation of them, and copy a lot of code. Especially in an RPG, where the player has lots of choices at any one time, you can end up with really complex state graphs. Example: casting a spell. Goes from NormalState -> SelectSpellState -> SelectTargetState -> InvalidTargetState (if it failed) -> DoSpellAnimationState -> NormalState. And that's just one action. – extropic-engine Feb 13 '11 at 10:13
• No. NO. NO. NOPE. Please no. Oh wait you said you don't like it. – Bartek Banachewicz Dec 12 '13 at 14:31

The first important point to make is that there is no one 'good' answer to this question.

The closest thing to a right answer would be something like: It very much depends on the type of game, target platform, constraints (time) etc.

That said there are some really good articles out there that will show you how other people have tried to answer this problem (as i have tried to find info on this in the past).
As The communist duck mentioned the enginuity article on game dev helped me understand some parts of game architecture.

My current design is a hybrid of Quake3/Doom3 and a little bit of the .NET class library :)

I have two libraries (static or dynamic depends on how you want to build/deliver) the Framework and the Library.

The Library contains all helper classes which are there to help with the production of game software but aren't limited to this kind of product. ie it has an implementation of a linked list which is optimized for game code but could be used by anything which needs the service of a linked list.

The Framework is the guts of the 'engine' if you want to call it that. A lot of this follows Quake3's design philosophies (just in a more object orientated way). It contains the CLI, timing management, OS specific code, and eventually networking layers etc.

These two are then linked against the actual app that is being produced. The Game if you like, which contains the game specific code. In much the same way Quake3 loads DLL's depending on which 'mod' is being played.

To give you an idea of structure here is a quick breakdown of folders and contents for each lib:

• Framework
• IO (Specialist file management classes, Text Printing classes (eg to the CLI), and logging etc.)
• Network
• Client (classes which represent what the Framework considers to be a 'person playing/connected to the game')
• Server (classes to manage connection into the framework and manage the player(s))
• Platform (Keyboard/mouse/controllers handling classes, OS specific routines like getTime())
• System (very low level classes like an error class to aid printing of error messages, Timing classes, and the CLI itself.)
• Renderer (self explanatory)
• etc.

• Library
• Collections (classes which represent collections of data, linked lists/hashtables etc.)
• Math (basic math helper classes like Vectors and matrices)
• etc.

HTH! Should give you some pointers...

Things to consider

• Object oriented languages have problems because they usually don't have first class functions or not all data is object (like integer or float in Java). Design patterns adress these problems with several patterns. It is generally faster to code and easier to mantain to use language which can do them (first class objects); for example Python (which also allows object oriented design), You will have slower speed thuough.
• Event Calculus, to AI at least
• Hoare logic, use preconditions and postconditions to at least test Your code
• Agents, look at Quake entities
• Relational Databases, powerful way to store data

Good design

• make ER diagram
• make it correct
• create your database, objects or data strutures from it

Data is key to programming. If You desing Your data good, algorithm usually emerge from them (if You don't count some numerical algorithms, like computing determinant).

• -1 since this answer is very vague and confusing. Relational databases have absolutely nothing to do with an OO-engine. I can understand that English is not your first language, but could you possibly explain what you mean in your first paragraph? It seems contradictory (OO languages have problems but it's easier to program in languages with design patterns..even though design patterns are nearly always OO structures). – The Communist Duck Jul 3 '11 at 17:00
• @duck Contradictory? OO have problems which does not exist in other languages, DP solve them see c2.com/cgi/wiki?DesignPatternsInDynamicProgramming . – user712092 Jul 3 '11 at 17:45
• @Duck 1) You can use SQL in C++ 2) You can infer attributes of objects from ER (althought it is not recommended practice) 3) You can data structeres from relations (this is a list because I need to reorder the elemenents at willm this is a hash because I need fast lookup) – user712092 Jul 3 '11 at 17:47
• @Duck I apologize, I made mistake by reordering. I did not wanted to claim "DP are easier to XY" but "languages who can do first class are...". :) – user712092 Jul 3 '11 at 17:51
• Yes, there may be advantages to functional languages. However, even impartially, I feel an OO approach makes logical sense from a gamedev perspective. Also, there is no reason you need a relational database anywhere. Sure, they can be useful. It does not, however, become a needed component anywhere. – The Communist Duck Jul 3 '11 at 17:53