Mercurial > malikania
changeset 41:3645200f46bf
Misc: switch to Boost.Timer, closes #586
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 27 Nov 2016 20:50:38 +0100 |
parents | 1e206fdc7021 |
children | a47a4477f347 |
files | CMakeLists.txt libclient/malikania/animator.cpp libclient/malikania/animator.hpp libcommon-js/malikania/js-elapsed-timer.cpp libcommon/CMakeLists.txt libcommon/malikania/elapsed-timer.cpp libcommon/malikania/elapsed-timer.hpp tests/libclient/animation/main.cpp tests/libclient/js-animation/main.cpp tests/libcommon/CMakeLists.txt tests/libcommon/elapsed-timer/CMakeLists.txt tests/libcommon/elapsed-timer/main.cpp |
diffstat | 12 files changed, 25 insertions(+), 293 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Sun Nov 27 20:00:13 2016 +0100 +++ b/CMakeLists.txt Sun Nov 27 20:50:38 2016 +0100 @@ -43,7 +43,7 @@ include_directories(${CMAKE_BINARY_DIR}) enable_testing() -find_package(Boost REQUIRED COMPONENTS filesystem system) +find_package(Boost REQUIRED COMPONENTS filesystem system timer) find_package(OpenSSL REQUIRED) find_package(ZIP REQUIRED)
--- a/libclient/malikania/animator.cpp Sun Nov 27 20:00:13 2016 +0100 +++ b/libclient/malikania/animator.cpp Sun Nov 27 20:50:38 2016 +0100 @@ -33,9 +33,9 @@ if (m_current >= total) return; - if (m_timer.elapsed() >= m_animation[m_current].delay()) { + if (m_timer.elapsed().wall / 1000000LL >= m_animation[m_current].delay()) { m_current ++; - m_timer.reset(); + m_timer.start(); } }
--- a/libclient/malikania/animator.hpp Sun Nov 27 20:00:13 2016 +0100 +++ b/libclient/malikania/animator.hpp Sun Nov 27 20:50:38 2016 +0100 @@ -19,13 +19,13 @@ #ifndef MALIKANIA_ANIMATOR_HPP #define MALIKANIA_ANIMATOR_HPP +#include <boost/timer/timer.hpp> + /** * \file animator.hpp * \brief Draw animations. */ -#include "elapsed-timer.hpp" - namespace malikania { class Animation; @@ -40,8 +40,8 @@ */ class Animator { private: + boost::timer::cpu_timer m_timer; Animation &m_animation; - ElapsedTimer m_timer; unsigned m_current{0}; public:
--- a/libcommon-js/malikania/js-elapsed-timer.cpp Sun Nov 27 20:00:13 2016 +0100 +++ b/libcommon-js/malikania/js-elapsed-timer.cpp Sun Nov 27 20:50:38 2016 +0100 @@ -16,10 +16,11 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <boost/timer/timer.hpp> + #include <cassert> #include <string> -#include "elapsed-timer.hpp" #include "duktape.hpp" namespace malikania { @@ -28,7 +29,7 @@ const std::string Signature("\xff" "\xff" "malikania-elapsed-timer-ptr"); -ElapsedTimer *self(duk_context *ctx) +boost::timer::cpu_timer& self(duk_context *ctx) { StackAssert sa(ctx); @@ -40,7 +41,7 @@ if (!ptr) duk_error(ctx, DUK_ERR_TYPE_ERROR, "not an ElapsedTimer object"); - return static_cast<ElapsedTimer *>(ptr); + return *static_cast<boost::timer::cpu_timer*>(ptr); } /* @@ -51,7 +52,7 @@ */ duk_ret_t pause(duk_context *ctx) { - self(ctx)->pause(); + self(ctx).stop(); return 0; } @@ -64,7 +65,7 @@ */ duk_ret_t reset(duk_context *ctx) { - self(ctx)->reset(); + self(ctx).start(); return 0; } @@ -77,7 +78,7 @@ */ duk_ret_t restart(duk_context *ctx) { - self(ctx)->restart(); + self(ctx).resume(); return 0; } @@ -93,7 +94,7 @@ */ duk_ret_t elapsed(duk_context *ctx) { - duk_push_uint(ctx, self(ctx)->elapsed()); + duk_push_uint(ctx, self(ctx).elapsed().wall / 1000000LL); return 1; } @@ -107,7 +108,7 @@ duk_ret_t constructor(duk_context *ctx) { duk_push_this(ctx); - duk_push_pointer(ctx, new ElapsedTimer); + duk_push_pointer(ctx, new boost::timer::cpu_timer); duk_put_prop_string(ctx, -2, Signature.c_str()); duk_pop(ctx); @@ -123,7 +124,7 @@ duk_ret_t destructor(duk_context *ctx) { duk_get_prop_string(ctx, 0, Signature.c_str()); - delete static_cast<ElapsedTimer *>(duk_to_pointer(ctx, -1)); + delete static_cast<boost::timer::cpu_timer*>(duk_to_pointer(ctx, -1)); duk_pop(ctx); duk_del_prop_string(ctx, 0, Signature.c_str());
--- a/libcommon/CMakeLists.txt Sun Nov 27 20:00:13 2016 +0100 +++ b/libcommon/CMakeLists.txt Sun Nov 27 20:50:38 2016 +0100 @@ -20,7 +20,6 @@ HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/malikania/application.hpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/duktape.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/malikania/elapsed-timer.hpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/game.hpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/id.hpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/resources-loader.hpp @@ -31,7 +30,6 @@ set( SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/malikania/application.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/malikania/elapsed-timer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/resources-loader.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/resources-locator.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/util.cpp @@ -60,7 +58,10 @@ ${INCLUDES} $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/malikania> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> - LIBRARIES json ${LIBRARIES} + LIBRARIES + json + ${Boost_LIBRARIES} + ${LIBRARIES} ) set_target_properties(libcommon PROPERTIES PREFIX "")
--- a/libcommon/malikania/elapsed-timer.cpp Sun Nov 27 20:00:13 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -/* - * elapsed-timer.cpp -- measure elapsed time - * - * Copyright (c) 2013-2016 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include "elapsed-timer.hpp" - -using std::chrono::duration_cast; -using std::chrono::high_resolution_clock; -using std::chrono::milliseconds; - -namespace malikania { - -ElapsedTimer::ElapsedTimer() noexcept -{ - m_last = high_resolution_clock::now(); -} - -void ElapsedTimer::pause() noexcept -{ - /* - * When we put the timer on pause, do not forget to set the already - * elapsed time. - */ - (void)elapsed(); - m_paused = true; -} - -void ElapsedTimer::restart() noexcept -{ - m_paused = false; - m_last = high_resolution_clock::now(); -} - -void ElapsedTimer::reset() noexcept -{ - m_elapsed = 0; - m_last = high_resolution_clock::now(); -} - -unsigned ElapsedTimer::elapsed() noexcept -{ - if (!m_paused) { - m_elapsed += duration_cast<milliseconds>(high_resolution_clock::now() - m_last).count(); - m_last = high_resolution_clock::now(); - } - - return m_elapsed; -} - -} // !malikania
--- a/libcommon/malikania/elapsed-timer.hpp Sun Nov 27 20:00:13 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -/* - * elapsed-timer.hpp -- measure elapsed time - * - * Copyright (c) 2013-2016 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef MALIKANIA_ELAPSED_TIMER_HPP -#define MALIKANIA_ELAPSED_TIMER_HPP - -/** - * \file elapsed-timer.hpp - * \brief Measure elapsed time - */ - -#include <chrono> - -namespace malikania { - -/** - * \class ElapsedTimer - * \brief Measure elapsed time - * - * This class provides an abstraction to measure elapsed time since the - * construction of the object. - * - * It uses std::chrono::high_resolution_clock for more precision and uses - * milliseconds only. - */ -class ElapsedTimer { -public: - using TimePoint = std::chrono::time_point<std::chrono::high_resolution_clock>; - -private: - TimePoint m_last; - bool m_paused{false}; - unsigned m_elapsed{0}; - -public: - /** - * Construct the elapsed timer, start counting. - */ - ElapsedTimer() noexcept; - - /** - * Put the timer on pause, the already elapsed time is stored. - */ - void pause() noexcept; - - /** - * Restart the timer, does not reset it. - */ - void restart() noexcept; - - /** - * Reset the timer to 0. - */ - void reset() noexcept; - - /** - * Get the number of elapsed milliseconds. - * - * \return the milliseconds - */ - unsigned elapsed() noexcept; -}; - -} // !malikania - -#endif // !MALIKANIA_ELAPSED_TIMER_HPP
--- a/tests/libclient/animation/main.cpp Sun Nov 27 20:00:13 2016 +0100 +++ b/tests/libclient/animation/main.cpp Sun Nov 27 20:50:38 2016 +0100 @@ -76,7 +76,7 @@ TEST_F(TestAnimation, draw) { - ElapsedTimer timer; + boost::timer::cpu_timer timer; try { Animation animation = m_loader.loadAnimation("animations/margins.json"); @@ -85,7 +85,7 @@ auto x = (400 / 2) - (animation.sprite().cell().width() / 2); auto y = (400 / 2) - (animation.sprite().cell().height() / 2); - while (timer.elapsed() < 8000) { + while (timer.elapsed().wall / 1000000LL < 8000) { window.clear(); animator.draw(window, Point(x, y)); animator.update();
--- a/tests/libclient/js-animation/main.cpp Sun Nov 27 20:00:13 2016 +0100 +++ b/tests/libclient/js-animation/main.cpp Sun Nov 27 20:50:38 2016 +0100 @@ -21,7 +21,6 @@ #include <gtest/gtest.h> -#include <malikania/elapsed-timer.hpp> #include <malikania/js-client-resources-loader.hpp> #include <malikania/js-animation.hpp> #include <malikania/js-animator.hpp> @@ -64,9 +63,9 @@ if (ret != 0) throw dukx_exception(m_ctx, -1); - ElapsedTimer timer; + boost::timer::cpu_timer timer; - while (timer.elapsed() < 8000) { + while (timer.elapsed().wall / 1000000LL < 8000) { auto ret = duk_peval_string(m_ctx, "w.setDrawingColor('lightskyblue');" "w.clear();"
--- a/tests/libcommon/CMakeLists.txt Sun Nov 27 20:00:13 2016 +0100 +++ b/tests/libcommon/CMakeLists.txt Sun Nov 27 20:50:38 2016 +0100 @@ -16,6 +16,5 @@ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # -add_subdirectory(elapsed-timer) +add_subdirectory(util) add_subdirectory(js-elapsed-timer) -add_subdirectory(util)
--- a/tests/libcommon/elapsed-timer/CMakeLists.txt Sun Nov 27 20:00:13 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -# -# CMakeLists.txt -- CMake build system for malikania -# -# Copyright (c) 2013-2016 Malikania Authors -# -# Permission to use, copy, modify, and/or distribute this software for any -# purpose with or without fee is hereby granted, provided that the above -# copyright notice and this permission notice appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -# - -malikania_create_test( - NAME elapsedtimer - LIBRARIES libcommon - SOURCES main.cpp -)
--- a/tests/libcommon/elapsed-timer/main.cpp Sun Nov 27 20:00:13 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,100 +0,0 @@ -/* - * main.cpp -- test ElapsedTimer - * - * Copyright (c) 2013-2016 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <thread> - -#include <gtest/gtest.h> - -#include <malikania/elapsed-timer.hpp> - -using namespace malikania; -using namespace std::chrono_literals; - -/* - * For all tests, we tolerate 30 ms because some systems have bigger lags. - */ -static constexpr int margin = 30; - -class TestElapsedTimer : public testing::Test { -protected: - ElapsedTimer m_timer; - - inline void assertRange(int value, int expected) const noexcept - { - if (value < (expected - margin) || value > (expected + margin)) { - FAIL() << value << " is bigger than [" << (expected - margin) << ", " << (expected + margin) << "]"; - } - } -}; - -TEST_F(TestElapsedTimer, standard) -{ - std::this_thread::sleep_for(300ms); - - assertRange(m_timer.elapsed(), 300); -} - -TEST_F(TestElapsedTimer, reset) -{ - std::this_thread::sleep_for(300ms); - - m_timer.reset(); - - assertRange(m_timer.elapsed(), 0); -} - -TEST_F(TestElapsedTimer, pause) -{ - /* - * Simulate a pause in the game like this: - * - * start pause restart elapsed - * | 10ms |.5ms.| 6ms | - * - * Since the game was paused, the 5ms must not be totalized. - */ - std::this_thread::sleep_for(10ms); - - m_timer.pause(); - - std::this_thread::sleep_for(5ms); - - m_timer.restart(); - - std::this_thread::sleep_for(6ms); - - assertRange(m_timer.elapsed(), 16); -} - -TEST_F(TestElapsedTimer, doublecheck) -{ - std::this_thread::sleep_for(50ms); - - (void)m_timer.elapsed(); - - std::this_thread::sleep_for(50ms); - - assertRange(m_timer.elapsed(), 100); -} - -int main(int argc, char **argv) -{ - testing::InitGoogleTest(&argc, argv); - - return RUN_ALL_TESTS(); -}