@@ -416,8 +416,11 @@ public class PGL {
416416 protected static PGLListener listener ;
417417
418418 /** Desired target framerate */
419- protected float targetFramerate = 60 ;
420- protected boolean setFramerate = false ;
419+ protected float targetFps = 60 ;
420+ protected float currentFps = 60 ;
421+ protected boolean setFps = false ;
422+ protected int fcount , lastm ;
423+ protected int fint = 3 ;
421424
422425 /** Which texturing targets are enabled */
423426 protected static boolean [] texturingTargets = { false , false };
@@ -557,7 +560,7 @@ public PGL(PGraphicsOpenGL pg) {
557560
558561
559562 protected void setFrameRate (float framerate ) {
560- if (targetFramerate != framerate ) {
563+ if (targetFps != framerate ) {
561564 if (60 < framerate ) {
562565 // Disables v-sync
563566 gl .setSwapInterval (0 );
@@ -566,15 +569,15 @@ protected void setFrameRate(float framerate) {
566569 } else {
567570 gl .setSwapInterval (2 );
568571 }
569- if ((60 < framerate && targetFramerate <= 60 ) ||
570- (framerate <= 60 && 60 < targetFramerate )) {
572+ if ((60 < framerate && targetFps <= 60 ) ||
573+ (framerate <= 60 && 60 < targetFps )) {
571574 // Enabling/disabling v-sync, we force a
572575 // surface reinitialization to avoid screen
573576 // no-paint issue observed on MacOSX.
574577 pg .initialized = false ;
575578 }
576- targetFramerate = framerate ;
577- setFramerate = true ;
579+ targetFps = currentFps = framerate ;
580+ setFps = true ;
578581 }
579582 }
580583
@@ -593,7 +596,7 @@ protected void initSurface(int antialias) {
593596 pg .parent .remove (canvasNEWT );
594597 }
595598 sinkFBO = backFBO = frontFBO = null ;
596- setFramerate = false ;
599+ setFps = false ;
597600 }
598601
599602 // Setting up the desired GL capabilities;
@@ -617,6 +620,9 @@ protected void initSurface(int antialias) {
617620
618621 if (toolkit == AWT ) {
619622 canvasAWT = new GLCanvas (caps );
623+
624+ //canvas = new GLCanvas(caps, context);
625+
620626 canvasAWT .setBounds (0 , 0 , pg .width , pg .height );
621627 canvasAWT .setBackground (new Color (pg .backgroundColor , true ));
622628 canvasAWT .setFocusable (true );
@@ -694,8 +700,8 @@ protected void deleteSurface() {
694700
695701
696702 protected void update () {
697- if (!setFramerate ) {
698- setFrameRate (targetFramerate );
703+ if (!setFps ) {
704+ setFrameRate (targetFps );
699705 }
700706 if (USE_JOGL_FBOLAYER ) return ;
701707
@@ -1138,7 +1144,6 @@ protected void beginDraw(boolean clear0) {
11381144 }
11391145 }
11401146
1141-
11421147 protected void endDraw (boolean clear0 ) {
11431148 if (USE_JOGL_FBOLAYER ) {
11441149 if (!clear0 && isFBOBacked () && !isMultisampled ()) {
@@ -1175,8 +1180,19 @@ protected void endDraw(boolean clear0) {
11751180 backTex = temp ;
11761181 }
11771182 }
1178- // flush();
1179- finish ();
1183+
1184+ // call (gl)finish() only if the rendering of each frame is taking too long,
1185+ // to make sure that commands are not accumulating in the GL command queue.
1186+ fcount += 1 ;
1187+ int m = pg .parent .millis ();
1188+ if (m - lastm > 1000 * fint ) {
1189+ currentFps = (float )(fcount ) / fint ;
1190+ fcount = 0 ;
1191+ lastm = m ;
1192+ }
1193+ if (currentFps < 0.5f * targetFps ) {
1194+ finish ();
1195+ }
11801196 }
11811197
11821198
0 commit comments