source: GraphicTest/DrawMethod.pas

Last change on this file was 573, checked in by chronos, 5 months ago
  • Modified: Build with Lazarus 3.4.
  • Modified: Removed U prefix from unit names.
File size: 6.7 KB
Line 
1unit DrawMethod;
2
3interface
4
5uses
6 Classes, SysUtils, ExtCtrls, Platform, FastBitmap, Graphics, Controls,
7 LCLType, IntfGraphics, fpImage, GraphType, DateUtils, Forms,
8 {$IFDEF OPENGL}GL, GLExt, OpenGLContext,{$ENDIF}
9 LclIntf;
10
11type
12 TPaintObject = (poImage, poPaintBox, poOpenGL, poCanvas);
13
14 { TDrawMethod }
15
16 TDrawMethod = class
17 private
18 FControl: TControl;
19 FFPS: Real;
20 FParent: TWinControl;
21 public
22 Caption: string;
23 Description: TStringList;
24 Terminated: Boolean;
25 FrameDuration: TDateTime;
26 StepDuration: TDateTime;
27 PaintObject: TPaintObject;
28 FrameCounter: Integer;
29 FrameCounterStart: TDateTime;
30 FrameCounterStop: TDateTime;
31 function GetFPS: Real;
32 property FPS: Real read FFPS write FFPS;
33 procedure Init(Parent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat); virtual;
34 procedure Done; virtual;
35 constructor Create; virtual;
36 destructor Destroy; override;
37 procedure DrawFrame(FastBitmap: TFastBitmap); virtual;
38 procedure DrawFrameTiming(FastBitmap: TFastBitmap);
39 procedure UpdateSettings; virtual;
40 property Control: TControl read FControl;
41 end;
42
43 TDrawMethodClass = class of TDrawMethod;
44
45 { TDrawMethodImage }
46
47 TDrawMethodImage = class(TDrawMethod)
48 Image: TImage;
49 procedure UpdateSettings; override;
50 procedure Init(Parent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat); override;
51 procedure Done; override;
52 end;
53
54 { TDrawMethodPaintBox }
55
56 TDrawMethodPaintBox = class(TDrawMethod)
57 PaintBox: TPaintBox;
58 procedure Paint(Sender: TObject); virtual;
59 procedure UpdateSettings; override;
60 procedure Init(Parent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat); override;
61 procedure Done; override;
62 end;
63
64 { TDrawMethodCanvas }
65
66 TDrawMethodCanvas = class(TDrawMethod)
67 Canvas: TCanvas;
68 procedure UpdateSettings; override;
69 procedure Init(Parent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat); override;
70 procedure Done; override;
71 end;
72
73 {$IFDEF OPENGL}
74
75 { TDrawMethodOpenGL }
76
77 TDrawMethodOpenGL = class(TDrawMethod)
78 OpenGLControl: TOpenGLControl;
79 TextureId: GLuint;
80 OpenGLBitmap: Pointer;
81 procedure UpdateSettings; override;
82 procedure InitGL;
83 procedure OpenGLControlResize(Sender: TObject);
84 procedure Init(AParent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat); override;
85 procedure Done; override;
86 end;
87
88 {$ENDIF}
89
90
91implementation
92
93{ TDrawMethodCanvas }
94
95procedure TDrawMethodCanvas.UpdateSettings;
96begin
97 inherited;
98end;
99
100procedure TDrawMethodCanvas.Init(Parent: TWinControl; Size: TPoint;
101 PixelFormat: TPixelFormat);
102begin
103 Canvas := TForm(Parent).Canvas;
104end;
105
106procedure TDrawMethodCanvas.Done;
107begin
108 inherited;
109end;
110
111{ TDrawMethodPaintBox }
112
113procedure TDrawMethodPaintBox.Paint(Sender: TObject);
114begin
115end;
116
117procedure TDrawMethodPaintBox.UpdateSettings;
118begin
119 inherited;
120 PaintBox.ControlStyle := FParent.ControlStyle;
121end;
122
123procedure TDrawMethodPaintBox.Init(Parent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat);
124begin
125 inherited;
126 PaintBox := TPaintBox.Create(Parent);
127 PaintBox.Parent := Parent;
128 PaintBox.SetBounds(0, 0, Size.X, Size.Y);
129 PaintBox.OnPaint := Paint;
130 PaintBox.Show;
131 UpdateSettings;
132end;
133
134procedure TDrawMethodPaintBox.Done;
135begin
136 FreeAndNil(PaintBox);
137 inherited;
138end;
139
140{ TDrawMethodImage }
141
142procedure TDrawMethodImage.UpdateSettings;
143begin
144 inherited;
145 Image.ControlStyle := FParent.ControlStyle;
146end;
147
148procedure TDrawMethodImage.Init(Parent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat);
149begin
150 inherited;
151 Image := TImage.Create(Parent);
152 Image.Parent := Parent;
153 Image.SetBounds(0, 0, Size.X, Size.Y);
154 Image.Picture.Bitmap.PixelFormat := PixelFormat;
155 Image.Picture.Bitmap.SetSize(Size.X, Size.Y);
156 Image.Show;
157 UpdateSettings;
158end;
159
160procedure TDrawMethodImage.Done;
161begin
162 FreeAndNil(Image);
163 inherited;
164end;
165
166{$IFDEF OPENGL}
167
168{ TDrawMethodOpenGL }
169
170procedure TDrawMethodOpenGL.Init(AParent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat);
171begin
172 inherited;
173 OpenGLControl := TOpenGLControl.Create(AParent);
174 with OpenGLControl do begin
175 Name := 'OpenGLControl';
176 Parent := AParent;
177 SetBounds(0, 0, Size.X, Size.Y);
178 InitGL;
179 //OnPaint := OpenGLControl1Paint;
180 OnResize := OpenGLControlResize;
181 end;
182 GetMem(OpenGLBitmap, OpenGLControl.Width * OpenGLControl.Height * SizeOf(Integer));
183 UpdateSettings;
184end;
185
186procedure TDrawMethodOpenGL.Done;
187begin
188 FreeMem(OpenGLBitmap, OpenGLControl.Width * OpenGLControl.Height);
189 FreeAndNil(OpenGLControl);
190 inherited;
191end;
192
193procedure TDrawMethodOpenGL.OpenGLControlResize(Sender: TObject);
194begin
195 glViewport(0, 0, OpenGLControl.Width, OpenGLControl.Height);
196end;
197
198procedure TDrawMethodOpenGL.UpdateSettings;
199begin
200 inherited;
201 OpenGLControl.ControlStyle := FParent.ControlStyle;
202end;
203
204procedure TDrawMethodOpenGL.InitGL;
205begin
206 glMatrixMode(GL_PROJECTION);
207 glLoadIdentity;
208 glOrtho(0, OpenGLControl.Width, OpenGLControl.Height, 0, 0, 1);
209// glOrtho(0, 1, 1, 0, 0, 1);
210 glMatrixMode(GL_MODELVIEW);
211 glLoadIdentity();
212 glDisable(GL_DEPTH_TEST);
213 glViewport(0, 0, OpenGLControl.Width, OpenGLControl.Height);
214 //gluPerspective( 45.0, (GLfloat)(OpenGLControl1.Width)/(GLfloat)(OpenGLControl1.Height), 0.1f, 500.0 );
215
216 //glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
217 //glTranslatef (0.0, 0.0,-3.0);
218 // glClearColor(0.0, 0.0, 0.0, 1.0);
219
220 glGenTextures(1, @TextureId);
221 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
222end;
223
224{$ENDIF}
225
226{ TDrawMethod }
227
228function TDrawMethod.GetFPS: Real;
229var
230 StopTime: TDateTime;
231begin
232 if FrameCounterStop <> 0 then StopTime := FrameCounterStop
233 else StopTime := NowPrecise;
234 if FrameCounter > 0 then begin
235 Result := FrameCounter / ((StopTime - FrameCounterStart) / OneSecond);
236 //FrameCounter := 0;
237 //FrameCounterStart := NowPrecise;
238 end else Result := FFPS;
239end;
240
241procedure TDrawMethod.Init(Parent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat);
242begin
243 FParent := Parent;
244end;
245
246procedure TDrawMethod.Done;
247begin
248end;
249
250constructor TDrawMethod.Create;
251begin
252 Description := TStringList.Create;
253end;
254
255destructor TDrawMethod.Destroy;
256begin
257 FreeAndNil(Description);
258 inherited;
259end;
260
261procedure TDrawMethod.DrawFrame(FastBitmap: TFastBitmap);
262begin
263end;
264
265procedure TDrawMethod.DrawFrameTiming(FastBitmap: TFastBitmap);
266var
267 StartTime: TDateTime;
268begin
269 StartTime := NowPrecise;
270 DrawFrame(FastBitmap);
271 FrameDuration := NowPrecise - StartTime;
272end;
273
274procedure TDrawMethod.UpdateSettings;
275begin
276end;
277
278end.
279
Note: See TracBrowser for help on using the repository browser.