@@ -11,13 +11,13 @@ export class Application {
1111 * Construct method of the object
1212 * @returns Application
1313 */
14- constructor ( { width, height, renderer, logFPS} = { } ) {
14+ constructor ( { width, height, renderer, logFPS, minFrameTime } = { } ) {
1515 let _this = this
1616
1717 this . scenes = {
1818 all : [ ] ,
1919 active : undefined ,
20- activeName : undefined
20+ activeName : undefined
2121 }
2222
2323 //settings
@@ -34,15 +34,15 @@ export class Application {
3434 }
3535
3636 this . frames = {
37- "frame" : 0 ,
37+ frame : 0 ,
38+ minFrameTime : minFrameTime !== undefined ? Math . floor ( 1000 / minFrameTime ) : undefined ,
39+ lastTimestamp : undefined
3840 }
3941
40- this . fps = {
41- "starttime" : undefined ,
42- "rate" : 1000 / 60 ,
43- "deltatime" : 0 ,
44- "timestep" : 1 ,
45- "maxskippingframes" : 5
42+ this . time = {
43+ startTime : undefined ,
44+ deltaTime : 0 ,
45+ timestep : 1
4646 }
4747
4848 if ( logFPS ) {
@@ -81,14 +81,22 @@ export class Application {
8181 */
8282 render ( timestamp , _this = this ) {
8383 //calc fps rate
84- if ( _this . fps . starttime === undefined ) {
85- _this . fps . starttime = timestamp
86- _this . fps . lastFrame = Math . round ( ( timestamp - _this . fps . starttime ) / _this . fps . rate )
84+ if ( _this . time . startTime === undefined ) {
85+ _this . time . startTime = timestamp
86+ _this . frames . lastTimestamp = timestamp
8787 } else {
88- let currentFrame = Math . round ( ( timestamp - _this . fps . starttime ) / _this . fps . rate ) ;
89- _this . fps . deltatime = ( currentFrame - _this . fps . lastFrame ) * ( 1000 / 60 ) ;
90- _this . fps . timestep = Math . min ( _this . fps . deltatime / _this . fps . rate , _this . fps . maxskippingframes )
91- _this . fps . lastFrame = currentFrame
88+ let deltaTime = timestamp - _this . frames . lastTimestamp
89+ _this . frames . lastTimestamp = timestamp
90+ _this . time . deltaTime += deltaTime
91+
92+ if ( _this . time . minFrameTime !== undefined && _this . time . deltaTime < _this . frames . minFrameTime ) {
93+ window . requestAnimationFrame ( function ( t ) {
94+ _this . render ( t , _this )
95+ } )
96+ return
97+ }
98+
99+ _this . time . timestep = _this . time . deltaTime / ( 1000 / 60 )
92100 }
93101
94102 // TODO: check every timestep needed?
@@ -98,13 +106,17 @@ export class Application {
98106 if ( _this . project . logFPS ) this . stats . begin ( )
99107
100108 // calc the next frame
101- _this . scenes . active . calc ( { timestep : _this . fps . timestep } )
109+ _this . scenes . active . calc ( { timestep : _this . time . timestep } )
102110
103111 // render the camera-views to the offscreen canvases
104112 _this . scenes . active . render ( { app : _this } )
105113
106114 if ( _this . project . logFPS ) this . stats . end ( )
107115 }
116+
117+ // reset delta time
118+ _this . time . deltaTime = 0
119+
108120 window . requestAnimationFrame ( function ( t ) {
109121 _this . render ( t , _this )
110122 } )
@@ -140,16 +152,16 @@ export class Application {
140152 /**
141153 * Destructs the current scene
142154 */
143- destroyCurrentScene ( ) {
144- if ( this . scenes . activeName ) {
155+ destroyCurrentScene ( ) {
156+ if ( this . scenes . activeName ) {
145157 this . scenes . all [ this . scenes . activeName ] . destroy ( )
146158 }
147159 }
148160
149161 /**
150162 * Re-inits the current scene
151163 */
152- reloadCurrentScene ( ) {
164+ reloadCurrentScene ( ) {
153165 // destroy current scene
154166 this . destroyCurrentScene ( )
155167
@@ -160,7 +172,7 @@ export class Application {
160172 /**
161173 * Re-inits the current scene
162174 */
163- getCurrentSceneName ( ) {
175+ getCurrentSceneName ( ) {
164176 // init scene
165177 return this . scenes . activeName
166178 }
0 commit comments