/
uScreen.pas
299 lines (262 loc) · 7.29 KB
/
uScreen.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
{
Kali Engine (C) Martok
https://github.com/martok/kali/
-----------------------------------------------------------------------
For copyright information see file license.txt.
}
unit uScreen;
interface
uses Types, SysUtils, Windows, Forms, Controls, Classes, Graphics, uSurface, uUtil;
type
TSurfaceEvent = procedure(Sender: TObject; Surface: TSurface) of object;
TTickEvent = procedure(Sender: TObject; DT: single) of object;
TMouseButtons = set of TMouseButton;
TMouse = record
Hover: boolean;
X, Y: integer;
Buttons: TMouseButtons;
Wheel: integer;
Focused: boolean;
end;
TScreen = class(TForm)
private
FRunning: Boolean;
FFrameLimiter: Boolean;
FFrameTarget: integer;
FFPS: single;
FTimeStart,
FTimeLastCount,
FTimeLastFrame: PrecisionTime;
FFrameCount: Integer;
FActualFrame: TRect;
FWorldTime: double;
FOnRender: TSurfaceEvent;
FOnInitSurface: TSurfaceEvent;
FOnTick: TTickEvent;
FSurface: TSurface;
FFullscreen: boolean;
FMouse: TMouse;
protected
procedure AppIdle(Sender: TObject; var Done: Boolean);
procedure DoClose(var Action: TCloseAction); override;
procedure Resize; override;
procedure InitSurface;
procedure Tick(const DT: single);
procedure Render;
procedure Blit;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure Activate; override;
procedure Deactivate; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X: Integer; Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;
function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
public
procedure AfterConstruction; override;
procedure Run;
procedure GoFullscreen;
procedure ExitFullscreen;
published
property FrameLimiter: Boolean read FFrameLimiter write FFrameLimiter;
property FrameTarget: integer read FFrameTarget write FFrameTarget;
property CurrentFPS: single read FFPS;
property Fullscreen: boolean read FFullscreen;
property WorldTime: Double read FWorldTime;
property Mouse: TMouse read FMouse;
property OnInitSurface: TSurfaceEvent read FOnInitSurface write FOnInitSurface;
property OnTick: TTickEvent read FOnTick write FOnTick;
property OnRender: TSurfaceEvent read FOnRender write FOnRender;
end;
implementation
uses Math;
{ TScreen }
procedure TScreen.AfterConstruction;
begin
inherited;
Color:= clBlack;
Position:= poScreenCenter;
Application.OnIdle:= AppIdle;
FFrameTarget:= 60;
FFrameLimiter:= true;
end;
procedure TScreen.DoClose(var Action: TCloseAction);
begin
inherited;
Application.Terminate;
end;
procedure TScreen.Run;
var
P: Pointer;
begin
FRunning:= True;
try
InitSurface;
QueryPerformanceCounter(FTimeStart);
FTimeLastCount:= FTimeStart;
FTimeLastFrame:= FTimeStart;
FFrameCount:= 0;
FWorldTime:= 0;
P:= @Application.Mainform;
Pointer(P^):= Self;
Show;
repeat
try
Application.HandleMessage;
except
Application.HandleException(Self);
end;
until Application.Terminated;
finally
FRunning:= False;
end;
end;
procedure TScreen.AppIdle(Sender: TObject; var Done: Boolean);
var
ts, te: PrecisionTime;
dt, ft: single;
begin
Done:= false;
QueryPerformanceCounter(ts);
dt:= (ts - FTimeLastFrame) / PerformanceFrequency;
FTimeLastFrame:= ts;
FWorldTime:= FWorldTime + dt;
Tick(dt);
Render;
Blit;
QueryPerformanceCounter(te);
inc(FFrameCount);
if FFrameLimiter then begin
ft:= 1 / FFrameTarget - (te - ts) / PerformanceFrequency;
if ft > 0 then
Sleep(trunc(ft * 1000));
end;
dt:= (te - FTimeLastCount) / PerformanceFrequency;
if dt > 0.1 then begin
FFPS:= 0.5 * FFPS + 0.5 * (FFrameCount / dt);
QueryPerformanceCounter(FTimeLastCount);
FFrameCount:= 0;
end;
FMouse.Wheel:= 0;
end;
procedure TScreen.InitSurface;
var
s: single;
begin
FSurface:= TSurface.Create;
if Assigned(FOnInitSurface) then
FOnInitSurface(Self, FSurface);
s:= min(800 / FSurface.Width, 600 / FSurface.Height);
if s < 1 then begin
ClientWidth:= FSurface.Width;
ClientHeight:= FSurface.Height;
end else begin
s:= floor(s);
ClientWidth:= trunc(FSurface.Width * s);
ClientHeight:= trunc(FSurface.Height * s);
end;
end;
procedure TScreen.Render;
begin
if Assigned(FOnRender) then
FOnRender(Self, FSurface);
end;
procedure TScreen.Tick(const DT: single);
begin
if Mouse.Focused and not Active then
Deactivate;
if not Mouse.Focused and Active then
Activate;
if Assigned(FOnTick) then
FOnTick(Self, DT);
end;
procedure TScreen.Blit;
var
s: single;
fr, su: TRect;
begin
s:= min(ClientWidth / FSurface.Width, ClientHeight / FSurface.Height);
if s < 1 then
s:= 1 / ceil(1 / s)
else
s:= floor(s);
fr:= ClientRect;
su:= Bounds(0, 0, Trunc(FSurface.Width * s), Trunc(FSurface.Height * s));
CenterRectInRect(fr, su);
Canvas.Brush.Color:= Color;
Canvas.Brush.Style:= bsSolid;
Canvas.FillRect(Rect(0, 0, su.Left, ClientHeight));
Canvas.FillRect(Rect(0, 0, ClientWidth, su.Top));
Canvas.FillRect(Rect(su.Right, 0, ClientWidth, ClientHeight));
Canvas.FillRect(Rect(0, su.Bottom, ClientWidth, ClientHeight));
SetStretchBltMode(Canvas.Handle, BLACKONWHITE);
StretchBlt(Canvas.Handle, su.Left, su.Top, su.Right - su.Left, su.Bottom - su.Top,
FSurface.Canvas.Handle, 0, 0, FSurface.Width, FSurface.Height, SRCCOPY);
FActualFrame:= su;
end;
procedure TScreen.Resize;
begin
inherited;
if (ClientWidth > 0) and (ClientHeight > 0) and Assigned(FSurface) then
Blit;
end;
procedure TScreen.GoFullscreen;
begin
BorderStyle:= bsNone;
WindowState:= wsMaximized;
FFullscreen:= true;
end;
procedure TScreen.ExitFullscreen;
begin
WindowState:= wsNormal;
BorderStyle:= bsSizeable;
FFullscreen:= false;
end;
procedure TScreen.KeyDown(var Key: Word; Shift: TShiftState);
begin
if (Key = VK_RETURN) and (shift = [ssAlt]) then begin
if FFullscreen then
ExitFullscreen
else
GoFullscreen;
end else
inherited;
end;
procedure TScreen.Activate;
begin
inherited;
FMouse.Focused:= true;
end;
procedure TScreen.Deactivate;
begin
inherited;
FMouse.Buttons:= [];
FMouse.Focused:= false;
end;
procedure TScreen.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
Include(FMouse.Buttons, Button);
end;
procedure TScreen.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
inherited;
FMouse.Hover:= PtInRect(FActualFrame, Point(X, Y));
if FMouse.Hover then begin
FMouse.X:= (X - FActualFrame.Left) * FSurface.Width div (FActualFrame.Right - FActualFrame.Left);
FMouse.Y:= (Y - FActualFrame.Top) * FSurface.Height div (FActualFrame.Bottom - FActualFrame.Top);
end;
end;
procedure TScreen.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
Exclude(FMouse.Buttons, Button);
end;
function TScreen.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean;
begin
if WheelDelta > 0 then
inc(FMouse.Wheel)
else if WheelDelta < 0 then
dec(FMouse.Wheel);
Result:= inherited DoMouseWheel(Shift, WheelDelta, MousePos);
end;
end.