forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameScreen.Preview.pas
208 lines (181 loc) · 6.34 KB
/
GameScreen.Preview.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
unit GameScreen.Preview;
{$include lem_directives.inc}
interface
uses
LCLIntf, LCLType, Classes, Controls, Graphics,
SysUtils,
GR32, GR32_Image, GR32_Layers,
Base.Utils,
Dos.Consts, Dos.Structures,
Prog.Types, Prog.App, Prog.Data, Prog.Strings,
Game.Rendering,
Styles.Base,
Level.Base,
Form.Message,
GameScreen.Base, GameScreen.Player;
type
TGamePreviewScreen = class(TGameBaseScreen)
private
fLevelErrorCount: Integer;
procedure Form_KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure Form_MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure Form_MouseWheelDown(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
procedure Form_MouseWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
procedure Img_MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
procedure ShowNextLevel(forwards: Boolean);
function GetScreenText: string;
protected
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
procedure BuildScreen; override;
procedure PrepareGameParams; override;
end;
implementation
{ TGamePreviewScreen }
constructor TGamePreviewScreen.Create(aOwner: TComponent);
begin
inherited;
OnKeyDown := Form_KeyDown;
OnMouseDown := Form_MouseDown;
ScreenImg.OnMouseDown := Img_MouseDown;
OnMouseWheelDown := Form_MouseWheelDown;
OnMouseWheelUp := Form_MouseWheelUp;
// MainPal := GetDosMainMenuPaletteColors32;
InitializeImageSizeAndPosition(640, 350);
ExtractBackGround;
ExtractPurpleFont;
end;
procedure TGamePreviewScreen.BuildScreen;
var
Mainpal: TArrayOfColor32;
Temp, W: TBitmap32;
DstRect: TRect;
begin
ScreenImg.BeginUpdate;
try
MainPal := GetDosMainMenuPaletteColors32;
// prepare the renderer, this is a little bit shaky (not sure if this is the right place)
App.GraphicSet.Load(App.Level.Info.GraphicSet, App.Level.Info.GraphicSetEx);
App.Renderer.Prepare(TRenderInfoRec.Create(App.Level, App.GraphicSet, TMiscOption.RepairCustomLevelErrors in App.Config.MiscOptions), fLevelErrorCount);
if fLevelErrorCount > 0 then
DlgWarning('This level contrains ' + fLevelErrorCount.ToString + ' errors.');
Temp := TBitmap32.Create;
W := TBitmap32.Create;
try
Temp.SetSize(640, 350);
Temp.Clear(0);
// draw level preview
W.SetSize(GAME_BMPWIDTH, 160);
W.Clear(0);
App.Renderer.RenderWorld(W, True);
DstRect := Rect(0, 0, 400, 40); // div 4
DstRect.Offset(120, 20); // set location
W.DrawTo(Temp, DstRect, W.BoundsRect);
// draw background
TileBackgroundBitmap(0, 78, Temp);
// draw text
DrawPurpleText(Temp, GetScreenText, 0, 80);
ScreenImg.Bitmap.Assign(Temp);
finally
W.Free;
Temp.Free;
end;
finally
ScreenImg.EndUpdate;
if Visible then begin
ScreenImg.Changed;
ScreenImg.Invalidate;
end;
end;
end;
destructor TGamePreviewScreen.Destroy;
begin
inherited;
end;
procedure TGamePreviewScreen.Form_KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
case Key of
VK_ESCAPE : CloseScreen(TGameScreenType.Menu);
VK_RETURN : CloseScreen(TGameScreenType.Play);
VK_UP : ShowNextLevel(True);
VK_DOWN : ShowNextLevel(False);
end;
end;
procedure TGamePreviewScreen.Form_MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
CloseScreen(TGameScreenType.Play);
end;
procedure TGamePreviewScreen.Form_MouseWheelDown(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
Handled := True;
ShowNextLevel(False);
end;
procedure TGamePreviewScreen.Form_MouseWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
Handled := True;
ShowNextLevel(True);
end;
procedure TGamePreviewScreen.Img_MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
begin
if Button = mbLeft then
CloseScreen(TGameScreenType.Play);
end;
function TGamePreviewScreen.GetScreenText: string;
var
PercentSaved: Integer;
PercStr: string;
ReplayString: string;
StyleDecriptor: string;
begin
Assert(App <> nil);
if App.ReplayFileName <> '' then
ReplayString := ' (Replay)'
else
ReplayString := '';
StyleDecriptor := App.CurrentLevelInfo.Style.StyleInformation.Description;
if StyleDecriptor.Length > 24 then
StyleDecriptor := App.CurrentLevelInfo.Style.Name;
PercentSaved := Percentage(App.Level.Info.LemmingsCount, App.Level.Info.RescueCount);
PercStr := (PercentSaved.ToString + '%').PadRight(4);
Result := Format(SPreviewString_dsdsss,
[App.CurrentLevelInfo.LevelIndex + 1, // 1-based levelindex
App.Level.Info.Title.Trim,
App.Level.Info.LemmingsCount,
PercStr,
App.Level.Info.ReleaseRate,
App.Level.Info.TimeLimit,
App.CurrentLevelInfo.Section.SectionName + ReplayString,
StyleDecriptor //App.CurrentLevelInfo.Style.Name // todo: optional
]);
end;
procedure TGamePreviewScreen.PrepareGameParams;
begin
inherited PrepareGameParams;
if App.CurrentLevelInfo = nil then
App.CurrentLevelInfo := App.Style.LevelSystem.FirstLevel;
if not Assigned( App.CurrentLevelInfo) then
Throw('Level loading information is nil', 'Prepare');
App.CurrentLevelInfo.LoadLevel(App.Level);
end;
procedure TGamePreviewScreen.ShowNextLevel(forwards: Boolean);
begin
if not App.Config.GetMiscOption(TMiscOption.UseCheatScrollingInPreviewScreen) then
Exit;
if forwards then begin
App.CurrentLevelInfo := App.CurrentLevelInfo.Next;
if App.CurrentLevelInfo = nil then
App.CurrentLevelInfo := App.Style.LevelSystem.FirstLevel;
PrepareGameparams;
BuildScreen;
end
else begin
App.CurrentLevelInfo := App.CurrentLevelInfo.Prev;
if App.CurrentLevelInfo = nil then
App.CurrentLevelInfo := App.Style.LevelSystem.LastLevel;
PrepareGameparams;
BuildScreen;
end;
end;
end.