Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

extra.h

00001 // $Id: extra.h,v 1.1.1.1 2003/06/24 15:33:11 sjcomp Exp $
00002 // Copyright (c) 2003, Alexander Shyrokov aka SJ
00003 // E-mail: sjcomp[@]users.sourceforge.com
00004 // Web:    http://sjgui.sourceforge.net
00005 //
00006 // This code is under BSD license agreement.
00007 //
00008 // Alexander Shyrokov makes no representations about the suitability of this software 
00009 // for any purpose. It is provided "AS IS" without express or implied warranty.
00010 
00011 // This file contains extra controls, which are based on "common names" of the current
00012 // style.
00013 
00014 #if !defined(EXTRA_H_INCLUDED)
00015 #define EXTRA_H_INCLUDED
00016 
00017 #include <time.h> // clock()
00018 #include <stdio.h> // sprintf()
00019 #include "styles.h"
00020 
00021 
00022 namespace sjgui
00023 {
00024     using namespace SJGUI_USE_STYLE;
00025     namespace extra
00026     {
00034         class CFPSLabel : public CLabel
00035         {
00036             long        m_time; // time to the next calculation
00037             long        m_oldtime; // time of the previous calculation
00038             int         m_iFPS; // new fps
00039             // previous fps, so if it is the same, I should not
00040             // change text of the label.
00041             int         m_iOldFPS;
00042             // period of calculation (milliseconds)
00043             // should not be 0
00044             long        m_lPeriod;
00045             // standard printf/scanf format, to make a custom message
00046             // give %d at the place you want fps to be displayed
00047             std::string m_strTemplate;
00048         public:
00049             CFPSLabel():CLabel()
00050             {
00051                 m_iFPS=0;
00052                 m_iOldFPS=0;
00053                 m_strTemplate="FPS:%d";
00054                 m_time=clock();
00055                 m_lPeriod=1000;
00056                 CLabel::SetText("0");
00057             }
00064             void SetText(std::string str){m_strTemplate=str;};
00066             void OnDraw()
00067             {
00068                 m_iFPS++;
00069                 long new_time=clock();
00070                 // check for the second
00071                 if(m_time<new_time)
00072                 {
00073                     // m_oldtime and new_time should never be equal
00074                     int iFPS=(int)(m_iFPS/((double)(new_time-m_oldtime)/m_lPeriod));
00075                     if(m_iOldFPS!=iFPS)
00076                     {
00077                         char buf[50];
00078                         sprintf(buf,m_strTemplate.data(),iFPS);
00079                         CLabel::SetText(buf);
00080                         m_iOldFPS=iFPS;
00081                     }
00082                     // reset values
00083                     m_iFPS=0;
00084                     m_oldtime=new_time;
00085                     m_time=new_time+m_lPeriod;
00086                 }
00087                 CLabel::OnDraw(); // We still want label to be drawn
00088             }
00089         };
00100         class CFPSLabelDropIn : public CFPSLabel
00101         {
00102         public:
00104             virtual void OnReshape()
00105             {
00106                 // Place at the bottom of the parent window
00107                 if(GetParentWnd()!=NULL)
00108                     PosWnd(0,GetParentWnd()->GetHeight()-GetHeight(),
00109                            GetParentWnd()->GetWidth(), GetHeight());
00110                 else
00111                     PosWnd(0,GetScreenHeight()-GetHeight(),
00112                            GetScreenWidth(), GetHeight());
00113                 CFPSLabel::OnReshape();
00114             }
00115         };
00116 
00117 
00118     } // namespace extra
00119 } // namespace sjgui
00120 
00121 #endif // !defined(EXTRA_H_INCLUDED)
sjgui logo
Quick Links:

 News
 Description
 Screen Shots
 Projects
 Downloads
 Source Code
 Help/FAQ
 Want to help?
 Credits
 Disclaimer


Documentation:

 Documentation
 Reference
 Lessons


Useful links:

sjcomp logo
sjcomp

opengl logo

nehe logo

SourceForge.net Logo

Last modified:


Started by Alexander Shyrokov. Generated at Wed Apr 28 12:31:04 2004 for the sjgui by doxygen 1.3.1. SourceForge.net Logo