2013-11-27 44 views

回答

11

下面是一個用於從我正在編寫的庫中保存SDL 2截圖的功能。

bool saveScreenshotBMP(std::string filepath, SDL_Window* SDLWindow, SDL_Renderer* SDLRenderer) { 
    SDL_Surface* saveSurface = NULL; 
    SDL_Surface* infoSurface = NULL; 
    infoSurface = SDL_GetWindowSurface(SDLWindow); 
    if (infoSurface == NULL) { 
     std::cerr << "Failed to create info surface from window in saveScreenshotBMP(string), SDL_GetError() - " << SDL_GetError() << "\n"; 
    } else { 
     unsigned char * pixels = new (std::nothrow) unsigned char[infoSurface->w * infoSurface->h * infoSurface->format->BytesPerPixel]; 
     if (pixels == 0) { 
      std::cerr << "Unable to allocate memory for screenshot pixel data buffer!\n"; 
      return false; 
     } else { 
      if (SDL_RenderReadPixels(SDLRenderer, &infoSurface->clip_rect, infoSurface->format->format, pixels, infoSurface->w * infoSurface->format->BytesPerPixel) != 0) { 
       std::cerr << "Failed to read pixel data from SDL_Renderer object. SDL_GetError() - " << SDL_GetError() << "\n"; 
       pixels = NULL; 
       return false; 
      } else { 
       saveSurface = SDL_CreateRGBSurfaceFrom(pixels, infoSurface->w, infoSurface->h, infoSurface->format->BitsPerPixel, infoSurface->w * infoSurface->format->BytesPerPixel, infoSurface->format->Rmask, infoSurface->format->Gmask, infoSurface->format->Bmask, infoSurface->format->Amask); 
       if (saveSurface == NULL) { 
        std::cerr << "Couldn't create SDL_Surface from renderer pixel data. SDL_GetError() - " << SDL_GetError() << "\n"; 
        return false; 
       } 
       SDL_SaveBMP(saveSurface, filepath.c_str()); 
       SDL_FreeSurface(saveSurface); 
       saveSurface = NULL; 
      } 
      delete[] pixels; 
     } 
     SDL_FreeSurface(infoSurface); 
     infoSurface = NULL; 
    } 
    return true; 
} 

乾杯! -Neil

+2

'像素=空;'應該'刪除[]像素;',否則數組被泄漏。 – emlai

6

如果您使用OpenGL和SDL2,則可以直接調用glReadPixels而不是使用信息表面和渲染器。這裏是一個例子(沒有錯誤檢查)。

void Screenshot(int x, int y, int w, int h, const char * filename) 
{ 
    unsigned char * pixels = new unsigned char[w*h*4]; // 4 bytes for RGBA 
    glReadPixels(x,y,w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 

    SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0,0,0,0); 
    SDL_SaveBMP(surf, filename); 

    SDL_FreeSurface(surf); 
    delete [] pixels; 
} 

這裏的SDL wiki page與窗口和OpenGL上下文設置的一個例子。