-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from KomodoPlatform/roman_dev
feat(timer): add timer module and unit tests
- Loading branch information
Showing
8 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
add_subdirectory(core) | ||
add_subdirectory(timer) | ||
add_subdirectory(ecs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
modules/timer/antara/gaming/timer/antara.timer.time.step.tests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}; | ||
}; | ||
} |