Skyscraper 2.0
timer.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Timer Object
3 The Skyscraper Project - Version 2.0
4 Copyright (C)2004-2024 Ryan Thoryk
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22*/
23
24#include "globals.h"
25#include "sbs.h"
26#include "timer.h"
27
28namespace SBS {
29
30TimerObject::TimerObject(Object *parent, const std::string &name) : Object(parent)
31{
32 //set up SBS object
33 SetValues("Timer", name, true, false);
34
35 Interval = 0;
36 OneShot = false;
37 LastHit = 0;
38 Running = false;
39 CurrentTime = 0;
40 StartTime = 0;
41}
42
44{
45 if (Running == true)
46 Stop();
47}
48
49void TimerObject::Start(int milliseconds, bool oneshot)
50{
51 Interval = milliseconds;
52 OneShot = oneshot;
53 Running = true;
55 LastHit = 0;
57
58 if (sbs->Verbose)
59 Report("Start");
60}
61
63{
64 if (Running == false)
65 return;
66
67 Running = false;
69
70 if (sbs->Verbose)
71 Report("Stop");
72}
73
75{
76 return Running;
77}
78
80{
81 if (Running == false)
82 return;
83
85
86 if (CurrentTime - LastHit >= (unsigned long)Interval)
87 {
88 if (sbs->Verbose)
89 Report("Notify");
90
91 Notify();
93
94 if (OneShot == true)
95 Stop();
96 }
97}
98
100{
101 return CurrentTime;
102}
103
104void TimerObject::Report(const std::string &message)
105{
106 Object::Report("Timer '" + GetName() + "', parent '" + GetParent()->GetName() + "': " + message);
107}
108
109}
const std::string & GetName()
Definition object.cpp:53
Object * GetParent()
Definition object.cpp:42
virtual void Report(const std::string &message)
Definition object.cpp:78
void SetValues(const std::string &type, const std::string &name, bool is_permanent, bool is_movable=true)
Definition object.cpp:144
bool RegisterTimerCallback(TimerObject *timer)
Definition sbs.cpp:2119
unsigned long GetCurrentTime()
Definition sbs.cpp:3284
bool UnregisterTimerCallback(TimerObject *timer)
Definition sbs.cpp:2138
bool Verbose
Definition sbs.h:186
virtual void Notify()
Definition timer.h:37
unsigned long CurrentTime
Definition timer.h:46
bool IsRunning()
Definition timer.cpp:74
TimerObject(Object *parent, const std::string &name)
Definition timer.cpp:30
unsigned long LastHit
Definition timer.h:47
unsigned long GetCurrentTime()
Definition timer.cpp:99
virtual ~TimerObject()
Definition timer.cpp:43
unsigned long StartTime
Definition timer.h:48
void Start(int milliseconds=-1, bool oneshot=false)
Definition timer.cpp:49
void Report(const std::string &message)
Definition timer.cpp:104