Am I using RenderTextures in the proper way?
Yes. It's hard to say what's wrong, could you write a complete and minimal code that reproduces the problem, so that we can test it?
Here's a beautiful code to test.
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace std;
int main()
{
// define and init stuff
int iterations = 10000;
sf::RenderWindow window(sf::VideoMode(640, 480), "RenderWindow vs RenderTexture performance test");
sf::Texture texture;
texture.loadFromFile("img/hotel2.png");
sf::Sprite sprite(texture);
sf::RenderTexture screenBuffer;
screenBuffer.create(window.getSize().x, window.getSize().y);
sf::Clock clock;
sf::Event event;
sf::CircleShape shape(100.f);
int time1, time2;
// drawing directly to the RenderWindow
cout << "RenderWindow.draw x " << iterations << " = ";
clock.restart();
for(int i=0; i<iterations; ++i)
{
window.clear();
sprite.setColor(sf::Color(255, 255, 255, 100));
sprite.setPosition(20, 10);
window.draw(sprite);
sprite.setPosition(60, 10);
window.draw(sprite);
window.display();
}
time1 = clock.getElapsedTime().asMilliseconds();
cout << time1 << " ms." << endl;
// drawing to the RenderTexture
cout << "RenderTexture.draw x " << iterations << " = ";
clock.restart();
for(int i=0; i<iterations; ++i)
{
screenBuffer.clear();
sprite.setColor(sf::Color(255, 255, 255, 255));
sprite.setPosition(20, 10);
screenBuffer.draw(sprite);
sprite.setPosition(60, 10);
screenBuffer.draw(sprite);
screenBuffer.display();
sf::Sprite sp(screenBuffer.getTexture());
sp.setColor(sf::Color(255, 255, 255, 100));
window.clear();
window.draw(sp);
window.display();
}
time2 = clock.getElapsedTime().asMilliseconds();
cout << time2 << " ms." << endl;
// display results
if(time1 < time2)
{
cout << endl << "RenderWindow is " << time2/static_cast<double>(time1) << " times faster than RenderTexture." << endl;
}
else
{
cout << endl << "RenderWindow is " << time1/static_cast<double>(time2) << " times slower than RenderTexture." << endl;
}
// wait for key pressed and close screen
cout << endl << "Push any key to exit..." << endl;
while(window.isOpen())
{
while (window.pollEvent(event))
{
if(event.type == sf::Event::KeyPressed || event.type == sf::Event::Closed)
{
window.close();
}
}
}
return 0;
}
BUT! If you comment the 62nd line (
window.display(); after
window.draw(sp);), that is, the 2nd display in the 2nd test, the times are equals.
So, in this new test everything seems right, I don't know why in the old code was 15 times slower... maybe because of using and drawing shapes? :-/
At least, the effect of transparency works fine