ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/frontend/client/widgets/stats_widget.cpp
Revision: 1.1
Committed: Sun Oct 17 08:15:13 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
Log Message:
initial import

File Contents

# User Rev Content
1 sf-pippijn 1.1 #include "dclient/widgets/stats.h"
2    
3     #include "deliantra/data/counters.h"
4     #include "deliantra/data/map.h"
5     #include "deliantra/data/statistics.h"
6    
7     #include <ndk++/pen.hh>
8    
9     stats_widget::stats_widget (ndk::panel *parent)
10     : frame ("Statistics", parent)
11     , stats (nullptr)
12     {
13     }
14    
15     void
16     stats_widget::update (data::statistics const &stats)
17     {
18     this->stats = &stats;
19     }
20    
21     #include <sys/time.h>
22     #include <sys/resource.h>
23    
24     static void
25     render_rusage (ndk::window &widget, int row = 8)
26     {
27     static timeval start;
28    
29     ndk::pen pen (widget);
30     pen.move (2, row);
31    
32     if (!start.tv_sec)
33     gettimeofday (&start, NULL);
34    
35     timeval now, diff;
36     gettimeofday (&now, NULL);
37     timersub (&now, &start, &diff);
38    
39     struct rusage usage;
40     getrusage (RUSAGE_SELF, &usage);
41    
42     pen.write ( "[CPU: %ld.%02lds"
43     , usage.ru_utime.tv_sec + usage.ru_stime.tv_sec
44     , (usage.ru_utime.tv_usec + usage.ru_stime.tv_usec)
45     / 10000
46     );
47     pen.write ( " Cells: %d (%dKiB)"
48     , data::cell_count
49     , data::cell_count * sizeof (data::map::cell) / 1024
50     );
51     pen.write ( " RES: %ldKiB"
52     , usage.ru_maxrss
53     );
54     pen.write ( " RT: %ld:%02ld]"
55     , diff.tv_sec / 60
56     , diff.tv_sec % 60
57     );
58     }
59    
60     void
61     stats_widget::draw ()
62     {
63     frame::draw ();
64    
65     if (!stats)
66     return;
67    
68     ndk::pen pen (*this);
69    
70     int const margin = 2;
71    
72     pen.move (margin - 1, 1); pen.write (stats->s_range);
73     pen.move (margin, 2); pen.write (stats->s_title);
74     pen.move (margin, 3);
75     if (stats->s_exp64)
76     pen.write ("Exp: %'ld", stats->s_exp64);
77     else if (stats->s_exp)
78     pen.write ("Exp: %d", stats->s_exp);
79     else
80     pen.write ("Exp: 0");
81    
82     int row = 4;
83     {
84     int const shift = 12;
85     int col = 0;
86    
87     pen.move (margin + shift * col++, row); pen.write ("HP: %d/%d", stats->s_hp, stats->s_maxhp);
88     pen.move (margin + shift * col++, row); pen.write ("SP: %d/%d", stats->s_sp, stats->s_maxsp);
89     pen.move (margin + shift * col++, row); pen.write ("GP: %d/%d", stats->s_grace, stats->s_maxgrace);
90     pen.move (margin + shift * col++, row); pen.write ("Food: %d/%d", stats->s_food, 999);
91     }
92     row++;
93     {
94     int const shift = 12;
95     int col = 0;
96    
97     pen.move (margin + shift * col++, row); pen.write ("WC: %d", stats->s_wc);
98     pen.move (margin + shift * col++, row); pen.write ("AC: %d", stats->s_ac);
99     pen.move (margin + shift * col++, row); pen.write ("Dam: %d", stats->s_dam);
100     pen.move (margin + shift * col++, row); pen.write ("Speed: %d", stats->s_speed);
101     pen.move (margin + shift * col++, row); pen.write ("Weapon speed: %d", stats->s_weap_sp);
102     }
103     row++;
104     {
105     int const shift = 8;
106     int col = 0;
107    
108     pen.move (margin + shift * col++, row); pen.write ("Str: %d", stats->s_str);
109     pen.move (margin + shift * col++, row); pen.write ("Dex: %d", stats->s_dex);
110     pen.move (margin + shift * col++, row); pen.write ("Con: %d", stats->s_con);
111     pen.move (margin + shift * col++, row); pen.write ("Int: %d", stats->s_int);
112     pen.move (margin + shift * col++, row); pen.write ("Wis: %d", stats->s_wis);
113     pen.move (margin + shift * col++, row); pen.write ("Pow: %d", stats->s_pow);
114     pen.move (margin + shift * col++, row); pen.write ("Cha: %d", stats->s_cha);
115     }
116    
117     render_rusage (*this);
118     }