00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 #include <stdio.h>
00010 #include "FPS.h"
00011 #include "DisplayDevice.h"
00012 #include "Displayable.h"
00013 #include "DispCmds.h"
00014 #include "Scene.h"
00015 
00016 FPS::FPS(DisplayDevice *d, Displayable *par) 
00017 : Displayable(par), disp(d) {
00018   last_update = time_of_day(); 
00019   loop_count = 0;
00020 
00021   
00022   rot_off();
00023   glob_trans_off();
00024   cent_trans_off();
00025   scale_off();
00026 
00027   
00028   colorCat = scene->add_color_category("Display");
00029   if (colorCat < 0)
00030     colorCat = scene->category_index("Display");
00031 
00032   usecolor = scene->add_color_item(colorCat, "FPS", REGWHITE);
00033   do_color_changed(colorCat);
00034 }
00035 
00036 void FPS::prepare() {
00037   if (!displayed()) return;
00038   ++loop_count;
00039   double curtime = time_of_day();
00040   
00041   need_matrix_recalc();
00042   
00043   if (curtime - last_update < 0.5) return;
00044   double rate = loop_count / (curtime - last_update);
00045   last_update = curtime;
00046   loop_count = 0;
00047 
00048   reset_disp_list();
00049   append(DMATERIALOFF);
00050 
00051   float asp = disp->aspect();
00052   float poscale = 1.2f;
00053   float pos[3];
00054   pos[0] = asp * poscale;
00055   pos[1] = poscale;
00056   pos[2] = 0;
00057   
00058   DispCmdColorIndex cmdColor;
00059   cmdColor.putdata(usecolor, cmdList);
00060 
00061   DispCmdText cmdText;
00062   char buf[20];
00063   sprintf(buf, "%5.2f", rate);
00064   cmdText.putdata(pos, buf, 1.0f, 1.0f, 0, 0, cmdList);
00065 }
00066 
00067 void FPS::do_color_changed(int clr) {
00068   if (clr == colorCat) {
00069     usecolor = scene->category_item_value(colorCat, "FPS");
00070   }
00071 }
00072