source: golgotha/src/i4/time/gui_prof.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: 5.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 "window/window.hh"
10#include "window/style.hh"
11#include "window/win_evt.hh"
12#include "device/event.hh"
13#include "time/timedev.hh"
14#include "time/profile.hh"
15#include "time/profile_stack.hh"
16
17//2345678*012345678*012345678*012345678*012345678*012345678*012345678*012345678*012345678*012345678*
18i4_profile_class pf_profile_draw("profile window refresh");
19
20// sort in descending order
21static int profile_compare(const void *a, const void *b)
22{
23  i4_profile_class *ap=*((i4_profile_class **)a);
24  i4_profile_class *bp=*((i4_profile_class **)b);
25
26  if (ap->total_time<bp->total_time)
27    return 1;
28  else if (ap->total_time>bp->total_time)
29    return -1;
30  else return 0;               
31}
32
33
34
35enum { CLOSE_WIN, POLL };
36class i4_prof_win_class : public i4_parent_window_class
37{
38  i4_bool update_stats;
39  i4_graphical_style_class *style;
40  i4_time_device_class::id poll_id;
41  i4_event_reaction_class *on_close;
42  w64 last_clock;
43
44public:
45  i4_prof_win_class(w16 w, w16 h, i4_graphical_style_class *style,
46                    i4_event_reaction_class *on_close)
47    : i4_parent_window_class(w,h),
48      style(style),
49      on_close(on_close)
50  {   
51    i4_object_message_event_class poll(this, POLL);
52    poll_id=i4_time_dev.request_event(this, &poll, 5000);  // update once every 5 secs
53
54    last_clock=i4_get_system_clock();
55
56     
57    // count how many profile classes are in memory
58    for (i4_profile_class *p=i4_profile_class::list; p; p=p->next)
59      p->total_time=0;
60
61    i4_profile_on=1;
62  }
63
64  void parent_draw(i4_draw_context_class &context)
65  {
66    int t=0,i=0;
67    double total_time_measured=0.0;
68    i4_profile_class *p=i4_profile_class::list;
69    // count how many profile classes are in memory
70    for (; p; p=p->next)
71    {
72      total_time_measured+=(double)(sw64)p->total_time;
73      t++;
74    }
75
76    pf_profile_draw.start();
77
78    local_image->clear(0, context);
79    if (update_stats)
80    {
81      // put them all in a list and sort them by clocks
82      i4_profile_class **plist;
83      plist=(i4_profile_class **)i4_malloc(sizeof(i4_profile_class *)*t,"pl");
84      for (p=i4_profile_class::list; p; p=p->next)
85      {       
86        plist[i]=p;
87        i++;
88      }
89      qsort(plist, t, sizeof(i4_profile_class *), profile_compare);
90
91     
92      char buf[200];
93
94      int y=0, h=height();
95      int x=50;
96
97      w64 current_clock = i4_get_system_clock();
98      double oo_total = 1.0/(double)((sw64)current_clock - (sw64)last_clock);
99      last_clock=i4_get_system_clock();   
100
101      i4_font_class *fnt=style->font_hint->small_font;
102      fnt->set_color(0x0000ff);   
103      sprintf(buf,"%2.2f Total", ((double)(sw64)total_time_measured * oo_total) * 100.0);
104      fnt->put_string(local_image, x+1, y, buf, context);
105      y+=fnt->height(buf);
106
107
108      fnt->set_color(0xffff00);
109
110
111
112      for (i=0; i<t; i++)
113      {
114        double percent = (sw64)plist[i]->total_time * oo_total;
115
116        sprintf(buf, "%2.2f %s", percent * 100.0, plist[i]->name);
117
118        int th=fnt->height(buf);
119        local_image->bar(x-(sw32)(percent * x), y, x, y+th-1, 0xffff, context);
120
121        plist[i]->total_time=0;
122        fnt->put_string(local_image, x+1, y, buf, context);
123       
124        y+=th;
125        if (y>h)
126          break;
127      }
128
129      i4_free(plist);
130      update_stats=i4_F;
131    }
132    else last_clock=i4_get_system_clock();   
133
134    pf_profile_draw.stop();
135  }
136
137  void receive_event(i4_event *ev);
138
139  ~i4_prof_win_class()
140  {
141    if (on_close)
142      delete on_close;
143    i4_time_dev.cancel_event(poll_id);
144
145    i4_profile_stack_top=0;
146    i4_profile_on=0;
147  }
148
149  char *name() { return "profile window"; }
150};
151
152i4_parent_window_class *i4_prof_win=0;
153
154void i4_prof_win_class::receive_event(i4_event *ev)
155{
156  CAST_PTR(oev, i4_object_message_event_class, ev);
157
158  if (ev->type()==i4_event::OBJECT_MESSAGE && oev->object==this)
159  {
160    if (oev->sub_type==CLOSE_WIN)     // close window
161    {
162      i4_prof_win=0;
163     
164      i4_kernel.send(on_close);
165    }
166    else if (oev->sub_type==1)  // update statics
167    {
168      update_stats=i4_T;
169      request_redraw();
170     
171      i4_object_message_event_class poll(this, POLL);
172      poll_id=i4_time_dev.request_event(this, &poll, 5000);  // update once every 5 secs
173    }
174  }
175  else i4_parent_window_class::receive_event(ev);
176}
177
178void i4_profile_watch(i4_graphical_style_class *style,
179                      i4_parent_window_class *parent,                     
180                      sw32 &win_x, sw32 &win_y,
181                      w32 w, w32 h,
182                      int open_type,  // 0==close, 1==open, 2==toggle window
183                      i4_event_reaction_class *on_close)
184{
185  if (i4_prof_win && (open_type==0 || open_type==2))
186  {
187    win_x=i4_prof_win->get_parent()->x();
188    win_y=i4_prof_win->get_parent()->y();
189
190    style->close_mp_window(i4_prof_win);
191    i4_prof_win=0;
192  }
193  else if (!i4_prof_win && (open_type==1 || open_type==2))
194  {
195    i4_prof_win_class *p=new i4_prof_win_class(w,h, style, on_close);
196
197    i4_event_reaction_class *re;
198    re=new i4_event_reaction_class(p, new i4_object_message_event_class(p,CLOSE_WIN,i4_event::NOW));
199
200    i4_prof_win=style->create_mp_window(win_x, win_y, w,h,
201                                        i4gets("prof_win_title",i4_F), re);
202    i4_prof_win->add_child(0,0,p);
203  }
204
205}
Note: See TracBrowser for help on using the repository browser.