/*
 *      @(#)TimingCanvas3D.java 1.3 98/08/05 14:37:17
 *
 * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 *
 * @(#)TimingCanvas3D.java	1.3 98/08/05
 * @Author: Doug Gehringer
 */


import java.awt.AWTEvent;
import java.awt.event.*;
import java.awt.*;
import sun.awt.*;

public class TimingCanvas3D extends javax.media.j3d.Canvas3D {
    static final int MAX_TIMES = 15;
    static final boolean debug = false;
	
    int	frameNumber = 0;
    long[] startTimes = new long[MAX_TIMES];
    long[] durations = new long[MAX_TIMES];
    Vrml97Viewer viewer;


    public TimingCanvas3D(GraphicsConfiguration gc, Vrml97Viewer viewer) {
	super(gc);
	this.viewer = viewer;
    }

    public void preRender() {
        frameNumber++;
	startTimes[frameNumber % MAX_TIMES] = System.currentTimeMillis();
	// Note: startTime[0] won't be used until frameNumber == MAX_TIMES
    }

    public void postRender() {
	int index = frameNumber % MAX_TIMES;
	durations[index] = System.currentTimeMillis() - startTimes[index];
	if (debug) {
	     long[] user = new long[10];
	     getFrameStartTimes(user);
	     System.out.println("frameNumber = " + frameNumber);
	     for (int i = 0; i < user.length; i++) {
		System.out.println("startTime[" + i + "] = " + user[i]);
	     }
	}
	viewer.postRender();
    }

    public void getFrameStartTimes(long[] user) {
	int count = 0;
	if (frameNumber > 10) {
	    count = MAX_TIMES;
	} else {
	    count = frameNumber;
	}
	if (count > user.length) {
	    count = user.length;
	}
	for (int i = 0; i < count; i++) {
	    user[i] = startTimes[(frameNumber - i) % MAX_TIMES];
	}
	if (user.length > count) {
	    for (int i = count; i < user.length; i++) {
		user[i] = 0;
	    }
	}
    }
    public void getFrameDurations(long[] user) {
	int count = 0;
	if (frameNumber > 10) {
	    count = MAX_TIMES;
	} else {
	    count = frameNumber;
	}
	if (count > user.length) {
	    count = user.length;
	}
	for (int i = 0; i < count; i++) {
	    user[i] = durations[(frameNumber - i) % MAX_TIMES];
	}
	if (user.length > count) {
	    for (int i = count; i < user.length; i++) {
		user[i] = 0;
	    }
	}
    }
    public long getLastFrameDuration() {
	return durations[frameNumber % MAX_TIMES];
    }
    public long getFrameNumber() {
	return frameNumber;
    }
}
