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.


Messages - freak1k

Pages: [1]
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.

2
General / How to pass by reference properly
« on: December 21, 2010, 02:21:40 pm »
Ahh, then I misunderstood your post before.  Thank you, maybe you've put me on the right track...(I had though that it belonged to the instance of SettingsContainer and that it would stick around until I decided to destroy it.  I see the problem now)

But don't get your hopes up.  :)

SOLVED:

changed:

Code: [Select]

    void SettingsContainer::init(AllSettings& settings)
    {
        sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");
        sf::Event Event;

        settings.App = &App;
        settings.Event = &Event;
    }


to:

Code: [Select]

    void SettingsContainer::init(AllSettings& settings)
    {
        settings->App = new sf::Window(sf::VideoMode(800, 600, 32), "SFML OpenGL");
        settings->Event = new sf::Event;
    }


Woot! you were a big help, Laurent.

3
General / How to pass by reference properly
« on: December 21, 2010, 01:55:15 pm »
well crap, now I just feel stupid.  It appears that I had a * in the wrong spot.  After all that I may as well post what I did (the correct code not my error)  But it still does not work the way that I want.

the problem occurs after the 4th line in main() where it says "container->init(settings);"

settings->App->IsOpened() returns false the line after I try to create and store a pointer to the window.  That is the problem that I am having and trying to work through.

I know the problem is in my part of the code, not SFML itself.

Code: [Select]

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



I hope that clarifies the situation.

Nevertheless I appreciate your patience.

4
General / How to pass by reference properly
« on: December 21, 2010, 01:22:23 pm »
Excuse me if it seems like I'm making too many replies, but the reason that I want a settings object that I can pass around is because if I controlled everything in one class, that class would be huge and unmanageable.

I've written part of this program a few times before (never finished) and I have found it so much easier to deal with as several classes.  One day I would like to make a finished product, but for now it is just fun coding it.  I want to spread out who does what, but in order for there to be communication among the various parts, it is better for me to have something that the parts can use to work together with.  And encapsulating that communication device also helps keep it manageable.

Or maybe I am just delusional.

edit: I will clarify by posting the broken code, I just need a few minutes to rewrite it back to the broken version.

5
General / How to pass by reference properly
« on: December 21, 2010, 01:05:57 pm »
Thank you for the quick response, but it did not answer my question.

I plan on having a true owner of the object, as it will be placed inside another class eventually.  I did this for clarity in the current setting to try and get the concepts worked out.

Then again, maybe my limited understanding of C++ is showing.  There are so many beginner tutorials out there, but nothing in the more advanced topics that seem to work with the way that I look at things.  I like C++, but lets face it, I am a procedural programmer that uses it just to keep things organized.

I only started messing with SFML today, and I already like the way it is laid out.  There are so many libraries that I have messed with that just don't fit what I am trying to do.  Maybe I will just have to figure out another way to do it.

edit: is there something that I need to clarify as to what I am attempting?  BTW, I usually do what you are suggesting and create run() or init() functions.  Thank you for that though.


Thank you though.

6
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]
anything