-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDemoMain.pas
More file actions
345 lines (302 loc) · 11.4 KB
/
DemoMain.pas
File metadata and controls
345 lines (302 loc) · 11.4 KB
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
(******************************************************************************
Pascal Game Development Community Engine (PGDCE)
The contents of this file are subject to the license defined in the file
'licence.md' which accompanies this file; you may not use this file except
in compliance with the license.
This file is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
either express or implied. See the license for the specific language governing
rights and limitations under the license.
The Original Code is demomain.pas
The Initial Developer of the Original Code is documented in the accompanying
help file PGDCE.chm. Portions created by these individuals are Copyright (C)
2015 of these individuals.
******************************************************************************)
{
@abstract(Crossplatform demo main unit)
This is main unit for crossplatform demo application
@author(George Bakhtadze (avagames@gmail.com))
}
{$Include PGDCE.inc}
unit DemoMain;
interface
uses
CEBaseApplication,
CEBaseRenderer,
CEMesh, CE2DMesh, CEGameEntity,
CEMessage, CEInputMessage, CEBaseTypes, CEMaterial, CECore, CESplines;
type
TRotatingTriangle = class(TCEGameEntity)
public
procedure Update(const DeltaTime: Single); override;
end;
TDemo = class(TObject)
private
Spline: TSpline;
App: TCEBaseApplication;
Renderer: TCEBaseRenderer;
Core: TCECore;
PolyMesh: TCEPolygonMesh;
LineMesh: TCELineMesh;
SpriteMesh1, SpriteMesh2: TCESpriteMesh;
PolyPass, LinePass, SpritePass, MonsterSpritePass, PointPass: TCERenderPass;
PointMesh: TCECircleMesh;
Triangle: TRotatingTriangle;
ClickPoint: TCEVector2f;
Ind: Integer;
Ids: array[0..$FF] of Integer;
Speed: Single;
SplineResolution: Integer;
ControlPoints: P2DPointArray;
ControlPointsCount: Integer;
LastFrameNum: Cardinal;
LastFPSTime: Int64;
procedure ApplyPoints(Mesh: TCENPointMesh; CPoints: P2DPointArray; Count: Integer);
public
constructor Create(Application: TCEBaseApplication);
destructor Destroy(); override;
procedure HandleKey(Msg: TKeyboardMsg);
procedure HandleMessage(const Msg: TCEMessage);
function Process(): Boolean;
end;
// Example mesh class
TCERotatingTriangleMesh = class(TCEMesh)
private
FAngle: Single;
procedure SetAngle(const Value: Single);
public
property Angle: Single read FAngle write SetAngle;
procedure WriteMeshData(Destinations: PDataDestinations); override;
end;
implementation
uses
{$IFDEF WINDOWS}
CEWindowsApplication,
{$IFDEF OPENGLES_EMULATION}
CEOpenGLES2Renderer,
{$ELSE}
CEOpenGL4Renderer,
{$ENDIF}
{$ELSE}
{$IFDEF XWINDOW}
CEOpenGL4Renderer,
{$ENDIF}
{$IFDEF MOBILE}
CEOpenGLES2Renderer,
{$ENDIF}
{$ENDIF}
CEOSMessageInput,
sysutils, CELog, CEOSUtils, CEUniformsManager,
CECommon, CEBaseInput, CEEntityMessage, CEVectors;
procedure TCERotatingTriangleMesh.SetAngle(const Value: Single);
begin
FAngle := Value;
InvalidateData(dbtVertex1, true);
end;
procedure TCERotatingTriangleMesh.WriteMeshData(Destinations: PDataDestinations);
var
a: Single;
v: ^TVBPos;
begin
inherited;
a := Angle * pi / 180;
v := Destinations^[dbtVertex1];
Vec3f(cos(a), sin(a), 0, v^[0].vec);
//v^[0].u := 0; v^[0].v := 0;
Vec3f(cos(a + 2 * pi / 3), sin(a + 2 * pi / 3), 0, v^[1].vec);
//v^[1].u := 1; v^[1].v := 0;
Vec3f(cos(a + 4 * pi / 3), sin(a + 4 * pi / 3), 0, v^[2].vec);
//v^[2].u := 0.5; v^[2].v := 0.5;
FVerticesCount := 3;
SetDataSize(dbtVertex1, SizeOf(TVBRecPos));
end;
procedure TRotatingTriangle.Update(const DeltaTime: Single);
begin
//TCERotatingTriangleMesh(Mesh).Angle := TCERotatingTriangleMesh(Mesh).Angle + speed * DeltaTime;
end;
procedure TDemo.ApplyPoints(Mesh: TCENPointMesh; CPoints: P2DPointArray; Count: Integer);
var
i: Integer;
begin
Mesh.Count := (Count - 1) * (SplineResolution) + 1;
//CalcCatmullRom2D(Count, SplineResolution, CPoints, Mesh.Points);
for i := 0 to Mesh.Count - 1 do
begin
Mesh.Points^[i].x := Spline.Value[i*(Count-1)/(Mesh.Count - 1), 0];
Mesh.Points^[i].y := Spline.Value[i*(Count-1)/(Mesh.Count - 1), 1];
end;
end;
constructor TDemo.Create(Application: TCEBaseApplication);
begin
App := Application;
Renderer := TCERendererClass.Create(App);
CELog.Info('Creating core');
Core := TCECore.Create();
Core.Application := App;
Core.Renderer := Renderer;
CELog.Info('Creating input');
Core.Input := TCEOSMessageInput.Create();
App.MessageHandler := HandleMessage;
//Triangle := TRotatingTriangle.Create(Core.EntityManager);
PolyMesh := TCEPolygonMesh.Create(Core.EntityManager);
PolyMesh.Count := 4;
PolyMesh.Point[3] := Vec2f(-0.5, -0.5);
PolyMesh.Point[2] := Vec2f( 0.0, -0.4);
PolyMesh.Point[1] := Vec2f( 0.2, 0.4);
PolyMesh.Point[0] := Vec2f(-0.3, 0.5);
PolyMesh.Softness := 2 / 1024 * 1.5;
PolyMesh.Color := GetColor(100, 200, 150, 255);
LineMesh := TCELineMesh.Create(Core.EntityManager);
LineMesh.Softness := 2 / 1024 * 1.3 * 1;
LineMesh.Width := 1 / 1024 * 1.3*40;
Spline := TSpline.Create();
SplineResolution := 1;
ControlPointsCount := 6;
GetMem(ControlPoints, (ControlPointsCount + 1) * SizeOf(TCEVector2f));
ControlPoints^[0] := Vec2f(-0.5, 0.5 + 0.1);
//ControlPoints^[0] := Vec2f(-0.46484375, 0.462890625);
{pnt^[2] := Vec2f(0.4, 0.25);
pnt^[1] := Vec2f(0.5, 0.3);}
ControlPoints^[1] := Vec2f( 0.0, 0.0);
ControlPoints^[2] := Vec2f( 0.5, -0.5);
//ControlPoints^[2] := Vec2f(0.37890625, -0.376953125);
ControlPoints^[3] := Vec2f( 0.5, -0.5);
ControlPoints^[4] := Vec2f( 0.0, -0.5);
ControlPoints^[5] := Vec2f(-0.5, -0.5);
Spline.SetSamples(ControlPointsCount, PSingleBuffer(ControlPoints));
ApplyPoints(LineMesh, ControlPoints, ControlPointsCount);
SpriteMesh1 := TCESpriteMesh.Create(Core.EntityManager);
SpriteMesh1.X := -0.5;
SpriteMesh1.SetTextureParameters(4, 4);
SpriteMesh2 := TCESpriteMesh.Create(Core.EntityManager);
SpriteMesh2.X := 0.5;
SpriteMesh2.Width := 0.5;
SpriteMesh2.Height := 0.5;
PointMesh := TCECircleMesh.Create(Core.EntityManager);
PointMesh.Radius := 0.02;
PointMesh.Color := GetColor($FFFF9F40);
CELog.Info('Loading materials');
PolyPass := CreateRenderPass(Core.EntityManager, true, '', 'asset://vs_poly.glsl', 'asset://fs_poly.glsl');
LinePass := CreateRenderPass(Core.EntityManager, true, '', 'asset://vs_line.glsl', 'asset://fs_line.glsl');
SpritePass := CreateRenderPass(Core.EntityManager, true, 'asset://sprites.bmp', 'asset://vs_sprite.glsl', 'asset://fs_sprite.glsl');
MonsterSpritePass := CreateRenderPass(Core.EntityManager, true, 'asset://chest.bmp', 'asset://vs_sprite.glsl', 'asset://fs_sprite.glsl');
PointPass := CreateRenderPass(Core.EntityManager, true, '', 'asset://vs_circle.glsl', 'asset://fs_circle.glsl');
{Mat := TCEMaterial.Create(Core.EntityManager);
Mat.TotalTechniques := 1;
Mat.Technique[0] := TCERenderTechnique.Create(Core.EntityManager);
Mat.Technique[0].TotalPasses := 1;
Mat.Technique[0].Pass[0] := PolyPass;
Triangle.Material := Mat;
Line.Mesh := LineMesh;}
//Core.OnUpdateDelegate := Mesh.Update;
FillChar(Ids, SizeOf(Ids), 255);
LastFPSTime := GetCurrentMs();
end;
destructor TDemo.Destroy();
begin
FreeAndNil(Core);
FreeAndNil(Spline);
inherited Destroy();
end;
procedure TDemo.HandleKey(Msg: TKeyboardMsg);
begin
if Msg.Action = iaUp then
begin
case Msg.Key of
vkNUMPAD_DIVIDE: begin
SplineResolution := MaxI(1, SplineResolution - 1);
ApplyPoints(LineMesh, ControlPoints, ControlPointsCount);
end;
vkNUMPAD_MULTIPLY: begin
SplineResolution := MinI(200, SplineResolution + 1);
ApplyPoints(LineMesh, ControlPoints, ControlPointsCount);
end;
vkNUMPAD_MINUS: begin
LineMesh.Width := MaxS(0.5/1024, LineMesh.Width - ClampS(LineMesh.Width*0.1, 0.1/1024, 5/1024));
ApplyPoints(LineMesh, ControlPoints, ControlPointsCount);
end;
vkNUMPAD_PLUS: begin
LineMesh.Width := MinS(100/1024, LineMesh.Width + ClampS(LineMesh.Width*0.1, 0.1/1024, 5/1024));
ApplyPoints(LineMesh, ControlPoints, ControlPointsCount);
end;
vkNUMPAD_2: begin // 2 / 1024 * 1.3;
LineMesh.Softness := MaxS(0, LineMesh.Softness - 0.4/1024);
ApplyPoints(LineMesh, ControlPoints, ControlPointsCount);
end;
vkNUMPAD_8: begin
LineMesh.Softness := MinS(20/1024, LineMesh.Softness + 0.4/1024);
ApplyPoints(LineMesh, ControlPoints, ControlPointsCount);
end;
end;
end;
end;
procedure TDemo.HandleMessage(const Msg: TCEMessage);
begin
Core.HandleMessage(Msg);
if Msg.ClassType() = TAppActivateMsg then
Core.EntityManager.BroadcastMessage(nil, TEntityDataReloadRequestMessage.Create(nil))
else if Msg.ClassType() = TKeyboardMsg then
HandleKey(TKeyboardMsg(Msg))
else if Msg.ClassType() = TTouchMsg then
if (Renderer.Width > 0) and (Renderer.Height > 0) then
begin
case TTouchMsg(Msg).Action of
iaDown: begin
ClickPoint := Vec2f(Core.Input.MouseState.X / Renderer.Width * 2 - 1, 1 - Core.Input.MouseState.Y / Renderer.Height * 2);
Ids[TTouchMsg(Msg).PointerId] := GetNearestPointIndex(PolyMesh.Points, PolyMesh.Count, ClickPoint);
PolyMesh.Point[Ids[TTouchMsg(Msg).PointerId]] := ClickPoint;
end;
iaUp: Ids[TTouchMsg(Msg).PointerId] := -1;
iaMotion: PolyMesh.Point[Ids[TTouchMsg(Msg).PointerId]] :=
Vec2f(Core.Input.MouseState.X / Renderer.Width * 2 - 1, 1 - Core.Input.MouseState.Y / Renderer.Height * 2);
else FillChar(Ids, SizeOf(Ids), 255);
end;
//CELog.Debug('Touch: ' + IntToStr(Ord(Action)) + ', but: ' + IntToStr(Ord(Button)));
end;
end;
function TDemo.Process(): Boolean;
const
FPS_INTERVAL = 500;
var
LTime: Int64;
begin
if App.Terminated then begin
Result := false;
Exit;
end;
Result := true;
Renderer.Clear([cfColor, cfDepth], GetColor(40, 130, 130, 0), 1.0, 0);
Renderer.ApplyRenderPass(PolyPass);
Renderer.RenderMesh(PolyMesh);
Renderer.ApplyRenderPass(LinePass);
Renderer.RenderMesh(LineMesh);
Renderer.ApplyRenderPass(SpritePass);
Renderer.RenderMesh(SpriteMesh1);
Renderer.ApplyRenderPass(MonsterSpritePass);
Renderer.RenderMesh(SpriteMesh2);
Renderer.ApplyRenderPass(PointPass);
Renderer.RenderMesh(PointMesh);
if Core.Input.Pressed[vkNUMPAD_6] or (Core.Input.MouseState.Buttons[mbLeft] = iaDown) then Speed := Speed + 4;
if Core.Input.Pressed[vkNUMPAD_4] or (Core.Input.MouseState.Buttons[mbRight] = iaDown) then Speed := Speed - 4;
Speed := Clamps(Speed, -360, 360);
if Core.Input.Pressed[vkALT] and Core.Input.Pressed[vkX] then App.Terminated := True;
if Core.Input.MouseState.Buttons[mbLeft] = iaDown then
begin
if (Renderer.Width > 0) and (Renderer.Height > 0) then
begin
ClickPoint := Vec2f(Core.Input.MouseState.X / Renderer.Width * 2 - 1, 1 - Core.Input.MouseState.Y / Renderer.Height * 2);
Ind := GetNearestPointIndex(ControlPoints, ControlPointsCount, ClickPoint);
ControlPoints^[Ind] := ClickPoint;
ApplyPoints(PolyMesh, ControlPoints, ControlPointsCount);
SpriteMesh1.Frame := SpriteMesh1.Frame + 1;
end;
end;
Core.Process();
LTime := CEOSUtils.GetCurrentMs();
if (LTime - LastFPSTime) > FPS_INTERVAL then
begin
CELog.Info('FPS: ' + FloatToStr((Core.Renderer.FrameNum - LastFrameNum) * 1000 / (LTime - LastFPSTime)));
LastFrameNum := Core.Renderer.FrameNum;
LastFPSTime := LTime;
end;
end;
end.