Advanced Strategic Command
autoprogressbar.cpp
Go to the documentation of this file.
1 
2 /***************************************************************************
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  * *
9  ***************************************************************************/
10 
11 #include "autoprogressbar.h"
12 
13 #include <pgapplication.h>
14 #include "../events.h"
15 
16 #include "../basestrm.h"
17 
18 void AutoProgressBar :: tick()
19 {
20 
21  newTickTimes.push_back ( ticker - starttime );
22 
23  // limit to 25 Hz to reduce graphic updates
24  if ( lastdisplaytime + 4 < ticker ) {
25  double p;
26  // double p = double(ticker - starttime) * 100 / time;
27  if ( counter < prevTickTimes.size() && time ) {
28  int a = prevTickTimes[counter];
29  p = 100 * a / time;
30  } else
31  p = counter / 100;
32 
33  if ( p > 99 )
34  p = 99;
35 
36  SetProgress( p );
37  lastdisplaytime = ticker;
38  }
39 
40  ++counter;
41 
42 
43  lastticktime = ticker;
44 };
45 
46 AutoProgressBar :: AutoProgressBar( sigc::signal<void>& tickSignal, PG_Widget *parent, const PG_Rect &r, const std::string &style ) : PG_ProgressBar( parent, r, style ), lastticktime(-1), counter(0)
47 {
48  lastdisplaytime = starttime = ticker;
49 
50  tickSignal.connect( sigc::mem_fun( *this, &AutoProgressBar::tick ));
51 
52  try {
53  tnfilestream stream ( "progress.dat", tnstream::reading );
54  stream.readInt(); // version
55  time = stream.readInt( );
56  readClassContainer( prevTickTimes, stream );
57  }
58  catch ( ... ) {
59  time = 200;
60  };
61 };
62 
64 {
65  try {
66  tnfilestream stream ( "progress.dat", tnstream::writing );
67  stream.writeInt( 1 );
68  stream.writeInt( lastticktime - starttime );
69  writeClassContainer( newTickTimes, stream );
70  }
71  catch ( ... ) {
72  }
73 }
74 
AutoProgressBar(sigc::signal< void > &tickSignal, PG_Widget *parent, const PG_Rect &r=PG_Rect::null, const std::string &style="Progressbar")
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
void readClassContainer(C &c, tnstream &stream)
Definition: basestrm.h:752
void writeClassContainer(const C &c, tnstream &stream)
Definition: basestrm.h:742
GameTime time
the time in the game, mesured in a turns and moves
Definition: gamemap.h:235
volatile int ticker
Definition: events.cpp:234