1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
////////////////////////////////////////////////////////////////////////////////
// All code below is exclusively owned by author of Chess4Net - Pavel Perminov
// (packpaul@mail.ru, packpaul1@gmail.com).
// Any changes, modifications, borrowing and adaptation are a subject for
// explicit permition from the owner.
unit BitmapResUnit;
interface
uses
Graphics, Types,
// Chess4Net
ChessBoardHeaderUnit, ChessRulesEngine;
type
// Bitmap resources - introduced for 2009.1 (sizable board feature)
TBitmapRes = class // parametrizable class factory
private
m_ResSet: TBitmap;
m_iSetNumber: integer;
m_iSquareSize: integer;
procedure FCalculateClientBoardSizes(InitialSize: TSize);
function FGetOptimalBoardSize(const ClientSize: TSize; out iSetNumber: integer): TSize;
procedure FCalculateSetNumberFromSquareSize;
procedure FLoadPieceSet(iSetNumber: integer);
function FGetBoardResName(iSetNumber: integer): string;
function FGetSetResName(iSetNumber: integer): string;
public
constructor Create(const ClientBoardSize: TSize); overload;
constructor Create(iSquareSize: integer); overload;
destructor Destroy; override;
procedure CreateBoardBitmap(ClientBoardSize: TSize; const BackgroundColor: TColor;
out Bitmap: TBitmap);
procedure CreateFigureBitmap(const Figure: TFigure; out Bitmap: TBitmap);
function GetOptimalBoardSize(ClientSize: TSize): TSize;
property SquareSize: integer read m_iSquareSize;
end;
implementation
{$R ChessSet_PNG.RES}
uses
SysUtils, Classes, Math, pngimage;
{$J+}
const
CHB_RES_X = 4; CHB_RES_Y = 4; // starting coordinates of A8 field in resources
var
arrClientBoardSizes: array[1..7] of TSize;
g_bClientBoardSizesCalculated: boolean = FALSE;
////////////////////////////////////////////////////////////////////////////////
// TBitmapRes
constructor TBitmapRes.Create(const ClientBoardSize: TSize);
begin
inherited Create;
FCalculateClientBoardSizes(ClientBoardSize);
end;
constructor TBitmapRes.Create(iSquareSize: integer);
begin
inherited Create;
m_iSquareSize := iSquareSize;
end;
destructor TBitmapRes.Destroy;
begin
m_ResSet.Free;
inherited;
end;
procedure TBitmapRes.CreateBoardBitmap(ClientBoardSize: TSize; const BackgroundColor: TColor;
out Bitmap: TBitmap);
var
Png: TPngObject;
ResBoard: TBitmap;
iSetNumber: integer;
begin
Png := nil;
ResBoard := nil;
FGetOptimalBoardSize(ClientBoardSize, iSetNumber);
if (iSetNumber = 0) then
exit;
Bitmap := TBitMap.Create;
with Bitmap do
try
Png := TPngObject.Create;
Png.LoadFromResourceName(HInstance, FGetBoardResName(iSetNumber));
ResBoard := TBitmap.Create;
ResBoard.Assign(Png);
Width := arrClientBoardSizes[iSetNumber].cx;
Height := arrClientBoardSizes[iSetNumber].cy;
Canvas.Brush.Color := BackgroundColor;
Canvas.FillRect(Bounds(0, 0, Width, Height));
Canvas.Draw(CHB_X - CHB_RES_X, CHB_Y - CHB_RES_Y, ResBoard);
// Load appropriate set
FLoadPieceSet(iSetNumber);
finally;
m_iSetNumber := iSetNumber;
ResBoard.Free;
Png.Free;
end;
end;
procedure TBitmapRes.FLoadPieceSet(iSetNumber: integer);
var
Png: TPngObject;
begin
if (Assigned(m_ResSet) and (iSetNumber = m_iSetNumber)) then
exit;
FreeAndNil(m_ResSet);
Png := TPngObject.Create;
try
Png.LoadFromResourceName(HInstance, FGetSetResName(iSetNumber));
m_ResSet := TBitmap.Create;
m_ResSet.Assign(Png);
m_iSquareSize := m_ResSet.Height;
finally
Png.Free;
end;
end;
procedure TBitmapRes.CreateFigureBitmap(const Figure: TFigure; out Bitmap: TBitmap);
const
PNG_SET_POS: array[TFigure] of integer = (2, 4, 6, 8, 10, 12, 0, 3, 5, 7, 9, 11, 13);
var
iWidth: integer;
begin
if (m_iSetNumber = 0) then
begin
FCalculateSetNumberFromSquareSize;
if (m_iSetNumber = 0) then
exit;
end;
FLoadPieceSet(m_iSetNumber);
iWidth := IfThen((Figure = ES), m_iSquareSize + m_iSquareSize, m_iSquareSize);
Bitmap := TBitMap.Create;
Bitmap.Width := iWidth;
Bitmap.Height := m_iSquareSize;
Bitmap.Canvas.CopyRect(Bounds(0, 0, iWidth, m_iSquareSize), m_ResSet.Canvas,
Bounds(m_iSquareSize * PNG_SET_POS[Figure], 0, iWidth, m_iSquareSize));
Bitmap.Transparent:= TRUE;
end;
procedure TBitmapRes.FCalculateSetNumberFromSquareSize;
var
i: integer;
begin
m_iSetNumber := 0;
with TPngObject.Create do
try
for i := High(arrClientBoardSizes) downto Low(arrClientBoardSizes) do
begin
LoadFromResourceName(HInstance, FGetSetResName(i));
if (Height <= m_iSquareSize) then
begin
m_iSetNumber := i;
exit;
end;
end;
finally
Free;
end;
end;
function TBitmapRes.GetOptimalBoardSize(ClientSize: TSize): TSize;
var
iDummy: integer;
begin
Result := FGetOptimalBoardSize(ClientSize, iDummy);
end;
function TBitmapRes.FGetOptimalBoardSize(const ClientSize: TSize; out iSetNumber: integer): TSize;
var
i: integer;
begin
iSetNumber := 0;
for i := High(arrClientBoardSizes) downto Low(arrClientBoardSizes) do
begin
if ((ClientSize.cx >= arrClientBoardSizes[i].cx) and
(ClientSize.cy >= arrClientBoardSizes[i].cy)) then
begin
Result := arrClientBoardSizes[i];
iSetNumber := i;
exit;
end;
end; { for i }
Result := Size(0, 0);
end;
procedure TBitmapRes.FCalculateClientBoardSizes(InitialSize: TSize);
var
i: integer;
iOptimal: integer;
iAddX, iAddY: integer;
begin
if (g_bClientBoardSizesCalculated) then
exit;
// Load board sizes from resources
with TPngObject.Create do
try
for i := Low(arrClientBoardSizes) to High(arrClientBoardSizes) do
begin
LoadFromResourceName(HInstance, FGetBoardResName(i));
arrClientBoardSizes[i] := Size(Width, Height);
end;
finally
Free;
end;
// Find optimal board size from resources
iOptimal := 0;
for i := High(arrClientBoardSizes) downto Low(arrClientBoardSizes) do
begin
if ((InitialSize.cx > (arrClientBoardSizes[i].cx + CHB_X - CHB_RES_X)) and
(InitialSize.cy > (arrClientBoardSizes[i].cy + CHB_Y - CHB_RES_Y))) then
begin
iOptimal := i;
break;
end;
end;
Assert(iOptimal > 0);
// Calculate board sizes for client
iAddX := InitialSize.cx - arrClientBoardSizes[iOptimal].cx;
iAddY := InitialSize.cy - arrClientBoardSizes[iOptimal].cy;
for i := Low(arrClientBoardSizes) to High(arrClientBoardSizes) do
begin
inc(arrClientBoardSizes[i].cx, iAddX);
inc(arrClientBoardSizes[i].cy, iAddY);
end;
g_bClientBoardSizesCalculated := TRUE;
end;
function TBitmapRes.FGetBoardResName(iSetNumber: integer): string;
begin
Result := 'BOARD' + IntToStr(iSetNumber);
end;
function TBitmapRes.FGetSetResName(iSetNumber: integer): string;
begin
Result := 'SET' + IntToStr(iSetNumber);
end;
end.
|