1 | unit DrawMethod;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
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 |
|
---|
11 | type
|
---|
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 |
|
---|
91 | implementation
|
---|
92 |
|
---|
93 | { TDrawMethodCanvas }
|
---|
94 |
|
---|
95 | procedure TDrawMethodCanvas.UpdateSettings;
|
---|
96 | begin
|
---|
97 | inherited;
|
---|
98 | end;
|
---|
99 |
|
---|
100 | procedure TDrawMethodCanvas.Init(Parent: TWinControl; Size: TPoint;
|
---|
101 | PixelFormat: TPixelFormat);
|
---|
102 | begin
|
---|
103 | Canvas := TForm(Parent).Canvas;
|
---|
104 | end;
|
---|
105 |
|
---|
106 | procedure TDrawMethodCanvas.Done;
|
---|
107 | begin
|
---|
108 | inherited;
|
---|
109 | end;
|
---|
110 |
|
---|
111 | { TDrawMethodPaintBox }
|
---|
112 |
|
---|
113 | procedure TDrawMethodPaintBox.Paint(Sender: TObject);
|
---|
114 | begin
|
---|
115 | end;
|
---|
116 |
|
---|
117 | procedure TDrawMethodPaintBox.UpdateSettings;
|
---|
118 | begin
|
---|
119 | inherited;
|
---|
120 | PaintBox.ControlStyle := FParent.ControlStyle;
|
---|
121 | end;
|
---|
122 |
|
---|
123 | procedure TDrawMethodPaintBox.Init(Parent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat);
|
---|
124 | begin
|
---|
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;
|
---|
132 | end;
|
---|
133 |
|
---|
134 | procedure TDrawMethodPaintBox.Done;
|
---|
135 | begin
|
---|
136 | FreeAndNil(PaintBox);
|
---|
137 | inherited;
|
---|
138 | end;
|
---|
139 |
|
---|
140 | { TDrawMethodImage }
|
---|
141 |
|
---|
142 | procedure TDrawMethodImage.UpdateSettings;
|
---|
143 | begin
|
---|
144 | inherited;
|
---|
145 | Image.ControlStyle := FParent.ControlStyle;
|
---|
146 | end;
|
---|
147 |
|
---|
148 | procedure TDrawMethodImage.Init(Parent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat);
|
---|
149 | begin
|
---|
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;
|
---|
158 | end;
|
---|
159 |
|
---|
160 | procedure TDrawMethodImage.Done;
|
---|
161 | begin
|
---|
162 | FreeAndNil(Image);
|
---|
163 | inherited;
|
---|
164 | end;
|
---|
165 |
|
---|
166 | {$IFDEF OPENGL}
|
---|
167 |
|
---|
168 | { TDrawMethodOpenGL }
|
---|
169 |
|
---|
170 | procedure TDrawMethodOpenGL.Init(AParent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat);
|
---|
171 | begin
|
---|
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;
|
---|
184 | end;
|
---|
185 |
|
---|
186 | procedure TDrawMethodOpenGL.Done;
|
---|
187 | begin
|
---|
188 | FreeMem(OpenGLBitmap, OpenGLControl.Width * OpenGLControl.Height);
|
---|
189 | FreeAndNil(OpenGLControl);
|
---|
190 | inherited;
|
---|
191 | end;
|
---|
192 |
|
---|
193 | procedure TDrawMethodOpenGL.OpenGLControlResize(Sender: TObject);
|
---|
194 | begin
|
---|
195 | glViewport(0, 0, OpenGLControl.Width, OpenGLControl.Height);
|
---|
196 | end;
|
---|
197 |
|
---|
198 | procedure TDrawMethodOpenGL.UpdateSettings;
|
---|
199 | begin
|
---|
200 | inherited;
|
---|
201 | OpenGLControl.ControlStyle := FParent.ControlStyle;
|
---|
202 | end;
|
---|
203 |
|
---|
204 | procedure TDrawMethodOpenGL.InitGL;
|
---|
205 | begin
|
---|
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);
|
---|
222 | end;
|
---|
223 |
|
---|
224 | {$ENDIF}
|
---|
225 |
|
---|
226 | { TDrawMethod }
|
---|
227 |
|
---|
228 | function TDrawMethod.GetFPS: Real;
|
---|
229 | var
|
---|
230 | StopTime: TDateTime;
|
---|
231 | begin
|
---|
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;
|
---|
239 | end;
|
---|
240 |
|
---|
241 | procedure TDrawMethod.Init(Parent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat);
|
---|
242 | begin
|
---|
243 | FParent := Parent;
|
---|
244 | end;
|
---|
245 |
|
---|
246 | procedure TDrawMethod.Done;
|
---|
247 | begin
|
---|
248 | end;
|
---|
249 |
|
---|
250 | constructor TDrawMethod.Create;
|
---|
251 | begin
|
---|
252 | Description := TStringList.Create;
|
---|
253 | end;
|
---|
254 |
|
---|
255 | destructor TDrawMethod.Destroy;
|
---|
256 | begin
|
---|
257 | FreeAndNil(Description);
|
---|
258 | inherited;
|
---|
259 | end;
|
---|
260 |
|
---|
261 | procedure TDrawMethod.DrawFrame(FastBitmap: TFastBitmap);
|
---|
262 | begin
|
---|
263 | end;
|
---|
264 |
|
---|
265 | procedure TDrawMethod.DrawFrameTiming(FastBitmap: TFastBitmap);
|
---|
266 | var
|
---|
267 | StartTime: TDateTime;
|
---|
268 | begin
|
---|
269 | StartTime := NowPrecise;
|
---|
270 | DrawFrame(FastBitmap);
|
---|
271 | FrameDuration := NowPrecise - StartTime;
|
---|
272 | end;
|
---|
273 |
|
---|
274 | procedure TDrawMethod.UpdateSettings;
|
---|
275 | begin
|
---|
276 | end;
|
---|
277 |
|
---|
278 | end.
|
---|
279 |
|
---|