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.
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.
...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
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.
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.
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.