Page 1 of 1

A question of technique

PostPosted: Apr 18, 2006 @ 10:06pm
by Guest
Hi, y'all,

I'm going to admit my serious lack of experience here, but I need some help. I'm trying to use the PocketFrog Game framework to write a simple game. Although this isn't my first project using PocketFrog, it is more complicated than any of my others and I'm having trouble figuring out how to organize my code.

I think I understand the basics: Most of my processing should be done in GameLoop and I write functions for ButtonDown, ButtonUP, StylusDown, etc that reacts to the various inputs. That's all well and good.

However - My game is going to have different modes. For instance: Main menu, Introduction screen, Map displays, Information displays, Next Turn prompts, etc. How do I keep track of what mode I'm in and what my game should be doing at any particular time? Let me try to explain myself with an example:

If the game is in the main menu mode and the stylus is pressed on the screen, the game will need to decide if it was pressed inside of a menu item and respond appropriately.

However, if the game is displaying the introduction and the stylus is pressed on the screen, the display will need to change to the next screen in the introduction.

How do I handle the different modes in this situation? For instance, how do I get my StylusDown function to act according to the current game mode? I have thought of two solutions.

Solution 1: Have a variable that keeps track of the current mode and put a switch statement in my StylusDown function. Have the switch statement call other functions (like MainMenuStylusDown, IntroScreenStylusDown, etc.) to execute mode-specific code.

Solution 2: Create a CMode class with a virtual StylusDown function. Then I could derive specialized child classes (CMainMenu, CIntroScreen, etc) for each mode. I could then create a CMode pointer that points to the current game mode object and dynamically create game mode objects as they're needed. Then (assuming my CMode pointer is called pCurrentMode) in my main StylusDown function, I could just call pCurrentMode->StylusDown () to execute mode-specific code.

Solution 1 seems more intuitive and less likely to cause memory errors, but could make organizing my code difficult. I would need to have a separate StylusDown function for each mode. Extending this to the other input functions (StylusMove, ButtonDown, etc), there could be a real problem.

Solution 2 makes code organization fairly simple. I can put each mode class in its own source file. However, is the overhead of virtual functions something I need to worry about? Is this solution introducing more problems than it's fixing?

I guess I just need advice from someone wiser than I. What do the rest of you do in your games? If any of you can make sense of what I'm trying to ask, please help me.

Thanks.

PostPosted: Apr 18, 2006 @ 10:18pm
by BradK3
Not that it matters, but I forgot to login... :oops:

PostPosted: Apr 18, 2006 @ 11:34pm
by Kzinti
I would go with solution 2. I don't understand this fear of virtual functions that people seem to have around here. It's irrelevant. The cost of that virtual is no more (and probably less) then the cost of using a switch() statement.

PostPosted: Apr 19, 2006 @ 6:46am
by Andy

PostPosted: Apr 19, 2006 @ 8:32am
by jaguard

PostPosted: Apr 19, 2006 @ 2:14pm
by BradK3

PostPosted: Apr 19, 2006 @ 2:30pm
by ml

PostPosted: Apr 20, 2006 @ 3:44am
by fast_rx

PostPosted: Apr 20, 2006 @ 4:16am
by Kzinti

PostPosted: Apr 20, 2006 @ 4:47am
by fast_rx

PostPosted: Apr 20, 2006 @ 6:02am
by fzammetti

PostPosted: Apr 27, 2006 @ 5:33am
by InexorableTash

PostPosted: Apr 27, 2006 @ 4:30pm
by Conan
does anyone have a simple worked example? I have to admit to using 'method 1' and have not seen the virtual function approach in action so I'm not sure how it works.

PostPosted: Apr 27, 2006 @ 7:14pm
by BradK3

PostPosted: Apr 27, 2006 @ 9:18pm
by Kzinti
I think you pretty much nailed down the idea behind method #2. This is extremely flexible as it allows you to change the framework depending on the current mode (context).