forked from RyoleBg/PE-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPS.hx
114 lines (94 loc) · 2.68 KB
/
FPS.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
package openfl.display;
import haxe.Timer;
import openfl.events.Event;
import openfl.text.TextField;
import openfl.text.TextFormat;
import flixel.math.FlxMath;
#if gl_stats
import openfl.display._internal.stats.Context3DStats;
import openfl.display._internal.stats.DrawCallContext;
#end
#if flash
import openfl.Lib;
#end
#if openfl
import openfl.system.System;
#end
/**
The FPS class provides an easy-to-use monitor to display
the current frame rate of an OpenFL project
**/
#if !openfl_debug
@:fileXml('tags="haxe,release"')
@:noDebug
#end
class FPS extends TextField
{
/**
The current frame rate, expressed using frames-per-second
**/
public var currentFPS(default, null):Int;
private var memoryMegasPeak:Float = 0;
@:noCompletion private var cacheCount:Int;
@:noCompletion private var currentTime:Float;
@:noCompletion private var times:Array<Float>;
public function new(x:Float = 10, y:Float = 10, color:Int = 0x000000)
{
super();
this.x = x;
this.y = y;
currentFPS = 144;
selectable = false;
mouseEnabled = true;
defaultTextFormat = new TextFormat (Paths.font("vcr.ttf"), 13, color); //this is how to change you're fonts, piss babies
autoSize = LEFT;
multiline = true;
text = "FPS: ";
cacheCount = 0;
currentTime = 0;
times = [];
#if flash
addEventListener(Event.ENTER_FRAME, function(e)
{
var time = Lib.getTimer();
__enterFrame(time - currentTime);
});
#end
}
// Event Handlers
@:noCompletion
private #if !flash override #end function __enterFrame(deltaTime:Float):Void
{
currentTime += deltaTime;
times.push(currentTime);
while (times[0] < currentTime - 1000)
{
times.shift();
}
var currentCount = times.length;
currentFPS = Math.round((currentCount + cacheCount) / 2);
if (currentFPS > ClientPrefs.framerate) currentFPS = ClientPrefs.framerate;
if (currentCount != cacheCount /*&& visible*/)
{
text = "FPS: " + currentFPS;
var memoryMegas:Float = 0;
#if openfl
memoryMegas = Math.abs(FlxMath.roundDecimal(System.totalMemory / 1000000, 1));
if (memoryMegas > memoryMegasPeak) memoryMegasPeak = memoryMegas;
if (ClientPrefs.showMEM) text += "\nMEM: " + memoryMegas + " RAM" + "\nMEM PEAK: " + memoryMegasPeak + " RAM";
#end
textColor = 0xFF9C9C9C;
if (memoryMegas > 3000 || currentFPS <= ClientPrefs.framerate / 2)
{
textColor = 0xFFFF0037;
}
#if (gl_stats && !disable_cffi && (!html5 || !canvas))
text += "\ntotalDC: " + Context3DStats.totalDrawCalls();
text += "\nstageDC: " + Context3DStats.contextDrawCalls(DrawCallContext.STAGE);
text += "\nstage3DDC: " + Context3DStats.contextDrawCalls(DrawCallContext.STAGE3D);
#end
text += "\n";
}
cacheCount = currentCount;
}
}