Skip to content

Commit

Permalink
Merge pull request #9 from KomodoPlatform/roman_dev
Browse files Browse the repository at this point in the history
feat(timer): add timer module and unit tests
  • Loading branch information
Milerius authored Sep 13, 2019
2 parents 784826c + fe1f62d commit de44002
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 0 deletions.
1 change: 1 addition & 0 deletions ci/travis_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function run_coverage() {
lcov -r coverage.info "${TRAVIS_BUILD_DIR}/*.test.*" -o coverage.info
lcov -r coverage.info "${TRAVIS_BUILD_DIR}/modules/core/antara/gaming/core/*.tests.*" -o coverage.info
lcov -r coverage.info "${TRAVIS_BUILD_DIR}/modules/ecs/antara/gaming/ecs/*.tests.*" -o coverage.info
lcov -r coverage.info "${TRAVIS_BUILD_DIR}/modules/timer/antara/gaming/timer/*.tests.*" -o coverage.info
lcov -r coverage.info "${TRAVIS_BUILD_DIR}/cmake-build-${BUILD_TYPE}/_deps/*" -o coverage.info
lcov -l coverage.info
bash <(curl -s https://codecov.io/bash) -f coverage.info || echo "Codecov did not collect coverage reports"
Expand Down
1 change: 1 addition & 0 deletions modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(core)
add_subdirectory(timer)
add_subdirectory(ecs)
36 changes: 36 additions & 0 deletions modules/timer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## shared sources between the module and his unit tests
add_library(antara_timer_shared_sources INTERFACE)
target_sources(antara_timer_shared_sources INTERFACE antara/gaming/timer/time.step.cpp)
target_include_directories(antara_timer_shared_sources INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(antara_timer_shared_sources INTERFACE antara::default_settings)
add_library(antara::timer_shared_sources ALIAS antara_timer_shared_sources)


##! module definition
add_library(antara_timer OBJECT)
target_link_libraries(antara_timer PUBLIC antara::timer_shared_sources)
add_library(antara::timer ALIAS antara_timer)

##! antara timer tests
add_executable(antara_timer_tests)
target_sources(antara_timer_tests PUBLIC
antara/gaming/timer/antara.timer.tests.cpp
antara/gaming/timer/antara.timer.time.step.tests.cpp)
target_link_libraries(antara_timer_tests PRIVATE doctest PUBLIC antara::timer_shared_sources)
set_target_properties(antara_timer_tests
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/unit_tests"
)
target_enable_coverage(antara_timer_tests)

if (EMSCRIPTEN)
message(STATUS "Emscripten detected")
if (ENABLE_HTML_COMPILATION)
message(STATUS "Html compilation enabled")
set_target_properties(antara_timer_tests PROPERTIES LINK_FLAGS "-s FORCE_FILESYSTEM=1 -s EXIT_RUNTIME=1"
SUFFIX ".html")
else ()
message(STATUS "Local js compilation")
set_target_properties(antara_timer_tests PROPERTIES LINK_FLAGS "-s FORCE_FILESYSTEM=1 -s NODERAWFS=1 -s EXIT_RUNTIME=1")
endif ()
endif ()
18 changes: 18 additions & 0 deletions modules/timer/antara/gaming/timer/antara.timer.tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>
58 changes: 58 additions & 0 deletions modules/timer/antara/gaming/timer/antara.timer.time.step.tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#include <doctest/doctest.h>
#include "antara/gaming/timer/time.step.hpp"

namespace antara::gaming::timer::tests
{
TEST_SUITE ("timestep tests")
{
time_step timestep;
TEST_CASE ("start timer")
{
timestep.start();
}

TEST_CASE ("start frame")
{
timestep.start_frame();
}

TEST_CASE ("perform update")
{
timestep.perform_update();
}

TEST_CASE ("is update required")
{
CHECK_FALSE(timestep.is_update_required());
}

TEST_CASE ("get fixed delta time")
{
CHECK_GT(time_step::get_fixed_delta_time(), 0.0f);
}

TEST_CASE ("change delta time")
{
time_step::change_fps(_120fps);
CHECK_GT(time_step::get_fixed_delta_time(), 0.0f);
time_step::change_fps(_144fps);
CHECK_GT(time_step::get_fixed_delta_time(), 0.0f);
}
}
}
28 changes: 28 additions & 0 deletions modules/timer/antara/gaming/timer/fps.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#pragma once

#include <chrono>

namespace antara::gaming::timer
{
using namespace std::chrono_literals;

constexpr std::chrono::nanoseconds _60fps{16666666ns};
constexpr std::chrono::nanoseconds _120fps{8333333ns};
constexpr std::chrono::nanoseconds _144fps{6944444ns};
}
55 changes: 55 additions & 0 deletions modules/timer/antara/gaming/timer/time.step.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#include "antara/gaming/timer/time.step.hpp"

namespace antara::gaming::timer
{
std::chrono::nanoseconds time_step::fps_ = _60fps;
float time_step::fixed_delta_time{std::chrono::duration<float, std::ratio<1>>(fps_).count()};
void time_step::start() noexcept
{
start_ = clock::now();
}

void time_step::start_frame() noexcept
{
auto deltaTime = clock::now() - start_;
start_ = clock::now();
lag_ += std::chrono::duration_cast<std::chrono::nanoseconds>(deltaTime);
}

bool time_step::is_update_required() const noexcept
{
return (lag_ >= fps_);
}

void time_step::perform_update() noexcept
{
lag_ -= fps_;
}

void time_step::change_fps(std::chrono::nanoseconds new_fps_rate)
{
fps_ = new_fps_rate;
fixed_delta_time = std::chrono::duration<float, std::ratio<1>>(fps_).count();
}

float time_step::get_fixed_delta_time() noexcept
{
return fixed_delta_time;
}
}
46 changes: 46 additions & 0 deletions modules/timer/antara/gaming/timer/time.step.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#pragma once

#include <chrono>
#include "antara/gaming/timer/fps.hpp"

namespace antara::gaming::timer
{
class time_step
{
public:
void start() noexcept;

void start_frame() noexcept;

[[nodiscard]] bool is_update_required() const noexcept;

void perform_update() noexcept;

static void change_fps(std::chrono::nanoseconds new_fps_rate);

static float get_fixed_delta_time() noexcept;

private:
static std::chrono::nanoseconds fps_;
static float fixed_delta_time;
using clock = std::chrono::steady_clock;
std::chrono::nanoseconds lag_{0ns};
clock::time_point start_{clock::now()};
};
}

0 comments on commit de44002

Please sign in to comment.