MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StatsRenderer.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  StatsRenderer.h SDL_Surface MFM statistics renderer
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef STATSRENDERER_H
28 #define STATSRENDERER_H
29 
30 #include "AbstractButton.h"
31 #include "AssetManager.h"
32 #include "SDL_ttf.h"
33 #include "Grid.h"
34 #include "Utils.h"
35 #include "itype.h"
36 #include "Point.h"
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <pwd.h>
40 
41 namespace MFM {
42 
43  template <class GC>
45  {
46  // Extract short type names
47  typedef typename GC::CORE_CONFIG CC;
48 
49  public:
51  {
52  public:
53  virtual const char * GetLabel() const = 0;
54 
61  virtual void GetValue(ByteSink & bs, bool endOfEpoch) const = 0;
62 
63  virtual ~DataReporter()
64  { }
65  };
66 
68  {
69  public:
70  virtual const char * GetLabel() const = 0;
71 
72  virtual s32 GetDecimalPlaces() const = 0;
73 
74  virtual double GetValue(bool endOfEpoch) const = 0;
75 
76  virtual void GetValue(ByteSink & bs, bool endOfEpoch) const
77  {
78  const u32 FMT_BUFFER_SIZE = 32;
79  char fmtBuffer[FMT_BUFFER_SIZE];
80 
81  const u32 STR_BUFFER_SIZE = 128;
82  char strBuffer[STR_BUFFER_SIZE];
83 
84  s32 places = GetDecimalPlaces();
85  if (places >= 0)
86  {
87  snprintf(fmtBuffer, FMT_BUFFER_SIZE, "%%8.%df",places);
88  snprintf(strBuffer, STR_BUFFER_SIZE, fmtBuffer, GetValue(endOfEpoch));
89  }
90  bs.Print(strBuffer);
91  }
92 
93  virtual ~CapturableStatistic()
94  { }
95  };
96 
97  class ElementCount : public DataReporter
98  {
99  private:
100  const Element<CC> * m_element;
101  const Grid<GC> * m_grid;
102 
103  public:
104  ElementCount() :
105  m_element(0),
106  m_grid(0)
107  { }
108 
109  void Set(const Grid<GC> * grid, const Element<CC> * elt)
110  {
111  m_element = elt;
112  m_grid = grid;
113  }
114 
115  virtual const char * GetLabel() const
116  {
117  if (m_element)
118  {
119  return m_element->GetUUID().GetLabel();
120  }
121  return "<unset>";
122  }
123 
124  virtual void GetValue(ByteSink & bs, bool endOfEpoch) const
125  {
126  if (!m_element || !m_grid)
127  {
128  return;
129  }
130  u32 type = m_element->GetType();
131  u32 allSites = m_grid->CountActiveSites();
132  u32 count = m_grid->GetAtomCount(type);
133  double pct = 100.0 * count / allSites;
134 
135  if (pct == 0 || pct >= 1)
136  {
137  bs.Printf("%d %2d", count, (int) pct);
138  }
139  else
140  {
141  bs.Printf("%d .%d", count, (int) (10 * pct));
142  }
143  }
144  };
145 
147  {
148  private:
149  Grid<GC> * m_grid;
150  const char * m_label;
151  u32 m_elementType;
152  u32 m_slot;
153  u32 m_outOfSlots;
154  bool m_resetOnRead;
155 
156  public:
158  m_grid(0),
159  m_label(0),
160  m_elementType(0),
161  m_slot(0),
162  m_outOfSlots(0),
163  m_resetOnRead(false)
164  { }
165 
166  void Set(Grid<GC> & grid, const char * label, u32 elementType,
167  u32 slot, u32 outOfSlots, bool resetOnRead)
168  {
169  m_grid = &grid;
170  m_label = label;
171  m_elementType = elementType;
172  m_slot = slot;
173  m_outOfSlots = outOfSlots;
174  m_resetOnRead = resetOnRead;
175  }
176 
177  virtual const char * GetLabel() const
178  {
179  if (m_label)
180  {
181  return m_label;
182  }
183  return "<unset>";
184  }
185 
186  virtual s32 GetDecimalPlaces() const
187  {
188  return 0;
189  }
190  virtual double GetValue(bool endOfEpoch) const
191  {
192  if (!m_grid)
193  {
194  return -1;
195  }
196  if (m_slot >= m_outOfSlots)
197  {
198  return -1;
199  }
200 
201  u64 sum = 0;
202  for (typename Grid<GC>::iterator_type i = m_grid->begin(); i != m_grid->end(); ++i)
203  {
204  Tile<CC> * t = *i;
205  ElementTable<CC> & et = t->GetElementTable();
206  u64 * eds = et.GetElementDataSlotsFromType(m_elementType,m_outOfSlots);
207  if (!eds)
208  {
209  continue;
210  }
211  sum += eds[m_slot];
212  if (endOfEpoch && m_resetOnRead)
213  {
214  eds[m_slot] = 0;
215  }
216  }
217 
218  return (double) sum;
219  }
220  };
221 
222  private:
223  UPoint m_dimensions;
224  SPoint m_drawPoint;
225 
226  static const u32 MAX_TYPES = 32;
227  const DataReporter *(m_reporters[MAX_TYPES]);
228  u32 m_reportersInUse;
229 
230  ElementCount m_displayElements[MAX_TYPES];
231  u32 m_displayElementsInUse;
232 
233  u32 m_displayAER;
234  u32 m_maxDisplayAER;
235 
236  static const u32 MAX_BUTTONS = 16;
237  AbstractButton* m_buttons[MAX_BUTTONS];
238  u32 m_registeredButtons;
239 
240  public:
241  StatsRenderer() :
242  m_reportersInUse(0),
243  m_displayElementsInUse(0),
244  m_displayAER(0),
245  m_maxDisplayAER(5),
246  m_registeredButtons(0)
247  { }
248 
249  void ClearButtons()
250  {
251  m_registeredButtons = 0;
252  }
253 
254  enum {
255  BUTTON_HEIGHT_PIXELS = 20,
256  LINE_HEIGHT_PIXELS = 32,
257  DETAIL_LINE_HEIGHT_PIXELS = 32
258  };
259  void ReassignButtonLocations()
260  {
261  SPoint loc(4, m_reportersInUse * BUTTON_HEIGHT_PIXELS);
262  loc.SetY(loc.GetY() + m_displayAER*LINE_HEIGHT_PIXELS);
263  for(u32 i = 0; i < m_registeredButtons; i++)
264  {
265  m_buttons[i]->SetLocation(loc);
266  loc.SetY(loc.GetY() + BUTTON_HEIGHT_PIXELS);
267  }
268  }
269 
270  void AddButton(AbstractButton* b)
271  {
272  if (m_registeredButtons >= MAX_BUTTONS)
273  FAIL(OUT_OF_ROOM);
274 
275  SPoint dimensions(268, BUTTON_HEIGHT_PIXELS - 2);
276  SPoint location(4, (m_reportersInUse + m_registeredButtons) * BUTTON_HEIGHT_PIXELS);
277  b->SetDimensions(dimensions);
278  b->SetLocation(location);
279 
280  m_buttons[m_registeredButtons++] = b;
281  }
282 
283  void OnceOnly()
284  { }
285 
286  u32 GetDisplayAER() const
287  {
288  return m_displayAER;
289  }
290 
291  u32 GetMaxDisplayAER() const
292  {
293  return m_maxDisplayAER;
294  }
295 
296  void SetDisplayAER(u32 displayAER)
297  {
298  m_displayAER = displayAER % (m_maxDisplayAER + 1);
299  ReassignButtonLocations();
300  }
301 
302  bool DisplayDataReporter(const DataReporter * cs)
303  {
304  if (m_reportersInUse >= MAX_TYPES)
305  {
306  return false;
307  }
308  u32 index = m_reportersInUse++;
309  m_reporters[index] = cs;
310  return true;
311  }
312 
313  bool DisplayStatsForElement(const Grid<GC> & grd, const Element<CC> & elt)
314  {
315  if (m_displayElementsInUse >= MAX_TYPES)
316  {
317  return false;
318  }
319  u32 index = m_displayElementsInUse++;
320  m_displayElements[index].Set(&grd,&elt);
321  return DisplayDataReporter(&m_displayElements[index]);
322  }
323 
324  void SetDrawPoint(Point<s32> drawPoint)
325  {
326  m_drawPoint = drawPoint;
327  }
328 
329  void SetDimensions(Point<u32> dimensions)
330  {
331  m_dimensions = dimensions;
332  }
333 
334  void RenderGridStatistics(Drawing & drawing, Grid<GC>& grid, double aeps, double aer, u32 AEPSperFrame, double overhead, bool endOfEpoch);
335 
336  void WriteRegisteredCounts(ByteSink & fp, bool writeHeader, Grid<GC>& grid, double aeps, double aer, u32 AEPSperFrame, double overhead, bool doResets);
337  };
338 } /* namespace MFM */
339 #include "StatsRenderer.tcc"
340 
341 #endif /*STATSRENDERER_H*/
Definition: StatsRenderer.h:67
void SetLocation(const SPoint &location)
Definition: AbstractButton.h:151
Definition: Grid.h:225
virtual void GetValue(ByteSink &bs, bool endOfEpoch) const
Definition: StatsRenderer.h:124
Definition: AbstractButton.h:44
Definition: StatsRenderer.h:50
virtual void GetValue(ByteSink &bs, bool endOfEpoch) const
Definition: StatsRenderer.h:76
Definition: Grid.h:47
Definition: StatsRenderer.h:146
Definition: ByteSink.h:47
void Print(const char *str, s32 fieldWidth=-1, u8 padChar= ' ')
Definition: ByteSink.cpp:31
Definition: StatsRenderer.h:44
Definition: EventWindow.h:41
Definition: ElementTable.h:46
ElementTable< CC > & GetElementTable()
Definition: Tile.h:599
virtual void GetValue(ByteSink &bs, bool endOfEpoch) const =0
Definition: StatsRenderer.h:97
Definition: Atom.h:43