I'm trying to initialize a window (win) in the constructor of an class (Program), but the window closes as soon as the the constructor finishes, preventing the main loop (mainLoop) from running. There are no compiler errors, and I'm using SFML 1.6 with Visual Studio C++ 2008 Express. My code is shown below:
// program.h
//--------------
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
class Program
{
public:
sf::Window* win;
Program(unsigned int w, unsigned int h, unsigned int bitdepth, char* title);
void mainLoop();
void eventHandler();
void drawScreen();
~Program();
};
//program.cpp
//--------------
#include "program.h"
Program::Program(unsigned int w, unsigned int h, unsigned int bitdepth, char* title)
{
// Create window
win = &sf::Window(sf::VideoMode(w, h, bitdepth), title);
}
void Program::mainLoop()
{
while (win->IsOpened())
{
eventHandler();
drawScreen();
}
}
void Program::eventHandler()
{
sf::Event Event;
while (win->GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
win->Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
win->Close();
}
}
void Program::drawScreen()
{
win->Display();
}
Program::~Program()
{}
//main.cpp
//----------
#include "program.h"
int main()
{
Program p(800, 600, 32, "Test");
p.mainLoop();
return 1;
}