Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SDL_Surface *Video::LoadImageResource(std::string resname,unsigned int resid)
- {
- SDL_Surface* temp_bmp = nullptr;
- BITMAP bm;
- HMODULE module = LoadLibrary( resname.c_str() );
- if(!module) return temp_bmp = nullptr;
- HBITMAP bitmap = LoadBitmap( module, MAKEINTRESOURCE( resid ) );
- if (!bitmap)
- {
- FreeLibrary(module);
- return temp_bmp = nullptr;
- }
- if (!GetObject( bitmap, sizeof( bm ), reinterpret_cast<LPSTR>( &bm ) ) )
- {
- DeleteObject(bitmap);
- FreeLibrary(module);
- return temp_bmp = nullptr;
- }
- temp_bmp = SDL_CreateRGBSurface(SDL_SWSURFACE, bm.bmWidth, bm.bmHeight , 32, 0, 0, 0, 0);
- if (!temp_bmp)
- {
- DeleteObject(bitmap);
- FreeLibrary(module);
- return temp_bmp = nullptr;
- }
- SDL_LockSurface(temp_bmp);
- BYTE *data = reinterpret_cast<BYTE *>(temp_bmp->pixels);
- int pitch = bm.bmWidth * temp_bmp->format->BytesPerPixel;
- pitch = (pitch + 3 ) & ~3;
- BYTE *pixels = new BYTE[bm.bmHeight * pitch];
- BITMAPINFOHEADER bi;
- ZeroMemory(&bi, sizeof(BITMAPINFOHEADER));
- bi.biSize = sizeof(BITMAPINFOHEADER);
- bi.biBitCount = 32; //need to get actuall bitcount
- bi.biPlanes = 1;
- bi.biWidth = bm.bmWidth;
- bi.biHeight = -abs( bm.bmHeight );
- bi.biClrUsed = 256;
- bi.biCompression = BI_RGB;
- BITMAPINFO *binfo = static_cast<BITMAPINFO *>( std::malloc( sizeof( BITMAPINFO ) + sizeof( RGBQUAD ) * 256 ) );
- binfo->bmiHeader = bi;
- HDC hdc = GetDC( NULL );
- GetDIBits(hdc, bitmap, 0, bm.bmHeight, pixels, binfo, DIB_RGB_COLORS);
- delete(binfo);
- int height = std::abs(std::min (int(bm.bmHeight), int(temp_bmp->h)));
- int width = std::abs(std::min(pitch,int(temp_bmp->pitch)));
- for ( int y = 0; y < height; ++y )
- std::memcpy( data + temp_bmp->pitch * y, pixels + pitch * y, width );
- ReleaseDC( NULL, hdc );
- DeleteObject( bitmap );
- FreeLibrary( module );
- delete[] pixels;
- SDL_UnlockSurface(temp_bmp);
- if (temp_bmp)return temp_bmp;
- return temp_bmp = nullptr;
- }
Add Comment
Please, Sign In to add comment