Guest User

crapload egf

a guest
Jan 19th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. SDL_Surface *Video::LoadImageResource(std::string resname,unsigned int resid)
  2. {
  3.     SDL_Surface* temp_bmp = nullptr;
  4.     BITMAP bm;
  5.  
  6.     HMODULE module = LoadLibrary( resname.c_str() );
  7.     if(!module) return temp_bmp = nullptr;
  8.  
  9.     HBITMAP bitmap = LoadBitmap( module, MAKEINTRESOURCE( resid ) );
  10.     if (!bitmap)
  11.     {
  12.         FreeLibrary(module);
  13.         return temp_bmp = nullptr;
  14.     }
  15.  
  16.     if (!GetObject( bitmap, sizeof( bm ), reinterpret_cast<LPSTR>( &bm ) ) )
  17.     {
  18.         DeleteObject(bitmap);
  19.         FreeLibrary(module);
  20.         return temp_bmp = nullptr;
  21.     }
  22.  
  23.     temp_bmp = SDL_CreateRGBSurface(SDL_SWSURFACE, bm.bmWidth, bm.bmHeight , 32, 0, 0, 0, 0);
  24.     if (!temp_bmp)
  25.     {
  26.         DeleteObject(bitmap);
  27.         FreeLibrary(module);
  28.         return temp_bmp = nullptr;
  29.     }
  30.  
  31.     SDL_LockSurface(temp_bmp);
  32.     BYTE *data = reinterpret_cast<BYTE *>(temp_bmp->pixels);
  33.     int pitch = bm.bmWidth * temp_bmp->format->BytesPerPixel;
  34.     pitch = (pitch + 3 ) & ~3;
  35.  
  36.     BYTE *pixels = new BYTE[bm.bmHeight * pitch];
  37.  
  38.     BITMAPINFOHEADER bi;
  39.     ZeroMemory(&bi, sizeof(BITMAPINFOHEADER));
  40.     bi.biSize = sizeof(BITMAPINFOHEADER);
  41.     bi.biBitCount = 32; //need to get actuall bitcount
  42.     bi.biPlanes = 1;
  43.     bi.biWidth = bm.bmWidth;
  44.     bi.biHeight = -abs( bm.bmHeight );
  45.     bi.biClrUsed = 256;
  46.     bi.biCompression = BI_RGB;
  47.  
  48.     BITMAPINFO *binfo = static_cast<BITMAPINFO *>( std::malloc( sizeof( BITMAPINFO ) + sizeof( RGBQUAD ) * 256 ) );
  49.     binfo->bmiHeader = bi;
  50.  
  51.     HDC hdc = GetDC( NULL );
  52.     GetDIBits(hdc, bitmap, 0, bm.bmHeight, pixels, binfo, DIB_RGB_COLORS);
  53.  
  54.     delete(binfo);
  55.  
  56.     int height = std::abs(std::min (int(bm.bmHeight), int(temp_bmp->h)));
  57.     int width = std::abs(std::min(pitch,int(temp_bmp->pitch)));
  58.  
  59.     for ( int y = 0; y < height; ++y )
  60.         std::memcpy( data + temp_bmp->pitch * y, pixels + pitch * y, width );
  61.  
  62.     ReleaseDC( NULL, hdc );
  63.     DeleteObject( bitmap );
  64.     FreeLibrary( module );
  65.  
  66.     delete[] pixels;
  67.  
  68.     SDL_UnlockSurface(temp_bmp);
  69.  
  70.     if (temp_bmp)return temp_bmp;
  71.  
  72.     return temp_bmp = nullptr;
  73. }
Add Comment
Please, Sign In to add comment