source: trunk/UDrawMethod.pas

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