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

Author Topic: Problem with tutorial  (Read 1233 times)

0 Members and 1 Guest are viewing this topic.

Miraz

  • Newbie
  • *
  • Posts: 8
    • View Profile
Problem with tutorial
« on: May 05, 2014, 01:42:03 pm »
Hello,

im trying to get http://www.sfml-dev.org/tutorials/2.1/graphics-draw.php  "Drawing from threads" part to work. Im not getting any compiler errors but renderwindow is freezing when i run program.

Whats wrong ?

#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
using namespace std;

void RenderingThread(sf::RenderWindow* window)
{
    // Rendering loop
    cout << "Starting rendering loop"<< endl;
    while(window->isOpen())
    {
        // draw...

        // end current frame
        window->display();
    }
}


int main()
{
    // Create window
    cout << "Init window"<< endl;
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    // deactivate its OpenGL context
    cout << "Deactivate its OpenGL context"<< endl;
    window.setActive(false);

    // launch the rendering thread
    cout << "Launch the rendering thread"<< endl;
    sf::Thread thread1(&RenderingThread, &window);
    thread1.launch();

    // Events
    sf::Event event;
    while(window.isOpen())
    {
        // Check window events
        if(event.type == sf::Event::Closed)
            window.close();
    }

    return 0;
}
 

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with tutorial
« Reply #1 on: May 05, 2014, 01:47:13 pm »
You need to call pollEvent in the main loop.
« Last Edit: May 05, 2014, 01:49:27 pm by Ixrec »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Problem with tutorial
« Reply #2 on: May 05, 2014, 01:49:10 pm »
"Freezing" means that the application hasn't been unqueuing the events, thus the OS marks the application as "non-responsive". So yes, as Ixrec said, you need to process the events properly with pollEvent. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Miraz

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Problem with tutorial
« Reply #3 on: May 05, 2014, 02:33:04 pm »
Nice forums :) I edited code and its working now, thank you both ! Here is edited code if someone else is having problem with it.

int main()
{
    // Create window
    cout << "Init window"<< endl;
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    // deactivate its OpenGL context
    cout << "Deactivate its OpenGL context"<< endl;
    window.setActive(false);

    // launch the rendering thread
    cout << "Launch the rendering thread"<< endl;
    sf::Thread thread1(&RenderingThread, &window);
    thread1.launch();

    while (window.isOpen())
    {

    // Events
    sf::Event event;
    while(window.pollEvent(event))
    {
        // Close request event
        if(event.type == sf::Event::Closed)
            window.close();
    }

    }
    return 0;
}