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

Author Topic: RenderWindow inside of a class  (Read 2485 times)

0 Members and 1 Guest are viewing this topic.

milawynsrealm

  • Newbie
  • *
  • Posts: 1
    • View Profile
RenderWindow inside of a class
« 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
RenderWindow inside of a class
« Reply #1 on: October 28, 2010, 08:01:00 am »
What happens? Compiler errors? Linker errors? Crash? Unexpected behaviour?
Laurent Gomila - SFML developer

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
RenderWindow inside of a class
« Reply #2 on: October 28, 2010, 10:40:52 am »
Do you ever use the Create function on the window?

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
RenderWindow inside of a class
« Reply #3 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;
}

 

anything