forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameScreen.Base.pas
463 lines (406 loc) · 12 KB
/
GameScreen.Base.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
unit GameScreen.Base;
{$include lem_directives.inc}
interface
uses
LCLIntf,
Classes, Controls, Graphics, Forms, SysUtils, Dialogs,
GR32, GR32_Image, GR32_Layers,
Dos.Structures,
Base.Utils, Base.Bitmaps,
Form.Base,
Prog.Base, Prog.Types, Prog.App,
Dos.MainDat;
const
DEF_STRETCHED = TRUE;
const
PURPLEFONTCOUNT = ord('~') - ord('!') + 1;
PurpleFontCharSet = ['!'..'~'];
type
TPurpleFont = class(TComponent)
private
function GetBitmapOfChar(Ch: Char): TBitmap32;
procedure Combine(F: TColor32; var B: TColor32; M: TColor32);
protected
public
fBitmaps: array[0..PURPLEFONTCOUNT - 1] of TBitmap32;
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
property BitmapOfChar[Ch: Char]: TBitmap32 read GetBitmapOfChar;
procedure SaveBitmaps(const aDir: string);
end;
// This is the ancestor for all DOS-forms that are used in the program.
// Using the Prog.App unit indicates that the global App can be used.
TGameBaseScreen = class(TBaseDosForm)
private
fMainDatExtractor : TMainDatExtractor;
fScreenImg : TImage32;
fBackGround : TBitmap32;
fBackBuffer : TBitmap32; // general purpose buffer
fPurpleFont : TPurpleFont;
fOriginalImageBounds : TRect;
fStretched : Boolean;
fCloseDelay : Integer;
procedure SetStretched(const Value: Boolean);
procedure AdjustImage;
procedure MakeList(const S: string; aList: TStrings);
protected
procedure PrepareGameParams; override;
procedure BeforeCloseScreen(aNextScreen: TGameScreenType); override;
property MainDatExtractor: TMainDatExtractor read fMainDatExtractor;
property CloseDelay: Integer read fCloseDelay write fCloseDelay;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
procedure TileBackgroundBitmap(X, Y: Integer; Dst: TBitmap32 = nil);
procedure ExtractBackGround;
procedure ExtractPurpleFont;
procedure DrawPurpleText(Dst: TBitmap32; const S: string; X, Y: Integer; aRestoreBuffer: TBitmap32 = nil);
procedure DrawPurpleTextCentered(Dst: TBitmap32; const S: string; Y: Integer; aRestoreBuffer: TBitmap32 = nil; EraseOnly: Boolean = False);
function CalcPurpleTextSize(const S: string): TRect;
procedure FadeOut;
function InitializeImageSizeAndPosition(aWidth, aHeight: Integer): TRect;
property ScreenImg: TImage32 read fScreenImg;
property BackGround: TBitmap32 read fBackGround;
property BackBuffer: TBitmap32 read fBackBuffer;
property Stretched: Boolean read fStretched write SetStretched default DEF_STRETCHED;
property PurpleFont: TPurpleFont read fPurpleFont;
end;
implementation
{ TPurpleFont }
procedure TPurpleFont.Combine(F: TColor32; var B: TColor32; M: TColor32);
// just show transparent
begin
if F <> 0 then B := F;
end;
constructor TPurpleFont.Create(aOwner: TComponent);
// The purple font has it's own internal pixelcombine.
// I don't think this ever has to be different.
var
i: Integer;
begin
inherited;
for i := 0 to PURPLEFONTCOUNT - 1 do begin
fBitmaps[i] := TBitmap32.Create;
fBitmaps[i].OnPixelCombine := Combine;
fBitmaps[i].DrawMode := dmCustom;
end;
end;
destructor TPurpleFont.Destroy;
var
i: Integer;
begin
for i := 0 to PURPLEFONTCOUNT - 1 do
fBitmaps[i].Free;
inherited;
end;
function TPurpleFont.GetBitmapOfChar(Ch: Char): TBitmap32;
var
Idx: Integer;
begin
Assert(CharInSet(Ch, ['!'..'~'])); // paranoid
Idx := Ord(Ch) - ord('!');
Result := fBitmaps[Idx];
end;
procedure TPurpleFont.SaveBitmaps(const aDir: string);
var
i: Byte;
begin
ForceDirectories(aDir);
for i := 0 to PURPLEFONTCOUNT - 1 do
begin
fBitmaps[i].SaveToFile(aDir + '' + 'purplefont' + LeadZeroStr(i, 2) + '.bmp');
end;
end;
{ TGameBaseScreen }
procedure TGameBaseScreen.AdjustImage;
begin
case fStretched of
False:
begin
fScreenImg.Align := alNone;
fScreenImg.ScaleMode := smNormal;
end;
True:
begin
fScreenImg.Align := alClient;
fScreenImg.ScaleMode := smResize;
fScreenImg.BitmapAlign := baCenter;
end;
end;
end;
procedure TGameBaseScreen.BeforeCloseScreen(aNextScreen: TGameScreenType);
begin
if aNextScreen <> TGameScreenType.Interrupted then begin
if fCloseDelay > 0 then begin
Update;
Sleep(fCloseDelay);
end;
if App.Config.GetMiscOption(TMiscOption.UseFadeOut) then
FadeOut;
end;
end;
constructor TGameBaseScreen.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
fScreenImg := TImage32.Create(Self);
fScreenImg.Parent := Self;
fScreenImg.Color := clBlack;
fPurpleFont := TPurpleFont.Create(nil);
fBackGround := TBitmap32.Create;
fBackBuffer := TBitmap32.Create;
fMainDatExtractor := TMainDatExtractor.Create;
fStretched := DEF_STRETCHED;
if not App.Config.GetMiscOption(TMiscOption.ShowDefaultCursor) then begin
Cursor := crNone;
ScreenImg.Cursor := crNone;
end;
fMainDatExtractor.FileName := App.Style.MainDatFileName;
end;
destructor TGameBaseScreen.Destroy;
begin
fBackGround.Free;
fMainDatExtractor.Free;
fPurpleFont.Free;
fBackBuffer.Free;
inherited Destroy;
end;
function TGameBaseScreen.CalcPurpleTextSize(const S: string): TRect;
// Linefeeds increment 16 pixels
// Spaces increment 16 pixels
var
C: Char;
CX, i: Integer;
begin
CX := 0;
FillChar(Result, SizeOf(Result), 0);
if S <> '' then
Result.Bottom := 16;
for i := 1 to Length(S) do
begin
C := S[i];
case C of
#13:
begin
Inc(Result.Bottom, 16);
CX := 0;
end;
'!'..'~', ' ':
begin
Inc(CX, 16);
if CX > Result.Right then
Result.Right := CX;
end;
end;
end;
end;
procedure TGameBaseScreen.DrawPurpleText(Dst: TBitmap32; const S: string; X, Y: Integer; aRestoreBuffer: TBitmap32 = nil);
// Linefeeds increment 16 pixels
// Spaces increment 16 pixels
var
C: Char;
CX, CY, i: Integer;
R: TRect;
bmp: TBitmap32;
begin
if aRestoreBuffer <> nil then
begin
R := CalcPurpleTextSize(S);
R.OffSet(X, Y);
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
#13:
begin
Inc(CY, 16);
CX := X;
end;
' ':
begin
Inc(CX, 16);
end;
'!'..'~':
begin
bmp := fPurpleFont.BitmapOfChar[C];
bmp.DrawTo(Dst, CX, CY);
// todo: add optional recoloring
Inc(CX, 16);
end;
end;
end;
end;
procedure TGameBaseScreen.DrawPurpleTextCentered(Dst: TBitmap32; const S: string; Y: Integer; aRestoreBuffer: TBitmap32 = nil; EraseOnly: Boolean = False);
// Linefeeds increment 16 pixels
// Spaces increment 16 pixels
var
X, i: Integer;
R: TRect;
List: TStringList;
H: string;
begin
List := TStringList.Create;
MakeList(S, List);
if aRestoreBuffer <> nil then begin
R := CalcPurpleTextSize(S);
R.Offset((Dst.Width - (R.Right - R.Left)) div 2, Y);
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 - 16 * Length(H)) div 2;
if H <> #13 then
DrawPurpleText(Dst, H, X, Y)
else
Inc(Y, 16);
end;
List.Free;
end;
procedure TGameBaseScreen.ExtractBackGround;
begin
fMainDatExtractor.ExtractBrownBackGround(fBackGround);
end;
procedure TGameBaseScreen.ExtractPurpleFont;
var
Pal: TArrayOfColor32;
i: Integer;
PurpleFontPos: Integer;
begin
Pal := GetDosMainMenuPaletteColors32;
if Consts.StyleDef = TStyleDef.Ohno
then PurpleFontPos := $69B0 + 972 // there are 5 signs in ohno stored before the purple font (5 sections)
else PurpleFontPos := $69B0;
// read first
MainDatExtractor.ExtractBitmap(fPurpleFont.fBitmaps[0], 4, PurpleFontPos, 16, 16, 3, Pal);
// the position is now updated automatically by stream reading (-1 parameter)
for i := 1 to PURPLEFONTCOUNT - 1 do
MainDatExtractor.ExtractBitmap(fPurpleFont.fBitmaps[i], 4, -1, 16, 16, 3, Pal);
(*
for i := 0 to Length(PurpleFont.fBitmaps) - 1 do begin
var bmp: TBitmap32 := TBitmap32.Create;
bmp.Assign(PurpleFont.BitmapOfChar['A']);
bmp.Recolor(clRed32);
var filename := Consts.PathToDebugFiles + 'Ared' + '.bmp';
bmp.SaveToFile(filename);
bmp.Assign(PurpleFont.BitmapOfChar['A']);
bmp.Recolor(clMaroon32);
filename := Consts.PathToDebugFiles + 'Amaroon' + '.bmp';
bmp.SaveToFile(filename);
bmp.Assign(PurpleFont.BitmapOfChar['A']);
bmp.Recolor(clGreen32);
filename := Consts.PathToDebugFiles + 'Agreen' + '.bmp';
bmp.SaveToFile(filename);
bmp.Assign(PurpleFont.BitmapOfChar['A']);
bmp.Recolor(clLime32);
filename := Consts.PathToDebugFiles + 'Alime' + '.bmp';
bmp.SaveToFile(filename);
bmp.Free;
end;
*)
end;
function TGameBaseScreen.InitializeImageSizeAndPosition(aWidth, aHeight: Integer): TRect;
begin
fScreenImg.Bitmap.SetSize(aWidth, aHeight);
Result.Left := (Screen.Width - aWidth) div 2;
Result.Top := (Screen.Height - aHeight) div 2;
Result.Right := Result.Left + aWidth;
Result.Bottom := Result.Top + aHeight;
fOriginalImageBounds := Result;
fScreenImg.BoundsRect := fOriginalImageBounds;
AdjustImage;
end;
procedure TGameBaseScreen.PrepareGameParams;
begin
inherited PrepareGameParams;
end;
procedure TGameBaseScreen.SetStretched(const Value: Boolean);
begin
fStretched := Value;
AdjustImage;
end;
procedure TGameBaseScreen.TileBackgroundBitmap(X, Y: Integer; Dst: TBitmap32 = nil);
var
aX, aY: Integer;
begin
Assert(fBackground.Width > 0);
Assert(fBackground.Height > 0);
if Dst = nil then
Dst := fScreenImg.Bitmap;
// #EL 2020 changed the <= to <
aY := Y;
aX := X;
while aY <{=} Dst.Height do begin
while aX <{=} Dst.Width do begin
fBackground.DrawTo(Dst, aX, aY);
Inc(aX, fBackground.Width);
end;
Inc(aY, fBackground.Height);
aX := X;
end;
end;
procedure TGameBaseScreen.MakeList(const S: string; aList: TStrings);
var
StartP, P: PChar;
NewS: string;
begin
StartP := PChar(S);
P := StartP;
repeat
case P^ of
#13 :
begin
if P >= StartP then begin
SetString(NewS, StartP, P - StartP);
aList.Add(NewS);
while P^ = #13 do begin
aList.Add(#13);
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; // case
Inc(P);
if P = #0 then Break;
until False;
end;
procedure TGameBaseScreen.FadeOut;
// todo: for the gamescreen this does not work. the skillpanel is not faded
var
A: Integer;
begin
// var bmp := TBitmap.Create;
// bmp.SetSize(32, 32);
// bmp.Canvas.Brush.Color := clBack;
//
// while A >= 0 do begin
// bmp.ScreenImg.Bitmap.ResetAlpha(Byte(A));
// Sleep(20);
// Dec(A, 8);
// end;
ScreenImg.Bitmap.DrawMode := dmBlend;
ScreenImg.RepaintMode := rmDirect;
// this is around 640 milliseconds - assuming a fast screenoutput which delays a little bit more we get around 750 ms
A := 255;
while A >= 0 do begin
ScreenImg.Bitmap.ResetAlpha(Byte(A));
Sleep(20);
Dec(A, 8);
end;
end;
end.