0

假设我有一个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;
    }
}
4

1 回答 1

1

我想你应该

  • 创建一个比初始曲面长 3 倍的新曲面
  • img使用类似于您所拥有的代码 (SDL_BlitSurface)从复制到新表面,除了将目标作为新表面
  • 原件上的 SDL_FreeSurfaceimg
  • 将新表面分配给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;
}
于 2012-11-26T05:00:00.303 回答