2012-11-26 100 views
0

假設我有一個SDL_Surface只是一個圖像。 如果我想讓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; 
    } 
} 
+0

有任何原因,你不能只是幾次blit原始圖像? – Xymostech

+0

@Xymostech,我不應該觸摸實際繪製表面到屏幕上的代碼。 –

+0

我認爲問題在於'img'不夠大,無法在其中保存3個副本。看起來好像沒有一種創建具有任意大小的新曲面的好方法。爲什麼不能觸摸實際繪製的代碼? – Xymostech

回答

1

我想你應該

  • 創建一個新的表面,只要3次爲一個初始
  • img複製到新表面使用類似於您擁有的代碼(SDL_BlitSurface),但將目標作爲新表面
  • SDL你原來的img
  • _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; 
} 
+0

我這樣做了,但它沒有在屏幕上顯示任何內容? –

+0

@ l19你確定你確實改變了真實的圖像,而不僅僅是你的指針嗎?您可能已經釋放了正確的圖像,然後忘記保存新圖像。提示:你將需要一個雙指針來正確地做到這一點。 – Xymostech

+0

'SDL_Surface * surface = SDL_CreateRGBSurface(....); !SDL_BlitSurface(IMG,NULL,表面,&rect); 如果(表面= NULL){ \t \t SDL_FreeSurface(IMG); \t \t IMG =表面; \t}' –