Skip to main content
added markdown to code in text, removed extra line breaks from code, fixed vertical code block alignment
Source Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

I have a main window whose loop is just in the program and a WindoWWindow class in which separate sf::RenderWindowsf::RenderWindow objects are created and the loops for them are in std::threadstd::thread, so when adding a button to such a window, the animation is simply ignored, although if you add exactly the same button to the main window, then everything will work fine.

I run this function in the std::threadstd::thread:

    void Render() {
    
    sf::Event event;
    MSG msg = {};

    if (prepare_window_function != nullptr) {
        prepare_window_function(this);
    }

    while (Window_ptr->isOpen()) {

        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {

            if (msg.message == 160) {
                std::ofstream coords("DaTa_hats/_ParamS_/coords");
                coords << Window_ptr->getPosition().x << " " << Window_ptr->getPosition().y << std::endl;
                coords.close();
            }

            TranslateMessage(&msg);
            DispatchMessage(&msg);
 
        }
 
        
        while (Window_ptr->pollEvent(event)) {

            if (event.type == sf::Event::KeyReleased and event.key.code == sf::Keyboard::D) {
                Developer_Mode = 1;
            }
            if (event.type == sf::Event::KeyReleased and event.key.code == sf::Keyboard::Escape) {
                Developer_Mode = 0;
            }

 

            Handle_Events(event);
 
        }
        
        Window_ptr->clear(sf::Color(100, 100, 100, 255));

        if (Is_Rendering) {
            Draw_All();
        }

        Window_ptr->display();

        if (Pause_After_Start) {
            Pause();
            Pause_After_Start = 0;
        }
 
    }
 
}

Handel_EventsHandel_Events and Draw_All isDraw_All are functions from dlla DLL, their contents are the same as the functions of the main window

I have a main window whose loop is just in the program and a WindoW class in which separate sf::RenderWindow objects are created and the loops for them are in std::thread, so when adding a button to such a window, the animation is simply ignored, although if you add exactly the same button to the main window, then everything will work fine.

I run this function in the std::thread:

    void Render() {
    
    sf::Event event;
    MSG msg = {};

    if (prepare_window_function != nullptr) {
        prepare_window_function(this);
    }

    while (Window_ptr->isOpen()) {

        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {

            if (msg.message == 160) {
                std::ofstream coords("DaTa_hats/_ParamS_/coords");
                coords << Window_ptr->getPosition().x << " " << Window_ptr->getPosition().y << std::endl;
                coords.close();
            }

            TranslateMessage(&msg);
            DispatchMessage(&msg);
 
        }
 
        
        while (Window_ptr->pollEvent(event)) {

            if (event.type == sf::Event::KeyReleased and event.key.code == sf::Keyboard::D) {
                Developer_Mode = 1;
            }
            if (event.type == sf::Event::KeyReleased and event.key.code == sf::Keyboard::Escape) {
                Developer_Mode = 0;
            }

 

            Handle_Events(event);
 
        }
        
        Window_ptr->clear(sf::Color(100, 100, 100, 255));

        if (Is_Rendering) {
            Draw_All();
        }

        Window_ptr->display();

        if (Pause_After_Start) {
            Pause();
            Pause_After_Start = 0;
        }
 
    }
 
}

Handel_Events and Draw_All is functions from dll, their contents are the same as the functions of the main window

I have a main window whose loop is just in the program and a Window class in which separate sf::RenderWindow objects are created and the loops for them are in std::thread, so when adding a button to such a window, the animation is simply ignored, although if you add exactly the same button to the main window, then everything will work fine.

I run this function in the std::thread:

void Render() {

    sf::Event event;
    MSG msg = {};

    if (prepare_window_function != nullptr) {
        prepare_window_function(this);
    }

    while (Window_ptr->isOpen()) {

        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {

            if (msg.message == 160) {
                std::ofstream coords("DaTa_hats/_ParamS_/coords");
                coords << Window_ptr->getPosition().x << " " << Window_ptr->getPosition().y << std::endl;
                coords.close();
            }

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        while (Window_ptr->pollEvent(event)) {

            if (event.type == sf::Event::KeyReleased and event.key.code == sf::Keyboard::D) {
                Developer_Mode = 1;
            }
            if (event.type == sf::Event::KeyReleased and event.key.code == sf::Keyboard::Escape) {
                Developer_Mode = 0;
            }

            Handle_Events(event);
        }
        
        Window_ptr->clear(sf::Color(100, 100, 100, 255));

        if (Is_Rendering) {
            Draw_All();
        }

        Window_ptr->display();

        if (Pause_After_Start) {
            Pause();
            Pause_After_Start = 0;
        }
    }
}

Handel_Events and Draw_All are functions from a DLL, their contents are the same as the functions of the main window

Source Link

c++ SFML Window in std::thread ignore animation

I have a main window whose loop is just in the program and a WindoW class in which separate sf::RenderWindow objects are created and the loops for them are in std::thread, so when adding a button to such a window, the animation is simply ignored, although if you add exactly the same button to the main window, then everything will work fine.

I run this function in the std::thread:

    void Render() {
    
    sf::Event event;
    MSG msg = {};

    if (prepare_window_function != nullptr) {
        prepare_window_function(this);
    }

    while (Window_ptr->isOpen()) {

        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {

            if (msg.message == 160) {
                std::ofstream coords("DaTa_hats/_ParamS_/coords");
                coords << Window_ptr->getPosition().x << " " << Window_ptr->getPosition().y << std::endl;
                coords.close();
            }

            TranslateMessage(&msg);
            DispatchMessage(&msg);

        }

        
        while (Window_ptr->pollEvent(event)) {

            if (event.type == sf::Event::KeyReleased and event.key.code == sf::Keyboard::D) {
                Developer_Mode = 1;
            }
            if (event.type == sf::Event::KeyReleased and event.key.code == sf::Keyboard::Escape) {
                Developer_Mode = 0;
            }



            Handle_Events(event);

        }
        
        Window_ptr->clear(sf::Color(100, 100, 100, 255));

        if (Is_Rendering) {
            Draw_All();
        }

        Window_ptr->display();

        if (Pause_After_Start) {
            Pause();
            Pause_After_Start = 0;
        }

    }

}

Handel_Events and Draw_All is functions from dll, their contents are the same as the functions of the main window