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

Author Topic: Threads not working with simple window  (Read 3594 times)

0 Members and 1 Guest are viewing this topic.

zweifel

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Threads not working with simple window
« on: August 05, 2013, 05:47:29 pm »
Hi,

Going directly to the point, I wanted to try a rendering thread to decrease my the FPS count.
It ended up giving me strange results and unstable behavior. Sometimes I get:
XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
      after 104 requests (104 known processed) with 0 events remaining.

Sometimes, it almost work but I cannot change the window. If I do, I can not get back again.

By the error you should already know I am running Linux. I am using the 2.0 SFML version from the Gentoo ebuild.

My makefile is:
         g++ -o o main.cpp  -lsfml-graphics -lsfml-window -lsfml-system -std=c++0x -pthread
 

Below is my simple code:

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

#include<iostream>
#include <thread>

using namespace std;

#define DIR_MEDIA_TEXTURES "media/textures/"

sf::Sprite sprite;

//return if the number of frames should be reset
bool frameTiming(float number_of_frames, float accum_elapsed_seconds)
{
        if(accum_elapsed_seconds > 5.0)
        {
                cout << "FPS: " << number_of_frames/accum_elapsed_seconds << endl;
                return true;
        }
        return false;
}


void renderingThread(sf::Window* window)
{

        // activate the window's context
        window->setActive(true);

        sf::RenderWindow* render_window= (sf::RenderWindow*) window;

        // the rendering loop
        while (window->isOpen())
        {
                // draw...
                render_window->clear();
                //window.draw(shape);
                render_window->draw(sprite);
                //render_window->draw(sprite);

                // end the current frame -- this is a rendering function (it requires the context to be active)
                window->display();
        }
}

int main()
{
        sf::RenderWindow window(sf::VideoMode(1024, 800), "Triturus Project", sf::Style::Default);
        //sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "Triturus Project", sf::Style::Fullscreen);
        window.setVerticalSyncEnabled(true);

        sf::CircleShape shape(100.f);
        shape.setFillColor(sf::Color::Green);
        sf::Texture texture;
        if (!texture.loadFromFile(DIR_MEDIA_TEXTURES"grass94.tga"))
        //if (!texture.loadFromFile("test.png"))
        //if (!texture.loadFromFile(DIR_MEDIA_TEXTURES"plant142.tga"))
        //if (!texture.loadFromFile(DIR_MEDIA_TEXTURES"stone240.tga"))
        {
                    // error...
                    cout << "Error: Failed loading texture" << endl;
        }

        texture.setRepeated(true);
        //texture.setSmooth(true);


        sprite.setTexture(texture);
        sprite.setScale(1,1);
        sf::IntRect rect(0,0,1000,1000);
        sprite.setTextureRect(rect);
        // deactivate its OpenGL context
        window.setActive(false);

        // launch the rendering thread
        sf::Thread thread(&renderingThread, &window);
        //std::thread render_thread( renderingThread, &window);
        //render_thread.join();
        thread.launch();



        sf::Clock clock;
        sf::Time accum_time;
        float number_of_frames;

        while (window.isOpen())
        {
                //------------- Event Handling -------------
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }


                //------------- Time -------------

                //get the time elapsed
                sf::Time elapsed = clock.restart();
                //updateGame(elapsed);
                //cout << elapsed.asSeconds() << endl;

                //frame counter
                accum_time+= elapsed;
                number_of_frames++;
                bool reset_frame_counter= frameTiming(number_of_frames, accum_time.asSeconds());
                if(reset_frame_counter==true)
                {
                        number_of_frames=0.0f;
                        accum_time= sf::seconds(0.0f);
                }

        }

        return 0;
}
 

zweifel

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Threads not working with simple window
« Reply #1 on: August 05, 2013, 05:49:53 pm »
Ah, I forgot to say (though if you read the code you will find the tests commented out),  using std::threads does not change the behavior.

 

anything