Get the latest Education e-news
 

Go Back   Game Career Guide Forums > Programming
Forum Home Register Members List Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 10-13-2009, 02:33 PM   #1
tks2103
Junior Member

Activity Longevity
0/20 13/20
Today Posts
0/11 ssssssss3
Default ridding myself of global variables

Hey everyone!

In reading a lot of the free tutorials available on OpenGL and Windows programming, I see a lot of people rely on the use of global variables. For example, check out the NeHe tutorials: http://nehe.gamedev.net/data/lessons....asp?lesson=07.

You can see that, to check for keyboard input, he has a global array of booleans called 'keys' that he uses to check when a key has been pressed. That doesn't seem a valid solution when inputs and game state variables become more complex, as they are in some of the games that I'm programming.

What's the best way to avoid implementing things like input and state variables without resorting to global declarations in C++?

I'm working with OpenGL and C++ in Windows XP if that makes any difference.
tks2103 is offline   Reply With Quote
Old 10-13-2009, 03:28 PM   #2
Adrir
Administrator
 
Adrir's Avatar

Activity Longevity
2/20 20/20
Today Posts
0/11 sssss1461
Location: London, UK
Default

Take advantage of object-orientation (within reason / performance constraints), design patterns (where appropriate) and ensure that you properly encapsulate your classes by declaring members as private.

In your example:

Usually, I simply capture the command state (keyboard, controller, instruction, etc) near the start of the update. The state is stored in some data structure in some class that the update method of the game loop can access. Read access is provided to relevent classes responsible for the update when the game is initialised. These classes may then query the command state as appropriate.
__________________
Michael 'Adrir' Scott :: Games, Virtual Worlds, Education
Networking | Current Project | Research | Teaching

Last edited by Adrir : 10-13-2009 at 03:39 PM.
Adrir is offline   Reply With Quote
Old 10-13-2009, 03:30 PM   #3
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
0/11 sssss2245
Location: UK
Default

The problem with the Win32 libraries is that it was designed to be used with C hence the heavy reliance of globals unless. There are ways to encapsulate all this though.

With some of the others, you just have them local and pass them to the functions that need them.
yaustar is offline   Reply With Quote
Old 10-13-2009, 06:34 PM   #4
tks2103
Junior Member

Activity Longevity
0/20 13/20
Today Posts
0/11 ssssssss3
Default

Thanks for that link Yaustar, it looks like I can make a window class. Let me see if I understand it.

I guess I should write a larger game class with the game logic, state variables, etc., and I can include, as a member, that window class that I write. Then, in winmain, I can declare an instance of my game class, and use that for everything. Something like:

class Game
{
... other game state variables...
Window mywindow;
... other game classes...
};

int winmain(...)
{
Game mygame;
mygame->mywindow->makewindow
while(!mygame->mywindow->handlemessages)
{
mygame->runGame();
}
}

Is that the logic I should be using?

Thanks again guys, this is a big help...
tks2103 is offline   Reply With Quote
Old 10-14-2009, 01:55 AM   #5
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
0/11 sssss2245
Location: UK
Default

Looking at the GameDev article, it looks like the Window class should contain the game rather then the other way round as it needs to pass the messages to the game somehow. You could have them as completely separate classes and pass the instance of the game class to the windows handle function.
yaustar is offline   Reply With Quote
Old 11-19-2009, 11:14 PM   #6
forumaccount
Junior Member

Activity Longevity
0/20 17/20
Today Posts
0/11 ssssssss6
Default

Step 1, make sure you understand why you're not using globals. "Gamedev.net told me globals are bad" is not a good reason, and if you're not very experienced your desire to avoid globals can lead to a mess of pointless classes.

Step 2, once you're past that... people seem to avoid globals by making everything a class, explicitly including references to every thing the class depends on, and hooking up all the references in the constructor.

I've never seen this done for a large project but I suspect that it becomes a huge pain if it's not done well. I mean... I'm just speculating here, but I assume it ends up something like this:

int main (int argc, char* argv[])
{
GameOptionDefaults god;
CommandLineOptionParser clop (argc, argv);
GameOptions go (god, clop);
MemoryManager mm (go);
GraphicsDevice gd (mm, go);
SoundDevice sd (mm, go);
InputDevice id (mm, go);
GameLogicFlow glf (mm, go);
NetworkDevice nd (mm, go);
Server server (mm, nd);
Client client (mm, nd);
Game game (mm, gd, sd, id, glf, server, client);
game.Loop ();
}

Actually that's kind of funny, maybe I'll try making a game this way.
________
AMATEUR ****

Last edited by forumaccount : 03-29-2011 at 04:24 AM.
forumaccount is offline   Reply With Quote
Old 11-20-2009, 12:59 AM   #7
DTR
Member

Activity Longevity
0/20 17/20
Today Posts
0/11 sssssss94
Location: Helsinki, Finland
Default

Don't you run a risk of stack(frame) overflow by doing that?

As far as I can see, every single object there is declared on stack.

Last edited by DTR : 11-20-2009 at 01:46 AM.
DTR is offline   Reply With Quote
Old 11-20-2009, 05:45 AM   #8
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
0/11 sssss2245
Location: UK
Default

Quote:
Originally Posted by DTR View Post
Don't you run a risk of stack(frame) overflow by doing that?

As far as I can see, every single object there is declared on stack.
Depends on how big the classes are but you shouldn't unless you have a HUGE array on the stack.
yaustar is offline   Reply With Quote
Old 11-28-2009, 03:21 AM   #9
forumaccount
Junior Member

Activity Longevity
0/20 17/20
Today Posts
0/11 ssssssss6
Default

Quote:
Originally Posted by DTR View Post
Don't you run a risk of stack(frame) overflow by doing that?

As far as I can see, every single object there is declared on stack.
Don't read too much in to one hastily-typed example. It depends heavily on what's inside each class and how they allocate it.
________
SUZUKI AERIO SPECIFICATIONS

Last edited by forumaccount : 03-29-2011 at 04:24 AM.
forumaccount is offline   Reply With Quote
Old 11-28-2009, 03:30 AM   #10
DTR
Member

Activity Longevity
0/20 17/20
Today Posts
0/11 sssssss94
Location: Helsinki, Finland
Default

Indeed it does.
Just worried about the collaborative effect of all the things combined.

1: The stuff we have there, however its allocated internally.
2: Possibly several threads (As those tend to cut the available stack size)
3: A carefree recursive algorithm here and there with a few thousand "rounds", each happily declaring several D3DXVECTOR3's as its helper variables in its wake.

At some point the limit will be hit.

Really depends on circumstances, but its nice to be aware of the real possibility of such scenario and its causes, should one be encountered.

Last edited by DTR : 11-28-2009 at 06:16 AM.
DTR is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 12:25 AM.






UBM Tech