简体   繁体   English

SDL2(C ++)如何缩小图像

[英]SDL2 (C++) How to render an image smaller

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. 我终于开始习惯于SDL 2的基本渲染功能,并且偶然发现了一个我相信公众可能会回答的问题。 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). 在我的代码中,我生成了一些文本,并使用了教程中的一些代码,能够将文本加载为纹理(即Lazy foo的教程)。 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. 这个正方形是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. 例如,如果文本纹理为100x160,我希望正方形现在可以渲染为120x180图像(实质上将其压缩为与文本纹理相似的大小)。

tl;dr: TL;博士:

I have 200x200 square. 我有200x200平方。

I have 100x160 text texture 我有100x160的文字纹理

I want to render 200x200 square as a 120x160 square and render 100x160 text inside square. 我想将200x200正方形渲染为120x160正方形,并在正方形内部渲染100x160文本。

***loadFromRenderedText takes a ttf font, a string, and a color (RGBA) to create an image texture based on the string -> generates own width/height *** loadFromRenderedText采用ttf字体,字符串和颜色(RGBA)以基于字符串创建图像纹理->生成自己的宽度/高度

    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 *** menuSize是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). TextRender和render做相同的事情,除了render使用缩放因子将片段大小乘以更大(我留为空白-> clip为NULL并使用基本的高度/宽度)。 For TextRender, I specify the render dimensions by passing the menuSize SDL rect. 对于TextRender,我通过传递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. 这需要200x200的正方形,并且仅在(XmenuRenderLocX,XmenuRenderLocY)处渲染正方形的120x160 ...因此实际上是裁剪正方形,这不是我想要的...我想调整正方形的大小。

Any help will be greatly appreciated 任何帮助将不胜感激

Originally, I was using the provided LTexture::render function that was created for Lazy Foo's tutorial. 最初,我使用的是为Lazy Foo的教程创建的LTexture :: render函数。 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) 但是因为直到现在我还不完全了解该函数的呈现方式,所以我实际上并没有告诉它以新的尺寸呈现(除非使用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. 现在一切正常,就像我想要的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM