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.2
Committed: Mon Oct 18 14:11:50 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
Changes since 1.1: +22 -20 lines
Log Message:
check and log but do not die when ncurses stuff fails

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