loadbmp.cpp 6.3 KB

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