Sign in to follow this  
Roof Top Pew Wee

GUI development and scope issues

Recommended Posts

This may be somewhat of a lengthy post, but I'd like to get some good discussion on this topic. In short, I am attempting to remake the way messages are processed in my GUI, but the way I'm thinking of implementing seems to have some scope issues. I use C#, but I think that the theory spreads across other OO languages. First, let me explain how my loop is set up - yes, it is for a game. My main method is inside of my form class, and inside of my main loop I have the game loop. I have a management class for my GUI called GuiManager. Every frame, the GuiManager surveys my input objects (mouse, keyboard, joysticks), and the location and current state of the gui objects (windows, buttons, text boxes, list boxes - the window class being the base class). This information is stored in a class called GuiResult. The GuiResult contains a few bools keeping track of what the mouse buttons are doing, as well as references to windows, such as window the cursor is currently dragging, the window the cursor is over, and the window that has just been closed. Of course, the information held in the GuiResult is not limited to this. So, here's some simplified code:

public class GameForm : Form
{

  GuiManager guiManager = new GuiManager();
  GuiResult guiResult = new GuiResult();

  // create a button
  Button myButton = guiManager.AddButton();

  static void Main()
  {
     while(frm.Created)
     {
       guiManager.control(guiResult);
       
       if(guiResult.primaryClick && guiResult.windowResult == myButton)
       {
         // Do things with game data here
       }
     }
  }
}


I realize that generally, GUIs do not work this way. Usually they are message driven. So I want to do this with delegates and events. . . that is, call an event whenever the user does particular actions on a GUI object like clicking or dragging. By practice, I declare all of my game data in Main scope. If I'm going to use methods declared in other classes, these methods won't be able to access any of my Main game data. So, I ask, how would you suggest I go around this? Should all of my data be public in my object's (the form)'s scope? And have the methods declared inside of a class which can see the Main object? Although I've done a lot of coding, I've stuck to the same method and used the same templates for all of my games, so this is still somewhat new ground for me. Again, I'd like to get discussion on the subject, so feel free to post ideas, and pros and cons of each. I'm changing the way my engine's handled GUI. . . a way that I've been doing it for almost 2 years, so I want to be sure I get it right. --Vic--

Share this post


Link to post
Share on other sites
Why not just have GuiManager respond appopriately to the data it finds before passing it off to GuiResult? Even if you didn't want the GuiManager to respond immediately to some of the data it gather, you could just declare some of GuiManager's functions to be friend functions inside of the GuiResult class, allowing you to access it's private data any time you need. Even if you didn't want to do it that way at all, friend functions are an idea you might keep. It allows methods to access private data of classes.

Share this post


Link to post
Share on other sites
My first thought on getting a better access solution is to use container class exposing Game Data as public static members of the class. The data could be private with public methods to access, which is more pleasing solution from a design perspective.

Also, I dont know if you really want to have a loop running as fast as possible collecting input. you should think about capping the looping rate (like with a Timer to collect input every few milliseconds). The processor cycles you save could go into doing something more interesting. ALso the timer driven loop lets you slow down the game processing, perhaps for easier debugging.

A static game class for a checkers game might look like below

class gameData {

static private int RedCheckers, BlackCheckers;
static gameData(){
//static constructors sets up data that will never change.
//Checkers game always starts with same number of checkers
RedCheckers = 16;
BlackCheckers = 16;

}

//public method exposes game logic, not game data..
static public void RedJumpsBlack(int NumberOfJumps){
gameData.BlackCheckers = Math.Min(0,gameData.BlackCheckers-NumberOfJumps);

}

}

Maybe some other solutions, but most anyone I would use would definitely get the data in some container and out of the Main function.




Share this post


Link to post
Share on other sites
Alan,

Yes, you're right. I've also noticed that my main function gets cluttered and unorganized with all of the data in there.

I did think of this. Why not create a singleton containing all of my game data, and make it public (if it needs to be), then just have all methods that need to modify game data have a reference to this singleton. That would allow me to separate my logic to any class I wanted, letting me organize my code better, and of course, incorporate a better GUI method.

--Vic--

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this