I'm trying to update a game made in C with SDL to C++ with SFML and I have some code I think it's right but I don't know enough yet of SFML to know it's right. It's a replacemente for SDL_RenderCopyEx. The problem is the window gets created but nothing shows on screen. Maybe it's a bigger problem in my code but I think it's better to check if the base is right before checking higher level.
The code is:
sf::IntRect getBox(const sf::RenderTexture &texture, int orig_x, int orig_y)
{
sf::IntRect result;
result.left = orig_x;
result.top = orig_y;
result.width = texture.getSize().x;
result.height = texture.getSize().y;
return result;
}
sf::IntRect getBox(const sf::Texture &texture, int orig_x, int orig_y)
{
sf::IntRect result;
result.left = orig_x;
result.top = orig_y;
result.width = texture.getSize().x;
result.height = texture.getSize().y;
return result;
}
void RenderCopyEx(sf::RenderTexture &destination, sf::Texture &texture, sf::IntRect srcrect, sf::IntRect dstrect, const double angle, sf::Vector2i center, bool flip)
{
if(srcrect.width == 0 && srcrect.height == 0)
srcrect = getBox(texture);
if(dstrect.width == 0 && dstrect.height == 0)
dstrect = getBox(destination);
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setTextureRect(srcrect);
sprite.setOrigin(center.x, center.y);
sprite.rotate(angle);
if(flip)
sprite.scale(-1, 1);
sprite.move(dstrect.left, dstrect.top);
destination.draw(sprite);
}
Thank you everyone for reading :)
Everything kojack said would be the first things I would check. Did that fix it for you?
It looks suspiciously like you're using SFML 2.. ;D
I noticed that you're using integers for positions so could those positions be pixel positions instead of co-ordinates? If so, make sure your view is correct and convert between the types if necessary.
Also, consider if you are drawing in the range of the window.
sf::Sprite::rotate takes a float (in SFML 2) so you could just pass it as a float.
All in all, remember that for each frame, you must:
window.clear();
window.draw(someObject);
window.display();
If you're using a render texture, you'd need to:
//RENDER TEXTURE: clear, draw, display
renderTexture.clear();
renderTexture.draw(someObject);
renderTexture.display();
// use a "render" sprite to draw the render texture
renderSprite.setTexture(renderTexture.getTexture());
// WINDOW: clear, draw, display
window.clear();
window.draw(renderSprite);
window.display();