Proper way to scale an SDL Surface without clipping?

给你一囗甜甜゛ 提交于 2019-12-01 01:10:03

问题


what is the proper way to scale an SDL Surface? I found one explanation online but it required redrawing the Surface pixel by pixel. It seems like there should be some way of doing this natively through SDL rather than redrawing the image like that. I haven't been able to find anything in the SDL documentation that covers this. I am able to resize surfaces without any problem by modifying the surfaces width and height, but the resulting surface is clipped.


回答1:


SDL doesn't support scaled blitting. According to the documentation of SDL_BlitSurface:

Note: the SDL blitter does not (yet) have the capability of scaling the blitted surfaces up or down like it is the case with other more sophisticated blitting mechanisms. You have to figure something out for yourself if you want to scale images (e.g. use SDL_gfx).

You could find SDL_gfx here. Writing your own blitting function isn't that bad, it might be a fun and useful learning experiment (though you'd be reinventing the wheel). Using OpenGL is also an option, as stuff like scaling and rotating could be done in a single function call.




回答2:


I know this answer is way too late to assist the person who asked it, but I decided to write this to help anyone who stumbles upon this question. In SDL 2.0 You can use the SDL_BlitScaled() function to scale a surface into a targeted SDL_Rect. There's a tutorial by LazyFoo which describes this, or check out this documentation.




回答3:


For completeness, and because the question does not specify SDL version, scaling is possible in SDL2 using the API method SDL_RenderCopyEx. No additional libs besides the basic SDL2 lib are needed.

int SDL_RenderCopyEx(SDL_Renderer*          renderer,
                     SDL_Texture*           texture,
                     const SDL_Rect*        srcrect,
                     const SDL_Rect*        dstrect,
                     const double           angle,
                     const SDL_Point*       center,
                     const SDL_RendererFlip flip)

By setting the size of dstrect one can scale the texture to an integer number of pixels. It is also possible to rotate and flip the texture at the same time.

Technically this is not scaling a surface but rather scaling a texture. The procedure should be as relevant though as surfaces are almost always converted to textures before the rendering happens in SDL2 based applications.

Reference: https://wiki.libsdl.org/SDL_RenderCopyEx

Create your textures as usual:

surface = IMG_Load(filePath);
texture = SDL_CreateTextureFromSurface(renderer, surface);

And when it's time to render it, call SDL_RenderCopyEx instead of SDL_RenderCopy



来源:https://stackoverflow.com/questions/328500/proper-way-to-scale-an-sdl-surface-without-clipping

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!