source: golgotha/src/golg/statistics.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 3.8 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "statistics.hh"
10#include "window/window.hh"
11#include "window/style.hh"
12#include "font/font.hh"
13#include "error/error.hh"
14#include "time/timedev.hh"
15#include "app/app.hh"
16
17static i4_event_handler_reference_class<i4_parent_window_class> g1_stat_win;
18
19class g1_stat_win_class : public i4_window_class
20{
21  int t_strings; 
22  i4_graphical_style_class *style;
23  i4_time_device_class::id poll_id;
24
25public:
26  int x2() { return 50; }
27  g1_stat_win_class(i4_graphical_style_class *style)
28    : i4_window_class(0,0), style(style)
29     
30  { 
31    int min_w=0, min_h=0;
32    t_strings=0;
33    i4_font_class *fnt=style->font_hint->small_font;
34
35    min_w=fnt->width("Object polygons");
36    min_h=fnt->largest_height() * g1_statistics_counter_class::LAST;
37
38    min_h+=fnt->largest_height();   // save room for fps (first line)
39    min_w+=x2();                 // add room for numbers
40
41    resize(min_w, min_h);
42
43    i4_user_message_event_class poll(0);
44    poll_id=i4_time_dev.request_event(this, &poll, 2000);  // update once every 2 secs
45  }
46
47  void draw_float(int x, int y, float v, i4_font_class *fnt, i4_draw_context_class &context)
48  {
49    char buf[30];
50    int ipart, fpart;
51    ipart=(int)v;
52    fpart=(int)(v * 10) - ipart*10;
53   
54    sprintf(buf, "%d.%d", ipart, fpart);
55
56    for (char *c=buf; *c; c++)
57    {
58      i4_char ch(*c);
59      fnt->put_character(local_image, x,y, ch, context);
60      x+=fnt->width(ch);
61    }   
62  }
63
64  void draw(i4_draw_context_class &context)
65  {
66    local_image->clear(0, context);
67
68    if (g1_stat_counter.t_frames)
69    {     
70      i4_font_class *fnt=style->font_hint->small_font;
71      fnt->set_color(0xffff00);
72
73      float oo_tframes = 1.0/(float)g1_stat_counter.t_frames;
74     
75      i4_time_class now;
76      draw_float(0,0, (float)g1_stat_counter.t_frames /
77                 (now.milli_diff(g1_stat_counter.last_update_time)/1000.0),
78                 fnt, context);
79
80      fnt->put_string(local_image, x2(), 0, i4gets("fps"), context);
81      int y=fnt->largest_height();
82
83      char *strs[]=
84      {"Polys",
85       "Object polys",
86       "Terrain polys",
87       "Objects",
88       "Terrain",
89       "Sprites",
90       "Sfx mixed",
91       "Demo Frames"};
92
93      for (int i=0; i<g1_statistics_counter_class::LAST; i++)
94      {
95        draw_float(0, y, g1_stat_counter.counter_array[i] * oo_tframes, fnt, context);
96        fnt->put_string(local_image, x2(), y, strs[i], context);
97        y+=fnt->height(strs[i]);
98      }
99    }
100
101    g1_stat_counter.reset();
102
103  }
104
105  void receive_event(i4_event *ev)
106  {
107    if (ev->type()==i4_event::USER_MESSAGE)  // update statics
108    {
109      request_redraw();     
110      i4_user_message_event_class poll(0);
111      poll_id=i4_time_dev.request_event(this, &poll, 2000);  // update once every 2 secs
112    }
113  }
114
115  ~g1_stat_win_class()
116  {
117    i4_time_dev.cancel_event(poll_id);
118  }
119
120  char *name() { return "stat_win"; }
121};
122
123void g1_statistics_counter_class::show()
124{
125  if (!g1_stat_win.get())
126  {
127    i4_graphical_style_class *style=i4_current_app->get_style();
128    i4_window_class *swin = new g1_stat_win_class(style);
129
130    g1_stat_win = style->create_mp_window(-1, -1, swin->width(), swin->height(),
131                                          i4gets("stat_win_title"));
132
133    g1_stat_win->add_child(0, 0, swin);
134  } 
135}
Note: See TracBrowser for help on using the repository browser.