PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Code Organization - Classes, etc.

Hello,

I'm curious how most people organize their code as they write a GapiDraw application. For example, do you create a separate class for each screen of the application (main screen, settings screen, how to play screen, etc.). Or, do you group your classes by functionality (drawing, sounds, etc.)?

Better yet, if someone could give a brief outline of the class structure of their GapiDraw application, it would be most appreciated!

Thanks!
:D
InexorableTash · 12 Aug 2008, 07:10 UTC
(Note: I'm describing a hobby project, which is not and never will be finished, but it's a pretty good architecture.)

I ended up with a pattern similar to one outlined in... darn, not finding it. Game Programming Gems 3 - possibly called "Layers" there? Anyway, I called mine "Modules".

Everything in my app is done with vaguely COM-like interfaces to hide implementations as much as possible. A module just needs to implement IGameModule, which mostly handles rendering, and is derived from IGameInputClient which handles events; the implementation can be completely distinct.

The main app derives from GapiApplication and deals with managing the IGameModule stack. Modules can be pushed/popped, or the entire stack can be swapped. Rendering happens top-down through the stack, events go bottom-up.

One implementation of IGameModule is DialogModule which handles collections of UI widgets (buttons, labels, spinners, etc). It is subclassed by specific dialogs, and handles dynamic layout of the controls (e.g. for different display sizes, orientations, etc). Another module is the TerrainModule, for voxel-based rendering of outdoor environments. Another is a 3D polygonal rendering module. I use subclasses of these for distinct behavior; as much as possible is data driven.

The app starts up and instantiates a 3D "background animation" module, then layers a DialogModule-based menu on top of that - so you get a lovely animation playing "underneath" a translucent menu (along with a soundtrack). As you drill into the menus, additional dialogs are pushed onto the layer stack. (There is logic in the rendering delegation so that covered dialogs don't render, although they're still present and ending a dialog pops from the module stack.)

Once you enter the game, the root module is swapped for a game module (the departing module shuts down its soundtrack, the new module starts up its soundtrack). If you change settings during the game, yet another dialog module is pushed onto the module stack; it actually suppresses *events* (including time ticks) from going to the base game module but permits rendering, so the translucent settings dialog (the same code as the settings accessible from the intro menu) floats above the frozen action. When play is completed, another instance of the menu module swapped back into place on the stack.

Resources (bitmaps, sounds) are requested via unique identifiers from a resource manager class which checks to see if they're already loaded, loads them if necessary, and returns a ref-counted smart pointer.
Thanks for the very detailed explanation of your environment. That sounds like a very reasonable approach. I appreciate it!
:D
InexorableTash · 10 Nov 2008, 04:53 UTC
BTW, I finally posted a video of my hobby project on teh interwebs:

http://www.youtube.com/watch?v=Yjmn7WTLZPY
Wow, that's awesome! You are miles above me! Congratulations on your project....looks great! :)