This site is no longer active and is available for archival purposes only. Registration and login is disabled.

A question of technique


A question of technique

Postby Guest » Apr 18, 2006 @ 10:06pm

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.
Guest
 


Postby BradK3 » Apr 18, 2006 @ 10:18pm

Not that it matters, but I forgot to login... :oops:
User avatar
BradK3
pm Member
 
Posts: 19
Joined: May 27, 2003 @ 7:34pm
Location: Clearfield, Utah


Postby Kzinti » Apr 18, 2006 @ 11:34pm

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.
Kzinti
pm Member
 
Posts: 3238
Joined: Jan 13, 2002 @ 5:23am


Postby Andy » Apr 19, 2006 @ 6:46am

Andy
<font color=red size=3>Troll++</font>
 
Posts: 1288
Joined: Nov 1, 2003 @ 7:36am


Postby jaguard » Apr 19, 2006 @ 8:32am

jaguard
pm Member
 
Posts: 230
Joined: Mar 2, 2004 @ 6:45pm


Postby BradK3 » Apr 19, 2006 @ 2:14pm

User avatar
BradK3
pm Member
 
Posts: 19
Joined: May 27, 2003 @ 7:34pm
Location: Clearfield, Utah


Postby ml » Apr 19, 2006 @ 2:30pm

ml
pm Member
 
Posts: 30
Joined: Oct 7, 2005 @ 4:22pm


Postby fast_rx » Apr 20, 2006 @ 3:44am

User avatar
fast_rx
pm Member
 
Posts: 660
Joined: Jun 10, 2003 @ 4:24pm


Postby Kzinti » Apr 20, 2006 @ 4:16am

Kzinti
pm Member
 
Posts: 3238
Joined: Jan 13, 2002 @ 5:23am


Postby fast_rx » Apr 20, 2006 @ 4:47am

User avatar
fast_rx
pm Member
 
Posts: 660
Joined: Jun 10, 2003 @ 4:24pm


Postby fzammetti » Apr 20, 2006 @ 6:02am

...and so I said to Mr. Gates: "$640 billion should be enough for anyone!"
User avatar
fzammetti
pm Insider
 
Posts: 1496
Joined: Jun 4, 2002 @ 6:21pm
Location: Omnytex Technologies


Postby InexorableTash » Apr 27, 2006 @ 5:33am

User avatar
InexorableTash
pm Member
 
Posts: 99
Joined: Sep 13, 2002 @ 6:14am


Postby Conan » Apr 27, 2006 @ 4:30pm

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.
What is Best in Life ?
User avatar
Conan
pm Member
 
Posts: 1309
Joined: Dec 24, 2001 @ 5:16am
Location: the Shades, Ankh-Morpock


Postby BradK3 » Apr 27, 2006 @ 7:14pm

User avatar
BradK3
pm Member
 
Posts: 19
Joined: May 27, 2003 @ 7:34pm
Location: Clearfield, Utah


Postby Kzinti » Apr 27, 2006 @ 9:18pm

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).
Kzinti
pm Member
 
Posts: 3238
Joined: Jan 13, 2002 @ 5:23am


Return to PocketFrog & PocketHAL


Sort


Forum Description

SDKs for fast and robust device-independent access to Pocket PC display hardware.

Moderators:

sponge, Kzinti

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron