1 //
2 //  Copyright (C) 2010  Nick Gasson
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #include "IRenderStats.hpp"
19 #include "GameScreens.hpp"
20 #include "IMesh.hpp"
21 
22 #include <boost/lexical_cast.hpp>
23 
24 class RenderStats : public IRenderStats {
25 public:
26    RenderStats(gui::ILayoutPtr layout, const string& path);
27 
28    // IRenderStats interface
29    void update(int delta);
30 
31 private:
32    int ticks_until_update;
33    gui::ILayoutPtr layout;   // Hold to ensure validity of label reference
34    gui::Label& label;
35 };
36 
RenderStats(gui::ILayoutPtr layout,const string & path)37 RenderStats::RenderStats(gui::ILayoutPtr layout, const string& path)
38    : ticks_until_update(0),
39      layout(layout),
40      label(layout->cast<gui::Label>(path))
41 {
42 
43 }
44 
update(int delta)45 void RenderStats::update(int delta)
46 {
47    ticks_until_update -= delta;
48 
49    if (ticks_until_update <= 0) {
50       int avg_triangles = get_average_triangle_count();
51 
52       label.text(
53          "FPS: " + boost::lexical_cast<string>(get_game_window()->get_fps())
54          + " [" + boost::lexical_cast<string>(avg_triangles) + " triangles]");
55 
56       ticks_until_update = 1000;
57    }
58 }
59 
make_render_stats(gui::ILayoutPtr layout,const string & path)60 IRenderStatsPtr make_render_stats(gui::ILayoutPtr layout, const string& path)
61 {
62    return IRenderStatsPtr(new RenderStats(layout, path));
63 }
64 
65