Grant Allan
Grant Allan

Reputation: 319

How do I resize a texture in SDL?

I have this function:

void Texture::render(int x, int y, int w, int h, SDL_Renderer *&renderer, double angle, SDL_Point* center, SDL_RendererFlip flip)
{
    // Set a destination value to -1 to keep the current value
    if (x < 0) { x = rect.x; }
    if (y < 0) { y = rect.y; }
    if (w < 0) { w = rect.w; }
    if (h < 0) { h = rect.h; }

    // Create destination rectangle
    SDL_Rect dstRect = { x, y, w, h };

    // Render to screen
    SDL_RenderCopyEx(renderer, texture, &rect, &dstRect, angle, center, flip);
}

It works. It creates an image of the correct size at the location I want. But I want to add a chunk of code where it resizes the texture itself to be the size given in the destRect.

Upvotes: 0

Views: 2803

Answers (1)

Grant Allan
Grant Allan

Reputation: 319

So, anyone who finds this and reads the conversation I had with Nelfeal in the comments will see that I had a misunderstanding of how SDL_RenderCopyEx works. There's no need to resize the texture. If you need to do something like that, you can just use the dstRect when you copy it.

Actually, as far as I can find, there isn't a method to resize the actual texture itself. I'm sure one exists, but it's definitely not something people are commonly using. Which is usually a sign that it's a bad idea.

I've tweaked my code to try and simplify it, for anybody who's trying to do something similar to me:

void render(SDL_Renderer *&renderer, SDL_Rect *dstRect=NULL, SDL_Rect &srcRect=NULL, double angle=0.0, SDL_Point* center=NULL, SDL_RendererFlip flip=SDL_FLIP_NONE);
void Texture::render(SDL_Renderer *&renderer, SDL_Rect *dstRect, SDL_Rect *srcRect, double angle, SDL_Point* center, SDL_RendererFlip flip)
{
    // Check to see if a destination was provided
    bool check = false;
    if (dstRect == NULL)
    {
        check = true;
        dstRect = new SDL_Rect();
        dstRect->x = 0;
        dstRect->y = 0;
        dstRect->w = SCREEN_WIDTH;
        dstRect->h = SCREEN_HEIGHT;
    }

    // Check to see if the entire texture is being copied
    if (srcRect == NULL) { srcRect = &rect; }

    // Render to screen
    SDL_RenderCopyEx(renderer, texture, srcRect, dstRect, angle, center, flip);

    // Free dstRect
    if (check) delete dstRect;}

And it looks like this when using the function:

bgTex.render(renderer);
blobTex.render(renderer, &blobDstRect);

Upvotes: 0

Related Questions