A small step

21 Feb 2014

Just a quick and short update this time. After implementing the interfaces required to create and populate the first planet of the game, I went ahead and implemented step methods to K14Planet and K14Entity, the former of which will be responsible for updating the Box2D physics simulation, and calling the step methods of all active entities. The step methods on K14Entity are intended to be overridden by entity subclasses, and will contain entity-specific game logic such as handling collisions coming from Box2D callbacks, reacting to inputs, entity ‘AI’ such as deciding where and when to shoot, etc.

With the step methods in place, it felt like a good time to introduce a view controller to drive the game loop, and an associated view to render to. From an earlier OpenGL-based iOS side project I worked on long ago (iOS 3 era) I remembered having to mess around with custom view controllers, display links, custom Core Animation OpenGL layers, and lots of manual OpenGL initialization to even get just a working drawable surface, so I fully expected a long and frustrating time-to-first-triangle. Luckily, after some initial research, I found out Apple added a new framework called GLKit in iOS 5, which contains a view controller and view (GLKViewController and GLKView) that make it dead easy to create and initialize an OpenGL view with a delegate that implements a callback fired at regular intervals by the view controller. There really wasn’t anything more to it that subclassing GLKViewController and setting it as the the application window root view controller, and adding a renderer class that implements the GLKViewDelegate protocol.

The GLKViewController schedules a callback update that is called at a regular interval, which is intended to update game state. After this callback, its view delegate glkView:drawInRect selector is called, which is where all the OpenGL rendering code should go. I added a simple glClear with a random color to the view delegate render selector, some debug logging to the K14Entity step selectors to output their positions, and hooked up the K14Planet step method to the view controller by calling it from its update selector. The view controller was configured to update at a 1 second interval. Running the application in the simulator now produced a screen flashing random colors, and the following in the debug console:

2014-02-21 16:13:03.278 2k14: The Game[3401:70b] Planet update: 0.00 seconds
2014-02-21 16:13:03.280 2k14: The Game[3401:70b] Entity update: 4, (-4.08, 1.42)
2014-02-21 16:13:03.280 2k14: The Game[3401:70b] Entity update: 3, (4.00, 4.25)
2014-02-21 16:13:03.280 2k14: The Game[3401:70b] Entity update: 1, (-9.00, 10.00)
2014-02-21 16:13:03.281 2k14: The Game[3401:70b] Entity update: 2, (-9.00, 2.50)
2014-02-21 16:13:04.320 2k14: The Game[3401:70b] Planet update: 1.04 seconds
2014-02-21 16:13:04.321 2k14: The Game[3401:70b] Entity update: 4, (-4.08, 1.42)
2014-02-21 16:13:04.321 2k14: The Game[3401:70b] Entity update: 3, (4.00, 4.25)
2014-02-21 16:13:04.321 2k14: The Game[3401:70b] Entity update: 1, (-9.00, 8.00)
2014-02-21 16:13:04.322 2k14: The Game[3401:70b] Entity update: 2, (-9.00, 2.50)
2014-02-21 16:13:05.320 2k14: The Game[3401:70b] Planet update: 1.00 seconds
2014-02-21 16:13:05.321 2k14: The Game[3401:70b] Entity update: 4, (-4.08, 1.42)
2014-02-21 16:13:05.321 2k14: The Game[3401:70b] Entity update: 3, (4.00, 4.25)
2014-02-21 16:13:05.322 2k14: The Game[3401:70b] Entity update: 1, (-9.00, 6.00)
2014-02-21 16:13:05.322 2k14: The Game[3401:70b] Entity update: 2, (-9.00, 2.50)
2014-02-21 16:13:06.321 2k14: The Game[3401:70b] Planet update: 1.00 seconds
2014-02-21 16:13:06.322 2k14: The Game[3401:70b] Entity update: 4, (-4.08, 1.42)
2014-02-21 16:13:06.322 2k14: The Game[3401:70b] Entity update: 3, (4.00, 4.25)
2014-02-21 16:13:06.322 2k14: The Game[3401:70b] Entity update: 1, (-9.00, 4.00)
2014-02-21 16:13:06.323 2k14: The Game[3401:70b] Entity update: 2, (-9.00, 2.50)
2014-02-21 16:13:07.321 2k14: The Game[3401:70b] Planet update: 1.00 seconds
2014-02-21 16:13:07.322 2k14: The Game[3401:70b] Entity update: 4, (-4.08, 1.42)
2014-02-21 16:13:07.322 2k14: The Game[3401:70b] Entity update: 3, (4.00, 4.25)
2014-02-21 16:13:07.322 2k14: The Game[3401:70b] Entity update: 1, (-9.00, 3.76)
2014-02-21 16:13:07.323 2k14: The Game[3401:70b] Entity update: 2, (-9.00, 2.50)
2014-02-21 16:13:08.322 2k14: The Game[3401:70b] Planet update: 1.00 seconds
2014-02-21 16:13:08.323 2k14: The Game[3401:70b] Entity update: 4, (-4.08, 1.42)
2014-02-21 16:13:08.323 2k14: The Game[3401:70b] Entity update: 3, (4.00, 4.25)
2014-02-21 16:13:08.323 2k14: The Game[3401:70b] Entity update: 1, (-9.00, 3.76)
2014-02-21 16:13:08.324 2k14: The Game[3401:70b] Entity update: 2, (-9.00, 2.50)
2014-02-21 16:13:09.322 2k14: The Game[3401:70b] Planet update: 1.00 seconds

You can see the entity step selectors being called and updating and logging their positions. Entity number 1 is the player spaceship, which is initially positioned at some distance above the planet surface, and starts to fall until it hits the ground, which you can see from the decreasing y-coordinate. All other entities are static, so they don’t change.

Now that we have a view controller plus OpenGL view, a game loop, a planet surface, some entities and step methods tied to the game loop, the next step will be implementing a very simple initial renderer to see the game world on-screen in the simulator, instead of as a debug log. This is what I’ll be working on the coming week.

Development scoreboard

Most of the time spent since the last post went into reading up on GLKit, to actually add the step methods and the GLKViewController only took about 2 hours, which brings the total development time to approximately 8 hours. The SLOC count (source lines excluding comments and whitespace) comes in at about 300 lines.