2 // CardLib - Card bitmap support
5 // Copyright J Brown 2001
11 #pragma comment( lib, "..\\CardLib\\cards16.lib" )
13 extern "C" HINSTANCE WINAPI
LoadLibrary16( PSTR
);
14 extern "C" void WINAPI
FreeLibrary16( HINSTANCE
);
17 #define NUMCARDBITMAPS (52+16)
19 void PaintRect(HDC hdc
, RECT
*rect
, COLORREF col
);
21 void LoadCardBitmapsFromLibrary(HINSTANCE hCardDll
, int *pwidth
, int *pheight
)
30 for(i
= 0; i
< NUMCARDBITMAPS
; i
++)
32 //convert into the range used by the cdt_xxx functions
35 if(i
< 52) val
= (i
% 4) * 13 + (i
/4);
38 hBitmap
= LoadBitmap(hCardDll
, MAKEINTRESOURCE(val
+ 1));
39 GetObject(hBitmap
, sizeof(bmp
), &bmp
);
42 height
= bmp
.bmHeight
;
44 if(i
== 0) //if first time through, create BIG bitmap..
47 __hdcCardBitmaps
= CreateCompatibleDC(hdc
);
48 __hbmCardBitmaps
= CreateCompatibleBitmap(hdc
, width
* NUMCARDBITMAPS
, height
);
49 SelectObject(__hdcCardBitmaps
, __hbmCardBitmaps
);
51 hdcCard
= CreateCompatibleDC(0);
56 hOld
= SelectObject(hdcCard
, hBitmap
);
57 BitBlt(__hdcCardBitmaps
, i
*width
, 0, width
, height
, hdcCard
, 0, 0, SRCCOPY
);
58 SelectObject(hdcCard
, hOld
);
60 //Now draw a black border around each card...
62 MoveToEx(__hdcCardBitmaps
, xpos
+2, 0, 0);
63 LineTo(__hdcCardBitmaps
, xpos
+width
- 3, 0);
64 LineTo(__hdcCardBitmaps
, xpos
+width
- 1, 2);
65 LineTo(__hdcCardBitmaps
, xpos
+width
- 1, height
- 3); //vertical
66 LineTo(__hdcCardBitmaps
, xpos
+width
- 3, height
- 1);
67 LineTo(__hdcCardBitmaps
, xpos
+2, height
- 1);
68 LineTo(__hdcCardBitmaps
, xpos
+0, height
- 3);
69 LineTo(__hdcCardBitmaps
, xpos
+0, 2);
70 LineTo(__hdcCardBitmaps
, xpos
+2, 0);
72 DeleteObject(hBitmap
);
82 void LoadCardBitmaps(void)
87 //If Windows NT/2000/XP
88 if(GetVersion() < 0x80000000)
90 hCardDll
= LoadLibrary(TEXT("cards.dll"));
94 MessageBox(0, TEXT("Error loading cards.dll (32bit)"), TEXT("Shed"), MB_OK
| MB_ICONEXCLAMATION
);
99 LoadCardBitmapsFromLibrary(hCardDll
, &__cardwidth
, &__cardheight
);
101 FreeLibrary(hCardDll
);
107 hCardDll
= LoadLibrary16("cards.dll");
111 MessageBox(0, "Error loading cards.dll (16bit)", "Shed", MB_OK
| MB_ICONEXCLAMATION
);
116 LoadCardBitmapsFromLibrary(hCardDll
, &__cardwidth
, &__cardheight
);
118 FreeLibrary16(hCardDll
);
123 void FreeCardBitmaps()
125 DeleteObject (__hbmCardBitmaps
);
126 DeleteDC (__hdcCardBitmaps
);
129 // Paint a checkered rectangle, with each alternate
130 // pixel being assigned a different colour
132 static void DrawCheckedRect(HDC hdc
, RECT
*rect
, COLORREF fg
, COLORREF bg
)
134 static WORD wCheckPat
[8] =
136 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555
141 COLORREF fgold
, bgold
;
143 hbmp
= CreateBitmap(8, 8, 1, 1, wCheckPat
);
144 hbr
= CreatePatternBrush(hbmp
);
146 //UnrealizeObject(hbr);
148 SetBrushOrgEx(hdc
, rect
->left
, rect
->top
, 0);
150 hbrold
= (HBRUSH
)SelectObject(hdc
, hbr
);
152 fgold
= SetTextColor(hdc
, fg
);
153 bgold
= SetBkColor(hdc
, bg
);
155 PatBlt(hdc
, rect
->left
, rect
->top
,
156 rect
->right
- rect
->left
,
157 rect
->bottom
- rect
->top
,
160 SetBkColor(hdc
, bgold
);
161 SetTextColor(hdc
, fgold
);
163 SelectObject(hdc
, hbrold
);
168 void GetSinkCols(COLORREF crBase
, COLORREF
*fg
, COLORREF
*bg
, COLORREF
*sh1
, COLORREF
*sh2
)
171 if(fg
) *fg
= ColorScaleRGB(crBase
, RGB(255,255,255), 0.2);//RGB(49, 99, 140);
172 if(sh1
) *sh1
= ColorScaleRGB(crBase
, RGB(0,0,0), 0.4);
173 if(sh2
) *sh2
= ColorScaleRGB(crBase
, RGB(0,0,0), 0.2);
176 HBITMAP
CreateSinkBmp(HDC hdcCompat
, HDC hdc
, COLORREF col
, int width
, int height
)
179 HBITMAP hbm
= CreateCompatibleBitmap(hdcCompat
, width
, height
);
181 HPEN hpfg
, hpbg
, hpsh
, hpsh2
;
184 COLORREF fg
, bg
, shadow
, shadow2
;
186 GetSinkCols(col
, &fg
, &bg
, &shadow
, &shadow2
);
188 hold
= SelectObject(hdc
, hbm
);
190 //fill with a solid base colour
191 SetRect(&rect
, 0,0,width
,height
);
192 PaintRect(hdc
, &rect
, MAKE_PALETTERGB(bg
));
195 hpfg
= CreatePen(PS_SOLID
, 0, MAKE_PALETTERGB(fg
));
196 hpbg
= CreatePen(PS_SOLID
, 0, MAKE_PALETTERGB(bg
));
197 hpsh
= CreatePen(PS_SOLID
, 0, MAKE_PALETTERGB(shadow
));
198 hpsh2
= CreatePen(PS_SOLID
, 0, MAKE_PALETTERGB(shadow2
));
200 hpold
= SelectObject(hdc
, hpsh
);
201 MoveToEx(hdc
, 2, 0, NULL
);
202 LineTo (hdc
, width
-3,0);
203 LineTo (hdc
, width
-1, 2);
205 SelectObject(hdc
, hpold
);
206 hpold
= SelectObject(hdc
, hpsh2
);
207 LineTo (hdc
, width
-1, height
-3); //vertical
208 LineTo (hdc
, width
-3, height
-1);
209 LineTo (hdc
, 2, height
-1);
210 LineTo (hdc
, 0, height
-3);
211 SelectObject(hdc
, hpold
);
212 hpold
= SelectObject(hdc
, hpsh
);
214 //MoveToEx( hdc, 0, height-3,0);
218 SelectObject(hdc
, hpold
);
220 //draw the highlight (vertical)
221 hpold
= SelectObject(hdc
, hpfg
);
222 MoveToEx(hdc
, width
- 2, 3, NULL
);
223 LineTo (hdc
, width
- 2, height
- 2);
226 MoveToEx(hdc
, width
- 3, height
-2, NULL
);
227 LineTo (hdc
, 3, height
-2);
228 SelectObject(hdc
, hpold
);
230 //draw the background
231 InflateRect(&rect
, -2, -2);
232 DrawCheckedRect(hdc
, &rect
, MAKE_PALETTERGB(bg
), MAKE_PALETTERGB(fg
));
234 //overwrite the top-left background pixel
235 SetPixel(hdc
, 2, 2, MAKE_PALETTERGB(bg
));
248 void CopyColor(PALETTEENTRY
*pe
, COLORREF col
)
250 pe
->peBlue
= GetBValue(col
);
251 pe
->peGreen
= GetGValue(col
);
252 pe
->peRed
= GetRValue(col
);
256 HPALETTE
MakePaletteFromCols(COLORREF cols
[], int nNumColours
)
261 // Allocate memory for the logical palette
262 lp
= (LOGPALETTE
*)HeapAlloc(
263 GetProcessHeap(), 0, sizeof(LOGPALETTE
) + sizeof(PALETTEENTRY
) * nNumColours
);
265 lp
->palNumEntries
= (WORD
)nNumColours
;
266 lp
->palVersion
= 0x300;
268 //copy the colours into the logical palette format
269 for(int i
= 0; i
< nNumColours
; i
++)
271 CopyColor(&lp
->palPalEntry
[i
], cols
[i
]);
275 hPalette
= CreatePalette(lp
);
277 HeapFree(GetProcessHeap(), 0, lp
);