Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
[FEAT]: add clock
Browse files Browse the repository at this point in the history
  • Loading branch information
bobis33 committed Apr 12, 2024
1 parent 2c15557 commit b9776b4
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 0 deletions.
63 changes: 63 additions & 0 deletions App/include/RayTracer/Clock/Clock.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
** EPITECH PROJECT, 2024
** Arcade
** File description:
** Clock.hpp
*/

#ifndef ARCADE_CLOCK_HPP
#define ARCADE_CLOCK_HPP

#include <chrono>

#include "RayTracer/Clock/Time.hpp"

namespace Arcade
{
class Clock
{
public:
/**
* @brief Construct a new Clock object
*/
Clock() { restart(); };

~Clock() = default;

/**
* @brief Restart the clock
*/
void restart() { m_start = std::chrono::high_resolution_clock::now(); };

/**
* @brief Pause the clock
*/
void pause();

/**
* @brief Resume the clock
*/
void resume();

/**
* @brief Get the elapsed time since the last restart
*
* @return Time The elapsed time
*/
[[nodiscard]] Time getElapsedTime() const;

/**
* @brief TimePoint is a type alias for a time point which is a very long and complicated type in the standard library
*/
using TimePoint = std::chrono::time_point<std::chrono::high_resolution_clock>;

private:
TimePoint m_start{};
TimePoint m_pause{};
bool m_paused{false};

}; // Clock

} // namespace Arcade

#endif // ARCADE_CLOCK_HPP
43 changes: 43 additions & 0 deletions App/include/RayTracer/Clock/Time.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
** EPITECH PROJECT, 2024
** Arcade
** File description:
** time.hpp
*/

#ifndef ARCADE_TIME_HPP
#define ARCADE_TIME_HPP

namespace Arcade
{
class Time
{
public:
/**
* @brief Construct a new Time object
*/
explicit Time(double seconds) : m_seconds(seconds) {};

/**
* @brief Transform the time to seconds
*/
[[nodiscard]] int asSeconds() const { return static_cast<int>(m_seconds); };

/**
* @brief Transform the time to milliseconds
*/
[[nodiscard]] int asMilliseconds() const { return static_cast<int>(m_seconds * 1000); }

/**
* @brief Transform the time to microseconds
*/
[[nodiscard]] int asMicroseconds() const { return static_cast<int>(m_seconds * 1000000); };

private:
double m_seconds{0.0F};

}; // Time

} // namespace Arcade

#endif // ARCADE_TIME_HPP
36 changes: 36 additions & 0 deletions App/src/clock/clock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
** EPITECH PROJECT, 2024
** Arcade
** File description:
** Clock.cpp
*/

#include "RayTracer/Clock/Clock.hpp"

void Arcade::Clock::pause()
{
if (!m_paused) {
m_pause = std::chrono::high_resolution_clock::now();
m_paused = true;
}
}

void Arcade::Clock::resume()
{
if (m_paused) {
m_start += std::chrono::high_resolution_clock::now() - m_pause;
m_paused = false;
}
}

Arcade::Time Arcade::Clock::getElapsedTime() const
{
TimePoint now = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> elapsed_time{};
if (m_paused) {
elapsed_time = m_pause - m_start;
} else {
elapsed_time = now - m_start;
}
return Time(elapsed_time.count());
}
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ target_sources(${PROJECT_NAME} PRIVATE
${SRC_DIR}/main.cpp
${SRC_DIR}/parser.cpp
${SRC_DIR}/core.cpp
${SRC_DIR}/clock/clock.cpp
)

target_include_directories(${PROJECT_NAME} PRIVATE ${INCLUDE_DIR})
Expand Down
Binary file modified doc/RayTracer.pdf
Binary file not shown.

0 comments on commit b9776b4

Please sign in to comment.