summaryrefslogtreecommitdiff
path: root/plugins/Chess4Net/BitmapResUnit.pas
blob: bda6d1a1e95503639f28b78995ce34e1ce2480e4 (plain)
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
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);
  public
    constructor Create(const ClientBoardSize: TSize);
    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
  g_BitmapResInstance: TBitmapRes = nil;
  arrClientBoardSizes: array[1..7] of TSize;
  bClientBoardSizesCalculated: boolean = FALSE;

////////////////////////////////////////////////////////////////////////////////
// TBitmapRes

constructor TBitmapRes.Create(const ClientBoardSize: TSize);
begin
  if (not bClientBoardSizesCalculated) then
    FCalculateClientBoardSizes(ClientBoardSize);
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;
begin
  Png := nil;
  ResBoard := nil;
  m_iSquareSize := 0;

  GetOptimalBoardSize(ClientBoardSize); // To refresh m_iSetNumber
  if (m_iSetNumber = 0) then
    exit;

  Bitmap := TBitMap.Create;
  with Bitmap do
  try
    Png := TPngObject.Create;
    Png.LoadFromResourceName(HInstance, 'BOARD' + IntToStr(m_iSetNumber));
    ResBoard := TBitmap.Create;
    ResBoard.Assign(Png);

    Width := arrClientBoardSizes[m_iSetNumber].cx;
    Height := arrClientBoardSizes[m_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
    FreeAndNil(m_ResSet);
    Png.LoadFromResourceName(HInstance, 'SET' + IntToStr(m_iSetNumber));
    m_ResSet := TBitmap.Create;
    m_ResSet.Assign(Png);

    m_iSquareSize := m_ResSet.Height;
  finally;
    ResBoard.Free;
    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
  iSquareSize, iWidth: integer;
begin
  if (m_iSetNumber = 0) then
    exit;

  iSquareSize := m_ResSet.Height;   

  iWidth := IfThen((Figure = ES), iSquareSize + iSquareSize, iSquareSize);

  Bitmap := TBitMap.Create;
  Bitmap.Width := iWidth;
  Bitmap.Height := iSquareSize;

  Bitmap.Canvas.CopyRect(Bounds(0, 0, iWidth, iSquareSize), m_ResSet.Canvas,
    Bounds(iSquareSize * PNG_SET_POS[Figure], 0, iWidth, iSquareSize));
  Bitmap.Transparent:= TRUE;
end;


function TBitmapRes.GetOptimalBoardSize(ClientSize: TSize): TSize;
var
  i: integer;
begin
  m_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];
      m_iSetNumber := i;
      exit;
    end;
  end; { for i }

  Result := Size(0, 0);
end;


procedure TBitmapRes.FCalculateClientBoardSizes(InitialSize: TSize);
var
  i: integer;
  strResName: string;
  iOptimal: integer;
  iAddX, iAddY: integer;
begin
  // Load board sizes from resources
  with TPngObject.Create do
  try
    for i := Low(arrClientBoardSizes) to High(arrClientBoardSizes) do
    begin
      strResName := 'BOARD' + IntToStr(i);
      LoadFromResourceName(HInstance, strResName);
      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;

  bClientBoardSizesCalculated := TRUE;
end;

end.