SFML community forums

Help => Graphics => Topic started by: milawynsrealm on October 28, 2010, 05:59:19 am

Title: RenderWindow inside of a class
Post by: milawynsrealm on October 28, 2010, 05:59:19 am
How should I create a window inside of a class? I am new to this library (I used SDL in the past), and I want to know how to create a window. I may be still learning C++, but I also want to learn how to program by doing. Learning the details of how C++ works is one thing, but being able to apply it to a real-world program is another ball of wax.

Below, I have an example of my first, and failed attempt at trying to do it:

Code: [Select]

class cGame{
public:
     ...
private:
     ...
     sf::RenderWindow mainWindow;
};


And then for the main code, I tried to use this code:
Code: [Select]

bool cGame::Run()
{
mainWindow.Display();
return true; //returns as true
}

// ...

void cGame::Cleanup()
{
//releases all the resources and closes the program
mainWindow.Close(); //closes the window
}



What am I doing wrong?
Title: RenderWindow inside of a class
Post by: Laurent on October 28, 2010, 08:01:00 am
What happens? Compiler errors? Linker errors? Crash? Unexpected behaviour?
Title: RenderWindow inside of a class
Post by: Lupinius on October 28, 2010, 10:40:52 am
Do you ever use the Create function on the window?
Title: RenderWindow inside of a class
Post by: inlinevoid on October 28, 2010, 06:01:17 pm
This is usually how I set up my window class:
Code: [Select]
class Window
{
private:
static sf::RenderWindow RenderWindow;
static sf::Event Event;

public:
static void SetWindowSettins(float width, float height);
static void Draw(sf::Drawable & Drawable);
static bool UpdateWindow();
/*
More functions...
*/

};
sf::RenderWindow Window::RenderWindow;
sf::Event Window::Event;


Then I'm able to make calls to the window from anywhere:
Code: [Select]
int main()
{
Window::SetWindowSettings(640, 480);
sf::Image MyImage;
while(Window::UpdateWindow())
{
Window::Draw(MyImage);
}
return 0;
}