c++ - SDL 图像比例尺

标签 c++ c sdl

我正在使用 sdl 库,但它不支持缩放/调整表面大小,所以我下载了

SDL_image 1.2 和 SDL_gfx 库。我的功能/代码有效,但图像显示不佳/低

质量。

假设我有一张 100X100 的图像,如果我缩小到 95X95 或放大到 110X110

质量看起来很低,但如果我将它保留在 100X100,这与它出现在

中的尺寸相同

质量不错。如果按比例缩小,大多数图像质量都很好,但是......它确实存在

我的代码是:

int drawImage(SDL_Surface* display, const char * filename, int x, int y, int xx, int yy , const double newwidth, const double newheight, int transparent = NULL)
{



    SDL_Surface *image;
    SDL_Surface *temp;
    temp = IMG_Load(filename); if (temp == NULL) { printf("Unable to load image: %s\n", SDL_GetError()); return 1; }
    image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

        // Zoom function uses doubles for rates of scaling, rather than
    // exact size values. This is how we get around that:
    double zoomx = newwidth  / (float)image->w;
    double zoomy = newheight / (float)image->h;

    // This function assumes no smoothing, so that any colorkeys wont bleed.
    SDL_Surface* sized = zoomSurface( image, zoomx, zoomy, SMOOTHING_OFF );



    // If the original had an alpha color key, give it to the new one.
    if( image->flags & SDL_SRCCOLORKEY )
    {
        // Acquire the original Key
        Uint32 colorkey = image->format->colorkey;

        // Set to the new image
        SDL_SetColorKey( sized, SDL_SRCCOLORKEY, colorkey );
    }



    // The original picture is no longer needed.
    SDL_FreeSurface( image );

    // Set it instead to the new image.
    image =  sized;



    SDL_Rect src, dest;
    src.x = xx; src.y = yy; src.w = image->w; src.h = image->h; // size 
    dest.x =  x; dest.y = y; dest.w = image->w; dest.h = image->h;

    if(transparent == true )
     {

         //Set the color as transparent
         SDL_SetColorKey(image,SDL_SRCCOLORKEY|SDL_RLEACCEL,SDL_MapRGB(image->format,0x0,0x0,0x0)); 

     }

    else {

    }


    SDL_BlitSurface(image, &src, display, &dest);

    return true;
}

drawImage(display, "Image.png", 50, 100, NULL, NULL, 100, 100,true);

最佳答案

在不允许平滑的情况下缩放的图像会出现伪影。如果您从 SVG 开始并以您想要的比例渲染它,您可能会更幸运。 Here's an SVG -> SDL surface图书馆。

关于c++ - SDL 图像比例尺,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8645476/

相关文章:

c - C 中的胖指针

c++ - 在固定管道中使用 OpenGL 绘制时 mask 部分纹理

c - gcc 不会编译 SDL C 程序(对 SDL 函数的 undefined reference )

c++ - 如何将表驱动的 CRC 实现转换为按位实现?

c++ - 如何知道何时单击组合框的向下箭头?

C++11,使用 vs typedef,模板化

c - 提取coap查询

c++ - 在派生类的析构函数中抛出异常

比较 c 上的字符(字母)

c++ - 是否可以在 C++/SDL 中指定图像/ Sprite 的大小?