gpt4 book ai didi

c++ - 包含多个图像的 SDL_surface

转载 作者:行者123 更新时间:2023-11-28 07:52:07 26 4
gpt4 key购买 nike

假设我有一个只有一张图片的 SDL_Surface。如果我想让 SDL_Surface 拥有该图像的三个拷贝,一个在另一个下面怎么办?

我想出了这个函数,但它没有显示任何东西:

void ElementView::adjust() 
{
int imageHeight = this->img->h;
int desiredHeight = 3*imageHeight;

int repetitions = desiredHeight / imageHeight ;
int remainder = desiredHeight % imageHeight ;

SDL_Surface* newSurf = SDL_CreateRGBSurface(img->flags, img->w, desiredHeight, 32, img->format->Rmask, img->format->Gmask, img->format->Bmask,img->format->Amask);

SDL_Rect rect;
memset(&rect, 0, sizeof(SDL_Rect));
rect.w = this->img->w;
rect.h = this->img->h;

for (int i = 0 ; i < repetitions ; i++)
{
rect.y = i*imageHeight;
SDL_BlitSurface(img,NULL,newSurf,&rect);
}
rect.y += remainder;
SDL_BlitSurface(this->img,NULL,newSurf,&rect);

if (newSurf != NULL) {
SDL_FreeSurface(this->img);
this->img = newSurf;
}
}

最佳答案

我觉得应该

  • 创建一个比初始表面长 3 倍的新表面
  • 使用类似于您拥有的代码 (SDL_BlitSurface) 从 img 复制到新表面,除了将目的地作为新表面
  • 原始 img 上的 SDL_FreeSurface
  • 将您的新表面分配给 img

编辑:这是一些示例代码,虽然没有时间测试它...

void adjust(SDL_Surface** img)
{
SDL_PixelFormat *fmt = (*img)->format;
SDL_Surface* newSurf = SDL_CreateRGBSurface((*img)->flags, (*img)->w, (*img)->h * 3, fmt->BytesPerPixel * 8, fmt->Rmask, fmt->Gmask, fmt->Bmask, fmt->Amask);

SDL_Rect rect;
memset(&rect, 0, sizeof(SDL_Rect));
rect.w = (*img)->w;
rect.h = (*img)->h;

int i = 0;
for (i ; i < 3; i++)
{
SDL_BlitSurface(*img,NULL,newSurf,&rect);
rect.y += (*img)->h;
}

SDL_FreeSurface(*img);
*img = newSurf;
}

关于c++ - 包含多个图像的 SDL_surface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13558538/

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