2 // CardLib - Card bitmap support
5 // Copyright J Brown 2001
12 #pragma comment( lib, "..\\CardLib\\cards16.lib" )
14 extern "C" HINSTANCE WINAPI
LoadLibrary16( PSTR
);
15 extern "C" void WINAPI
FreeLibrary16( HINSTANCE
);
18 #define NUMCARDBITMAPS (52+16)
20 void PaintRect(HDC hdc
, RECT
*rect
, COLORREF col
);
22 void LoadCardBitmapsFromLibrary(HINSTANCE hCardDll
, int *pwidth
, int *pheight
)
31 for(i
= 0; i
< NUMCARDBITMAPS
; i
++)
33 //convert into the range used by the cdt_xxx functions
36 if(i
< 52) val
= (i
% 4) * 13 + (i
/4);
39 hBitmap
= LoadBitmap(hCardDll
, MAKEINTRESOURCE(val
+ 1));
40 GetObject(hBitmap
, sizeof(bmp
), &bmp
);
43 height
= bmp
.bmHeight
;
45 if(i
== 0) //if first time through, create BIG bitmap..
48 __hdcCardBitmaps
= CreateCompatibleDC(hdc
);
49 __hbmCardBitmaps
= CreateCompatibleBitmap(hdc
, width
* NUMCARDBITMAPS
, height
);
50 SelectObject(__hdcCardBitmaps
, __hbmCardBitmaps
);
52 hdcCard
= CreateCompatibleDC(0);
57 hOld
= SelectObject(hdcCard
, hBitmap
);
58 BitBlt(__hdcCardBitmaps
, i
*width
, 0, width
, height
, hdcCard
, 0, 0, SRCCOPY
);
59 SelectObject(hdcCard
, hOld
);
61 //Now draw a black border around each card...
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);
73 DeleteObject(hBitmap
);
83 void LoadCardBitmaps(void)
88 //If Windows NT/2000/XP
89 if(GetVersion() < 0x80000000)
91 hCardDll
= LoadLibrary(TEXT("cards.dll"));
95 MessageBox(0, TEXT("Error loading cards.dll (32bit)"), TEXT("Shed"), MB_OK
| MB_ICONEXCLAMATION
);
100 LoadCardBitmapsFromLibrary(hCardDll
, &__cardwidth
, &__cardheight
);
102 FreeLibrary(hCardDll
);
108 hCardDll
= LoadLibrary16("cards.dll");
112 MessageBox(0, "Error loading cards.dll (16bit)", "Shed", MB_OK
| MB_ICONEXCLAMATION
);
117 LoadCardBitmapsFromLibrary(hCardDll
, &__cardwidth
, &__cardheight
);
119 FreeLibrary16(hCardDll
);
124 void FreeCardBitmaps()
126 DeleteObject (__hbmCardBitmaps
);
127 DeleteDC (__hdcCardBitmaps
);
130 // Paint a checkered rectangle, with each alternate
131 // pixel being assigned a different colour
133 static void DrawCheckedRect(HDC hdc
, RECT
*rect
, COLORREF fg
, COLORREF bg
)
135 static WORD wCheckPat
[8] =
137 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555
142 COLORREF fgold
, bgold
;
144 hbmp
= CreateBitmap(8, 8, 1, 1, wCheckPat
);
145 hbr
= CreatePatternBrush(hbmp
);
147 //UnrealizeObject(hbr);
149 SetBrushOrgEx(hdc
, rect
->left
, rect
->top
, 0);
151 hbrold
= (HBRUSH
)SelectObject(hdc
, hbr
);
153 fgold
= SetTextColor(hdc
, fg
);
154 bgold
= SetBkColor(hdc
, bg
);
156 PatBlt(hdc
, rect
->left
, rect
->top
,
157 rect
->right
- rect
->left
,
158 rect
->bottom
- rect
->top
,
161 SetBkColor(hdc
, bgold
);
162 SetTextColor(hdc
, fgold
);
164 SelectObject(hdc
, hbrold
);
169 void GetSinkCols(COLORREF crBase
, COLORREF
*fg
, COLORREF
*bg
, COLORREF
*sh1
, COLORREF
*sh2
)
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);
177 HBITMAP
CreateSinkBmp(HDC hdcCompat
, HDC hdc
, COLORREF col
, int width
, int height
)
180 HBITMAP hbm
= CreateCompatibleBitmap(hdcCompat
, width
, height
);
182 HPEN hpfg
, hpbg
, hpsh
, hpsh2
;
185 COLORREF fg
, bg
, shadow
, shadow2
;
187 GetSinkCols(col
, &fg
, &bg
, &shadow
, &shadow2
);
189 hold
= SelectObject(hdc
, hbm
);
191 //fill with a solid base colour
192 SetRect(&rect
, 0,0,width
,height
);
193 PaintRect(hdc
, &rect
, MAKE_PALETTERGB(bg
));
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
));
201 hpold
= SelectObject(hdc
, hpsh
);
202 MoveToEx(hdc
, 2, 0, NULL
);
203 LineTo (hdc
, width
-3,0);
204 LineTo (hdc
, width
-1, 2);
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
);
215 //MoveToEx( hdc, 0, height-3,0);
219 SelectObject(hdc
, hpold
);
221 //draw the highlight (vertical)
222 hpold
= SelectObject(hdc
, hpfg
);
223 MoveToEx(hdc
, width
- 2, 3, NULL
);
224 LineTo (hdc
, width
- 2, height
- 2);
227 MoveToEx(hdc
, width
- 3, height
-2, NULL
);
228 LineTo (hdc
, 3, height
-2);
229 SelectObject(hdc
, hpold
);
231 //draw the background
232 InflateRect(&rect
, -2, -2);
233 DrawCheckedRect(hdc
, &rect
, MAKE_PALETTERGB(bg
), MAKE_PALETTERGB(fg
));
235 //overwrite the top-left background pixel
236 SetPixel(hdc
, 2, 2, MAKE_PALETTERGB(bg
));
249 void CopyColor(PALETTEENTRY
*pe
, COLORREF col
)
251 pe
->peBlue
= GetBValue(col
);
252 pe
->peGreen
= GetGValue(col
);
253 pe
->peRed
= GetRValue(col
);
257 HPALETTE
MakePaletteFromCols(COLORREF cols
[], int nNumColours
)
262 // Allocate memory for the logical palette
263 lp
= (LOGPALETTE
*)HeapAlloc(
264 GetProcessHeap(), 0, sizeof(LOGPALETTE
) + sizeof(PALETTEENTRY
) * nNumColours
);
266 lp
->palNumEntries
= (WORD
)nNumColours
;
267 lp
->palVersion
= 0x300;
269 //copy the colours into the logical palette format
270 for(int i
= 0; i
< nNumColours
; i
++)
272 CopyColor(&lp
->palPalEntry
[i
], cols
[i
]);
276 hPalette
= CreatePalette(lp
);
278 HeapFree(GetProcessHeap(), 0, lp
);