ScummVM API documentation
GameClock.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * This code is based on the CRAB engine
24  *
25  * Copyright (c) Arvind Raja Yadav
26  *
27  * Licensed under MIT
28  *
29  */
30 
31 #ifndef CRAB_GAMECLOCK_H
32 #define CRAB_GAMECLOCK_H
33 
34 #include "crab/numstr.h"
35 #include "crab/timer.h"
36 
37 namespace Crab {
38 
39 class GameClock {
40  Timer _timer;
41  uint32 _start;
42  Common::String _seperator;
43 
44 public:
45  GameClock() : _seperator(" : ") { _start = 0; }
46 
47  void start(uint32 initialTime = 0) {
48  _start = initialTime;
49  _timer.start();
50  }
51 
52  void start(const Common::String &str) {
53  uint32 ms = 0, hr = 0, min = 0, sec = 0;
54  Common::String strHrs, strMin, strSec;
55 
56  size_t found1 = str.findFirstOf(_seperator);
57  if (found1 > 0 && found1 != Common::String::npos) {
58  strHrs = str.substr(0, found1);
59  hr = stringToNumber<uint32>(strHrs);
60 
61  size_t found2 = str.findFirstOf(_seperator);
62  if (found2 > 0 && found2 != Common::String::npos) {
63  strSec = str.substr(found2 + 1, Common::String::npos);
64  sec = stringToNumber<uint32>(strSec);
65 
66  strMin = str.substr(found1 + _seperator.size(), found2 - (2 * _seperator.size()));
67  min = stringToNumber<uint32>(strMin);
68  }
69  }
70 
71  ms = 3600000 * hr + 60000 * min + 1000 * sec;
72  start(ms);
73  }
74 
75  Common::String getTime() {
76  uint32 ms = _start + _timer.ticks();
77 
78  uint32 x = ms / 1000;
79  uint32 seconds = x % 60;
80  x /= 60;
81  uint32 minutes = x % 60;
82  uint32 hours = x / 60;
83 
84  Common::String timeStr = numberToString(hours);
85  timeStr += _seperator;
86  timeStr += numberToString(minutes);
87  timeStr += _seperator;
88  timeStr += numberToString(seconds);
89 
90  return timeStr;
91  }
92 };
93 
94 } // End of namespace Crab
95 
96 #endif // CRAB_GAMECLOCK_H
Definition: str.h:59
size_t findFirstOf(value_type c, size_t pos=0) const
String substr(size_t pos=0, size_t len=npos) const
Definition: moveeffect.h:37
Definition: timer.h:43
Definition: GameClock.h:39