• Advertisement
Sign in to follow this  

Messing around in a lazyfoo tutorial. What is this?

This topic is 754 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 was exploring SDL2.0 learning from a lazy too tutorial. In lesson 10 the tutorial introduces a class for rendering. I carefully looked over this code and there is one thing I don't understand. What is "LTexture   gSpriteSheetTexture;"? The class is named LTexture yet in the global area outside of the main function I see this declaration. I don't understand. It's like he declare a variable of type LTexture.  Please look at the code of the tutorial and explain what, why, and how this strange declaration work.

 

http://lazyfoo.net/tutorials/SDL/10_color_keying/index.php

Edited by LAURENT*

Share this post


Link to post
Share on other sites
Advertisement


It's like he declare a variable of type LTexture.

Yes, that's exactly what he's doing. LTexture is a class type, his gSpriteSheet is a variable of type "class LTexture".

Share this post


Link to post
Share on other sites

I'm only used to standard variables being global. Even the custom made ones I defined in structs don't have functions. I don't like this but should I make an exception with this one class?

Share this post


Link to post
Share on other sites

If you're following the tutorial, it's fine to do it the way the tutorial says.

 

Globals can be any variable type - ints and floats, or custom classes. It doesn't matter what type it is, the same guideline applies.

 

In general, unless the variable is const, it's frowned upon to have global variables. If it's a global const variable, then it's fine to have dozens or even hundreds of those (basically, that's an intermediary step to making many of those configuration variables data-driven, and others you want to keep fixed at compile-time).

 

Usually, there are a few non-const global variables that escape the axe though: Logging instances (to output log messages), for example. Things can certainly be over-engineered in the other direction as well, if you try to contort too hard to avoid globals but aren't yet sure how.

 

Singletons are also just global variables in disguise, so the same rules apply: Limit their use as much as possible, but two or three globals in a large project aren't uncommon. It's hard to learn good architectural practice, and global variables or singletons are most often (but not always) a bandaid to work around bad architectures - in my inexperienced opinion.

Share this post


Link to post
Share on other sites

In general, unless the variable is const, it's frowned upon to have global variables. If it's a global const variable, then it's fine to have dozens or even hundreds of those (basically, that's an intermediary step to making many of those configuration variables data-driven, and others you want to keep fixed at compile-time).
 
Usually, there are a few non-const global variables that escape the axe though: Logging instances (to output log messages), for example. Things can certainly be over-engineered in the other direction as well, if you try to contort too hard to avoid globals but aren't yet sure how.
 
Singletons are also just global variables in disguise, so the same rules apply: Limit their use as much as possible, but two or three globals in a large project aren't uncommon. It's hard to learn good architectural practice, and global variables or singletons are most often (but not always) a bandaid to work around bad architectures - in my inexperienced opinion.
 

 

Good! You can also read this little piece  http://www.gamedev.net/topic/664328-yet-another-global-variable-debate/?view=findpost&p=5201453

Share this post


Link to post
Share on other sites


Used in moderation, global variables can be convenient, but the risk is you get addicted to that convenience and then stop using them "in moderation", and start using them by default, creating messier code logic that is harder to maintain and harder to debug.

 

Globals, not even once.

Share this post


Link to post
Share on other sites

I'm only used to standard variables being global. Even the custom made ones I defined in structs don't have functions. I don't like this but should I make an exception with this one class?


Personally I would argue that is the exact wrong around.

Globals are sometimes needed. Very sparingly, but there are situations where I have not seen a good, practical solution that completely avoid them.

Now, when you are in such a situation, what would you rather have? A bunch of variables lying around anyone can change at any time in any way? Or just some (member) functions to call for very specific tasks with descriptive names? I know what I would want.

Share this post


Link to post
Share on other sites

Another problem with mutable globals is thread safety - you need to protect access to the writeable data with a mutex or other synchronization. Immutable globals are fine though.

Share this post


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

  • Advertisement