Handmade Hero»Forums»Code
Russell
2 posts
Reusable Platform Layer
Edited by Russell on
The current platform layer runs at the bottom of the call stack, and calls the game code itself. Making it into a reusable engine/library could work but it would be like GLUT or something where you'd have to write your game as a bunch of callbacks.

One alternative that I've used in the past is to write the platform layer as a bunch of functions (or classes) for creating the window and initializing the graphics context, polling for events, etc.

There seems to be a tradeoff here- the way Handmade Hero's platform layer works makes its implementation (and porting it) very straightforward, and features like the live code editing are very simple to add. This comes at the cost of giving up some control for the game if it were to be made into a library (or even as it is now, since some of those restrictions are to enable those features).

How would you write a reusable platform layer? Or is that not really worth doing?
Allen Webster
473 posts / 4 projects
Heyo
Reusable Platform Layer
I think this platform layer is reusable already. Basically reusable just means "the fact that this code is written now means that in a future project I will be able to save some time."

When you think about writing libraries you're trying to write something that serves some purpose correctly, right out of the box without tweaks. This platform layer is purposely not meant to be a shipping library. But the code is still reusable, because just copying it into new projects still saves time.
Andrew Chronister
194 posts / 2 projects
Developer, administrator, and style wrangler
Reusable Platform Layer
Casey touched on this in response to a comment, but the platform layer is intentionally the opposite of a library

A library provides some functions which a program can call when it wants to do specific things. The more complicated the API for this, the harder it is to know what is happening in your code at any given time.

The platform layer is the other way around - it's the entry point, and it treats the game more like a library (actually, literally like a library -- we build a dll of the game code).

And while in a sense we are writing callbacks for the platform layer, it doesn't look like there will ever be more than 2 functions that the platform layer expects to see, and those are very basic to the structure of a game. Any game will have a main loop where it updates and renders, and any game with sound will be outputting to a sound device. The structure of the game is left up to the implementation for the most part.
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
Reusable Platform Layer
Yes - I suspect that our eventual platform layer will be completely reusable for basically any game, except for the input part. You would want to expand the notion of input to encompass generic input from a wide variety of devices, rather than just "gamepad-like" input.

- Casey