From 5537dd5eeefb9a56b63502e6604de336b2cb43f0 Mon Sep 17 00:00:00 2001 From: cxgeorge <> Date: Thu, 3 May 2001 06:23:27 +0000 Subject: [PATCH] add fps meter --- panda/src/wgldisplay/config_wgldisplay.cxx | 3 + panda/src/wgldisplay/config_wgldisplay.h | 2 + panda/src/wgldisplay/wglGraphicsWindow.cxx | 100 ++++++++++++++++++--- panda/src/wgldisplay/wglGraphicsWindow.h | 6 ++ 4 files changed, 98 insertions(+), 13 deletions(-) diff --git a/panda/src/wgldisplay/config_wgldisplay.cxx b/panda/src/wgldisplay/config_wgldisplay.cxx index ff875a3fd4..0dbce436c5 100644 --- a/panda/src/wgldisplay/config_wgldisplay.cxx +++ b/panda/src/wgldisplay/config_wgldisplay.cxx @@ -16,6 +16,9 @@ ConfigureFn(config_wgldisplay) { init_libwgldisplay(); } +bool gl_show_fps_meter = config_wgldisplay.GetBool("show-fps-meter", false); +float gl_fps_meter_update_interval = max(0.5,config_wgldisplay.GetFloat("fps-meter-update-interval", 1.7)); + //////////////////////////////////////////////////////////////////// // Function: init_libwgldisplay // Description: Initializes the library. This must be called at diff --git a/panda/src/wgldisplay/config_wgldisplay.h b/panda/src/wgldisplay/config_wgldisplay.h index 3b2eff0dae..35749a5589 100644 --- a/panda/src/wgldisplay/config_wgldisplay.h +++ b/panda/src/wgldisplay/config_wgldisplay.h @@ -13,6 +13,8 @@ NotifyCategoryDecl(wgldisplay, EXPCL_PANDAGL, EXPTP_PANDAGL); extern Filename get_icon_filename_(); +extern bool gl_show_fps_meter; +extern float gl_fps_meter_update_interval; extern EXPCL_PANDAGL void init_libwgldisplay(); diff --git a/panda/src/wgldisplay/wglGraphicsWindow.cxx b/panda/src/wgldisplay/wglGraphicsWindow.cxx index 5afef6e588..d08e0ae278 100644 --- a/panda/src/wgldisplay/wglGraphicsWindow.cxx +++ b/panda/src/wgldisplay/wglGraphicsWindow.cxx @@ -12,6 +12,7 @@ #include #include #include +#include #ifdef DO_PSTATS #include @@ -25,6 +26,8 @@ TypeHandle wglGraphicsWindow::_type_handle; #define MOUSE_ENTERED 0 #define MOUSE_EXITED 1 +#define FONT_BITMAP_OGLDISPLAYLISTNUM 1000 // some arbitrary # + //////////////////////////////////////////////////////////////////// // Function: Constructor // Access: @@ -52,7 +55,10 @@ wglGraphicsWindow(GraphicsPipe* pipe, const // Description: //////////////////////////////////////////////////////////////////// wglGraphicsWindow::~wglGraphicsWindow(void) { -// DestroyWindow(_mwindow); + + if(gl_show_fps_meter) + glDeleteLists(FONT_BITMAP_OGLDISPLAYLISTNUM, 128); + if(_visual!=NULL) free(_visual); @@ -768,19 +774,27 @@ void wglGraphicsWindow::config(void) { make_current(); make_gsg(); -//#ifdef _DEBUG -#if 1 - const GLubyte *vendorname=glGetString(GL_VENDOR); - if(vendorname!=NULL) { - if(strncmp((const char *)vendorname,"Microsoft",9)==0) { - wgldisplay_cat.info() << "wglGraphicsWindow:: GL VendorID: " << glGetString(GL_VENDOR) << " (Software Rendering)" << endl; - } else { - wgldisplay_cat.info() << "wglGraphicsWindow:: GL VendorID: " << glGetString(GL_VENDOR) << endl; - } - } else { - wgldisplay_cat.info() << "wglGraphicsWindow:: glGetString(GL_VENDOR) returns NULL!!!\n"; + if(gl_show_fps_meter) { + + // 128 handles all the ascii chars + // this creates a display list for each char. displist numbering starts + // at FONT_BITMAP_OGLDISPLAYLISTNUM. Might want to optimize just to save + // mem by just allocing bitmaps for chars we need (0-9 fps,SPC) + wglUseFontBitmaps(_hdc, 0, 128, FONT_BITMAP_OGLDISPLAYLISTNUM); + } + + if(wgldisplay_cat.is_debug()) { + const GLubyte *vendorname=glGetString(GL_VENDOR); + if(vendorname!=NULL) { + if(strncmp((const char *)vendorname,"Microsoft",9)==0) { + wgldisplay_cat.debug() << "wglGraphicsWindow:: GL VendorID: " << glGetString(GL_VENDOR) << " (Software Rendering)" << endl; + } else { + wgldisplay_cat.debug() << "wglGraphicsWindow:: GL VendorID: " << glGetString(GL_VENDOR) << endl; + } + } else { + wgldisplay_cat.info() << "wglGraphicsWindow:: glGetString(GL_VENDOR) returns NULL!!!\n"; + } } -#endif } //////////////////////////////////////////////////////////////////// @@ -850,6 +864,65 @@ void wglGraphicsWindow::setup_colormap(void) { // Description: Swaps the front and back buffers. //////////////////////////////////////////////////////////////////// void wglGraphicsWindow::end_frame(void) { + + if(gl_show_fps_meter) { + DWORD now = timeGetTime(); // this is win32 fn + + float time_delta = (now - _start_time)/1000.0; + + if(time_delta > gl_fps_meter_update_interval) { + // didnt use global clock object, it wasnt working properly when I tried, + // its probably slower due to cache faults, and I can easily track all the + // info I need in dxgsg + DWORD num_frames = _cur_frame_count - _start_frame_count; + + _current_fps = num_frames / time_delta; + _start_time = now; + _start_frame_count = _cur_frame_count; + } + + char fps_msg[20]; + sprintf(fps_msg, "%7.02f fps", _current_fps); + + // Note: we cant use simple GDI TextOut calls to draw FPS meter chars (like DX fps meter) + // because WGL doesnt support GDI in double-buffered mode. Instead we have to + // use glBitMap display lists created by wglUseFontBitmaps + + glColor3f(0.0,1.0,1.0); + + GLboolean tex_was_on = glIsEnabled(GL_TEXTURE_2D); + + if(tex_was_on) + glDisable(GL_TEXTURE_2D); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadMatrixf(LMatrix4f::ident_mat().get_data()); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadMatrixf(LMatrix4f::ident_mat().get_data()); + + glOrtho(_props._xorg,_props._xorg+_props._xsize, + _props._yorg,_props._yorg+_props._ysize,-1.0,1.0); + + glRasterPos2f(_props._xsize-75,_props._ysize-20); // these seem to be good for default font + + // set up for a string-drawing display list call + glListBase(FONT_BITMAP_OGLDISPLAYLISTNUM); + + // draw a string using font display lists. chars index their corresponding displist name + glCallLists(strlen(fps_msg), GL_UNSIGNED_BYTE, fps_msg); + + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + + if(tex_was_on) + glEnable(GL_TEXTURE_2D); + + _cur_frame_count++; // only used by fps meter right now + } + { #ifdef DO_PSTATS PStatTimer timer(_swap_pcollector); @@ -865,6 +938,7 @@ void wglGraphicsWindow::end_frame(void) { // Description: //////////////////////////////////////////////////////////////////// void wglGraphicsWindow::handle_reshape(int w, int h) { + // bugbug: this is not current working right?? if (_props._xsize != w || _props._ysize != h) { _props._xsize = w; _props._ysize = h; diff --git a/panda/src/wgldisplay/wglGraphicsWindow.h b/panda/src/wgldisplay/wglGraphicsWindow.h index e8197b702a..fc0a43cb6e 100644 --- a/panda/src/wgldisplay/wglGraphicsWindow.h +++ b/panda/src/wgldisplay/wglGraphicsWindow.h @@ -113,6 +113,12 @@ private: bool _ignore_key_repeat; int _full_height, _full_width; + // vars for frames/sec meter + DWORD _start_time; + DWORD _start_frame_count; + DWORD _cur_frame_count; + float _current_fps; + public: static TypeHandle get_class_type(void); static void init_type(void);