gpt4 book ai didi

c - 使用 SDL_BlitScaled 创建缩放的曲面副本

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:05 25 4
gpt4 key购买 nike

所以我正在研究一些 SDL2 Wrapper 的东西,我正在尝试使用 SDL_BlitScaled 将 src 表面中的数据复制到我已经创建的目标表面,就像这样

SDL_Surface *loaded = IMG_Load("test.png");
SDL_SetSurfaceBlendMode(loaded, SDL_BLENDMODE_NONE);
SDL_Surface *out = SDL_CreateRGBSurface(0, 100, 100, loaded->format->BitsPerPixel,
loaded->format->Rmask, loaded->format->Gmask, loaded->format->Bmask, loaded->format->Amask);
SDL_BlitScaled(loaded, NULL, out, NULL);
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, out);
SDL_Rect rec = {10, 10, 110, 110};
SDL_RenderCopy(ren, tex, NULL, &rec);

不要担心我的渲染器或窗口等。我已经将问题隔离到这段代码的某个地方。该图像不会出现在屏幕上,但如果我从加载的表面创建纹理,它就会出现。想法?我想我滥用了 CreateRGBSurface 或 BlitScaled(我确实看到了另一个关于此的问题,但解决方案尚不清楚)。

最佳答案

对我来说,我必须这样做:

SDL_SetSurfaceBlendMode(loaded , SDL_BLENDMODE_NONE);
SDL_SetSurfaceBlendMode(out, SDL_BLENDMODE_NONE);

要使其正常工作,否则会发生一些奇怪的混合。

这个函数的文档页面说:

To copy a surface to another surface (or texture) without blending with the existing data, the blendmode of the SOURCE surface should be set to 'SDL_BLENDMODE_NONE'.

所以设置加载可能就足够了。

编辑:最后我想到了这个:

struct FreeSurface_Functor
{
void operator() (SDL_Surface* pSurface) const
{
if (pSurface)
{
SDL_FreeSurface(pSurface);
}
}
};

typedef std::unique_ptr<SDL_Surface, FreeSurface_Functor> SDL_SurfacePtr;

class SDLHelpers
{
public:
SDLHelpers() = delete;

static SDL_SurfacePtr ScaledCopy(SDL_Surface* src, SDL_Rect* dstSize)
{
SDL_SurfacePtr scaledCopy(SDL_CreateRGBSurface(0,
dstSize->w, dstSize->h,
src->format->BitsPerPixel,
src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask));

// Get the old mode
SDL_BlendMode oldBlendMode;
SDL_GetSurfaceBlendMode(src, &oldBlendMode);

// Set the new mode so copying the source won't change the source
SDL_SetSurfaceBlendMode(src, SDL_BLENDMODE_NONE);

// Do the copy
if (SDL_BlitScaled(src, NULL, scaledCopy.get(), dstSize) != 0)
{
scaledCopy.reset();
}

// Restore the original blending mode
SDL_SetSurfaceBlendMode(src, oldBlendMode);
return scaledCopy;
}
};

关于c - 使用 SDL_BlitScaled 创建缩放的曲面副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25613526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com