LOADBMP.CPP 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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"loadbmp.h"
  19. LoadBmp::LoadBmp()
  20. {
  21. BitmapHandle_=NULL;
  22. PalHandle_=NULL;
  23. }
  24. LoadBmp::~LoadBmp()
  25. {
  26. // free resources
  27. DeleteObject(BitmapHandle_);
  28. DeleteObject(PalHandle_);
  29. }
  30. //
  31. // Load a specified bitmap for later display on a window
  32. //
  33. bit8 LoadBmp::init(char *filename,HWND hwnd)
  34. {
  35. int i;
  36. char string[128];
  37. HANDLE hBitmapFile;
  38. DWORD dwRead;
  39. BITMAPFILEHEADER bitmapHeader;
  40. BITMAPINFOHEADER bitmapInfoHeader;
  41. LPLOGPALETTE lpLogPalette;
  42. char *palData;
  43. HGLOBAL hmem2;
  44. LPVOID lpvBits;
  45. PAINTSTRUCT ps;
  46. HDC hdc;
  47. HPALETTE select;
  48. UINT realize;
  49. RECT rect;
  50. // Set the member for future reference
  51. WindowHandle_=hwnd;
  52. // Retrieve a handle identifying the file.
  53. hBitmapFile = CreateFile(
  54. filename,
  55. GENERIC_READ,
  56. FILE_SHARE_READ,
  57. (LPSECURITY_ATTRIBUTES) NULL,
  58. OPEN_EXISTING,
  59. FILE_ATTRIBUTE_READONLY,
  60. (HANDLE) NULL);
  61. if (hBitmapFile==NULL)
  62. return(FALSE);
  63. // Retrieve the BITMAPFILEHEADER structure.
  64. ReadFile(hBitmapFile, &bitmapHeader, sizeof(BITMAPFILEHEADER), &dwRead,
  65. (LPOVERLAPPED)NULL);
  66. // Retrieve the BITMAPFILEHEADER structure.
  67. ReadFile(hBitmapFile, &bitmapInfoHeader, sizeof(BITMAPINFOHEADER),
  68. &dwRead, (LPOVERLAPPED)NULL);
  69. // Allocate memory for the BITMAPINFO structure.
  70. HGLOBAL infoHeaderMem = GlobalAlloc(GHND, sizeof(BITMAPINFOHEADER) +
  71. ((1<<bitmapInfoHeader.biBitCount) * sizeof(RGBQUAD)));
  72. LPBITMAPINFO lpHeaderMem = (LPBITMAPINFO)GlobalLock(infoHeaderMem);
  73. // Load BITMAPINFOHEADER into the BITMAPINFO structure.
  74. lpHeaderMem->bmiHeader.biSize = bitmapInfoHeader.biSize;
  75. lpHeaderMem->bmiHeader.biWidth = bitmapInfoHeader.biWidth;
  76. lpHeaderMem->bmiHeader.biHeight = bitmapInfoHeader.biHeight;
  77. lpHeaderMem->bmiHeader.biPlanes = bitmapInfoHeader.biPlanes;
  78. lpHeaderMem->bmiHeader.biBitCount = bitmapInfoHeader.biBitCount;
  79. lpHeaderMem->bmiHeader.biCompression = bitmapInfoHeader.biCompression;
  80. lpHeaderMem->bmiHeader.biSizeImage = bitmapInfoHeader.biSizeImage;
  81. lpHeaderMem->bmiHeader.biXPelsPerMeter = bitmapInfoHeader.biXPelsPerMeter;
  82. lpHeaderMem->bmiHeader.biYPelsPerMeter = bitmapInfoHeader.biYPelsPerMeter;
  83. lpHeaderMem->bmiHeader.biClrUsed = bitmapInfoHeader.biClrUsed;
  84. lpHeaderMem->bmiHeader.biClrImportant = bitmapInfoHeader.biClrImportant;
  85. // Retrieve the color table.
  86. // 1 << bitmapInfoHeader.biBitCount == 2 ^ bitmapInfoHeader.biBitCount
  87. ReadFile(hBitmapFile, lpHeaderMem->bmiColors,
  88. ((1<<bitmapInfoHeader.biBitCount) * sizeof(RGBQUAD)),
  89. &dwRead, (LPOVERLAPPED) NULL);
  90. lpLogPalette=(LPLOGPALETTE)new char[(sizeof(LOGPALETTE)+
  91. sizeof(PALETTEENTRY)*256)];
  92. lpLogPalette->palVersion=0x300;
  93. lpLogPalette->palNumEntries=256;
  94. palData=(char *)lpHeaderMem->bmiColors;
  95. for (i=0; i<256; i++)
  96. {
  97. lpLogPalette->palPalEntry[i].peRed=*palData++;
  98. lpLogPalette->palPalEntry[i].peGreen=*palData++;
  99. lpLogPalette->palPalEntry[i].peBlue=*palData++;
  100. lpLogPalette->palPalEntry[i].peFlags=*palData++;
  101. }
  102. PalHandle_=CreatePalette(lpLogPalette);
  103. delete(lpLogPalette);
  104. // Allocate memory for the required number of bytes.
  105. hmem2 = GlobalAlloc(GHND, (bitmapHeader.bfSize - bitmapHeader.bfOffBits));
  106. lpvBits = GlobalLock(hmem2);
  107. // Retrieve the bitmap data.
  108. ReadFile(hBitmapFile, lpvBits, (bitmapHeader.bfSize - bitmapHeader.bfOffBits),
  109. &dwRead, (LPOVERLAPPED) NULL);
  110. // Create a bitmap from the data stored in the .BMP file.
  111. hdc=GetDC(hwnd);
  112. select=SelectPalette(hdc,PalHandle_,0);
  113. if (select==NULL)
  114. return(FALSE);
  115. realize=RealizePalette(hdc);
  116. if (realize==GDI_ERROR)
  117. return(FALSE);
  118. BitmapHandle_=CreateDIBitmap(hdc, &bitmapInfoHeader, CBM_INIT, lpvBits, lpHeaderMem, DIB_RGB_COLORS);
  119. ReleaseDC(hwnd,hdc);
  120. if (BitmapHandle_==NULL)
  121. return(FALSE);
  122. // Unlock the global memory objects and close the .BMP file.
  123. GlobalUnlock(infoHeaderMem);
  124. GlobalUnlock(hmem2);
  125. CloseHandle(hBitmapFile);
  126. if (BitmapHandle_==NULL)
  127. return(FALSE);
  128. // Inform windows the window needs to be repainted
  129. GetClientRect(hwnd, &rect);
  130. InvalidateRect(hwnd, &rect, TRUE);
  131. UpdateWindow(hwnd);
  132. return(TRUE);
  133. }
  134. bit8 LoadBmp::drawBmp(void)
  135. {
  136. // Paint the window (and draw the bitmap).
  137. PAINTSTRUCT ps;
  138. HDC hdc;
  139. char string[128];
  140. InvalidateRect(WindowHandle_,NULL,FALSE); // keep windows from screwing up the
  141. // redrawing (as much).
  142. hdc=BeginPaint(WindowHandle_,&ps);
  143. //Do palette stuff
  144. HPALETTE select=SelectPalette(ps.hdc,PalHandle_,0);
  145. if (select==NULL)
  146. {
  147. sprintf(string,"Select Pal Fail: %d",GetLastError());
  148. MessageBox(NULL,string,"OK",MB_OK);
  149. }
  150. UINT realize=RealizePalette(ps.hdc);
  151. if (realize==GDI_ERROR)
  152. {
  153. sprintf(string,"Realize Pal Fail: %d",GetLastError());
  154. MessageBox(NULL,string,"OK",MB_OK);
  155. }
  156. HDC hdcMem = CreateCompatibleDC(ps.hdc);
  157. SelectObject(hdcMem, BitmapHandle_);
  158. BITMAP bm;
  159. GetObject(BitmapHandle_, sizeof(BITMAP), (LPSTR) &bm);
  160. /// for non-stretching version
  161. ///////BitBlt(ps.hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
  162. RECT clientRect;
  163. GetClientRect(WindowHandle_,&clientRect);
  164. SetStretchBltMode(ps.hdc,COLORONCOLOR);
  165. StretchBlt(ps.hdc,0,0,clientRect.right,clientRect.bottom,hdcMem,0,0,bm.bmWidth,
  166. bm.bmHeight,SRCCOPY);
  167. DeleteDC(hdcMem);
  168. EndPaint(WindowHandle_,&ps);
  169. return(TRUE);
  170. }