Hi, I want to render objects in another thread. I can do this with RenderWindow, but can't with RenderTexture. My window and a mouse cursor freeze and blink when I start my program. It happens when I use std::future, but if I don't, it works fine.
Code:
#include <iostream>
#include <SFML/Graphics.hpp>
#include <vector>
#include <string>
#include "RoundedRectangleShape.hpp"
#include "Entity.hpp"
#include <future>
#ifdef __linux__
#include <X11/Xlib.h>
#endif
void render(sf::RenderTarget &target, const std::vector<sf::Drawable *> &objects, sf::RenderStates states = sf::RenderStates{})
{
#ifdef __linux__
XInitThreads();
#endif
target.clear();
for(auto &el : objects)
{
target.draw(*el, states);
}
}
int main()
{
#ifdef __linux__
XInitThreads();
#endif
sf::Texture texture_2;
texture_2.loadFromFile("img.jpg");
sf::RenderWindow window{sf::VideoMode{800,600}, "window"};
sf::RectangleShape scene;
std::vector<sf::Drawable *> objects;
sf::RoundedRectangleShape main{sf::Vector2f{600,300},10,30},
login{sf::Vector2f{450,50},10,30},
password{sf::Vector2f{450,50},10,30};
main.setOrigin(300,150);
login.setOrigin(225,75);
password.setOrigin(225,-25);
main.setOutlineColor(sf::Color::Red);
main.setOutlineThickness(8);
login.setFillColor(sf::Color(201,201,201,200));
password.setFillColor(sf::Color(201,201,201,200));
Entity entity;
entity.setEntities(&main, &login, &password);
objects.emplace_back(&entity);
sf::RenderTexture texture;
texture.create(8000,6000);
texture.setActive(false);
entity.setScale(10,10);
sf::View visibleArea{sf::FloatRect{0.f, 0.f, 800,600}};
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::Resized)
{
float width, height;
width = event.size.width;
height = event.size.height;
entity.setPosition(sf::Vector2f{texture.getSize()}/2.f);
visibleArea.reset(sf::FloatRect{0.f, 0.f, width,height});
visibleArea.setCenter(sf::Vector2f{width, height}/2.f);
scene.setSize(sf::Vector2f{width, width*6.f/8.f});
if(height>=scene.getSize().y)
scene.setSize(sf::Vector2f{height*8.f/6.f, height});
scene.setScale(1.03,1.03);
scene.setOrigin(scene.getSize()/2.f);
scene.setPosition(visibleArea.getCenter());
window.setView(visibleArea);
}
}
std::future rendering = std::async(std::launch::async, render, std::ref(texture), std::ref(objects), sf::RenderStates{});
//Something
rendering.get();
texture.display();
scene.setTexture(&texture.getTexture(),true);
window.clear();
window.draw(scene);
window.display();
}
return 0;
}
And the video:
https://streamable.com/r3qoi