-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLemMenuFont.pas
357 lines (315 loc) · 8.39 KB
/
LemMenuFont.pas
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
unit LemMenuFont;
interface
uses
Types, UITypes,
LemTypes, LemStrings,
Dialogs, Controls, Forms,
GR32,
Classes, SysUtils,
SharedGlobals;
const
MENU_FONT_COUNT = ord(#126) - ord('!') + 1;
MenuFontCharSet = [#32..#126] - [#32];
CHARACTER_WIDTH = 16;
CHARACTER_HEIGHT = 19;
HALF_LINE_FEED = 10;
type
TextLineInfo = record
Line: string;
yPos: Integer;
ColorShift: TColorDiff;
end;
TextLineArray = array of TextLineInfo;
TMenuFont = class
private
function GetBitmapOfChar(Ch: Char): TBitmap32;
procedure Combine(F: TColor32; var B: TColor32; M: Cardinal);
procedure MakeList(const S: string; aList: TStrings);
public
fBitmaps: array[0..MENU_FONT_COUNT - 1] of TBitmap32;
constructor Create;
destructor Destroy; override;
procedure Load;
procedure DrawText(Dst: TBitmap32; const S: string; X, Y: Integer; aRestoreBuffer: TBitmap32 = nil);
procedure DrawTextColored(Dst: TBitmap32; const HueShift: TColorDiff; const S: string; X, Y: Integer; aRestoreBuffer: TBitmap32 = nil; EraseOnly: Boolean = False);
procedure DrawTextCentered(Dst: TBitmap32; const S: string; Y: Integer; aRestoreBuffer: TBitmap32 = nil; EraseOnly: Boolean = False);
procedure DrawTextLines(const Lines: TextLineArray; Dst: TBitmap32; Y: Integer; aRestoreBuffer: TBitmap32 = nil; EraseOnly: Boolean = False);
function GetTextSize(const S: String): TRect;
property BitmapOfChar[Ch: Char]: TBitmap32 read GetBitmapOfChar;
end;
implementation
uses
PngInterface,
GameControl;
procedure TMenuFont.Combine(F: TColor32; var B: TColor32; M: Cardinal);
// Just show transparent
begin
if F <> 0 then B := F;
end;
constructor TMenuFont.Create;
var
i: Integer;
{-------------------------------------------------------------------------------
The purple font has it's own internal pixelcombine.
I don't think this ever has to be different.
-------------------------------------------------------------------------------}
begin
inherited;
for i := 0 to MENU_FONT_COUNT - 1 do
begin
fBitmaps[i] := TBitmap32.Create;
fBitmaps[i].OnPixelCombine := Combine;
fBitmaps[i].DrawMode := dmCustom;
end;
end;
destructor TMenuFont.Destroy;
var
i: Integer;
begin
for i := 0 to MENU_FONT_COUNT - 1 do
fBitmaps[i].Free;
inherited;
end;
function TMenuFont.GetBitmapOfChar(Ch: Char): TBitmap32;
var
Idx: Integer;
ACh: AnsiChar;
begin
ACh := AnsiChar(Ch);
// Ignore any character not supported by the purple font
if (not (ACh in [#32..#126])) and (ACh <> ' ') then
Idx := 0
else
Idx := Ord(ACh) - 33;
Result := fBitmaps[Idx];
end;
function TMenuFont.GetTextSize(const S: string): TRect;
var
C: Char;
CX, i: Integer;
begin
CX := 0;
FillChar(Result, SizeOf(Result), 0);
if S <> '' then
Result.Bottom := CHARACTER_HEIGHT;
for i := 1 to Length(S) do
begin
C := S[i];
case C of
#12:
begin
Inc(Result.Bottom, HALF_LINE_FEED);
CX := 0;
end;
#13:
begin
Inc(Result.Bottom, CHARACTER_HEIGHT);
CX := 0;
end;
#26..#126:
begin
Inc(CX, CHARACTER_WIDTH);
if CX > Result.Right then
Result.Right := CX;
end;
end;
end;
end;
procedure TMenuFont.DrawText(Dst: TBitmap32; const S: string; X, Y: Integer; aRestoreBuffer: TBitmap32 = nil);
var
C: Char;
CX, CY, i: Integer;
R: TRect;
begin
if aRestoreBuffer <> nil then
begin
R := GetTextSize(S);
Types.OffsetRect(R, X, Y);
Types.IntersectRect(R, R, aRestoreBuffer.BoundsRect); // Oops, again watch out for sourceretangle!
aRestoreBuffer.DrawTo(Dst, R, R);
end;
CX := X;
CY := Y;
for i := 1 to Length(S) do
begin
C := S[i];
case C of
#12:
begin
Inc(CY, HALF_LINE_FEED);
CX := X;
end;
#13:
begin
Inc(CY, CHARACTER_HEIGHT);
CX := X;
end;
' ':
begin
Inc(CX, CHARACTER_WIDTH);
end;
#33..#132:
begin
BitmapOfChar[C].DrawTo(Dst, CX, CY);
Inc(CX, CHARACTER_WIDTH);
end;
end;
end;
end;
procedure TMenuFont.DrawTextCentered(Dst: TBitmap32; const S: string; Y: Integer; aRestoreBuffer: TBitmap32 = nil;
EraseOnly: Boolean = False);
var
X, i: Integer;
R: TRect;
List: TStringList;
H: string;
begin
List := TStringList.Create;
MakeList(S, List);
if aRestoreBuffer <> nil then
begin
R := GetTextSize(S);
Types.OffsetRect(R, (Dst.Width - (R.Right - R.Left)) div 2, Y);
Types.IntersectRect(R, R, aRestoreBuffer.BoundsRect); // Oops, again watch out for sourceretangle!
aRestoreBuffer.DrawTo(Dst, R, R);
end;
if not EraseOnly then
for i := 0 to List.Count - 1 do
begin
H := List[i]; // <= 40 characters!!!
X := (Dst.Width - CHARACTER_WIDTH * Length(H)) div 2;
if (H <> #13) and (H <> #12) then
DrawText(Dst, H, X, Y)
else if H = #13 then
Inc(Y, CHARACTER_HEIGHT)
else
Inc(Y, HALF_LINE_FEED);
end;
List.Free;
end;
procedure TMenuFont.DrawTextColored(Dst: TBitmap32; const HueShift: TColorDiff;
const S: string; X, Y: Integer; aRestoreBuffer: TBitmap32 = nil; EraseOnly: Boolean = False);
var
C: Char;
CX, CY, i: Integer;
R: TRect;
tmpBitmap: TBitmap32;
begin
tmpBitmap := TBitmap32.Create;
if aRestoreBuffer <> nil then
begin
R := GetTextSize(S);
Types.OffsetRect(R, X, Y);
Types.IntersectRect(R, R, aRestoreBuffer.BoundsRect);
aRestoreBuffer.DrawTo(Dst, R, R);
end;
CX := X;
CY := Y;
for i := 1 to Length(S) do
begin
C := S[i];
case C of
#12:
begin
Inc(CY, HALF_LINE_FEED);
CX := X;
end;
#13:
begin
Inc(CY, CHARACTER_HEIGHT);
CX := X;
end;
' ':
begin
Inc(CX, CHARACTER_WIDTH);
end;
#33..#132:
begin
tmpBitmap.Assign(BitmapOfChar[C]);
ApplyColorShift(tmpBitmap, HueShift);
tmpBitmap.DrawTo(Dst, CX, CY);
Inc(CX, CHARACTER_WIDTH);
end;
end;
end;
tmpBitmap.Free;
end;
procedure TMenuFont.DrawTextLines(const Lines: TextLineArray; Dst: TBitmap32;
Y: Integer; aRestoreBuffer: TBitmap32 = nil; EraseOnly: Boolean = False);
var
i: Integer;
begin
for i := 0 to Length(Lines) - 1 do
begin
var LineInfo: TextlineInfo := Lines[i];
var X := (Dst.Width - CHARACTER_WIDTH * Length(LineInfo.Line)) div 2;
DrawTextColored(Dst, LineInfo.ColorShift, LineInfo.Line, X, LineInfo.yPos, aRestoreBuffer, EraseOnly);
end;
end;
procedure TMenuFont.MakeList(const S: string; aList: TStrings);
var
StartP, P: PChar;
NewS: string;
begin
StartP := PChar(S);
P := StartP;
repeat
case P^ of
#12, #13 :
begin
if P >= StartP then
begin
SetString(NewS, StartP, P - StartP);
aList.Add(NewS);
while (P^ = #12) or (P^ = #13) do
begin
aList.Add(P^);
Inc(P);
end;
if P^ = #0 then Break;
StartP := P;
end;
end;
#0:
begin
if P >= StartP then
begin
SetString(NewS, StartP, P - StartP);
aList.Add(NewS);
Break;
end;
end;
end;
Inc(P);
if P = #0 then Break;
until False;
end;
procedure TMenuFont.Load;
var
i: Integer;
TempBMP: TBitmap32;
buttonSelected: Integer;
begin
TempBMP := TBitmap32.Create;
if (not (GameParams.CurrentLevel = nil))
and FileExists(GameParams.CurrentLevel.Group.FindFile('menu_font.png')) then
TPngInterface.LoadPngFile(GameParams.CurrentLevel.Group.FindFile('menu_font.png'), TempBMP)
else if FileExists(AppPath + SFGraphicsMenu + 'menu_font.png') then
TPngInterface.LoadPngFile(AppPath + SFGraphicsMenu + 'menu_font.png', TempBMP)
else
begin
buttonSelected := MessageDlg('Could not find the menu font gfx\menu\menu_font.png. Try to continue?',
mtWarning, mbOKCancel, 0);
if buttonSelected = mrCancel then Application.Terminate();
end;
for i := 0 to MENU_FONT_COUNT-1 do
begin
fBitmaps[i].SetSize(CHARACTER_WIDTH, CHARACTER_HEIGHT);
fBitmaps[i].Clear(0);
TempBMP.DrawTo(fBitmaps[i], 0, 0, Rect(i*CHARACTER_WIDTH, 0, (i+1)*CHARACTER_WIDTH, CHARACTER_HEIGHT));
fBitmaps[i].DrawMode := dmBlend;
fBitmaps[i].CombineMode := cmMerge;
end;
TempBMP.Free;
end;
end.