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

Show Posts

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.


Topics - freak1k

Pages: [1]
1
General / How to pass by reference properly
« on: December 21, 2010, 12:43:29 pm »
Here is a stripped down version of what works, but isn't exactly what I want to do:

Code: [Select]

//--------------
//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;
    }
}

//---------------------


The problem is that I can't seem to get it to not be destroyed (which is what I believe is happening)  I would like to store the screen state in a struct that I can pass around instead of having a bunch of garbage in main().  I have added pointers in all sorts of crazy ways, but can not seem to get any of my implementations to work except for this one.  I even used new to allocate Blah::SettingsContainer, but to no avail because I could never get it to compile.

I know that this is more of a C++ question in general.  If anyone has suggestions, it would be much appreciated.  I am sure there that I have been vague in some way that I did not think of.  I am using Code::Blocks with MinGW 4.4.5 on a WinXP computer with an AMD processor if that matters.

Thank you

Pages: [1]