comparison examples/js-font/main.cpp @ 80:a162f380f02e

CMake: create examples, closes #615
author David Demelier <markand@malikania.fr>
date Sun, 22 Jan 2017 09:59:14 +0100
parents 78de82cc6bde
children 301599387b40
comparison
equal deleted inserted replaced
79:8b41e9a2e095 80:a162f380f02e
1 /* 1 /*
2 * main.cpp -- test Font (JavaScript binding) 2 * main.cpp -- test Font (JavaScript binding)
3 * 3 *
4 * Copyright (c) 2013-2016 Malikania Authors 4 * Copyright (c) 2013-2017 Malikania Authors
5 * 5 *
6 * Permission to use, copy, modify, and/or distribute this software for any 6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies. 8 * copyright notice and this permission notice appear in all copies.
9 * 9 *
17 */ 17 */
18 18
19 #include <chrono> 19 #include <chrono>
20 #include <thread> 20 #include <thread>
21 21
22 #define BOOST_TEST_MODULE "Javascript Font"
23 #include <boost/test/unit_test.hpp>
24
25 #include <malikania/js_client_resources_loader.hpp> 22 #include <malikania/js_client_resources_loader.hpp>
26 #include <malikania/js_font.hpp> 23 #include <malikania/js_font.hpp>
27 #include <malikania/js_window.hpp> 24 #include <malikania/js_window.hpp>
28 #include <malikania/resources_locator.hpp> 25 #include <malikania/resources_locator.hpp>
29 26
30 using namespace std::chrono_literals; 27 using namespace std::chrono_literals;
31 28
32 class test_font { 29 void basic(dukx_context& ctx)
33 protected: 30 {
34 mlk::directory_resources_locator m_locator; 31 auto ret = duk_peval_string(ctx,
35 mlk::client_resources_loader m_loader; 32 "w = new Malikania.Window();"
36 dukx_context m_ctx; 33 "f = new Malikania.Font('DejaVuSans.ttf', 10);"
34 "w.setDrawingColor('lightskyblue');"
35 "w.clear();"
36 "s = 'The world is Malikania.';"
37 "c = f.clip(s);"
38 "w.setDrawingColor('white');"
39 "w.drawText(s, f, { x: 320 - (c.width / 2), y: 240 - (c.height / 2) });"
40 "w.present();"
41 );
37 42
38 public: 43 if (ret != 0) {
39 test_font() 44 throw dukx_get_exception(ctx, -1);
40 : m_locator(SOURCE_DIRECTORY "/resources")
41 , m_loader(m_locator)
42 {
43 duk_push_object(m_ctx);
44 duk_put_global_string(m_ctx, "Malikania");
45 dukx_put_client_loader(m_ctx, m_loader);
46 mlk::dukx_load_font(m_ctx);
47 mlk::dukx_load_window(m_ctx);
48 } 45 }
49 };
50 46
51 BOOST_FIXTURE_TEST_SUITE(test_font_suite, test_font) 47 std::this_thread::sleep_for(3s);
48 }
52 49
53 BOOST_AUTO_TEST_CASE(basic) 50 int main()
54 { 51 {
55 try { 52 try {
56 auto ret = duk_peval_string(m_ctx, 53 mlk::directory_resources_locator locator(SOURCE_DIRECTORY);
57 "w = new Malikania.Window();" 54 mlk::client_resources_loader loader(locator);
58 "f = new Malikania.Font('DejaVuSans.ttf', 10);" 55 dukx_context ctx;
59 "w.setDrawingColor('lightskyblue');"
60 "w.clear();"
61 "s = 'The world is Malikania.';"
62 "c = f.clip(s);"
63 "w.setDrawingColor('white');"
64 "w.drawText(s, f, { x: 320 - (c.width / 2), y: 240 - (c.height / 2) });"
65 "w.present();"
66 );
67 56
68 if (ret != 0) { 57 duk_push_object(ctx);
69 throw dukx_get_exception(m_ctx, -1); 58 duk_put_global_string(ctx, "Malikania");
70 } 59 dukx_put_client_loader(ctx, loader);
60 mlk::dukx_load_font(ctx);
61 mlk::dukx_load_window(ctx);
71 62
72 std::this_thread::sleep_for(3s); 63 basic(ctx);
73 } catch (const std::exception &ex) { 64 } catch (const std::exception& ex) {
74 BOOST_FAIL(ex.what()); 65 std::cerr << ex.what() << std::endl;
66 return 1;
75 } 67 }
76 } 68 }
77
78 BOOST_AUTO_TEST_SUITE_END()