From 9ff8b1dc98681199abb9f7b1d7a2dccd229ce1ab Mon Sep 17 00:00:00 2001 From: Cary Sandvig Date: Sat, 5 May 2001 00:10:31 +0000 Subject: [PATCH] make framerate display work in *nix --- panda/src/audio/config_audio.cxx | 2 + panda/src/glxdisplay/config_glxdisplay.cxx | 3 + panda/src/glxdisplay/config_glxdisplay.h | 2 + panda/src/glxdisplay/glxGraphicsWindow.cxx | 67 ++++++++++++++++++++++ panda/src/glxdisplay/glxGraphicsWindow.h | 6 ++ panda/src/gui/guiChooser.h | 1 + panda/src/gui/guiListBox.h | 1 + 7 files changed, 82 insertions(+) diff --git a/panda/src/audio/config_audio.cxx b/panda/src/audio/config_audio.cxx index 2574f12056..990dc3a313 100644 --- a/panda/src/audio/config_audio.cxx +++ b/panda/src/audio/config_audio.cxx @@ -5,6 +5,7 @@ #include "config_audio.h" #include "audio_sound.h" +#include "audio_gui_functor.h" #include #include #include @@ -32,6 +33,7 @@ int audio_thread_priority; ConfigureFn(config_audio) { AudioSound::init_type(); + AudioGuiFunctor::init_type(); Config::ConfigTable::Symbol mode; config_audio.GetAll("audio-mode-flag", mode); diff --git a/panda/src/glxdisplay/config_glxdisplay.cxx b/panda/src/glxdisplay/config_glxdisplay.cxx index c9b8ad9b3f..13a3245875 100644 --- a/panda/src/glxdisplay/config_glxdisplay.cxx +++ b/panda/src/glxdisplay/config_glxdisplay.cxx @@ -22,3 +22,6 @@ ConfigureFn(config_glxdisplay) { glxGraphicsWindow::make_GlxGraphicsWindow); glxDisplay::init_type(); } + +bool gl_show_fps_meter = config_glxdisplay.GetBool("show-fps-meter", false); +float gl_fps_meter_update_interval = max((float)0.5,config_glxdisplay.GetFloat("fps-meter-update-interval", 1.7)); diff --git a/panda/src/glxdisplay/config_glxdisplay.h b/panda/src/glxdisplay/config_glxdisplay.h index 1614e2f9e6..6c0a9c3fdc 100644 --- a/panda/src/glxdisplay/config_glxdisplay.h +++ b/panda/src/glxdisplay/config_glxdisplay.h @@ -11,5 +11,7 @@ NotifyCategoryDecl(glxdisplay, EXPCL_PANDAGL, EXPTP_PANDAGL); +extern bool gl_show_fps_meter; +extern float gl_fps_meter_update_interval; #endif /* __CONFIG_GLXDISPLAY_H__ */ diff --git a/panda/src/glxdisplay/glxGraphicsWindow.cxx b/panda/src/glxdisplay/glxGraphicsWindow.cxx index 85af138d6f..afa6b00f9f 100644 --- a/panda/src/glxdisplay/glxGraphicsWindow.cxx +++ b/panda/src/glxdisplay/glxGraphicsWindow.cxx @@ -31,6 +31,8 @@ const char* glxGraphicsWindow::_glx_extensions = NULL; #define MOUSE_ENTERED 0 #define MOUSE_EXITED 1 +#define FONT_BITMAP_OGLDISPLAYLISTNUM 1000 // some arbitrary # + //////////////////////////////////////////////////////////////////// // Function: Constructor // Access: @@ -65,6 +67,9 @@ glxGraphicsWindow::~glxGraphicsWindow(void) // unmake_current(); // glXDestroyContext(_display, _context); + if (gl_show_fps_meter) + glDeleteLists(FONT_BITMAP_OGLDISPLAYLISTNUM, 128); + XDestroyWindow(_display, _xwindow); if (_colormap) XFreeColormap(_display, _colormap); @@ -472,6 +477,22 @@ void glxGraphicsWindow::config( void ) // to configure itself in the gsg make_current(); make_gsg(); + + 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 allocating bitmaps for chars we need (0-9,f,p,s,SPC) + XFontStruct* foo; + // I /know/ there has to be a way to get a useful handle to the default + // font, but I have to date been unable to find one + foo = XLoadQueryFont(_display, "-*-*-*-*-*--12-*-*-*-*-*-*-*"); + glXUseXFont(foo->fid, 0, 128, FONT_BITMAP_OGLDISPLAYLISTNUM); + _start_time = ClockObject::get_global_clock()->get_real_time(); + _start_frame_count = 0; + _cur_frame_count = 0; + _current_fps = 0.; + } } //////////////////////////////////////////////////////////////////// @@ -593,6 +614,52 @@ void glxGraphicsWindow::setup_properties(void) //////////////////////////////////////////////////////////////////// void glxGraphicsWindow::end_frame( void ) { + if (gl_show_fps_meter) { + ClockObject* co = ClockObject::get_global_clock(); + double now = co->get_real_time(); + double time_delta = now - _start_time; + if (time_delta > gl_fps_meter_update_interval) { + double 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); + glColor3f(0., 1., 1.); + 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); + + // these seem to be good for default font + glRasterPos2f(_props._xsize-75,_props._ysize-20); + + // 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 + } + { PStatTimer timer(_swap_pcollector); glXSwapBuffers(_display, _xwindow); diff --git a/panda/src/glxdisplay/glxGraphicsWindow.h b/panda/src/glxdisplay/glxGraphicsWindow.h index 2b1381ae56..f9d0c5f1b1 100644 --- a/panda/src/glxdisplay/glxGraphicsWindow.h +++ b/panda/src/glxdisplay/glxGraphicsWindow.h @@ -100,6 +100,12 @@ private: int _entry_state; bool _ignore_key_repeat; + // fps meter stuff + double _start_time; + long _start_frame_count; + long _cur_frame_count; + float _current_fps; + public: static TypeHandle get_class_type(void); static void init_type(void); diff --git a/panda/src/gui/guiChooser.h b/panda/src/gui/guiChooser.h index ac0176185a..b10356b372 100644 --- a/panda/src/gui/guiChooser.h +++ b/panda/src/gui/guiChooser.h @@ -104,6 +104,7 @@ public: GuiBehavior::init_type(); register_type(_type_handle, "GuiChooser", GuiBehavior::get_class_type()); + ChooseFunctor::init_type(); } virtual TypeHandle get_type(void) const { return get_class_type(); diff --git a/panda/src/gui/guiListBox.h b/panda/src/gui/guiListBox.h index 846080d04a..f55e56d970 100644 --- a/panda/src/gui/guiListBox.h +++ b/panda/src/gui/guiListBox.h @@ -99,6 +99,7 @@ public: GuiBehavior::init_type(); register_type(_type_handle, "GuiListBox", GuiBehavior::get_class_type()); + ListFunctor::init_type(); } virtual TypeHandle get_type(void) const { return get_class_type();