diff examples/animation/main.cpp @ 74:78de82cc6bde

Tests: move graphical examples into examples, closes #606
author David Demelier <markand@malikania.fr>
date Thu, 22 Dec 2016 15:17:43 +0100
parents
children a162f380f02e
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/animation/main.cpp	Thu Dec 22 15:17:43 2016 +0100
@@ -0,0 +1,98 @@
+/*
+ * main.cpp -- test animation
+ *
+ * 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 <chrono>
+#include <thread>
+#include <exception>
+
+#define BOOST_TEST_MODULE "Animation"
+#include <boost/test/unit_test.hpp>
+
+#include <malikania/animation.hpp>
+#include <malikania/animator.hpp>
+#include <malikania/client_resources_loader.hpp>
+#include <malikania/resources_locator.hpp>
+#include <malikania/window.hpp>
+
+namespace {
+
+mlk::window win(400, 400);
+
+} // !namespace
+
+class test_animation {
+protected:
+    mlk::directory_resources_locator m_locator;
+    mlk::client_resources_loader m_loader;
+
+public:
+    test_animation()
+        : m_locator(SOURCE_DIRECTORY "/resources")
+        , m_loader(m_locator)
+    {
+    }
+};
+
+BOOST_FIXTURE_TEST_SUITE(test_animation_suite, test_animation)
+
+BOOST_AUTO_TEST_CASE(standard)
+{
+    try {
+        auto animation = m_loader.load_animation("animations/margins.json");
+
+        BOOST_REQUIRE_EQUAL(12U, animation.frames().size());
+        BOOST_REQUIRE_EQUAL(500U, animation[0].delay());
+        BOOST_REQUIRE_EQUAL(501U, animation[1].delay());
+        BOOST_REQUIRE_EQUAL(502U, animation[2].delay());
+        BOOST_REQUIRE_EQUAL(503U, animation[3].delay());
+        BOOST_REQUIRE_EQUAL(504U, animation[4].delay());
+        BOOST_REQUIRE_EQUAL(505U, animation[5].delay());
+        BOOST_REQUIRE_EQUAL(506U, animation[6].delay());
+        BOOST_REQUIRE_EQUAL(507U, animation[7].delay());
+        BOOST_REQUIRE_EQUAL(508U, animation[8].delay());
+        BOOST_REQUIRE_EQUAL(509U, animation[9].delay());
+        BOOST_REQUIRE_EQUAL(510U, animation[10].delay());
+        BOOST_REQUIRE_EQUAL(511U, animation[11].delay());
+    } catch (const std::exception &ex) {
+        BOOST_FAIL(ex.what());
+    }
+}
+
+BOOST_AUTO_TEST_CASE(draw)
+{
+    boost::timer::cpu_timer timer;
+
+    try {
+        mlk::animation animation = m_loader.load_animation("animations/margins.json");
+        mlk::animator animator(animation);
+
+        auto x = (400 / 2) - (animation.sprite().cell().width() / 2);
+        auto y = (400 / 2) - (animation.sprite().cell().height() / 2);
+
+        while (timer.elapsed().wall / 1000000LL < 8000) {
+            win.clear();
+            animator.draw(win, mlk::point(x, y));
+            animator.update();
+            win.present();
+        }
+    } catch (const std::exception &ex) {
+        BOOST_FAIL(ex.what());
+    }
+}
+
+BOOST_AUTO_TEST_SUITE_END()