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