1
General / How to pass by reference properly
« on: December 21, 2010, 02:53:27 pm »
I agree it's not 100% as it is posted, I did put in the cleanup afterwards. I will consider your advice.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
void SettingsContainer::init(AllSettings& settings)
{
sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");
sf::Event Event;
settings.App = &App;
settings.Event = &Event;
}
void SettingsContainer::init(AllSettings& settings)
{
settings->App = new sf::Window(sf::VideoMode(800, 600, 32), "SFML OpenGL");
settings->Event = new sf::Event;
}
#include "SettingsContainer.h"
#include <iostream>
int main()
{
Blah::AllSettings* settings = new Blah::AllSettings;
Blah::SettingsContainer* container = new Blah::SettingsContainer;
container->init(settings);
std::cout << settings->App->IsOpened(); // <- it is destroyed as soon as it is created
while(settings->App->IsOpened())
{
while (settings->App->GetEvent(*settings->Event))
{
// Close window : exit
if (settings->Event->Type == sf::Event::Closed)
settings->App->Close();
// Escape key : exit
if ((settings->Event->Type == sf::Event::KeyPressed) && (settings->Event->Key.Code == sf::Key::Escape))
settings->App->Close();
// Resize event : adjust viewport
if (settings->Event->Type == sf::Event::Resized)
glViewport(0, 0, settings->Event->Size.Width, settings->Event->Size.Height);
}
// Set the active window before using OpenGL commands
// It's useless here because active window is always the same,
// but don't forget it if you use multiple windows or controls
settings->App->SetActive();
//some openGL calls
// Finally, display rendered frame on screen
settings->App->Display();
}
delete container;
container = NULL;
delete settings;
settings = NULL;
return EXIT_SUCCESS;
}
//--------------
//SettingsContainer.h
//--------------
#ifndef SETTINGSCONTAINER_H
#define SETTINGSCONTAINER_H
namespace Blah
{
struct AllSettings
{
sf::Window* App;
sf::Event* Event;
};
class SettingsContainer
{
protected:
private:
AllSettings settings;
public:
SettingsContainer();
virtual ~SettingsContainer();
void init(AllSettings*&);
};
};
#endif // SETTINGSCONTAINER_H
//--------------
//SettingsContainer.cpp
//--------------
namespace Blah
{
SettingsContainer::SettingsContainer()
{
//ctor
}
SettingsContainer::~SettingsContainer()
{
//dtor
}
void SettingsContainer::init(AllSettings*& settings)
{
sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");
sf::Event Event;
settings->App = &App;
settings->Event = &Event;
}
}
//--------------
//main.cpp
//--------------
#include "SettingsContainer.h"
#include <iostream>
int main()
{
Blah::AllSettings settings;
Blah::SettingsContainer container;
container.init(settings);
std::cout << settings.App->IsOpened(); // <- returns false because it is destroyed as soon as it is created
while(settings.App->IsOpened())
{
while (settings.App->GetEvent(*settings.Event))
{
// Close window : exit
if (settings.Event->Type == sf::Event::Closed)
settings.App->Close();
// Escape key : exit
if ((settings.Event->Type == sf::Event::KeyPressed) && (settings.Event->Key.Code == sf::Key::Escape))
settings.App->Close();
// Resize event : adjust viewport
if (settings.Event->Type == sf::Event::Resized)
glViewport(0, 0, settings.Event->Size.Width, settings.Event->Size.Height);
}
settings.App->SetActive();
}
return EXIT_SUCCESS;
}
//--------------
//SettingsContainer.h
//--------------
#ifndef SETTINGSCONTAINER_H
#define SETTINGSCONTAINER_H
#include <SFML/Window.hpp>
namespace Blah
{
struct AllSettings
{
sf::Window* App;
sf::Event* Event;
};
class SettingsContainer
{
protected:
private:
AllSettings settings;
public:
SettingsContainer();
virtual ~SettingsContainer();
void init(AllSettings&);
};
};
#endif // SETTINGSCONTAINER_H
//--------------
//SettingsContainer.cpp
//--------------
namespace Blah
{
SettingsContainer::SettingsContainer()
{
//ctor
}
SettingsContainer::~SettingsContainer()
{
//dtor
}
void SettingsContainer::init(AllSettings& settings)
{
sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");
sf::Event Event;
settings.App = &App;
settings.Event = &Event;
}
}
//---------------------