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

Author Topic: Is it possible for dynamic RenderWindow creation at runtime? (Multiple Windows)  (Read 4977 times)

0 Members and 1 Guest are viewing this topic.

Neobeum

  • Newbie
  • *
  • Posts: 6
    • View Profile
Hello,

Is it possible for dynamic RenderWindow creation at runtime?

At the moment, I am just allocating more than I need in an array.

        sf::RenderWindow* m_window;
        m_window = new sf::RenderWindow[100];

Thanks.
« Last Edit: July 16, 2015, 01:16:04 pm by Neobeum »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Why don't you just try? ;)

Yes it's possible and no it's not recommended to create a 100 windows.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
At the moment, I am just allocating more than I need in an array.
That sounds like you're not familiar with STL containers. I strongly recommend to have a look at them, they simplify dynamic management of multiple elements tremendously.

(Disregarding the fact that SFML windows are a pretty bad example for arrays or containers...)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Neobeum

  • Newbie
  • *
  • Posts: 6
    • View Profile
At the moment, I am just allocating more than I need in an array.
Quote
That sounds like you're not familiar with STL containers. I strongly recommend to have a look at them, they simplify dynamic management of multiple elements tremendously.

...and yes... I'm still new to programming.


I tried with Vectors... but it's a pain in the backside... was wondering if there are better options than this:



#include <SFML/Graphics.hpp>
#include <vector>

class WindowPanels {
public:
        WindowPanels(sf::RenderWindow &a_window) : m_window(a_window)
        {
        };
        WindowPanels(const WindowPanels& a_window) : m_window(a_window.m_window)
        {
        };
        WindowPanels& operator= (const WindowPanels& game_object)
        {
        };

        sf::RenderWindow& m_window;
};

int main()
{
        std::vector<WindowPanels> wp_test;
        sf::RenderWindow *t_win;
       
        for (int i = 0; i < 10; i++)
        {
                t_win = new sf::RenderWindow;
                WindowPanels p_win(*t_win);
                wp_test.push_back(p_win);
        }
       
        wp_test[0].m_window.create(sf::VideoMode(640, 480), "A.C.E. System Control", sf::Style::Titlebar | sf::Style::Close, sf::ContextSettings(0U, 0U, 8));
        wp_test[1].m_window.create(sf::VideoMode(320, 240), "A.C.E. Panel 01", sf::Style::Titlebar | sf::Style::Close, sf::ContextSettings(0U, 0U, 8));
        wp_test[2].m_window.create(sf::VideoMode(320, 240), "A.C.E. Panel 02", sf::Style::Titlebar | sf::Style::Close, sf::ContextSettings(0U, 0U, 8));
        wp_test[3].m_window.create(sf::VideoMode(320, 240), "A.C.E. Panel 03", sf::Style::Titlebar | sf::Style::Close, sf::ContextSettings(0U, 0U, 8));
        wp_test[4].m_window.create(sf::VideoMode(320, 240), "A.C.E. Panel 04", sf::Style::Titlebar | sf::Style::Close, sf::ContextSettings(0U, 0U, 8));
        wp_test[5].m_window.create(sf::VideoMode(320, 240), "A.C.E. Panel 05", sf::Style::Titlebar | sf::Style::Close, sf::ContextSettings(0U, 0U, 8));
        wp_test[6].m_window.create(sf::VideoMode(320, 240), "A.C.E. Panel 06", sf::Style::Titlebar | sf::Style::Close, sf::ContextSettings(0U, 0U, 8));
        wp_test[7].m_window.create(sf::VideoMode(320, 240), "A.C.E. Panel 07", sf::Style::Titlebar | sf::Style::Close, sf::ContextSettings(0U, 0U, 8));
        wp_test[8].m_window.create(sf::VideoMode(320, 240), "A.C.E. Panel 08", sf::Style::Titlebar | sf::Style::Close, sf::ContextSettings(0U, 0U, 8));


        sf::Event mE;

        while (wp_test[0].m_window.isOpen())
        {
                while (wp_test[0].m_window.pollEvent(mE))
                {
                        switch (mE.type)
                        {
                        case sf::Event::Closed:
                                wp_test[0].m_window.close();
                                break;
                        case sf::Event::KeyPressed:
                                if (mE.key.code == sf::Keyboard::Escape)
                                {
                                        wp_test[0].m_window.close();
                                }
                                break;
                        }
                }
                //clear/update/display
        }
}


I'm trying to make render manager to output to different windows depending on what state my application is in, and what objects need to be drawn.

this was my first attempt at it:
        m_output->Run();
        m_output->m_window[0].create(sf::VideoMode(640, 480), "A.C.E. System Control", sf::Style::Titlebar | sf::Style::Close, sf::ContextSettings(0U, 0U, 8));
        //m_output->m_window[0].setPosition(sf::Vector2i(0, 0));
        m_output->m_window[0].setPosition(sf::Vector2i(m_core->m_displayWidth - (m_output->m_window[0].getSize().x + 15), 0));
        while (m_output->m_window[0].isOpen())
        {
                while (m_output->m_window[0].pollEvent(m_input->m_event))
                {
                        switch (m_input->m_event.type)
                        {
                        case sf::Event::Closed:
                                m_output->m_window[0].close();
                                break;
                        case sf::Event::KeyPressed:
                                if (m_input->m_event.key.code == sf::Keyboard::Escape)
                                {
                                        m_output->m_window[0].close();
                                }
                        }//switch
                }//pollEvent

                m_output->m_window[0].clear();


                m_output->m_window[0].display();

        }//mainWindow.isOpen

Neobeum

  • Newbie
  • *
  • Posts: 6
    • View Profile
I tried again, and I think it's working, but does anyone have any suggestions as to what I could do to clean up my code a little?

class Output : public Module
{
public:
        struct WindowPanel
        {
                WindowPanel(sf::RenderWindow &a_window) : m_window(&a_window)
                {
                }

                WindowPanel(const WindowPanel& a_window) : m_window(a_window.m_window)
                {
                }

                WindowPanel& operator= (const WindowPanel& a_window)
                {
                }

                sf::RenderWindow *m_window;
        };
       
        Output();

        void CreatePanel();

        std::vector<WindowPanel> m_screen;

};

void Output::CreatePanel()
{
        sf::RenderWindow* f_win;
        f_win = new sf::RenderWindow;
        WindowPanel f_wpan(*f_win);
        m_screen.push_back(f_wpan);
}

void Application::Run()
{
        m_output->CreatePanel();
        m_output->m_screen[Module::MAINSCREEN].m_window->create(sf::VideoMode(640, 480), "A.C.E. System Control", sf::Style::Titlebar | sf::Style::Close, sf::ContextSettings(0U, 0U, 8));
        m_output->m_screen[Module::MAINSCREEN].m_window->setPosition(sf::Vector2i(m_core->GetDisplaySize().x - (m_output->m_screen[Module::MAINSCREEN].m_window->getSize().x + 15), 0));
        while (m_output->m_screen[Module::MAINSCREEN].m_window->isOpen())
        {
                while (m_output->m_screen[Module::MAINSCREEN].m_window->pollEvent(m_input->m_event))
                {
                        switch (m_input->m_event.type)
                        {
                        case sf::Event::Closed:
                                m_output->m_screen[Module::MAINSCREEN].m_window->close();
                                break;
                        case sf::Event::KeyPressed:
                                if (m_input->m_event.key.code == sf::Keyboard::Escape)
                                {
                                        m_output->m_screen[Module::MAINSCREEN].m_window->close();
                                }
                        }//switch
                }//pollEvent

                m_output->m_screen[Module::MAINSCREEN].m_window->clear();


                m_output->m_screen[Module::MAINSCREEN].m_window->display();

        }//mainWindow.isOpen
}

Thanks.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
There's no real reason for you to be using pointers (with the renderwindow), it will just cause more headaches (plus I don't see any deletes anywhere!)

Secondly, why don't you just use a vector of sf::RenderWindow? Like so:

 std::vector<sf::RenderWindow> m_screens

Your struct WindowPanel has absolutely no use, so just get rid of it.
Current Projects:
Technoport

G.

  • Hero Member
  • *****
  • Posts: 1590
    • View Profile
Secondly, why don't you just use a vector of sf::RenderWindow? Like so:
 std::vector<sf::RenderWindow> m_screens
sf::RenderWindow is non copyable.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile

Secondly, why don't you just use a vector of sf::RenderWindow? Like so:
 std::vector<sf::RenderWindow> m_screens
sf::RenderWindow is non copyable.

Is it really? Wow I didn't know that at all, never had to do this kind of stuff. Thanks for the clarification.
Current Projects:
Technoport

 

anything