I am attempting to separate out my rendering from my logic.
As it is, when I render inside of my update thread, everything is incredibly smoothly. But when I move the draw operations to their own thread, everything gets incredibly choppy.
I've done a lot of researching and apparently this is a common issue with just about any framework in existence, but I can't seem to find any real answers on how to remedy the situation, besides just not separating them in the first place.
If that's what I have to do, then I accept that, but it would be nice to be able to do this, if even just to know how. If the answer is in the documentation, or on the forums somewhere, please direct me to it. I would love to read it.
CODE IS AS FOLLOWS
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <functional>
void startGameClass();
void startMinimalExample();
void render(sf::Sprite *sprite);
sf::RenderWindow window;
int main()
{
startMinimalExample();
return 0;
}
void startMinimalExample()
{
window.create(sf::VideoMode(640, 480),"SFML Window");
window.setActive(false);
sf::Sprite sprite;
sf::Texture texture;
texture.loadFromFile(".\\resources\\test.png");
sprite.setTexture(texture);
sf::Thread t(std::bind(&render, &sprite));
//UNCOMMENT t.launch() TO LAUNCH RENDER THREAD
//COMMENT OUT WINDOW CALLS BELOW AS WELL
//t.launch();
sf::Clock updateClock;
float fps = 1000000.00f / 60.00f;
while (window.isOpen())
{
if (updateClock.getElapsedTime().asMicroseconds() >= fps)
{
sf::Time elapsed = updateClock.restart();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
sprite.move(.25f * elapsed.asMilliseconds(), 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
sprite.move(-.25f * elapsed.asMilliseconds(), 0);
}
//COMMENT NEXT THREE LINES WHEN USING RENDER THREAD
window.clear();
window.draw(sprite);
window.display();
}
}
}
void render(sf::Sprite *sprite)
{
sf::Clock renderclock;
float fps = 1000000.00f / 60.00f;
while (window.isOpen())
{
if (renderclock.getElapsedTime().asMicroseconds() >= fps)
{
renderclock.restart();
window.clear(sf::Color::Black);
window.draw(*sprite);
window.display();
}
}
}