loadbmp.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. if (hBitmapFile==NULL)
  60. return(FALSE);
  61. // Retrieve the BITMAPFILEHEADER structure.
  62. ReadFile(hBitmapFile, &bitmapHeader, sizeof(BITMAPFILEHEADER), &dwRead,
  63. (LPOVERLAPPED)NULL);
  64. // Retrieve the BITMAPFILEHEADER structure.
  65. ReadFile(hBitmapFile, &bitmapInfoHeader, sizeof(BITMAPINFOHEADER),
  66. &dwRead, (LPOVERLAPPED)NULL);
  67. // Allocate memory for the BITMAPINFO structure.
  68. HGLOBAL infoHeaderMem = GlobalAlloc(GHND, sizeof(BITMAPINFOHEADER) +
  69. ((1<<bitmapInfoHeader.biBitCount) * sizeof(RGBQUAD)));
  70. LPBITMAPINFO lpHeaderMem = (LPBITMAPINFO)GlobalLock(infoHeaderMem);
  71. // Load BITMAPINFOHEADER into the BITMAPINFO structure.
  72. lpHeaderMem->bmiHeader.biSize = bitmapInfoHeader.biSize;
  73. lpHeaderMem->bmiHeader.biWidth = bitmapInfoHeader.biWidth;
  74. lpHeaderMem->bmiHeader.biHeight = bitmapInfoHeader.biHeight;
  75. lpHeaderMem->bmiHeader.biPlanes = bitmapInfoHeader.biPlanes;
  76. lpHeaderMem->bmiHeader.biBitCount = bitmapInfoHeader.biBitCount;
  77. lpHeaderMem->bmiHeader.biCompression = bitmapInfoHeader.biCompression;
  78. lpHeaderMem->bmiHeader.biSizeImage = bitmapInfoHeader.biSizeImage;
  79. lpHeaderMem->bmiHeader.biXPelsPerMeter = bitmapInfoHeader.biXPelsPerMeter;
  80. lpHeaderMem->bmiHeader.biYPelsPerMeter = bitmapInfoHeader.biYPelsPerMeter;
  81. lpHeaderMem->bmiHeader.biClrUsed = bitmapInfoHeader.biClrUsed;
  82. lpHeaderMem->bmiHeader.biClrImportant = bitmapInfoHeader.biClrImportant;
  83. // Retrieve the color table.
  84. // 1 << bitmapInfoHeader.biBitCount == 2 ^ bitmapInfoHeader.biBitCount
  85. ReadFile(hBitmapFile, lpHeaderMem->bmiColors,
  86. ((1<<bitmapInfoHeader.biBitCount) * sizeof(RGBQUAD)),
  87. &dwRead, (LPOVERLAPPED) NULL);
  88. lpLogPalette=(LPLOGPALETTE)new char[(sizeof(LOGPALETTE)+
  89. sizeof(PALETTEENTRY)*256)];
  90. lpLogPalette->palVersion=0x300;
  91. lpLogPalette->palNumEntries=256;
  92. palData=(char *)lpHeaderMem->bmiColors;
  93. for (i=0; i<256; i++)
  94. {
  95. lpLogPalette->palPalEntry[i].peRed=*palData++;
  96. lpLogPalette->palPalEntry[i].peGreen=*palData++;
  97. lpLogPalette->palPalEntry[i].peBlue=*palData++;
  98. lpLogPalette->palPalEntry[i].peFlags=*palData++;
  99. }
  100. PalHandle_=CreatePalette(lpLogPalette);
  101. delete(lpLogPalette);
  102. // Allocate memory for the required number of bytes.
  103. hmem2 = GlobalAlloc(GHND, (bitmapHeader.bfSize - bitmapHeader.bfOffBits));
  104. lpvBits = GlobalLock(hmem2);
  105. // Retrieve the bitmap data.
  106. ReadFile(hBitmapFile, lpvBits, (bitmapHeader.bfSize - bitmapHeader.bfOffBits),
  107. &dwRead, (LPOVERLAPPED) NULL);
  108. // Create a bitmap from the data stored in the .BMP file.
  109. hdc=GetDC(hwnd);
  110. select=SelectPalette(hdc,PalHandle_,0);
  111. if (select==NULL)
  112. return(FALSE);
  113. realize=RealizePalette(hdc);
  114. if (realize==GDI_ERROR)
  115. return(FALSE);
  116. BitmapHandle_=CreateDIBitmap(hdc, &bitmapInfoHeader, CBM_INIT, lpvBits, lpHeaderMem, DIB_RGB_COLORS);
  117. ReleaseDC(hwnd,hdc);
  118. if (BitmapHandle_==NULL)
  119. return(FALSE);
  120. // Unlock the global memory objects and close the .BMP file.
  121. GlobalUnlock(infoHeaderMem);
  122. GlobalUnlock(hmem2);
  123. CloseHandle(hBitmapFile);
  124. if (BitmapHandle_==NULL)
  125. return(FALSE);
  126. // Inform windows the window needs to be repainted
  127. GetClientRect(hwnd, &rect);
  128. InvalidateRect(hwnd, &rect, TRUE);
  129. UpdateWindow(hwnd);
  130. return(TRUE);
  131. }
  132. bit8 LoadBmp::drawBmp(void)
  133. {
  134. // Paint the window (and draw the bitmap).
  135. PAINTSTRUCT ps;
  136. HDC hdc;
  137. char string[128];
  138. if (BitmapHandle_ == NULL) // NAK - new
  139. return(FALSE);
  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. }