Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SDL_RenderCopyEx equivalent  (Read 607 times)

0 Members and 1 Guest are viewing this topic.

bufalo1973

  • Newbie
  • *
  • Posts: 1
    • View Profile
SDL_RenderCopyEx equivalent
« on: May 04, 2025, 05:10:14 pm »
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  :)

kojack

  • Sr. Member
  • ****
  • Posts: 367
  • C++/C# game dev teacher.
    • View Profile
Re: SDL_RenderCopyEx equivalent
« Reply #1 on: May 04, 2025, 05:39:29 pm »
When drawing onto a render texture, you need to call the render texture's display() at the end to finalise it, otherwise you won't see the result.
Try putting destination.display(); after the draw.

(Also, are you drawing the render texture onto the window? You didn't show the code where that is being done so could also be an issue)

Hapax

  • Hero Member
  • *****
  • Posts: 3422
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SDL_RenderCopyEx equivalent
« Reply #2 on: May 05, 2025, 10:45:24 pm »
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();
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*