Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL_RenderCopyFlipped #399

Closed
SDLBugzilla opened this issue Feb 10, 2021 · 0 comments
Closed

SDL_RenderCopyFlipped #399

SDLBugzilla opened this issue Feb 10, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@SDLBugzilla
Copy link
Collaborator

This bug report was migrated from our old Bugzilla tracker.

Reported in version: HG 2.0
Reported for operating system, platform: Windows 7, x86

Comments on the original bug report:

On 2011-04-09 16:45:53 +0000, Mason Wheeler wrote:

Adding support for a RenderCopy operation flipped around the X, Y or both axes.

Add the following two definitions to SDL_render.h:

typedef enum
{
SDL_RENDERFLIP_HORIZ = 0x00000001,
SDL_RENDERFLIP_VERT = 0x00000002,
SDL_RENDERFLIP_BOTH = 0x00000003
} SDL_RenderFlip;

...

/**

  • \brief Copy a portion of the texture to the current rendering target, flipped
  •     around one or both axes.
    
  • \param texture The source texture.
  • \param srcrect A pointer to the source rectangle, or NULL for the entire
  •               texture.
    
  • \param dstrect A pointer to the destination rectangle, or NULL for the
  •               entire rendering target.
    
  • \param axes Which axes to flip the image around.
  • \return 0 on success, or -1 on error
    */
    extern DECLSPEC int SDLCALL SDL_RenderCopyFlipped(SDL_Renderer * renderer,
    SDL_Texture * texture,
    const SDL_Rect * srcrect,
    const SDL_Rect * dstrect,
    SDL_RenderFlip axes);

Then replace the implementation of SDL_RenderCopy with:

SDL_bool SDL_ClipRects(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect,
SDL_Rect * real_srcrect, SDL_Rect * real_dstrect)
{
real_srcrect->x = 0;
real_srcrect->y = 0;
real_srcrect->w = texture->w;
real_srcrect->h = texture->h;
if (srcrect) {
if (!SDL_IntersectRect(srcrect, real_srcrect, real_srcrect)) {
return SDL_FALSE;
}
}

real_dstrect->x = 0;
real_dstrect->y = 0;
real_dstrect->w = renderer->viewport.w;
real_dstrect->h = renderer->viewport.h;
if (dstrect) {
    if (!SDL_IntersectRect(dstrect, real_dstrect, real_dstrect)) {
        return SDL_FALSE;
    }
    /* Clip srcrect by the same amount as dstrect was clipped */
    if (dstrect->w != real_dstrect->w) {
        int deltax = (real_dstrect->x - dstrect->x);
        int deltaw = (real_dstrect->w - dstrect->w);
        real_srcrect->x += (deltax * real_srcrect->w) / dstrect->w;
        real_srcrect->w += (deltaw * real_srcrect->w) / dstrect->w;
    }
    if (dstrect->h != real_dstrect->h) {
        int deltay = (real_dstrect->y - dstrect->y);
        int deltah = (real_dstrect->h - dstrect->h);
        real_srcrect->y += (deltay * real_srcrect->h) / dstrect->h;
        real_srcrect->h += (deltah * real_srcrect->h) / dstrect->h;
    }
}

return SDL_TRUE;
}

int
SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
{
SDL_Rect real_srcrect;
SDL_Rect real_dstrect;

CHECK_RENDERER_MAGIC(renderer, -1);
CHECK_TEXTURE_MAGIC(texture, -1);

if (renderer != texture->renderer) {
    SDL_SetError("Texture was not created with this renderer");
    return -1;
}

if (!SDL_ClipRects(renderer, texture, srcrect, dstrect, &real_srcrect,
&real_dstrect))
return 0;

if (texture->native) {
    texture = texture->native;
}

return renderer->RenderCopy(renderer, texture, &real_srcrect,
                            &real_dstrect);

}

int
SDL_RenderCopyFlipped(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect,
int axes)
{
SDL_Rect real_srcrect;
SDL_Rect real_dstrect;

CHECK_RENDERER_MAGIC(renderer, -1);
CHECK_TEXTURE_MAGIC(texture, -1);

if (renderer != texture->renderer) {
    SDL_SetError("Texture was not created with this renderer");
    return -1;
}

if (!SDL_ClipRects(renderer, texture, srcrect, dstrect, &real_srcrect,
&real_dstrect))
return 0;

if (texture->native) {
    texture = texture->native;
}

if (axes & SDL_RENDERFLIP_HORIZ)
{
real_dstrect.x += real_dstrect.w;
real_dstrect.w *= -1;
}

if (axes & SDL_RENDERFLIP_VERT)
{
real_dstrect.y += real_dstrect.h;
real_dstrect.h *= -1;
}

return renderer->RenderCopy(renderer, texture, &real_srcrect,
                            &real_dstrect);

}

On 2012-01-07 00:59:00 +0000, Sam Lantinga wrote:

Does this work for the software and Direct3D renderers as well?

On 2012-01-08 05:31:23 +0000, Mason Wheeler wrote:

(In reply to comment # 1)

Does this work for the software and Direct3D renderers as well?

It should work for Direct3D, for the same reason it works for OpenGL: all it does is reverse the vertices for the texture coordinates. Not sure how it will work with non-accelerated renderers that don't use the same system.

On 2012-01-08 10:48:53 +0000, Sam Lantinga wrote:

I'm pretty sure the software renderer wouldn't do the right thing. ;)

There's very similar discussion in bug 1308, maybe you want to drop by and comment?

On 2012-06-21 07:51:57 +0000, Gabriel Jacobo wrote:

SDL_RenderCopyEx has landed.

@SDLBugzilla SDLBugzilla added the enhancement New feature or request label Feb 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant