Jump to content
  • Advertisement
Sign in to follow this  
Noods

"loose" variables...

This topic is 3671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have quite a few variables that store information about the state of my program and the image that is being displayed. These variables exist within the scope of my WinProc. I am thinking about encapsulating this information in a "ApplicationData" class. However, these vaiables are passed all over the place. Frames per second is important in my application and I am wondering if the overhead of accessor functions would be worth it. How do you all approach encapsulating "loose" variables in your programs?

Share this post


Link to post
Share on other sites
Advertisement
I'm confused... what's the difference between a "loose variable" as you're describing it, and a global variable? What are some examples of loose variables?

As for your question about accessor functions, accessor functions are generally inlined and have zero computational overhead (as long as they aren't actually computing things).

Share this post


Link to post
Share on other sites
refactor your code till they are no longer necessary.
Quote:
I am thinking about encapsulating this information in a "ApplicationData" class.
why? what difference will it make if your gloabals are all wrapped up in a class they are still globals.

Share this post


Link to post
Share on other sites
Quote:
Original post by stonemetal
refactor your code till they are no longer necessary.

Erm, really? Can you give an example of how declaring hInstance as a global variable, for instance, would cause problems that would be solved by refactoring to remove the global?

Share this post


Link to post
Share on other sites
When I say loose, I just mean not part of any object. These variables are static variables in my WinProc (Win32) not global variables.

Share this post


Link to post
Share on other sites
Quote:
Original post by Noods
When I say loose, I just mean not part of any object.

I don't think you do. If you have a for-loop, with a variable int i used as the counter, do you consider i to be a "loose variable"?

Share this post


Link to post
Share on other sites
Creating the main window is a standard approach used in every win32 application and it is in my oppinion comparable to the 'main' function in console apps. So I think you should identify the bare minimum needed to create the window, i.e. hinstance...
Then place all that into one .h file. Define entry functions such as Init, Update, Dispose and call them appropriately (passing parameters if needed). Then implement these functions in a separate .cpp file. From here on you should have no global variables, except perhaps one instantiation of a Main class.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by Noods
When I say loose, I just mean not part of any object.

I don't think you do. If you have a for-loop, with a variable int i used as the counter, do you consider i to be a "loose variable"?


I have no globals. The reason I bring all this up is that I may end up moving to a state pattern and it might make sense to encapsulate this data so it will be easier to point to from my state objects.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Erm, really? Can you give an example of how declaring hInstance as a global variable, for instance, would cause problems that would be solved by refactoring to remove the global?


Yeah, really. It doesn't really matter if it causes problems or not, sloppy code is sloppy code. Globals show a lack of thought and discipline.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!