SFML community forums
Help => Graphics => Topic started 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:
class cGame{
public:
...
private:
...
sf::RenderWindow mainWindow;
};
And then for the main code, I tried to use this code:
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?
-
What happens? Compiler errors? Linker errors? Crash? Unexpected behaviour?
-
Do you ever use the Create function on the window?
-
This is usually how I set up my window class:
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:
int main()
{
Window::SetWindowSettings(640, 480);
sf::Image MyImage;
while(Window::UpdateWindow())
{
Window::Draw(MyImage);
}
return 0;
}