源始终为 320x240,目标始终为 640x480。
void DoDoubleScaling(SDL_Surface* dest, SDL_Surface* source)
{
assert(dest->w == source->w*2);
assert(dest->h == source->h*2);
for (int y = 0; y < source->h; ++y)
{
for (int x = 0; x < source->w; ++x)
{
SetPixel(dest, x*2, y*2, GetPixel(source, x, y));
SetPixel(dest, x*2+1, y*2+1, GetPixel(source, x, y));
}
}
}
输出如下所示: (请务必以全尺寸查看)。本质上,每隔一个像素都会丢失。我尝试了各种可能性,但找不到哪里出错了。
GetPixel
和SetPixel
只需在给定X和Y[和颜色]的情况下设置/接收表面的颜色。
最佳答案
用途:
SetPixel(dest, x*2, y*2, GetPixel(source, x, y));
SetPixel(dest, x*2, y*2+1, GetPixel(source, x, y));
SetPixel(dest, x*2+1, y*2, GetPixel(source, x, y));
SetPixel(dest, x*2+1, y*2+1, GetPixel(source, x, y));
而不是:
SetPixel(dest, x*2, y*2, GetPixel(source, x, y));
SetPixel(dest, x*2+1, y*2+1, GetPixel(source, x, y));
为了加快速度:存储 GetPixel(source, x, y) 的返回值,因此您不需要每轮调用它 4 次。
关于c - 尝试将 SDL_Surface 的大小加倍严重失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7076665/