-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathProcess.hx
767 lines (615 loc) · 19.4 KB
/
Process.hx
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
package dn;
import dn.Lib;
import dn.struct.FixedArray;
import dn.debug.MemTrack.measure as MM;
class Process {
public static var MAX_PROCESSES = 1024;
/** FPS frequency of the `fixedUpdate` calls **/
public static var FIXED_UPDATE_FPS = 30;
public static var CUSTOM_STAGE_WIDTH = -1;
public static var CUSTOM_STAGE_HEIGHT = -1;
static var UNIQ_ID = 0;
static var ROOTS : FixedArray<Process> = new FixedArray("RootProcesses", MAX_PROCESSES);
static var BEGINNING_OF_FRAME_CALLBACKS : FixedArray< Void->Void > = new FixedArray(256);
static var END_OF_FRAME_CALLBACKS : FixedArray< Void->Void > = new FixedArray(256);
/** If TRUE, each Process.onResize() will be called *once* at the end of the frame **/
static var RESIZE_REQUESTED = true;
public var uniqId : Int;
/** Elapsed frames from the client start **/
public var ftime(default, null) : Float; // elapsed frames
/** Elapsed frames from the client start, as 32bits Int **/
public var itime(get, never) : Int;
/** Elapsed seconds from the client start **/
public var stime(get, never) : Float;
@:noCompletion
@:deprecated("Use isPaused() here")
public var paused(get,never) : Bool; inline function get_paused() return _manuallyPaused;
/** TRUE if this process was directly manually paused **/
var _manuallyPaused: Bool;
/** TRUE if process was marked for removal during garbage collection phase. Most stuff should stop from happening if this flag is set! **/
public var destroyed(default, null) : Bool;
/** Optional parent process ref **/
var parent(default, null) : Process;
/** Time modifier (1.0 if FPS is ok, >1 if some frames were lost, <1 if more frames are played than expected) **/
public var tmod(get,never) : Float; inline function get_tmod() return utmod * getComputedTimeMultiplier();
/** This `tmod` value is unaffected by the time multiplier **/
public var utmod : Float;
/** Elapsed frames not affected by time multiplier **/
public var uftime(default, null) : Float;
var baseTimeMul = 1.0;
/** Set this to TRUE if you want this Process to ignore any existing time multipliers, including the ones from parent Processes. For example, a UI component will probably want to ignore time multipliers, even if the game is running in slow-motion. **/
var ignoreTimeMultipliers = false;
@:noCompletion
@:deprecated("Use baseTimeMul instead")
var speedMod(get,set) : Float;
@:noCompletion inline function get_speedMod() return baseTimeMul;
@:noCompletion inline function set_speedMod(v) return baseTimeMul = v;
@:noCompletion
@:deprecated("Use tmod instead")
public var dt(get,never) : Float; inline function get_dt() return tmod;
/** Process display name **/
public var name : Null<String>;
var children : FixedArray<Process>;
/** Delayer allows for callbacks to be called in a future frame **/
public var delayer : dn.Delayer;
/** Cooldown allows to track various countdowns for tons of good reasons **/
public var cd : dn.Cooldown;
/** Tweenie tweens values **/
public var tw : dn.Tweenie;
/** Same as `delayer` but it isn't affected by time multiplier **/
public var udelayer : dn.Delayer;
/** Same as `cd` but it isn't affected by time multiplier **/
public var ucd : dn.Cooldown;
var _fixedUpdateAccu = 0.;
// Optional graphic context
#if( heaps || h3d )
/** Graphic context, can be null **/
public var root : Null<h2d.Layers>;
public var engine(get,never) : h3d.Engine;
#elseif flash
/** Graphic context, can be null **/
public var root : Null<flash.display.Sprite>;
var pt0 : flash.geom.Point; // to reduce allocations
#end
public function new(?parent : Process) {
init();
if (parent == null)
ROOTS.push(this);
else
parent.addChild(this);
resizeAll(false);
}
public function init(){
uniqId = UNIQ_ID++;
children = new FixedArray(MAX_PROCESSES);
_manuallyPaused = false;
destroyed = false;
ftime = 0;
uftime = 0;
utmod = 1;
baseTimeMul = 1;
cd = new Cooldown( getDefaultFrameRate() );
delayer = new Delayer( getDefaultFrameRate() );
tw = new Tweenie( getDefaultFrameRate() );
ucd = new Cooldown( getDefaultFrameRate() );
udelayer = new Delayer( getDefaultFrameRate() );
}
var _initOnceDone = false;
/** This special init method is only called once during next frame: call will happen *after* the constructor call and *before* any update. **/
function initOnceBeforeUpdate() {}
#if( heaps || flash )
/** Init graphic context **/
public function createRoot(
#if heaps ?ctx:h2d.Object
#elseif flash ?ctx:flash.display.Sprite #end
) {
if( root!=null )
throw this+": root already created!";
if( ctx==null ) {
if( parent==null || parent.root==null )
throw this+": context required";
ctx = parent.root;
}
#if( heaps || h3d )
root = new h2d.Layers(ctx);
root.name = getDisplayName();
#elseif flash
root = new flash.display.Sprite();
ctx.addChild(root);
pt0 = new flash.geom.Point();
#end
}
#end
#if heaps
/**
Init graphic context without adding it anywhere
**/
public function createRootInNoContext() {
if( root!=null )
throw this+": root already created!";
root = new h2d.Layers();
root.name = getDisplayName();
}
#end
#if heaps
/** Init graphic context in specified `h2d.Layers` **/
public function createRootInLayers(ctx:h2d.Layers, plan:Int) {
if( root!=null )
throw this+": root already exists";
root = new h2d.Layers();
root.name = getDisplayName();
ctx.add(root, plan);
}
#end
/**
For debug purpose, print the hierarchy of all children processes of this one.
**/
public function rprintChildren() : String {
function _crawl(p:dn.Process, depth=0) {
var out = [];
var indent = depth>0 ? " |--- " : "";
indent = dn.Lib.repeatChar(" ",6*(depth-1)) + indent;
out.push( indent + p.toString() );
for( cp in p.children )
out = out.concat( _crawl(cp, depth+1) );
return out;
}
return _crawl(this).join("\n");
}
public static function rprintAll() : String {
var all = [];
for( p in ROOTS )
all.push( p.rprintChildren() );
return all.join("\n");
}
public static function destroyAllExcept(excepts:Array<Process>) {
for(p in ROOTS) {
if( p.destroyed )
continue;
var keep = false;
for(e in excepts)
if( e==p ) {
keep = true;
break;
}
if( !keep )
p.destroy();
}
}
/** Set to TRUE to enabled Process timing profiling (warning: this will reduce performances)**/
public static var PROFILING = false;
/** List of current profiling times **/
public static var PROFILER_TIMES : Map<String,Float> = new Map();
var tmpProfilerTimes = new Map();
/** Clear current profiling times **/
public static inline function clearProfilingTimes() {
PROFILER_TIMES = new Map();
}
inline function markProfilingStart(id:String) {
if( PROFILING ) {
id = getDisplayName()+"."+id;
tmpProfilerTimes.set(id, haxe.Timer.stamp());
}
}
inline function markProfilingEnd(id:String) {
if( PROFILING ) {
id = getDisplayName()+"."+id;
if( tmpProfilerTimes.exists(id) ) {
var t = haxe.Timer.stamp()-tmpProfilerTimes.get(id);
tmpProfilerTimes.remove(id);
if( !PROFILER_TIMES.exists(id) )
PROFILER_TIMES.set(id,t);
else
PROFILER_TIMES.set(id, PROFILER_TIMES.get(id)+t);
}
}
}
/**
Return the current profiling times, sorted from highest to lowest
**/
public static function getSortedProfilerTimes() {
var all = [];
for(i in PROFILER_TIMES.keyValueIterator())
all.push(i);
all.sort( (a,b)->-Reflect.compare(a.value,b.value));
return all;
}
/** Called at the "beginning of the frame", before any Process update(), in declaration order **/
function preUpdate() {}
/** Called in the "middle of the frame", after all preUpdates() in declaration order **/
function update() {}
/**
Called in the "middle of the frame", after all updates(), but only X times per seconds (see `fixedUpdateFps`), in declaration order.
Eg. if the client is running 60fps, and fixedUpdateFps if 30fps, this method will only be called 1 frame out of 2.
**/
function fixedUpdate() { }
/**
Return a ratio (0-1) representing the progression of the time between last fixedUpdate and the next one.
This value is, for example, used to smooth position of elements that are updated at a lower FPS.
**/
public inline function getFixedUpdateAccuRatio() {
return _fixedUpdateAccu / ( getDefaultFrameRate() / FIXED_UPDATE_FPS );
}
/** Called at the "end of the frame", after all updates()/fixedUpdates(), in declaration order. That's were graphic updates should probably happen. **/
function postUpdate() { }
/** Called when client window is resized. **/
function onResize() { }
/** Called when Process is garbaged, that's where all the cleanup should happen **/
function onDispose() { }
/** "Overridable at runtime" callbacks **/
public dynamic function onUpdateCb() {}
public dynamic function onFixedUpdateCb() {}
public dynamic function onDisposeCb() {}
/** Get printable process instance name **/
@:keep
public function toString() {
return '#$uniqId ${getDisplayName()}${isPaused()?" [PAUSED]":""}';
}
/** Some human readable name for this Process instance **/
var _cachedClassName : String;
public inline function getDisplayName() {
return name!=null
? name
: {
if( _cachedClassName==null )
_cachedClassName = Type.getClassName( Type.getClass(this) );
return _cachedClassName;
}
}
// -----------------------------------------------------------------------
// private api
// -----------------------------------------------------------------------
inline function get_itime() return Std.int(ftime);
inline function get_stime() return ftime/getDefaultFrameRate();
#if( heaps || h3d )
inline function get_engine() return h3d.Engine.getCurrent();
#end
inline function addAlpha(c:Int, ?a=1.0) : #if flash UInt #else Int #end {
return Std.int(a*255)<<24 | c;
}
inline function rnd(min,max,?sign) return Lib.rnd(min,max,sign);
inline function irnd(min,max,?sign) return Lib.irnd(min,max,sign);
inline function rndSign() return Std.random(2)*2-1;
inline function rndSecondsF(min,max,?sign) return secToFrames( Lib.rnd(min,max,sign) );
inline function irndSecondsF(min,max,?sign) return secToFrames( Lib.rnd(min,max,sign) );
public inline function pretty(v:Float, ?precision=2) return M.pretty(v, precision);
public function secToFrames(v:Float) return v*getDefaultFrameRate();
public function framesToSec(v:Float) return v/getDefaultFrameRate();
public inline function msToFrames(v:Float) return (v/1000)*getDefaultFrameRate();
public inline function framesToMs(v:Float) return 1000*v/getDefaultFrameRate();
function getDefaultFrameRate() : Int {
#if heaps
return M.round( hxd.Timer.wantedFPS );
#elseif flash
return flash.Lib.current.stage.frameRate;
#else
return 30; // N/A
#end
}
/** Set this process time multiplier, which will affect tmod, cd, delayers, etc. **/
public function setTimeMultiplier(v:Float) {
baseTimeMul = v;
}
/** Get "total" time multiplier, including parent processes **/
function getComputedTimeMultiplier() : Float {
return ignoreTimeMultipliers ? 1.0 : M.fmax(0, baseTimeMul * ( parent==null ? 1 : parent.getComputedTimeMultiplier() ) );
}
/** Get graphical context width **/
public inline function w(){
if( CUSTOM_STAGE_WIDTH > 0 )
return CUSTOM_STAGE_WIDTH;
#if heaps
return hxd.Window.getInstance().width;
#elseif (flash||openfl)
return flash.Lib.current.stage.stageWidth;
#else
return 1;
#end
}
/** Get graphical context height **/
public inline function h(){
if( CUSTOM_STAGE_HEIGHT > 0 )
return CUSTOM_STAGE_HEIGHT;
#if heaps
return hxd.Window.getInstance().height;
#elseif (flash||openfl)
return flash.Lib.current.stage.stageHeight;
#else
return 1;
#end
}
inline function anyParentPaused() {
return parent!=null ? parent.isPaused() : false;
}
public function isPaused() {
if( _manuallyPaused )
return true;
else
return anyParentPaused();
}
public function pause() _manuallyPaused = true;
public function resume() _manuallyPaused = false;
public final inline function togglePause() : Bool {
if( _manuallyPaused )
resume();
else
pause();
return _manuallyPaused;
}
public final inline function destroy() destroyed = true;
public function addChild( p : Process ) {
if (p.parent == null) ROOTS.remove(p);
else p.parent.children.remove(p);
p.parent = this;
children.push(p);
}
public inline function isRootProcess() return parent==null;
public function moveChildToRootProcesses( p : Process ) {
if( p.parent!=this )
throw "Not a child of this process";
p.parent = null;
children.remove(p);
ROOTS.push(p);
}
public function removeAndDestroyChild( p : Process ) {
if( p.parent!=this )
throw "Not a child of this process";
p.parent = null;
children.remove(p);
ROOTS.push(p); // needed for garbage collector
p.destroy();
}
public function createChildProcess( ?onUpdate:Process->Void, ?onDispose:Process->Void, ?runUpdateImmediatly=false ) : Process {
var p = new dn.Process(this);
if( onUpdate!=null )
p.onUpdateCb = function() {
onUpdate(p);
}
if( onDispose!=null )
p.onDisposeCb = function() {
onDispose(p);
}
if( runUpdateImmediatly ) {
_doPreUpdate(p,1);
_doMainUpdate(p);
}
return p;
}
public function killAllChildrenProcesses() {
for(p in children)
p.destroy();
}
// -----------------------------------------------------------------------
// Internals statics
// -----------------------------------------------------------------------
static inline function canRun(p:Process) return !p.isPaused() && !p.destroyed;
static inline function _doPreUpdate(p:Process, utmod:Float) {
if( !canRun(p) )
return;
p.utmod = utmod;
p.ftime += p.tmod;
p.uftime += p.utmod;
p.delayer.update(p.tmod);
if( canRun(p) )
p.udelayer.update(p.utmod);
if( canRun(p) )
p.cd.update(p.tmod);
if( canRun(p) )
p.ucd.update(p.utmod);
if( canRun(p) )
p.tw.update(p.tmod);
if( canRun(p) ) {
if( !p._initOnceDone ) {
p.initOnceBeforeUpdate();
p._initOnceDone = true;
}
p.preUpdate();
}
if( canRun(p) )
for (c in p.children)
_doPreUpdate(c,p.utmod);
}
static function _doMainUpdate(p : Process) {
if( !canRun(p) )
return;
p.markProfilingStart("update");
p.update();
if( p.onUpdateCb!=null )
p.onUpdateCb();
p.markProfilingEnd("update");
if( canRun(p) )
for (p in p.children)
_doMainUpdate(p);
}
static function _doFixedUpdate(p : Process) {
if( !canRun(p) )
return;
p.markProfilingStart("fixed");
p._fixedUpdateAccu+=p.tmod;
while( p._fixedUpdateAccu >= p.getDefaultFrameRate() / FIXED_UPDATE_FPS ) {
p._fixedUpdateAccu -= p.getDefaultFrameRate() / FIXED_UPDATE_FPS;
if( canRun(p) ) {
p.fixedUpdate();
if( p.onFixedUpdateCb!=null )
p.onFixedUpdateCb();
}
}
p.markProfilingEnd("fixed");
if( canRun(p) )
for (p in p.children)
_doFixedUpdate(p);
}
static inline function _doPostUpdate(p : Process) {
if( !canRun(p) )
return;
p.markProfilingStart("post");
p.postUpdate();
p.markProfilingEnd("post");
if( !p.destroyed )
for (c in p.children)
_doPostUpdate(c);
}
static function _garbageCollector(plist:FixedArray<Process>) {
var i = 0;
var p : Process;
while (i < plist.allocated) {
p = plist.get(i);
if( p.destroyed )
_disposeProcess(p);
else {
_garbageCollector(p.children);
i++;
}
}
}
static function _disposeProcess(p : Process) {
// Children
for(p in p.children)
p.destroy();
_garbageCollector(p.children);
// Tools
p.delayer.destroy();
p.udelayer.destroy();
p.cd.dispose();
p.ucd.dispose();
p.tw.destroy();
// Unregister from lists
if (p.parent != null)
p.parent.children.remove(p);
else
ROOTS.remove(p);
// Graphic context
#if( heaps || flash )
if( p.root!=null ) {
#if( heaps || h3d )
p.root.remove();
#else
p.root.parent.removeChild(p.root);
#end
}
#end
// Callbacks
p.onDispose();
if( p.onDisposeCb != null )
p.onDisposeCb();
// Clean up
p.parent = null;
p.children = null;
p.cd = null;
p.ucd = null;
p.delayer = null;
p.udelayer = null;
p.tw = null;
#if( heaps || h3d )
p.root = null;
#elseif flash
p.root = null;
p.pt0 = null;
#end
}
static inline function _resizeProcess(p : Process) {
if ( !p.destroyed ){
p.onResize();
for ( p in p.children )
_resizeProcess(p);
}
}
// -----------------------------------------------------------------------
// Public static API
// -----------------------------------------------------------------------
public static function updateAll(utmod:Float) {
// Beginning of frame callbacks
if( BEGINNING_OF_FRAME_CALLBACKS.allocated>0 ) {
for( cb in BEGINNING_OF_FRAME_CALLBACKS )
cb();
BEGINNING_OF_FRAME_CALLBACKS.empty();
}
for (p in ROOTS)
_doPreUpdate(p, utmod);
for (p in ROOTS)
_doMainUpdate(p);
for (p in ROOTS)
_doFixedUpdate(p);
for (p in ROOTS)
_doPostUpdate(p);
if( RESIZE_REQUESTED ) {
RESIZE_REQUESTED = false;
for(p in ROOTS)
_resizeProcess(p);
}
_garbageCollector(ROOTS);
// End of frame callbacks
if( END_OF_FRAME_CALLBACKS.allocated>0 ) {
for(cb in END_OF_FRAME_CALLBACKS)
cb();
END_OF_FRAME_CALLBACKS.empty();
}
}
/** Request a onResize() call for all processes. If `immediately` is false (default), the call will only happen **once** and **at the end** of current frame. **/
public static function resizeAll(immediately=false) {
if( immediately ) {
for ( p in ROOTS )
_resizeProcess(p);
}
else
RESIZE_REQUESTED = true;
}
/** Request a onResize() call for ALL processes at the end of current frame. **/
inline function emitResizeAtEndOfFrame() {
resizeAll(false);
}
/** Request a onResize() call for ALL processes immediately. **/
inline function emitResizeNow() {
resizeAll(true);
}
/** Run a callback at the very beginning of the next frame, before any process. **/
public static function callAtTheBeginningOfNextFrame( cb:Void->Void ) {
BEGINNING_OF_FRAME_CALLBACKS.push(cb);
}
/** Run a callback at the absolute end of frame, after all processes and after dead processes garbage collection. **/
public static function callAtTheEndOfCurrentFrame( cb:Void->Void ) {
END_OF_FRAME_CALLBACKS.push(cb);
}
@:noCompletion
public static function __test() {
var root = new Process();
CiAssert.isNotNull(root);
CiAssert.isNotNull(root.cd);
CiAssert.isNotNull(root.ucd);
CiAssert.isNotNull(root.delayer);
CiAssert.isNotNull(root.udelayer);
CiAssert.isNotNull(root.tw);
CiAssert.equals( root.tmod, 1 );
CiAssert.equals( ROOTS.allocated, 1);
// Pause management
CiAssert.equals( root.togglePause(), true );
CiAssert.equals( root.togglePause(), false );
var c1 = new Process(root);
CiAssert.isNotNull(c1);
CiAssert.equals(root.children.allocated, 1);
CiAssert.isFalse( c1.isPaused() );
root.pause();
CiAssert.isTrue( c1.isPaused() );
root.resume();
var c2 = new Process(root);
CiAssert.isNotNull(c2);
CiAssert.equals(root.children.allocated, 2);
CiAssert.equals(c2.parent, root);
CiAssert.isFalse( c2.isPaused() );
root.pause();
CiAssert.isTrue( c2.isPaused() );
// Destruction
c2.destroy();
updateAll(1); // force GC
CiAssert.isFalse( c1.destroyed );
CiAssert.isTrue( c2.destroyed );
CiAssert.isFalse( root.destroyed );
CiAssert.equals(root.children.allocated, 1);
root.destroy();
updateAll(1); // force GC
CiAssert.isTrue( c1.destroyed );
CiAssert.isTrue( c2.destroyed );
CiAssert.isTrue( root.destroyed );
CiAssert.equals( ROOTS.allocated, 0);
}
}