home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include "timer.h"
- #include "stdio.h"
- #include <gl.h>
- #include <gl/get.h>
-
-
- timer::timer()
- {
- start();
- }
-
- void
- timer::start()
- {
- running = 1;
- gettimeofday(&beginTime, 0);
- }
-
- void
- timer::stop()
- {
- running = 0;
- gettimeofday(&endTime, 0);
- }
-
- float
- timer::elapse()
- {
- if (running)
- gettimeofday(&endTime, 0);
-
- float e = endTime.tv_sec - beginTime.tv_sec +
- (endTime.tv_usec - beginTime.tv_usec) / 1000000.0;
-
- // if (e <= 0.0)
- // return 0.000001;
- // else
- return e;
- }
-
- float
- timer::elapseStart()
- {
- float e = elapse();
- beginTime.tv_sec = endTime.tv_sec;
- beginTime.tv_usec = endTime.tv_usec;
-
- return e;
- }
-
-
- totalTimer::totalTimer(char * d)
- {
- description = d;
- totalTime = 0;
- count = 0;
- }
-
- void
- totalTimer::stop()
- {
- timer::stop();
- totalTime += elapse();
- count++;
- }
-
- float
- totalTimer::getTotal()
- {
- return totalTime;
- }
-
- int
- totalTimer::getCount()
- {
- return count;
- }
-
- void
- perfTimer::print()
- {
- fprintf(stderr, "%30s: total %6.2f in %0d samples.\n",
- description, getTotal(), getCount());
- }
-
- perfTimer::~perfTimer()
- {
- print();
- }
-
- /*---------------------------------------------------------------------------
- Frame Rate Stuff
- ---------------------------------------------------------------------------*/
-
- char * defaultRateDescription = "%2.0f Frames/Second";
- char * rateDescription;
- float rateMultiplier = 1;
- int rateActive = 1;
- float rateConvergence = 1.0;
- int useCmapMode = 0;
-
- float theFrameRate;
-
- timer * frameRateTimer = 0;
-
- extern "C" void
- showFrameRate()
- {
- if (frameRateTimer == 0) {
- frameRateTimer = new timer;
- rateDescription = defaultRateDescription;
-
- long mode = getdisplaymode();
- if ((mode == DMSINGLE) || (mode == DMDOUBLE))
- useCmapMode = TRUE;
- }
- else
- if (rateActive) {
-
- Boolean oldZbuf = getzbuffer();
- zbuffer(FALSE);
-
- long oldMode = getmmode();
- Matrix oldProjection;
- if (oldMode == MSINGLE)
- pushmatrix();
- else
- {
- static Matrix Identity = {
- { 1, 0, 0, 0 },
- { 0, 1, 0, 0 },
- { 0, 0, 1, 0 },
- { 0, 0, 0, 1 }
- };
-
- mmode(MVIEWING);
- pushmatrix();
- loadmatrix(Identity);
-
- mmode(MPROJECTION);
- getmatrix(oldProjection);
- }
-
- long winX, winY;
- getsize(&winX, &winY);
- ortho2(0, (int)winX -1, 0, (int)winY -1);
-
- // pushviewport();
- // viewport(0, (short)winX -1, 0, (short)winY -1);
-
- float t = frameRateTimer->elapseStart();
- float rate = 1 / t;
- float newPortion = t / rateConvergence;
- if (newPortion > 1)
- newPortion = 1;
- theFrameRate = rate * newPortion + theFrameRate * (1 - newPortion);
-
- char buffer[256];
- sprintf(buffer, rateDescription, theFrameRate * rateMultiplier);
-
- cmov(10, 10, 0);
- if (useCmapMode)
- color(4);
- else
- cpack(0x00ff0000);
- charstr(buffer);
- cmov(12, 12, 0);
- if (useCmapMode)
- color(7);
- else
- cpack(0x00ffffff);
- charstr(buffer);
-
- if (oldMode == MSINGLE)
- popmatrix();
- else
- {
- loadmatrix(oldProjection);
-
- mmode(MVIEWING);
- popmatrix();
-
- mmode((short)oldMode);
- }
-
- zbuffer(oldZbuf);
- // popviewport();
- }
- }
-
-
- extern "C" void
- setFrameRateMultiplier(float m)
- {
- rateMultiplier = m;
- }
-
-
- extern "C" void
- setFrameRateDescription(char * d)
- {
- rateDescription = d;
- }
-
-
- extern "C" void
- setFrameRateActive(int a)
- {
- rateActive = a;
- }
-
-
- extern "C" void
- setFrameRateConvergence(float c)
- {
- rateConvergence = c;
- }
-
-
- extern "C" void
- setFrameRateCmapMode(int t)
- {
- useCmapMode = t;
- }
-