BMP8.CPP 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "bmp8.h"
  19. //***********************************************************************************************
  20. BMP8::~BMP8()
  21. {
  22. // free resources
  23. if( hBitmap )
  24. ::DeleteObject( hBitmap );
  25. if( hPal )
  26. ::DeleteObject( hPal );
  27. }
  28. //***********************************************************************************************
  29. bool BMP8::Init( const char* szFile, HWND hWnd )
  30. {
  31. int i;
  32. char string[128];
  33. DWORD dwRead;
  34. BITMAPFILEHEADER bitmapHeader;
  35. BITMAPINFOHEADER bitmapInfoHeader;
  36. LPLOGPALETTE lpLogPalette;
  37. char *palData;
  38. HGLOBAL hmem2;
  39. LPVOID lpvBits;
  40. PAINTSTRUCT ps;
  41. HDC hdc;
  42. HPALETTE select;
  43. UINT realize;
  44. RECT rect;
  45. // Remember window handle for use later.
  46. this->hWnd = hWnd;
  47. // Retrieve a handle identifying the file.
  48. HANDLE hFile = ::CreateFile(
  49. szFile,
  50. GENERIC_READ,
  51. FILE_SHARE_READ,
  52. (LPSECURITY_ATTRIBUTES)NULL,
  53. OPEN_EXISTING,
  54. FILE_ATTRIBUTE_READONLY,
  55. (HANDLE)NULL );
  56. if( !hFile )
  57. return false;
  58. // Retrieve the BITMAPFILEHEADER structure.
  59. ::ReadFile( hFile, &bitmapHeader, sizeof(BITMAPFILEHEADER), &dwRead, (LPOVERLAPPED)NULL );
  60. // Retrieve the BITMAPFILEHEADER structure.
  61. ::ReadFile( hFile, &bitmapInfoHeader, sizeof(BITMAPINFOHEADER), &dwRead, (LPOVERLAPPED)NULL );
  62. // Allocate memory for the BITMAPINFO structure.
  63. HGLOBAL infoHeaderMem = ::GlobalAlloc( GHND, sizeof(BITMAPINFOHEADER) + ((1<<bitmapInfoHeader.biBitCount) * sizeof(RGBQUAD)) );
  64. LPBITMAPINFO lpHeaderMem = (LPBITMAPINFO)::GlobalLock( infoHeaderMem );
  65. // Load BITMAPINFOHEADER into the BITMAPINFO structure.
  66. lpHeaderMem->bmiHeader.biSize = bitmapInfoHeader.biSize;
  67. lpHeaderMem->bmiHeader.biWidth = bitmapInfoHeader.biWidth;
  68. lpHeaderMem->bmiHeader.biHeight = bitmapInfoHeader.biHeight;
  69. lpHeaderMem->bmiHeader.biPlanes = bitmapInfoHeader.biPlanes;
  70. lpHeaderMem->bmiHeader.biBitCount = bitmapInfoHeader.biBitCount;
  71. lpHeaderMem->bmiHeader.biCompression = bitmapInfoHeader.biCompression;
  72. lpHeaderMem->bmiHeader.biSizeImage = bitmapInfoHeader.biSizeImage;
  73. lpHeaderMem->bmiHeader.biXPelsPerMeter = bitmapInfoHeader.biXPelsPerMeter;
  74. lpHeaderMem->bmiHeader.biYPelsPerMeter = bitmapInfoHeader.biYPelsPerMeter;
  75. lpHeaderMem->bmiHeader.biClrUsed = bitmapInfoHeader.biClrUsed;
  76. lpHeaderMem->bmiHeader.biClrImportant = bitmapInfoHeader.biClrImportant;
  77. // Retrieve the color table.
  78. // 1 << bitmapInfoHeader.biBitCount == 2 ^ bitmapInfoHeader.biBitCount
  79. ::ReadFile( hFile, lpHeaderMem->bmiColors, ((1<<bitmapInfoHeader.biBitCount) * sizeof(RGBQUAD)),
  80. &dwRead, (LPOVERLAPPED)NULL );
  81. lpLogPalette = (LPLOGPALETTE)new char[ (sizeof(LOGPALETTE) + sizeof(PALETTEENTRY)*256) ];
  82. lpLogPalette->palVersion=0x300;
  83. lpLogPalette->palNumEntries=256;
  84. palData = (char*)lpHeaderMem->bmiColors;
  85. for( i = 0; i < 256; i++ )
  86. {
  87. lpLogPalette->palPalEntry[i].peRed = *palData++;
  88. lpLogPalette->palPalEntry[i].peGreen = *palData++;
  89. lpLogPalette->palPalEntry[i].peBlue = *palData++;
  90. lpLogPalette->palPalEntry[i].peFlags = *palData++;
  91. }
  92. hPal = ::CreatePalette( lpLogPalette );
  93. delete [] lpLogPalette;
  94. // Allocate memory for the required number of bytes.
  95. hmem2 = ::GlobalAlloc( GHND, (bitmapHeader.bfSize - bitmapHeader.bfOffBits) );
  96. lpvBits = ::GlobalLock( hmem2 );
  97. // Retrieve the bitmap data.
  98. ::ReadFile( hFile, lpvBits, (bitmapHeader.bfSize - bitmapHeader.bfOffBits), &dwRead, (LPOVERLAPPED)NULL );
  99. // Create a bitmap from the data stored in the .BMP file.
  100. hdc = ::GetDC( hWnd );
  101. select = ::SelectPalette( hdc, hPal, 0 );
  102. if( !select )
  103. return false;
  104. realize = ::RealizePalette( hdc );
  105. if( realize == GDI_ERROR )
  106. return false;
  107. hBMP = ::CreateDIBitmap( hdc, &bitmapInfoHeader, CBM_INIT, lpvBits, lpHeaderMem, DIB_RGB_COLORS );
  108. ::ReleaseDC( hWnd, hdc );
  109. // Unlock the global memory objects and close the .BMP file.
  110. ::GlobalUnlock( infoHeaderMem );
  111. ::GlobalUnlock( hmem2 );
  112. ::CloseHandle( hFile );
  113. if( !hBMP )
  114. return false;
  115. return true;
  116. }
  117. bit8 BMP8::drawBmp(void)
  118. {
  119. // Paint the window (and draw the bitmap).
  120. PAINTSTRUCT ps;
  121. HDC hdc;
  122. char string[128];
  123. InvalidateRect(WindowHandle_,NULL,FALSE); // keep windows from screwing up the
  124. // redrawing (as much).
  125. hdc=BeginPaint(WindowHandle_,&ps);
  126. //Do palette stuff
  127. HPALETTE select=SelectPalette(ps.hdc,PalHandle_,0);
  128. if (select==NULL)
  129. {
  130. sprintf(string,"Select Pal Fail: %d",GetLastError());
  131. MessageBox(NULL,string,"OK",MB_OK);
  132. }
  133. UINT realize=RealizePalette(ps.hdc);
  134. if (realize==GDI_ERROR)
  135. {
  136. sprintf(string,"Realize Pal Fail: %d",GetLastError());
  137. MessageBox(NULL,string,"OK",MB_OK);
  138. }
  139. HDC hdcMem = CreateCompatibleDC(ps.hdc);
  140. SelectObject(hdcMem, BitmapHandle_);
  141. BITMAP bm;
  142. GetObject(BitmapHandle_, sizeof(BITMAP), (LPSTR) &bm);
  143. /// for non-stretching version
  144. ///////BitBlt(ps.hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
  145. RECT clientRect;
  146. GetClientRect(WindowHandle_,&clientRect);
  147. SetStretchBltMode(ps.hdc,COLORONCOLOR);
  148. StretchBlt(ps.hdc,0,0,clientRect.right,clientRect.bottom,hdcMem,0,0,bm.bmWidth,
  149. bm.bmHeight,SRCCOPY);
  150. DeleteDC(hdcMem);
  151. EndPaint(WindowHandle_,&ps);
  152. return(TRUE);
  153. }