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

Author Topic: Create 2 windows  (Read 4513 times)

0 Members and 1 Guest are viewing this topic.

manolismi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Create 2 windows
« on: July 09, 2014, 04:09:33 pm »
I have a game displayed on a fullscreen window that works fine. In this game i have an options button. When you click it, i want a second window(non fullscreen) to pop up and the fullscreen window to be unresponsive until you close the other. So in main.cpp i have a standard window loop:

main.cpp
static sf::RenderWindow window(sf::VideoMode(1152,864),"Palindromo test",sf::Style::Fullscreen);
int main()
{
        window.setVerticalSyncEnabled(true);
        menu = new Menu(&window);
        while (window.isOpen())
        {      
                sf::Event event;
                while (window.pollEvent(event))
                {
                        window.clear(sf::Color::White);
                        switch (event.type)
                        {
                                case sf::Event::Closed:
                                        window.close();
                                        break;
                                case sf::Event::MouseMoved:
                                        menu->mousemoved();
                                        break;
                                case sf::Event::MouseButtonPressed:
                                        menu->mouseclicked();
                                        break;
                                case sf::Event::KeyPressed:
                                        if (event.key.code == sf::Keyboard::Escape)
                                                exit(0);
                                                break;
                        }
                }
                menu->display();
                window.display();
        }
        return 0;
}
 

Menu is a class i created. So menu->mouseclicked() checks if some button of menu was clicked; if you click the options button this executes:

void Obutton::clicked()
{
        sf::RenderWindow owindow(sf::VideoMode(800,600),"Options",sf::Style::Close);
        owindow.setVerticalSyncEnabled(true);
        while (owindow.isOpen())
        {
                sf::Event event;
                while (owindow.pollEvent(event))
                {
                        switch (event.type)
                        {
                                case sf::Event::Closed:
                                        owindow.close();
                                        break;
                        }      
                }
                owindow.clear(sf::Color::Blue);
                owindow.display();
        }
}
 

My problem is that when i click the Obutton(options button), in my first computer, the game just freezes. In my other computer, a non-responsive transparent window pops up. After about 10-20 seconds it gets blue as desired and responds, but if you click outside it, it gets non-responsive, this time for ever, even if you click on it again. So i close it with the task manager. Why isn't it working? Do i need to activate the pop-up or something? Thanks.
« Last Edit: July 09, 2014, 05:21:23 pm by manolismi »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create 2 windows
« Reply #1 on: July 09, 2014, 04:32:40 pm »
Your drawing code is inside your event loop, which doesn't make sense.

Note that if you're using SFML 2.1 on Windows, there is a focus bug when you click inside / outside windows (you must click the titlebar to give the focus back to a window).
Laurent Gomila - SFML developer

manolismi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Create 2 windows
« Reply #2 on: July 09, 2014, 04:59:31 pm »
Yes, you're right. I moved the draw commands outside the event loop. Still though, in both of my computers a non-responsive blue window pops up. If i click 'x', it doesn't close. So i kill it with the task manager again. Yes, i have sfml2.1 under windows but even if i click on the titlebar the window doesn't get focus.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Create 2 windows
« Reply #3 on: July 09, 2014, 05:05:39 pm »
Show a minimal complete example of your current code.

And don't use exit(0), this function does not clean up automatic storage.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

manolismi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Create 2 windows
« Reply #4 on: July 09, 2014, 05:26:11 pm »
I modified the post to show my code after the changes suggested by Laurent.
main.cpp calls menu->mouseclicked(). menu->mouseclicked() checks where is the mouse and calls
obutton->clicked() (obutton is "options" button) if the mouse is over obutton. And obutton::clicked() opens the blue window. My fullscreen window doesn't refresh anymore of course because of the while(owindow.isOpen) loop, but the blue window doesn't respond either, so i cannot close it and get the fulscreen window responsive again.
« Last Edit: July 09, 2014, 05:29:13 pm by manolismi »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Create 2 windows
« Reply #5 on: July 09, 2014, 05:29:51 pm »
I meant a minimal complete example. Please read that post carefully.

And don't use global/static variables, especially not SFML ones. And again, avoid exit(0).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create 2 windows
« Reply #6 on: July 09, 2014, 07:21:44 pm »
The following code works as expected for me with a recent revision of SFML.

#include <SFML/Graphics.hpp>

void popup()
{
    sf::RenderWindow w2(sf::VideoMode(200, 200), "Sub window");

    while (w2.isOpen())
    {
        sf::Event event;
        while (w2.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                w2.close();
        }

        w2.clear(sf::Color::Green);
        w2.display();
    }
}

int main()
{
    sf::RenderWindow w1(sf::VideoMode(800, 600), "Main window", sf::Style::Fullscreen);

    while (w1.isOpen())
    {
        sf::Event event;
        while (w1.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                w1.close();
            if (event.type == sf::Event::MouseButtonPressed)
                popup();
        }

        w1.clear(sf::Color::Blue);
        w1.display();
    }

    return EXIT_SUCCESS;
}
 
Laurent Gomila - SFML developer

manolismi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Create 2 windows
« Reply #7 on: July 09, 2014, 08:30:34 pm »
I compiled your code and it doesn't work for me... The same problem. I have windows 7 32-bit, mingw 4.8.1 32-bit release and dev-cpp 5 .6.3 IDE. When i click the blue window, the green pops up and is non-responsive.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create 2 windows
« Reply #8 on: July 09, 2014, 09:51:45 pm »
Can you try with the latest sources?
Laurent Gomila - SFML developer

manolismi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Create 2 windows
« Reply #9 on: July 10, 2014, 12:12:05 pm »
I downloaded the latest stable version and compiled it again but nothing.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create 2 windows
« Reply #10 on: July 10, 2014, 12:19:32 pm »
Just to be sure, what do you call "the latest stable version"?
Laurent Gomila - SFML developer

manolismi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Create 2 windows
« Reply #11 on: July 11, 2014, 08:28:35 am »
Ok, i tried your program and mine with:
a)SFML 2.1 precompiled, the binary mingw distribution from
http://www.sfml-dev.org/download/sfml/2.1/
b)the source code of SFML2.1, which i built with cmake, from
http://www.sfml-dev.org/download/sfml/2.1/ and
c)the master source code master.zip, which i built with cmake, from https://github.com/SFML/SFML/archive/master.zip

Your code worked for me only with the master.zip edition (c), with a and b the pop-up was non-responsive.
Mine didn't work at all, with a and b it freezed when i clicked the options button and with c it didn't freeze, and the window was responsive (alt+f4 closed it and continued to the fullscreen window normally), but the window poped-up behind the fullscreen one. I don't see it, but if i click on the area the 'x' button is, it closes.

So, is there a way to move a window at the top, above every other and give it focus?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create 2 windows
« Reply #12 on: July 11, 2014, 08:34:36 am »
You can try this branch and the new Window::requestFocus() function:
https://github.com/SFML/SFML/tree/feature/window_focus
Laurent Gomila - SFML developer

manolismi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Create 2 windows
« Reply #13 on: July 11, 2014, 10:43:49 am »
God it doesn't work! The same! And i just realised that in winXP the application crashes upon execution both with the master and the focus-request branch. Would you like me to send you the whole project to test it?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create 2 windows
« Reply #14 on: July 11, 2014, 10:59:41 am »
If my code works, there's no need to send your project, you have a working code that you can use for debugging :P
Laurent Gomila - SFML developer