0

SDL_Surfaceたった1つの画像である があるとします。SDL_Surfaceその画像の 3 つのコピーを上下に並べて作成したい場合はどうすればよいでしょうか。

私はこの関数を思いつきましたが、何も表示されません:

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 倍の長さの新しいサーフェスを作成します
  • あなたが持っているもの(SDL_BlitSurface)と同様のコードを使用して、コピーimg先を新しいサーフェスにすることを除いて、から新しいサーフェスにコピーします。
  • オリジナルの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 に答える