SFML community forums
Help => Window => Topic started by: nailuj on August 23, 2011, 07:36:26 pm
-
Hi everybody,
I am using a RenderWindow as a class member in a class called DisplayManager. I want to initialize the RenderWindow with parameters from DisplayManager's constructor.
So I basically have this code:
DisplayManager.hpp
class DisplayManager {
public:
DisplayManager(int screenWidth, int screenHeight);
private:
void initDisplay();
sf::RenderWindow mainWindow;
int screenWidth, screenHeight;
};
DisplayManager.cpp
DisplayManager::DisplayManager(int screenWidth, int screenHeight) {
screenWidth = screenWidth;
screenHeight = screenHeight;
initDisplay();
}
void DisplayManager::initDisplay() {
// now this is where it becomes tricky
// normally I would write
// mainWindow = sf::RenderWindow(sf::VideoMode(screenWidth, screenHeight, 32), "FooBar"));
// however, this doesn't work, because RenderWindows overload the assignment operator to make sure they're not being copied, as far as I understand
// this is what I write instead
sf::RenderWindow mainWindow(sf::VideoMode(screenWidth, screenHeight, 32), "FooBar"));
// however, this seems to be a local variable, as the RenderWindow gets destructed as soon as I leave the initDisplay() function
}
My question is now, how do I initialize a RenderWindow member with parameters from the constructor? Maybe I'm missing something really obvious, but I can't seem to route around the copying check RenderWindow performs.
I'm not really familiar with the behaviour of C++ yet, so I don't really know whether the
sf::RenderWindow mainWindow(sf::VideoMode(screenWidth, screenHeight, 32), "FooBar"));
is expected to create a local variable or not, or whether I'm missing some obvious C++ feature that does what I want (initializer lists give me the NonCopyable error as well).
I am using a git checkout of SFML2 by the way (commit b9b38887884fa9ad0b87e648dc16e27cad84d609) on Fedora 15 with g++ 4.6.0
Thanks for any help in advance :)
-
Yes, declaring another variable with the same name creates a local variable which has nothing to do with your class member.
You can't "construct again" the window object, that's why the Create function exists... ;)
-
Yes, declaring another variable with the same name creates a local variable which has nothing to do with your class member.
You can't "construct again" the window object, that's why the Create function exists... ;)
Alright, that works perfectly. Thanks very much for the help!
Next time I'll scan the API documentation a bit more carefully :)
-
You can simplify your code a lot by using the initializer list.
DisplayManager::DisplayManager(int screenWidth, int screenHeight)
: screenWidth(screenWidth)
, screenHeight(screenHeight)
, mainWindow(sf::VideoMode(screenWidth, screenHeight, 32), "FooBar")
{
// constructor body is empty
}
Do you even need to store the width and height? Recall that sf::RenderWindow has two methods GetWidth() and GetHeight(). Then you could just do it like this:
DisplayManager::DisplayManager(int screenWidth, int screenHeight)
: mainWindow(sf::VideoMode(screenWidth, screenHeight, 32), "FooBar")
{
}
-
You can simplify your code a lot by using the initializer list.
Tthat did simplify it a lot indeed. I tried to construct with an initializer list before, but because I created a new instance of sf::RenderWindow to initialize mainWindow instead of just passing the arguments, I ran into problems. I didn't know you can just pass arguments in initializer lists.
I also saved myself saving the width and height now, thanks :)