I seem to have a problem with some classes being initialised/created. I have two GUI screen classes which contain all the UI controls for that particular screen (the two in question are main screen and player select screen). I declare a static pointer to each in the .h file where their classes are defined. I then create the classes in a function:
/* Creates the User Interface screens for the menus */
inline void createUIScreens() {
mainScreen = new MainScreen();
playerScreen = new PlayerScreen();
mainScreen->setVisible(true); // Show title screen
}
setVisible simply sets the screen to draw.
The problem i have is that when i try to display playerScreen, it won't work. Specifically, playerScreen is NULL. The strangest thing is that playerScreen->setVisible(true); is actually called, i don't get an access violation (null pointer exception). Both screens inherit from class Container, which is the super class for all UI windows/screens.
class Container : public Component {
...
inline void setVisible(bool b) { Component::active = (b ? this : NULL); }
};
class Component {
public:
static Component* active; // The component that is active
...
};
As you can probably see from the above code, active is set to the Container that calls setVisible(true). It is a pointer to the Container (screen) to draw, and is set to NULL if nothing is visible. This may not be the best way to do it but it's the way i'm doing it.
My problem is that when i call playerScreen->setVisible(true);, playerScreen is NULL yet the function still get's called and (as far as i know) 'this' is NULL; When i outputted some debug values i found that playerScreen & mainScreen were NULL when initialised, then set to some value (the pointer that is) when they were created, and then went
back to NULL when i called playerScreen->setVisible(true);.
Any ideas?
thanks