Lars Martin Hambro <lars_martin4 AT hotmail DOT com>
[reactos.git] / reactos / base / applications / games / solitaire / cardlib / cardbitmaps.cpp
1 //
2 // CardLib - Card bitmap support
3 //
4 // Freeware
5 // Copyright J Brown 2001
6 //
7 #include <windows.h>
8 #include "globals.h"
9 #include "cardcolor.h"
10
11 #ifndef __REACTOS__
12 #pragma comment( lib, "..\\CardLib\\cards16.lib" )
13
14 extern "C" HINSTANCE WINAPI LoadLibrary16( PSTR );
15 extern "C" void WINAPI FreeLibrary16( HINSTANCE );
16 #endif
17
18 #define NUMCARDBITMAPS (52+16)
19
20 void PaintRect(HDC hdc, RECT *rect, COLORREF col);
21
22 void LoadCardBitmapsFromLibrary(HINSTANCE hCardDll, int *pwidth, int *pheight)
23 {
24 HBITMAP hBitmap;
25 HDC hdcCard = NULL;
26 HANDLE hOld;
27 int i, xpos;
28 int width, height;
29 BITMAP bmp;
30
31 for(i = 0; i < NUMCARDBITMAPS; i++)
32 {
33 //convert into the range used by the cdt_xxx functions
34 int val;
35
36 if(i < 52) val = (i % 4) * 13 + (i/4);
37 else val = i;
38
39 hBitmap = LoadBitmap(hCardDll, MAKEINTRESOURCE(val + 1));
40 GetObject(hBitmap, sizeof(bmp), &bmp);
41
42 width = bmp.bmWidth;
43 height = bmp.bmHeight;
44
45 if(i == 0) //if first time through, create BIG bitmap..
46 {
47 HDC hdc = GetDC(0);
48 __hdcCardBitmaps = CreateCompatibleDC(hdc);
49 __hbmCardBitmaps = CreateCompatibleBitmap(hdc, width * NUMCARDBITMAPS, height);
50 SelectObject(__hdcCardBitmaps, __hbmCardBitmaps);
51
52 hdcCard = CreateCompatibleDC(0);
53
54 ReleaseDC(0, hdc);
55 }
56
57 hOld = SelectObject(hdcCard, hBitmap);
58 BitBlt(__hdcCardBitmaps, i*width, 0, width, height, hdcCard, 0, 0, SRCCOPY);
59 SelectObject(hdcCard, hOld);
60
61 //Now draw a black border around each card...
62 xpos = i*width;
63 MoveToEx(__hdcCardBitmaps, xpos+2, 0, 0);
64 LineTo(__hdcCardBitmaps, xpos+width - 3, 0);
65 LineTo(__hdcCardBitmaps, xpos+width - 1, 2);
66 LineTo(__hdcCardBitmaps, xpos+width - 1, height - 3); //vertical
67 LineTo(__hdcCardBitmaps, xpos+width - 3, height - 1);
68 LineTo(__hdcCardBitmaps, xpos+2, height - 1);
69 LineTo(__hdcCardBitmaps, xpos+0, height - 3);
70 LineTo(__hdcCardBitmaps, xpos+0, 2);
71 LineTo(__hdcCardBitmaps, xpos+2, 0);
72
73 DeleteObject(hBitmap);
74 }
75
76 DeleteDC(hdcCard);
77
78 *pwidth = width;
79 *pheight = height;
80
81 }
82
83 void LoadCardBitmaps(void)
84 {
85 HINSTANCE hCardDll;
86
87
88 //If Windows NT/2000/XP
89 if(GetVersion() < 0x80000000)
90 {
91 hCardDll = LoadLibrary(TEXT("cards.dll"));
92
93 if(hCardDll == 0)
94 {
95 MessageBox(0, TEXT("Error loading cards.dll (32bit)"), TEXT("Shed"), MB_OK | MB_ICONEXCLAMATION);
96 PostQuitMessage(0);
97 return;
98 }
99
100 LoadCardBitmapsFromLibrary(hCardDll, &__cardwidth, &__cardheight);
101
102 FreeLibrary(hCardDll);
103 }
104 #ifndef __REACTOS__
105 //Else, Win9X
106 else
107 {
108 hCardDll = LoadLibrary16("cards.dll");
109
110 if(hCardDll == 0)
111 {
112 MessageBox(0, "Error loading cards.dll (16bit)", "Shed", MB_OK | MB_ICONEXCLAMATION);
113 PostQuitMessage(0);
114 return;
115 }
116
117 LoadCardBitmapsFromLibrary(hCardDll, &__cardwidth, &__cardheight);
118
119 FreeLibrary16(hCardDll);
120 }
121 #endif
122 }
123
124 void FreeCardBitmaps()
125 {
126 DeleteObject (__hbmCardBitmaps);
127 DeleteDC (__hdcCardBitmaps);
128 }
129 //
130 // Paint a checkered rectangle, with each alternate
131 // pixel being assigned a different colour
132 //
133 static void DrawCheckedRect(HDC hdc, RECT *rect, COLORREF fg, COLORREF bg)
134 {
135 static WORD wCheckPat[8] =
136 {
137 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555
138 };
139
140 HBITMAP hbmp;
141 HBRUSH hbr, hbrold;
142 COLORREF fgold, bgold;
143
144 hbmp = CreateBitmap(8, 8, 1, 1, wCheckPat);
145 hbr = CreatePatternBrush(hbmp);
146
147 //UnrealizeObject(hbr);
148
149 SetBrushOrgEx(hdc, rect->left, rect->top, 0);
150
151 hbrold = (HBRUSH)SelectObject(hdc, hbr);
152
153 fgold = SetTextColor(hdc, fg);
154 bgold = SetBkColor(hdc, bg);
155
156 PatBlt(hdc, rect->left, rect->top,
157 rect->right - rect->left,
158 rect->bottom - rect->top,
159 PATCOPY);
160
161 SetBkColor(hdc, bgold);
162 SetTextColor(hdc, fgold);
163
164 SelectObject(hdc, hbrold);
165 DeleteObject(hbr);
166 DeleteObject(hbmp);
167 }
168
169 void GetSinkCols(COLORREF crBase, COLORREF *fg, COLORREF *bg, COLORREF *sh1, COLORREF *sh2)
170 {
171 if(bg) *bg = crBase;
172 if(fg) *fg = ColorScaleRGB(crBase, RGB(255,255,255), 0.2);//RGB(49, 99, 140);
173 if(sh1) *sh1 = ColorScaleRGB(crBase, RGB(0,0,0), 0.4);
174 if(sh2) *sh2 = ColorScaleRGB(crBase, RGB(0,0,0), 0.2);
175 }
176
177 HBITMAP CreateSinkBmp(HDC hdcCompat, HDC hdc, COLORREF col, int width, int height)
178 {
179 HANDLE hold, hpold;
180 HBITMAP hbm = CreateCompatibleBitmap(hdcCompat, width, height);
181
182 HPEN hpfg, hpbg, hpsh, hpsh2;
183
184 RECT rect;
185 COLORREF fg, bg, shadow, shadow2;
186
187 GetSinkCols(col, &fg, &bg, &shadow, &shadow2);
188
189 hold = SelectObject(hdc, hbm);
190
191 //fill with a solid base colour
192 SetRect(&rect, 0,0,width,height);
193 PaintRect(hdc, &rect, MAKE_PALETTERGB(bg));
194
195 //draw the outline
196 hpfg = CreatePen(PS_SOLID, 0, MAKE_PALETTERGB(fg));
197 hpbg = CreatePen(PS_SOLID, 0, MAKE_PALETTERGB(bg));
198 hpsh = CreatePen(PS_SOLID, 0, MAKE_PALETTERGB(shadow));
199 hpsh2= CreatePen(PS_SOLID, 0, MAKE_PALETTERGB(shadow2));
200
201 hpold = SelectObject(hdc, hpsh);
202 MoveToEx(hdc, 2, 0, NULL);
203 LineTo (hdc, width-3,0);
204 LineTo (hdc, width-1, 2);
205
206 SelectObject(hdc, hpold);
207 hpold = SelectObject(hdc, hpsh2);
208 LineTo (hdc, width-1, height-3); //vertical
209 LineTo (hdc, width-3, height-1);
210 LineTo (hdc, 2, height-1);
211 LineTo (hdc, 0, height-3);
212 SelectObject(hdc, hpold);
213 hpold = SelectObject(hdc, hpsh);
214
215 //MoveToEx( hdc, 0, height-3,0);
216 LineTo (hdc, 0, 2);
217 LineTo (hdc, 2, 0);
218
219 SelectObject(hdc, hpold);
220
221 //draw the highlight (vertical)
222 hpold = SelectObject(hdc, hpfg);
223 MoveToEx(hdc, width - 2, 3, NULL);
224 LineTo (hdc, width - 2, height - 2);
225
226 //(horz)
227 MoveToEx(hdc, width - 3, height-2, NULL);
228 LineTo (hdc, 3, height-2);
229 SelectObject(hdc, hpold);
230
231 //draw the background
232 InflateRect(&rect, -2, -2);
233 DrawCheckedRect(hdc, &rect, MAKE_PALETTERGB(bg), MAKE_PALETTERGB(fg));
234
235 //overwrite the top-left background pixel
236 SetPixel(hdc, 2, 2, MAKE_PALETTERGB(bg));
237
238 DeleteObject(hpsh);
239 DeleteObject(hpsh2);
240 DeleteObject(hpfg);
241 DeleteObject(hpbg);
242
243
244 return hbm;
245 }
246
247
248
249 void CopyColor(PALETTEENTRY *pe, COLORREF col)
250 {
251 pe->peBlue = GetBValue(col);
252 pe->peGreen = GetGValue(col);
253 pe->peRed = GetRValue(col);
254 pe->peFlags = 0;
255 }
256
257 HPALETTE MakePaletteFromCols(COLORREF cols[], int nNumColours)
258 {
259 LOGPALETTE *lp;
260 HPALETTE hPalette;
261
262 // Allocate memory for the logical palette
263 lp = (LOGPALETTE *)HeapAlloc(
264 GetProcessHeap(), 0, sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * nNumColours);
265
266 lp->palNumEntries = (WORD)nNumColours;
267 lp->palVersion = 0x300;
268
269 //copy the colours into the logical palette format
270 for(int i = 0; i < nNumColours; i++)
271 {
272 CopyColor(&lp->palPalEntry[i], cols[i]);
273 }
274
275 // create palette!
276 hPalette = CreatePalette(lp);
277
278 HeapFree(GetProcessHeap(), 0, lp);
279
280 return hPalette;
281 }