Skip to content

Commit 869f9ff

Browse files
committed
Add ITimer class
1 parent d264d7e commit 869f9ff

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ target_sources(scratchcpp
4646
include/scratchcpp/target.h
4747
include/scratchcpp/stage.h
4848
include/scratchcpp/sprite.h
49+
include/scratchcpp/itimer.h
4950
)
5051

5152
add_library(zip SHARED

include/scratchcpp/itimer.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
namespace libscratchcpp
6+
{
7+
8+
class ITimer
9+
{
10+
public:
11+
virtual ~ITimer() { }
12+
13+
virtual double value() const = 0;
14+
15+
virtual void reset() = 0;
16+
};
17+
18+
} // namespace libscratchcpp

src/engine/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ target_sources(scratchcpp
1111
script_p.h
1212
internal/engine.cpp
1313
internal/engine.h
14+
internal/timer.cpp
15+
internal/timer.h
1416
internal/blocksectioncontainer.cpp
1517
internal/blocksectioncontainer.h
1618
internal/global.h

src/engine/internal/timer.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "timer.h"
4+
5+
using namespace libscratchcpp;
6+
7+
Timer::Timer()
8+
{
9+
}
10+
11+
double Timer::value() const
12+
{
13+
return (std::chrono::steady_clock::now() - m_startTime).count() / 1000.0;
14+
}
15+
16+
void Timer::reset()
17+
{
18+
m_startTime = std::chrono::steady_clock::now();
19+
}

src/engine/internal/timer.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <scratchcpp/itimer.h>
6+
#include <chrono>
7+
8+
namespace libscratchcpp
9+
{
10+
11+
class Timer : public ITimer
12+
{
13+
public:
14+
Timer();
15+
Timer(const Timer &) = delete;
16+
17+
double value() const override;
18+
void reset() override;
19+
20+
private:
21+
std::chrono::steady_clock::time_point m_startTime = std::chrono::steady_clock::now();
22+
};
23+
24+
} // namespace libscratchcpp

0 commit comments

Comments
 (0)