diff examples/font/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/font/main.cpp	Thu Dec 22 15:17:43 2016 +0100
@@ -0,0 +1,123 @@
+/*
+ * main.cpp -- test Font
+ *
+ * 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>
+
+#define BOOST_TEST_MODULE "Font"
+#include <boost/test/unit_test.hpp>
+
+#include <malikania/client_resources_loader.hpp>
+#include <malikania/color.hpp>
+#include <malikania/font.hpp>
+#include <malikania/point.hpp>
+#include <malikania/resources_locator.hpp>
+#include <malikania/window.hpp>
+
+using namespace std::chrono_literals;
+
+namespace {
+
+mlk::window window(400, 400);
+
+} // !namespace
+
+class test_font {
+protected:
+    mlk::directory_resources_locator m_locator;
+    mlk::client_resources_loader m_loader;
+    mlk::font m_font;
+
+public:
+    test_font()
+        : m_locator(SOURCE_DIRECTORY "/resources")
+        , m_loader(m_locator)
+        , m_font(m_loader.load_font("DejaVuSans.ttf", 10))
+    {
+        window.set_drawing_color(mlk::color("black"));
+        window.clear();
+    }
+};
+
+BOOST_FIXTURE_TEST_SUITE(test_font_suite, test_font)
+
+BOOST_AUTO_TEST_CASE(topleft)
+{
+    window.set_drawing_color(mlk::color("white"));
+    window.draw_text("top left", m_font, mlk::point(10, 10));
+    window.present();
+
+    std::this_thread::sleep_for(1s);
+}
+
+BOOST_AUTO_TEST_CASE(topright)
+{
+    auto dim = m_font.clip("top right");
+
+    window.set_drawing_color(mlk::color("white"));
+    window.draw_text("top right", m_font, mlk::point(400 - dim.width() - 10, 10));
+    window.present();
+
+    std::this_thread::sleep_for(1s);
+}
+
+BOOST_AUTO_TEST_CASE(bottomleft)
+{
+    auto dim = m_font.clip("bottom left");
+
+    window.set_drawing_color(mlk::color("white"));
+    window.draw_text("bottom left", m_font, mlk::point(10, 400 - dim.height() - 10));
+    window.present();
+
+    std::this_thread::sleep_for(1s);
+}
+
+BOOST_AUTO_TEST_CASE(bottomright)
+{
+    auto dim = m_font.clip("bottom right");
+
+    window.set_drawing_color(mlk::color("white"));
+    window.draw_text("bottom right", m_font, mlk::point(400 - dim.width() - 10, 400 - dim.height() - 10));
+    window.present();
+
+    std::this_thread::sleep_for(1s);
+}
+
+BOOST_AUTO_TEST_CASE(center)
+{
+    auto dim = m_font.clip("center");
+
+    window.set_drawing_color(mlk::color("white"));
+    window.draw_text("center", m_font, mlk::point(200 - (dim.width() / 2), 200 - (dim.height() -2)));
+    window.present();
+
+    std::this_thread::sleep_for(1s);
+}
+
+BOOST_AUTO_TEST_CASE(center2)
+{
+    auto dim = m_font.clip("The world is Malikania.");
+
+    window.set_drawing_color(mlk::color("white"));
+    window.draw_text("The world is Malikania.", m_font, mlk::point(200 - (dim.width() / 2), 200 - (dim.height() -2)));
+    window.present();
+
+    std::this_thread::sleep_for(3s);
+}
+
+BOOST_AUTO_TEST_SUITE_END()