I tried recreating the problem but with minimal code. The result was the same, while the texture is being prepared the main the whole program goes horribly slow. I can't figure out what is causing this.
#include <SFML/Graphics.hpp>
bool doLoad = true;
bool status = false;
std::vector<sf::Sprite> SpriteVector;
sf::RenderTexture *OptiTexture;
sf::Texture MaskedTexture;
sf::Sprite OptiSprite;
sf::Image mMaskImage;
sf::View mNodeView;
void loadTextures()
{
while ( doLoad )
{
//Create a new rendertexture that we can draw to
if ( !OptiTexture )
{
OptiTexture = new sf::RenderTexture();
}
//Call the create command for it be valid for rendering
//This is vector containing sf::RenderTexture*
OptiTexture->create(1920, 1080);
//Set a view for the rendertexture
//The view specifies in which area of the screen that it should " look " at
OptiTexture->setView(mNodeView);
//Clear the textures with the color magenta
//The reason for this is cause it's a weird color that will probably never be used otherwise,
//which makes it ideal for masking out all the transparent areas of the texture
OptiTexture->clear(sf::Color::Magenta);
for ( std::vector<sf::Sprite>::size_type j = 0; j < SpriteVector.size(); j++ )
{
OptiTexture->draw(SpriteVector[j]);
}
//When all the tiles have been added to the texture, display it
OptiTexture->display();
//Mask away the magenta color to make the texture transparent in the right places
mMaskImage = OptiTexture->getTexture().copyToImage();
mMaskImage.createMaskFromColor(sf::Color::Magenta);
MaskedTexture.loadFromImage(mMaskImage);
OptiSprite.setTexture(MaskedTexture);
OptiSprite.setPosition(0,0);
//Signal that we are done
status = true;
}
}
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(1920, 1080), "SFML window");
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
sf::View view;
view.setSize(1920,1080);
// Load a sprite to display
sf::Texture texture;
if (!texture.loadFromFile("dirt.png"))
return EXIT_FAILURE;
sf::Sprite sprite(texture);
mNodeView.setSize(1920,1088);
mNodeView.setCenter((1920/2),(1088/2));
for ( int i = 0; i < 10; i++ )
{
for ( int j = 0; j < 10; j++ )
{
sprite.setPosition(i*16,j*16);
SpriteVector.push_back(sprite);
}
}
status = false;
sf::Thread loadThread(&loadTextures);
loadThread.launch();
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();
}
if ( sf::Keyboard::isKeyPressed(sf::Keyboard::W) )
{
view.move(sf::Vector2f(0,-10));
}
if ( sf::Keyboard::isKeyPressed(sf::Keyboard::S) )
{
view.move(sf::Vector2f(0,10));
}
if ( sf::Keyboard::isKeyPressed(sf::Keyboard::A) )
{
view.move(sf::Vector2f(-10,0));
}
if ( sf::Keyboard::isKeyPressed(sf::Keyboard::D) )
{
view.move(sf::Vector2f(10,0));
}
// Clear screen
window.clear();
window.setView(view);
sf::IntRect cameraBounds;
cameraBounds.left = view.getCenter().x - (view.getSize().x/2);
cameraBounds.top = view.getCenter().y - (view.getSize().y/2);
cameraBounds.width = view.getSize().x;
cameraBounds.height = view.getSize().y;
if ( cameraBounds.intersects(
sf::IntRect(OptiSprite.getPosition().x,
OptiSprite.getPosition().y,
1920,
1088)) )
{
if ( status == true )
{
window.draw(OptiSprite);
window.draw(sprite);
}
}
// Update the window
window.display();
}
return EXIT_SUCCESS;
}
I hope that you will be able to help me in some way, I've been trying to do this for a couple of days now.
Thank you!