Background2D.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <boost/test/unit_test.hpp>
  16. #include "engine/graphics/BackgroundScroll.h"
  17. #include "engine/graphics/BackgroundShake.h"
  18. #include "engine/graphics/BackgroundTileBuffer.h"
  19. #include "engine/graphics/QuadWriter.h"
  20. namespace{
  21. /**
  22. * An entity pointer. The scroll only stores it.
  23. */
  24. Entity* FakeEntity(){
  25. static int entity;
  26. return reinterpret_cast<Entity*>(&entity);
  27. }
  28. Ogre::AxisAlignedBox Limits(){return Ogre::AxisAlignedBox(-10, -20, 0, 10, 20, 1);}
  29. }
  30. BOOST_AUTO_TEST_CASE(TestQuadWriter){
  31. std::vector<float> vertices(QuadWriter::VERTEX_COUNT * QuadWriter::VERTEX_SIZE);
  32. float* end = vertices.data();
  33. const QuadWriter::Corners corners{{{1, 2}, {3, 4}, {5, 6}, {7, 8}}};
  34. QuadWriter::Write(end, corners, {0, 0.25f, 0.5f, 0.75f}, 9, Ogre::ColourValue(1, 0, 0, 1));
  35. BOOST_CHECK(end == vertices.data() + vertices.size());
  36. // The vertices are the corners 0, 1, 2, 0, 2 and 3.
  37. const auto vertex = [&vertices](int index){return &vertices[index * QuadWriter::VERTEX_SIZE];};
  38. const float expected[] = {5, 6, 9, 1, 0, 0, 1, 0.5f, 0.75f};
  39. BOOST_CHECK_EQUAL_COLLECTIONS(vertex(2), vertex(3), expected, expected + 9);
  40. BOOST_CHECK_EQUAL(vertex(5)[0], 7);
  41. BOOST_CHECK_EQUAL(vertex(5)[7], 0);
  42. QuadWriter::SetPositions(vertices.data(), {{{10, 11}, {12, 13}, {14, 15}, {16, 17}}});
  43. QuadWriter::SetTexture(vertices.data(), {1, 2, 3, 4});
  44. BOOST_CHECK_EQUAL(vertex(4)[0], 14);
  45. BOOST_CHECK_EQUAL(vertex(4)[2], 9);
  46. BOOST_CHECK_EQUAL(vertex(4)[7], 3);
  47. BOOST_CHECK_EQUAL(vertex(5)[8], 4);
  48. }
  49. BOOST_AUTO_TEST_CASE(TestBackgroundLayerRanges){
  50. BackgroundLayerRanges ranges;
  51. ranges.Add(0, 0, 6);
  52. ranges.Add(0, 6, 6);
  53. ranges.Add(2, 12, 6);
  54. // Out of range layers count as the nearest one.
  55. ranges.Add(9, 18, 6);
  56. BOOST_CHECK(ranges.AreContiguous());
  57. BOOST_CHECK_EQUAL(ranges.Of(0).count, 12u);
  58. BOOST_CHECK_EQUAL(ranges.Of(2).first, 12u);
  59. BOOST_CHECK_EQUAL(ranges.Of(3).first, 18u);
  60. ranges.Add(1, 24, 6);
  61. BOOST_CHECK(!ranges.AreContiguous());
  62. ranges.Clear();
  63. BOOST_CHECK(ranges.AreContiguous());
  64. BOOST_CHECK_EQUAL(ranges.Of(0).count, 0u);
  65. }
  66. BOOST_AUTO_TEST_CASE(TestBackgroundShake){
  67. BackgroundShake shake;
  68. BOOST_CHECK(!shake.Update(0.1f));
  69. BOOST_CHECK(!shake.Set(7, 1, 1, 1, 1));
  70. // Horizontal, 20 pixels, bounces of one second.
  71. BOOST_CHECK(shake.Set(1, 20, 30, 0, 0));
  72. BOOST_CHECK(shake.Update(1.0f));
  73. // The first bounce goes to half the amplitude times the first factor, -1.
  74. BOOST_CHECK_CLOSE(shake.Offset().x, -10.0f, 0.001f);
  75. BOOST_CHECK_EQUAL(shake.Offset().y, 0);
  76. // To stop, it ends the bounce, goes back to the center and stays there.
  77. BOOST_CHECK(shake.Set(0, 0, 30, 0, 30));
  78. shake.Update(1.0f);
  79. shake.Update(1.0f);
  80. BOOST_CHECK_EQUAL(shake.Offset().x, 0);
  81. BOOST_CHECK(!shake.IsRunning());
  82. }
  83. BOOST_AUTO_TEST_CASE(TestBackgroundScrollLimits){
  84. BackgroundScroll scroll([](const ScriptId&){});
  85. scroll.SetRange(Limits());
  86. scroll.SetPosition({100, -100});
  87. BOOST_CHECK(scroll.Position() == Ogre::Vector2(10, -20));
  88. BOOST_CHECK(!scroll.HasControl());
  89. // A scroll of a script to a position is not limited, and an instant one waits.
  90. scroll.ToPosition({100, -100}, BackgroundScroll::NONE, 0);
  91. BOOST_CHECK(scroll.HasControl());
  92. BOOST_CHECK(scroll.Position() == Ogre::Vector2(10, -20));
  93. BOOST_CHECK(scroll.ApplyPending());
  94. BOOST_CHECK(scroll.Position() == Ogre::Vector2(100, -100));
  95. BOOST_CHECK(!scroll.ApplyPending());
  96. // Following an entity limits it again.
  97. scroll.Follow(FakeEntity());
  98. scroll.SetPosition({100, 0});
  99. BOOST_CHECK(scroll.Position() == Ogre::Vector2(10, 0));
  100. }
  101. BOOST_AUTO_TEST_CASE(TestBackgroundScrollToAnEntity){
  102. std::vector<std::string> resumed;
  103. BackgroundScroll scroll([&resumed](const ScriptId& script){resumed.push_back(script.entity);});
  104. scroll.SetRange(Limits());
  105. BOOST_CHECK(!scroll.Wait(ScriptId()));
  106. BOOST_CHECK(!scroll.ToEntity(FakeEntity(), {50, 5}, BackgroundScroll::LINEAR, 2));
  107. BOOST_CHECK(scroll.End() == Ogre::Vector2(10, 5));
  108. BOOST_CHECK(scroll.TargetEntity() == FakeEntity());
  109. BOOST_CHECK(scroll.FollowedEntity() == nullptr);
  110. ScriptId script;
  111. script.entity = "cloud";
  112. BOOST_CHECK(scroll.Wait(script));
  113. scroll.Stop();
  114. BOOST_CHECK(scroll.FollowedEntity() == FakeEntity());
  115. BOOST_CHECK_EQUAL(scroll.Type(), BackgroundScroll::NONE);
  116. BOOST_REQUIRE_EQUAL(resumed.size(), 1u);
  117. BOOST_CHECK_EQUAL(resumed[0], "cloud");
  118. // An instant scroll moves at once and follows the entity.
  119. BOOST_CHECK(scroll.ToEntity(FakeEntity(), {-50, 0}, BackgroundScroll::NONE, 0));
  120. BOOST_CHECK(scroll.Position() == Ogre::Vector2(-10, 0));
  121. BOOST_CHECK(scroll.FollowedEntity() == FakeEntity());
  122. }
  123. BOOST_AUTO_TEST_CASE(TestBackgroundScrollReplacedAndCleared){
  124. int resumed = 0;
  125. BackgroundScroll scroll([&resumed](const ScriptId&){++ resumed;});
  126. scroll.ToEntity(FakeEntity(), {5, 5}, BackgroundScroll::SMOOTH, 1);
  127. scroll.Wait(ScriptId());
  128. // A new scroll ends the previous one without following its entity.
  129. scroll.ToPosition({1, 1}, BackgroundScroll::LINEAR, 1);
  130. BOOST_CHECK_EQUAL(resumed, 1);
  131. BOOST_CHECK(scroll.FollowedEntity() == nullptr);
  132. BOOST_CHECK(scroll.TargetEntity() == nullptr);
  133. scroll.Wait(ScriptId());
  134. scroll.Clear();
  135. BOOST_CHECK_EQUAL(resumed, 2);
  136. BOOST_CHECK(!scroll.HasControl());
  137. BOOST_CHECK(scroll.Position() == Ogre::Vector2::ZERO);
  138. }