Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Initializing RenderWindow as class member  (Read 8330 times)

0 Members and 1 Guest are viewing this topic.

nailuj

  • Newbie
  • *
  • Posts: 8
    • View Profile
Initializing RenderWindow as class member
« 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
Code: [Select]

class DisplayManager {
public:
    DisplayManager(int screenWidth, int screenHeight);
private:
    void initDisplay();
    sf::RenderWindow mainWindow;
    int screenWidth, screenHeight;
};

DisplayManager.cpp
Code: [Select]

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
Code: [Select]
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 :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Initializing RenderWindow as class member
« Reply #1 on: August 23, 2011, 08:05:04 pm »
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... ;)
Laurent Gomila - SFML developer

nailuj

  • Newbie
  • *
  • Posts: 8
    • View Profile
Initializing RenderWindow as class member
« Reply #2 on: August 23, 2011, 08:18:03 pm »
Quote from: "Laurent"
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 :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Initializing RenderWindow as class member
« Reply #3 on: August 23, 2011, 09:34:12 pm »
You can simplify your code a lot by using the initializer list.
Code: [Select]
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:
Code: [Select]
DisplayManager::DisplayManager(int screenWidth, int screenHeight)
: mainWindow(sf::VideoMode(screenWidth, screenHeight, 32), "FooBar")
{
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

nailuj

  • Newbie
  • *
  • Posts: 8
    • View Profile
Initializing RenderWindow as class member
« Reply #4 on: August 23, 2011, 10:52:06 pm »
Quote from: "Nexus"
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 :)

 

anything