annotate tests/libclient/animation/main.cpp @ 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 d4f5f7231b84
children a47a4477f347
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
1 /*
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
2 * main.cpp -- test Animation
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
3 *
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
4 * Copyright (c) 2013-2016 Malikania Authors
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
5 *
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
8 * copyright notice and this permission notice appear in all copies.
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
9 *
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
17 */
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
18
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
19 #include <chrono>
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
20 #include <thread>
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
21 #include <exception>
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
22
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
23 #include <gtest/gtest.h>
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
24
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
25 #include <malikania/animation.hpp>
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
26 #include <malikania/animator.hpp>
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
27 #include <malikania/client-resources-loader.hpp>
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
28 #include <malikania/resources-locator.hpp>
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
29 #include <malikania/window.hpp>
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
30
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
31 using namespace malikania;
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
32
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
33 using namespace std::chrono_literals;
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
34
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
35 namespace {
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
36
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
37 Window window(400, 400);
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
38
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
39 } // !namespace
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
40
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
41 class TestAnimation : public testing::Test {
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
42 protected:
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
43 ResourcesLocatorDirectory m_locator;
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
44 ClientResourcesLoader m_loader;
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
45
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
46 public:
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
47 TestAnimation()
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
48 : m_locator(SOURCE_DIRECTORY "/resources")
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
49 , m_loader(m_locator)
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
50 {
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
51 }
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
52 };
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
53
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
54 TEST_F(TestAnimation, standard)
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
55 {
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
56 try {
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
57 Animation animation = m_loader.loadAnimation("animations/margins.json");
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
58
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
59 ASSERT_EQ(12U, animation.frames().size());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
60 ASSERT_EQ(500U, animation[0].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
61 ASSERT_EQ(501U, animation[1].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
62 ASSERT_EQ(502U, animation[2].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
63 ASSERT_EQ(503U, animation[3].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
64 ASSERT_EQ(504U, animation[4].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
65 ASSERT_EQ(505U, animation[5].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
66 ASSERT_EQ(506U, animation[6].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
67 ASSERT_EQ(507U, animation[7].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
68 ASSERT_EQ(508U, animation[8].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
69 ASSERT_EQ(509U, animation[9].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
70 ASSERT_EQ(510U, animation[10].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
71 ASSERT_EQ(511U, animation[11].delay());
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
72 } catch (const std::exception &ex) {
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
73 FAIL() << ex.what();
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
74 }
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
75 }
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
76
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
77 TEST_F(TestAnimation, draw)
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
78 {
41
3645200f46bf Misc: switch to Boost.Timer, closes #586
David Demelier <markand@malikania.fr>
parents: 33
diff changeset
79 boost::timer::cpu_timer timer;
33
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
80
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
81 try {
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
82 Animation animation = m_loader.loadAnimation("animations/margins.json");
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
83 Animator animator(animation);
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
84
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
85 auto x = (400 / 2) - (animation.sprite().cell().width() / 2);
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
86 auto y = (400 / 2) - (animation.sprite().cell().height() / 2);
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
87
41
3645200f46bf Misc: switch to Boost.Timer, closes #586
David Demelier <markand@malikania.fr>
parents: 33
diff changeset
88 while (timer.elapsed().wall / 1000000LL < 8000) {
33
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
89 window.clear();
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
90 animator.draw(window, Point(x, y));
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
91 animator.update();
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
92 window.present();
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
93 }
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
94 } catch (const std::exception &ex) {
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
95 FAIL() << ex.what();
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
96 }
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
97 }
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
98
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
99 int main(int argc, char **argv)
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
100 {
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
101 testing::InitGoogleTest(&argc, argv);
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
102
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
103 return RUN_ALL_TESTS();
d4f5f7231b84 Misc: switch to .hpp, dos2unix everything while here, #478
David Demelier <markand@malikania.fr>
parents: 30
diff changeset
104 }