SDL2 (C++) How to render an image smaller

Viewed 907

I have finally started getting used to SDL 2's basic functions for rendering and I have stumbled across a problem that I believe the public might be able to answer. In my code I generate some text and using some code from a tutorial, am able to load the text as a texture (namely Lazy foo's tutorial). This texture now has a width and a height based on font size and how much text was entered. Another function I use loads in a square made of fancy boardering that I wish to use as a menu. This square is 200x200. As an example, if the text texture is 100x160, I want the square to now render as perhaps a 120x180 image (essentially compressing it to be a similar size as the text texture.

tl;dr:

I have 200x200 square.

I have 100x160 text texture

I want to render 200x200 square as a 120x160 square and render 100x160 text inside square.

***loadFromRenderedText takes a ttf font, a string, and a color (RGBA) to create an image texture based on the string -> generates own width/height

    menuTextTexture.loadFromRenderedText(menuFont, "Info Item Skill Back",menuTextColor);
    menuSize.x = 0;
    menuSize.y = 0;
    menuSize.w = menuTextTexture.getWidth() + boarderW;
    menuSize.h = menuTextTexture.getHeight() + boarderW;

***menuSize is an SDL_Rect

    menuBoxTexture.TextRender(XmenuRenderLocX, XmenuRenderLocY,  &menuSize, 0, NULL, SDL_FLIP_NONE);
    menuTextTexture.render(XmenuRenderLocX+boarderW, XmenuRenderLocY+boarderW);

TextRender and render do the same thing except render uses a scaling factor to multiply the clip size to be bigger (which I leave blank -> clip is then NULL and the basic height/width are used). For TextRender, I specify the render dimensions by passing the menuSize SDL rect. this takes the 200x200 square and renders only the 120x160 of the square at the (XmenuRenderLocX, XmenuRenderLocY)... thus essentially cropping the square, which is not what I want... I want to resize the square.

Any help will be greatly appreciated

1 Answers

Originally, I was using the provided LTexture::render function that was created for Lazy Foo's tutorial. See below code

    void LTexture::render( int x, int y, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip )
{
//Set rendering space and render to screen
SDL_Rect renderQuad = { x, y, mWidth, mHeight };

//Set clip rendering dimensions
if( clip != NULL )
{
    renderQuad.w = SCALE_SIZE*(clip->w);
    renderQuad.h = SCALE_SIZE*(clip->h);
}
else if(mTexture ==NULL)
{
    printf("Warning: Texture to Render is NULL!\n");
}

//Render to screen
SDL_RenderCopyEx( gRenderer, mTexture, clip, &renderQuad, angle, center, flip );
}

But because I didn't fully understand until now how exactly the function renders, I wasn't actually telling it to render at a new dimension (except in the case where I magnify everything with a SCALE_SIZE)

I made a new function for more control

    void LTexture::DefinedRender(SDL_Rect* Textureclip, SDL_Rect* renderLocSize, double angle, SDL_Point* center, SDL_RendererFlip flip)
{
//Set clip rendering dimensions
if(mTexture ==NULL)
{
    printf("Warning: Texture to Render is NULL!\n");
}
//Render to screen
SDL_RenderCopyEx( gRenderer, mTexture, Textureclip, renderLocSize, angle, center, flip );
}

And now everything works like I want it to.

Related