Choosing our engine type
Before we start programming our game, it is a good idea to come up with the performance needs of the game. AndEngine includes a few different types of engines we can choose to use, each with their own benefits. The benefits,of course, depend on the type of game we plan to create.
Getting ready
Carry out the Know the life cycle recipe in this chapter to get a basic AndEngine project set up in our IDE, then continue on to the How to do it... section.
How to do it…
In order for us to properly define a specific Engine
object for our game to use, we must override the onCreateEngine()
method, which is part of AndEngine's startup process. Add the following code to any base AndEngine activity in order to handle the Engine's creation manually:
/* The onCreateEngine method allows us to return a 'customized' Engine object * to the Activity which for the most part affects the way frame updates are * handled. Depending on the Engine object used, the overall feel of the * gameplay can alter drastically. */ @Override public Engine onCreateEngine(EngineOptions pEngineOptions) { return super.onCreateEngine(pEngineOptions); /* The returned super method above simply calls: return new Engine(pEngineOptions); */ }
How it works…
The following is an overview of the various Engine
objects available in AndEngine, as well as a brief code snippet displaying how to set up each of the Engine
objects:
Engine
: First and foremost, we have the ordinaryEngine
object. TheEngine
object is not ideal for most game development as it has absolutely no limitations in regards to frames per second. On two separate devices, it is very likely that you will notice differences in the speed of the game. One way to think of this is if two separate devices are watching a video which was started at the same time, the faster device is likely to finish the video first rather than both finishing at the same time. For this reason, noticeable issues can arise in devices which might not run as fast, especially when physics are a big part of the game. There are no extra steps involved in incorporating this type of engine into our game.FixedStepEngine
: The second type of engine we have at our disposal is theFixedStepEngine
. This is the ideal engine used in game development as it forces the game loop to update at a constant speed regardless of the device. This is done by updating the game based on the time passed rather than the device's ability to execute code faster.FixedStepEngine
requires us to pass theEngineOptions
object, as well as anint
value, in that order. Theint
value defines the number of steps per second that the engine will be forced to run at. The following code creates an engine that will run at a constant60
steps per second:@Override public Engine onCreateEngine(EngineOptions pEngineOptions) { // Create a fixed step engine updating at 60 steps per second return new FixedStepEngine(pEngineOptions, 60); }
LimitedFPSEngine
: TheLimitedFPSEngine
engine allows us to set a limit on the frames per second that the Engine will run at. This will cause the Engine to do some internal calculations, and if the difference between the preferred FPS is greater than the current FPS that the Engine is achieving, the Engine will wait a fraction of a second before proceeding with the next update.LimitedFPSEngine
requires two parameters in the constructor, including theEngineOptions
object and anint
value specifying the maximum frames per second. The following code creates an engine that will run at a maximum of 60 frames per second:@Override public Engine onCreateEngine(EngineOptions pEngineOptions) { // Create a limited FPS engine, which will run at a maximum of 60 FPS return new LimitedFPSEngine(pEngineOptions, 60); }
SingleSceneSplitScreenEngine
andDoubleSceneSplitScreenEngine
: TheSingleSceneSplitScreenEngine
engine andDoubleSceneSplitScreenEngine
engine allow us to create a game with two separate cameras, either with a single scene, most generally used for single player games, or two scenes for multiplayer games on a single device. These are just examples, however, but these two engine's can have a wide range of uses, including mini-maps, multiple perspectives, menu systems, and much more. See Chapter 4, Creating a Split-screen Game, for more specific details on setting up these types ofEngine
object.