Skip to content

Commit

Permalink
Implement new sf::Clock functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Sep 20, 2024
1 parent 77b3035 commit 2ebecc0
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 12 deletions.
45 changes: 45 additions & 0 deletions include/CSFML/System/Clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,36 @@ CSFML_SYSTEM_API void sfClock_destroy(const sfClock* clock);
////////////////////////////////////////////////////////////
CSFML_SYSTEM_API sfTime sfClock_getElapsedTime(const sfClock* clock);

////////////////////////////////////////////////////////////
/// \brief Check whether the clock is running
///
/// \param clock Clock object
///
/// \return `true` if the clock is running, `false` otherwise
///
////////////////////////////////////////////////////////////
CSFML_SYSTEM_API bool sfClock_isRunning(const sfClock* clock);

////////////////////////////////////////////////////////////
/// \brief Start the clock
///
/// \param clock Clock object
///
/// \see `stop`
///
////////////////////////////////////////////////////////////
CSFML_SYSTEM_API void sfClock_start(sfClock* clock);

////////////////////////////////////////////////////////////
/// \brief Stop the clock
///
/// \param clock Clock object
///
/// \see `start`
///
////////////////////////////////////////////////////////////
CSFML_SYSTEM_API void sfClock_stop(sfClock* clock);

////////////////////////////////////////////////////////////
/// \brief Restart a clock
///
Expand All @@ -85,3 +115,18 @@ CSFML_SYSTEM_API sfTime sfClock_getElapsedTime(const sfClock* clock);
///
////////////////////////////////////////////////////////////
CSFML_SYSTEM_API sfTime sfClock_restart(sfClock* clock);

////////////////////////////////////////////////////////////
/// \brief Reset the clock
///
/// This function puts the time counter back to zero, returns
/// the elapsed time, and leaves the clock in a paused state.
///
/// \param clock Clock object
///
/// \return Time elapsed
///
/// \see `restart`
///
////////////////////////////////////////////////////////////
CSFML_SYSTEM_API sfTime sfClock_reset(sfClock* clock);
32 changes: 32 additions & 0 deletions src/CSFML/System/Clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,41 @@ sfTime sfClock_getElapsedTime(const sfClock* clock)
}


////////////////////////////////////////////////////////////
bool sfClock_isRunning(const sfClock* clock)
{
assert(clock);
return clock->This.isRunning();
}


////////////////////////////////////////////////////////////
void sfClock_start(sfClock* clock)
{
assert(clock);
clock->This.start();
}


////////////////////////////////////////////////////////////
void sfClock_stop(sfClock* clock)
{
assert(clock);
clock->This.stop();
}


////////////////////////////////////////////////////////////
sfTime sfClock_restart(sfClock* clock)
{
assert(clock);
return {clock->This.restart().asMicroseconds()};
}


////////////////////////////////////////////////////////////
sfTime sfClock_reset(sfClock* clock)
{
assert(clock);
return {clock->This.reset().asMicroseconds()};
}
68 changes: 56 additions & 12 deletions test/System/Clock.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,64 @@

TEST_CASE("[System] sfClock")
{
sfClock* clock = sfClock_create();
using namespace std::chrono_literals;

CHECK(sfTime_asMicroseconds(sfClock_getElapsedTime(clock)) >= 0);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
CHECK(sfTime_asMicroseconds(sfClock_getElapsedTime(clock)) >= 1'000);
SECTION("sfClock_create/sfClock_copy")
{
const sfClock* clock1 = sfClock_create();
CHECK(sfTime_asMicroseconds(sfClock_getElapsedTime(clock1)) >= 0);
CHECK(sfClock_isRunning(clock1));

sfClock_restart(clock);
CHECK(sfTime_asMicroseconds(sfClock_getElapsedTime(clock)) >= 0);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
CHECK(sfTime_asMicroseconds(sfClock_getElapsedTime(clock)) >= 1'000);
const auto elapsed = sfClock_getElapsedTime(clock1);
const sfClock* clock2 = sfClock_copy(clock1);
CHECK(sfTime_asMicroseconds(sfClock_getElapsedTime(clock2)) >= sfTime_asMicroseconds(elapsed));
CHECK(sfClock_isRunning(clock2));

sfClock* anotherClock = sfClock_copy(clock);
CHECK(sfTime_asMicroseconds(sfClock_getElapsedTime(anotherClock)) >= 1'000);
sfClock_destroy(clock2);
sfClock_destroy(clock1);
}

sfClock_destroy(anotherClock);
sfClock_destroy(clock);
SECTION("sfClock_getElapsedTime")
{
const sfClock* clock = sfClock_create();
CHECK(sfTime_asMicroseconds(sfClock_getElapsedTime(clock)) >= 0);
const auto elapsed = sfClock_getElapsedTime(clock);
std::this_thread::sleep_for(1ms);
CHECK(sfTime_asMicroseconds(sfClock_getElapsedTime(clock)) >= sfTime_asMicroseconds(elapsed));
sfClock_destroy(clock);
}

SECTION("sfClock_start/sfClock_stop")
{
sfClock* clock = sfClock_create();
sfClock_stop(clock);
CHECK(!sfClock_isRunning(clock));
const auto elapsed = sfClock_getElapsedTime(clock);
std::this_thread::sleep_for(1ms);
CHECK(sfTime_asMicroseconds(sfClock_getElapsedTime(clock)) == sfTime_asMicroseconds(elapsed));

sfClock_start(clock);
CHECK(sfTime_asMicroseconds(sfClock_getElapsedTime(clock)) >= sfTime_asMicroseconds(elapsed));
CHECK(sfClock_isRunning(clock));
sfClock_destroy(clock);
}

SECTION("sfClock_restart")
{
sfClock* clock = sfClock_create();
CHECK(sfTime_asMicroseconds(sfClock_restart(clock)) >= 0);
CHECK(sfClock_isRunning(clock));
std::this_thread::sleep_for(1ms);
const auto elapsed = sfClock_restart(clock);
CHECK(sfTime_asMicroseconds(sfClock_restart(clock)) < sfTime_asMicroseconds(elapsed));
sfClock_destroy(clock);
}

SECTION("sfClock_reset")
{
sfClock* clock = sfClock_create();
CHECK(sfTime_asMicroseconds(sfClock_reset(clock)) >= 0);
CHECK(!sfClock_isRunning(clock));
sfClock_destroy(clock);
}
}

0 comments on commit 2ebecc0

Please sign in to comment.