Proper way to scale an SDL Surface without clipping?

前端 未结 3 583
星月不相逢
星月不相逢 2021-01-12 04:51

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 o

相关标签:
3条回答
  • 2021-01-12 05:09

    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.

    0 讨论(0)
  • 2021-01-12 05:09

    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

    0 讨论(0)
  • 2021-01-12 05:21

    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.

    0 讨论(0)
提交回复
热议问题