-
Notifications
You must be signed in to change notification settings - Fork 12
/
bglvirtualscreen.pas
471 lines (418 loc) · 14.3 KB
/
bglvirtualscreen.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
464
465
466
467
468
469
470
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | mailedivando@gmail.com
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGLVirtualScreen;
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
ExtCtrls, BGRABitmapTypes, BGRAOpenGL, OpenGLContext, BGRACanvasGL,
BGRASpriteGL;
type
TCustomBGLVirtualScreen = class;
TBGLRedrawEvent = procedure (Sender: TObject; BGLContext: TBGLContext) of object;
TBGLLoadTexturesEvent = procedure (Sender: TObject; BGLContext: TBGLContext) of object;
TBGLElapseEvent = procedure (Sender: TObject; BGLContext: TBGLContext; ElapsedMs: integer) of object;
TBGLFramesPerSecondEvent = procedure (Sender: TObject; BGLContext: TBGLContext; FramesPerSecond: integer) of object;
TBGLUseContextCallback = procedure (Sender: TObject; BGLContext: TBGLContext; Data: Pointer) of object;
{ TCustomBGLVirtualScreen }
TCustomBGLVirtualScreen = class(TCustomOpenGLControl)
private
{ Private declarations }
FOnRedraw: TBGLRedrawEvent;
FOnLoadTextures: TBGLLoadTexturesEvent;
FOnUnloadTextures: TBGLLoadTexturesEvent;
FOnElapse: TBGLElapseEvent;
FOnFramesPerSecond: TBGLFramesPerSecondEvent;
FSmoothedElapse: boolean;
FTexturesLoaded: boolean;
FBevelInner, FBevelOuter: TPanelBevel;
FBevelWidth: TBevelWidth;
FBorderWidth: TBorderWidth;
FRedrawOnIdle: boolean;
FSprites: TBGLCustomSpriteEngine;
FElapseAccumulator, FElapseCount, FStoredFPS: integer;
FSmoothedElapseAccumulator: single;
FContextPrepared: boolean;
FOldSprites: TBGLCustomSpriteEngine;
FShaderList,FOldShaderList: TStringList;
function GetCanvas: TBGLCustomCanvas;
procedure SetBevelInner(const AValue: TPanelBevel);
procedure SetBevelOuter(const AValue: TPanelBevel);
procedure SetBevelWidth(const AValue: TBevelWidth);
procedure SetBorderWidth(const AValue: TBorderWidth);
procedure SetRedrawOnIdle(AValue: Boolean);
procedure SetSmoothedElapse(AValue: boolean);
protected
class var FToRedrawOnIdle: array of TCustomBGLVirtualScreen;
{ Protected declarations }
procedure RedrawContent(ctx: TBGLContext); virtual;
procedure SetEnabled(Value: boolean); override;
class procedure OnAppIdle(Sender: TObject; var Done: Boolean);
procedure LoadTextures; virtual;
function PrepareBGLContext: TBGLContext;
procedure ReleaseBGLContext(ctx: TBGLContext);
public
{ Public declarations }
procedure DoOnPaint; override;
procedure QueryLoadTextures; virtual;
procedure UnloadTextures; virtual;
procedure UseContext(ACallback: TBGLUseContextCallback; AData: Pointer = nil);
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
public
property Canvas: TBGLCustomCanvas read GetCanvas;
property Sprites: TBGLCustomSpriteEngine read FSprites;
property OnLoadTextures: TBGLLoadTexturesEvent Read FOnLoadTextures Write FOnLoadTextures;
property OnUnloadTextures: TBGLLoadTexturesEvent Read FOnUnloadTextures Write FOnUnloadTextures;
property OnRedraw: TBGLRedrawEvent Read FOnRedraw Write FOnRedraw;
property OnElapse: TBGLElapseEvent Read FOnElapse Write FOnElapse;
property OnFramesPerSecond: TBGLFramesPerSecondEvent Read FOnFramesPerSecond Write FOnFramesPerSecond;
property RedrawOnIdle: Boolean read FRedrawOnIdle write SetRedrawOnIdle default False;
property BorderWidth: TBorderWidth Read FBorderWidth Write SetBorderWidth default 0;
property BevelInner: TPanelBevel Read FBevelInner Write SetBevelInner default bvNone;
property BevelOuter: TPanelBevel Read FBevelOuter Write SetBevelOuter default bvNone;
property BevelWidth: TBevelWidth Read FBevelWidth Write SetBevelWidth default 1;
property SmoothedElapse: boolean read FSmoothedElapse write SetSmoothedElapse default False;
end;
TBGLVirtualScreen = class(TCustomBGLVirtualScreen)
published
property OnRedraw;
property Align;
property Anchors;
property AutoSize;
property BorderSpacing;
property BevelInner;
property BevelOuter;
property BevelWidth;
property BidiMode;
property BorderWidth;
property BorderStyle;
property Caption;
property ChildSizing;
property ClientHeight;
property ClientWidth;
property Color;
property Constraints;
property DockSite;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property ParentBidiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property RedrawOnIdle;
property ShowHint;
property TabOrder;
property TabStop;
property UseDockManager default True;
property Visible;
property OnClick;
property OnContextPopup;
property OnDockDrop;
property OnDockOver;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnElapse;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnFramesPerSecond;
property OnGetSiteInfo;
property OnGetDockCaption;
property OnLoadTextures;
property OnUnloadTextures;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property OnUnDock;
property SmoothedElapse;
end;
procedure Register;
implementation
uses Types;
procedure Register;
begin
{$I bglvirtualscreen_icon.lrs}
RegisterComponents('OpenGL', [TBGLVirtualScreen]);
end;
{ TCustomBGLVirtualScreen }
procedure TCustomBGLVirtualScreen.SetBevelInner(const AValue: TPanelBevel);
begin
if FBevelInner = AValue then
exit;
FBevelInner := AValue;
Invalidate;
end;
function TCustomBGLVirtualScreen.GetCanvas: TBGLCustomCanvas;
begin
result := BGLCanvas;
end;
procedure TCustomBGLVirtualScreen.SetBevelOuter(const AValue: TPanelBevel);
begin
if FBevelOuter = AValue then
exit;
FBevelOuter := AValue;
Invalidate;
end;
procedure TCustomBGLVirtualScreen.SetBevelWidth(const AValue: TBevelWidth);
begin
if FBevelWidth = AValue then
exit;
FBevelWidth := AValue;
Invalidate;
end;
procedure TCustomBGLVirtualScreen.SetBorderWidth(const AValue: TBorderWidth);
begin
if FBorderWidth = AValue then
exit;
FBorderWidth := AValue;
Invalidate;
end;
procedure TCustomBGLVirtualScreen.SetRedrawOnIdle(AValue: Boolean);
var
i: Integer;
j: Integer;
begin
if FRedrawOnIdle=AValue then Exit;
FRedrawOnIdle:=AValue;
if FRedrawOnIdle then
begin
if length(FToRedrawOnIdle)= 0 then
Application.AddOnIdleHandler(@OnAppIdle);
setlength(FToRedrawOnIdle, length(FToRedrawOnIdle)+1);
FToRedrawOnIdle[high(FToRedrawOnIdle)] := self;
end
else
if length(FToRedrawOnIdle)> 0 then
begin
for i := 0 to high(FToRedrawOnIdle) do
begin
if FToRedrawOnIdle[i]=self then
begin
for j := i to high(FToRedrawOnIdle)-1 do
FToRedrawOnIdle[j] := FToRedrawOnIdle[j+1];
setlength(FToRedrawOnIdle, length(FToRedrawOnIdle)-1);
break;
end;
end;
if length(FToRedrawOnIdle) = 0 then
Application.RemoveOnIdleHandler(@OnAppIdle);
end;
end;
procedure TCustomBGLVirtualScreen.SetSmoothedElapse(AValue: boolean);
begin
if FSmoothedElapse=AValue then Exit;
FSmoothedElapse:=AValue;
end;
procedure TCustomBGLVirtualScreen.DoOnPaint;
var
ctx: TBGLContext;
knownFPS: Integer;
begin
if not FTexturesLoaded then LoadTextures;
ctx := PrepareBGLContext;
if Color = clNone then
BGLViewPort(ClientWidth,ClientHeight)
else
if Color = clDefault then
BGLViewPort(ClientWidth,ClientHeight,ColorToBGRA(ColorToRGB(clWindow)))
else
BGLViewPort(ClientWidth,ClientHeight,ColorToBGRA(ColorToRGB(Color)));
RedrawContent(ctx);
inherited DoOnPaint;
SwapBuffers;
FElapseAccumulator := FElapseAccumulator +FrameDiffTimeInMSecs;
Inc(FElapseCount);
if FElapseAccumulator >= 2000 then
begin
FStoredFPS := 1000*FElapseCount div FElapseAccumulator;
if Assigned(FOnFramesPerSecond) then
FOnFramesPerSecond(self, ctx, FStoredFPS);
FElapseAccumulator := 0;
FElapseCount := 0;
end;
If Assigned(FOnElapse) then
begin
if SmoothedElapse then
begin
If FStoredFPS <> 0 then
knownFPS:= FStoredFPS
else
if FElapseAccumulator >= 500 then
knownFPS := 1000*FElapseCount div FElapseAccumulator
else
knownFPS := 0;
if knownFPS > 0 then
begin
FSmoothedElapseAccumulator := FSmoothedElapseAccumulator +(1000/knownFPS);
end else
FSmoothedElapseAccumulator := FSmoothedElapseAccumulator +FrameDiffTimeInMSecs;
FOnElapse(self, ctx, Trunc(FSmoothedElapseAccumulator));
FSmoothedElapseAccumulator := FSmoothedElapseAccumulator -Trunc(FSmoothedElapseAccumulator);
end else
FOnElapse(self, ctx, FrameDiffTimeInMSecs);
end;
ReleaseBGLContext(ctx);
end;
procedure TCustomBGLVirtualScreen.QueryLoadTextures;
begin
FTexturesLoaded := false;
end;
procedure TCustomBGLVirtualScreen.LoadTextures;
var ctx: TBGLContext;
begin
if MakeCurrent then
begin
if Assigned(FOnLoadTextures) then
begin
ctx := PrepareBGLContext;
FOnLoadTextures(self, ctx);
ReleaseBGLContext(ctx);
end;
FTexturesLoaded:= true;
end;
end;
function TCustomBGLVirtualScreen.PrepareBGLContext: TBGLContext;
begin
if FContextPrepared then
raise exception.Create('Context already prepared');
FOldSprites := BGRASpriteGL.BGLSpriteEngine;
BGRASpriteGL.BGLSpriteEngine := FSprites;
FOldShaderList := BGLCanvas.Lighting.ShaderList;
BGLCanvas.Lighting.ShaderList := FShaderList;
result.Canvas := BGLCanvas;
result.Sprites := FSprites;
FContextPrepared := true;
end;
procedure TCustomBGLVirtualScreen.ReleaseBGLContext(ctx: TBGLContext);
begin
if not FContextPrepared then
raise exception.Create('Context not prepared');
ctx.Canvas.Lighting.ShaderList := FOldShaderList;
BGRASpriteGL.BGLSpriteEngine := FOldSprites;
FContextPrepared := false;
end;
procedure TCustomBGLVirtualScreen.UnloadTextures;
var ctx: TBGLContext;
begin
if MakeCurrent then
begin
ctx := PrepareBGLContext;
if Assigned(FOnUnloadTextures) then FOnUnloadTextures(self, ctx);
FSprites.Clear;
ctx.Canvas.Lighting.FreeShaders;
ReleaseBGLContext(ctx);
FTexturesLoaded := false;
end;
end;
procedure TCustomBGLVirtualScreen.UseContext(ACallback: TBGLUseContextCallback; AData: Pointer);
var
ctx: TBGLContext;
begin
if not MakeCurrent then
raise exception.Create('Unable to switch to the OpenGL context');
ctx := PrepareBGLContext;
try
ACallback(self, ctx, AData);
finally
ReleaseBGLContext(ctx);
end;
end;
procedure TCustomBGLVirtualScreen.RedrawContent(ctx: TBGLContext);
var
ARect: TRect;
w: integer;
begin
ARect := rect(0,0,ctx.Canvas.Width,ctx.Canvas.Height);
w := BevelWidth;
if w = 0 then w := 1;
// if BevelOuter is set then draw a frame with BevelWidth
if (BevelOuter <> bvNone) and (w > 0) then
ctx.Canvas.Frame3d(ARect, w, BevelOuter); // Note: Frame3D inflates ARect
InflateRect(ARect, -BorderWidth, -BorderWidth);
// if BevelInner is set then skip the BorderWidth and draw a frame with BevelWidth
if (BevelInner <> bvNone) and (w > 0) then
ctx.Canvas.Frame3d(ARect, w, BevelInner); // Note: Frame3D inflates ARect
if Assigned(FOnRedraw) then
FOnRedraw(self, ctx);
end;
procedure TCustomBGLVirtualScreen.SetEnabled(Value: boolean);
begin
if Value <> Enabled then Invalidate;
inherited SetEnabled(Value);
end;
class procedure TCustomBGLVirtualScreen.OnAppIdle(Sender: TObject; var Done: Boolean);
var
i: Integer;
begin
if length(FToRedrawOnIdle) > 0 then
begin
for i := 0 to high(FToRedrawOnIdle) do
if not (csDesigning in FToRedrawOnIdle[i].ComponentState) then
FToRedrawOnIdle[i].Invalidate;
Done:=false;
end;
end;
constructor TCustomBGLVirtualScreen.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
FTexturesLoaded:= False;
AutoResizeViewport := true;
FSprites := TBGLDefaultSpriteEngine.Create;
FShaderList:= TStringList.Create;
FStoredFPS := 0;
FElapseAccumulator := 0;
FElapseCount := 0;
FSmoothedElapseAccumulator := 0;
end;
destructor TCustomBGLVirtualScreen.Destroy;
var
i: Integer;
begin
for i := 0 to FShaderList.Count-1 do
FShaderList.Objects[i].Free;
FShaderList.Free;
RedrawOnIdle := false;
FSprites.Free;
inherited Destroy;
end;
end.