Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Extern Enumeration

Author
Message
Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 20th Jan 2010 19:23
Well, I looked up some tutorials, but I still don't understand how to get an enumeration accesable through the entire program and be able to change the state it is in, so how would I make this enumeration external?



The code should be added to main.h.

Isocadia

MACRO
21
Years of Service
User Offline
Joined: 10th Jun 2003
Location:
Posted: 20th Jan 2010 23:27
Hey,

This sounds to me like the perfect place for the Singleton design pattern...

Warning!!! this code is quick typed, messy and provides no thread safety but should serve to demonstrate the general pattern...



Then to use in the program...



The beauty of the singleton is it is global accessable but provides more functionality and control than a shed load of global variables. I use them all the time for config etc.

Hope that is helpful.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 21st Jan 2010 00:42 Edited at: 21st Jan 2010 00:43
It's the wrong place for a singleton since screen is not a class, and has no methods to manipulate it.

To make it accessible from all your files, declare the enum in a header file which is included by your .cpp files:



Also, declare the variable using extern in the same place:


Then, in one of your .cpp files only (preferably the one most relevent to the variable) add this code:


That should allow you to access CScreen from any file which includes the header.

MACRO
21
Years of Service
User Offline
Joined: 10th Jun 2003
Location:
Posted: 21st Jan 2010 01:08 Edited at: 21st Jan 2010 01:08
Yeah that will work.

I wouldn't say its necessarily the wrong place for a singleton it's just that in this case there are many ways to achieve the same thing - global access to centralised game state information. Singleton is just one approach and to be honest if all you are looking to do is make an enum instance global then using a singleton is a lot of extra code for not very much benefit - if you don't expect your global game state information to get any more complicated than a single enumerated value.

I tend to use them for this kind of thing because I often have a need for my global game state to be a lot more complicated with a multitude of data members and appropriate associated methods. I find that easier to manage than a load of global variables.

Login to post a reply

Server time is: 2024-10-01 23:39:51
Your offset time is: 2024-10-01 23:39:51