rotate.t.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // rotate_tc.h
  3. // datamodel
  4. //
  5. // Created by Sam Jaffe on 9/30/15.
  6. //
  7. //
  8. #include <cxxtest/TestSuite.h>
  9. #include "rotate.hpp"
  10. struct Point {
  11. float x = 0.f, y = 0.f;
  12. Point() = default;
  13. Point(float _x, float _y) : x(_x), y(_y) {}
  14. bool operator==(const Point& other) const {
  15. return !memcmp(this, &other, sizeof(*this));
  16. }
  17. };
  18. struct Dimension {
  19. float width = 0.f, height = 0.f;
  20. Dimension() = default;
  21. Dimension(float w, float h) : width(w), height(h) {}
  22. bool operator==(const Dimension& other) const {
  23. return !memcmp(this, &other, sizeof(*this));
  24. }
  25. };
  26. struct Material {
  27. int id = -1;
  28. Material() = default;
  29. Material(int i) : id(i) {}
  30. bool operator==(const Material& other) const {
  31. return !memcmp(this, &other, sizeof(*this));
  32. }
  33. };
  34. struct RenderInfo {
  35. Point center;
  36. Material material;
  37. Dimension texture_size, object_size;
  38. Point bottom_left, bottom_right, top_right, top_left;
  39. float rotation = 0.0f; // TODO
  40. RenderInfo(Point c, Dimension sz, Material mat = {0}, Dimension tx_sz = {1.f, 1.f})
  41. : center(c)
  42. , material(mat)
  43. , texture_size(tx_sz)
  44. , object_size(sz)
  45. , bottom_left ({c.x - sz.width / 2.f, c.y - sz.height / 2.f})
  46. , bottom_right({c.x - sz.width / 2.f, c.y + sz.height / 2.f})
  47. , top_right ({c.x + sz.width / 2.f, c.y + sz.height / 2.f})
  48. , top_left ({c.x + sz.width / 2.f, c.y - sz.height / 2.f})
  49. {
  50. }
  51. RenderInfo() = default;
  52. bool operator==(const RenderInfo& other) const {
  53. return !memcmp(this, &other, sizeof(*this));
  54. }
  55. };
  56. DEFINE_ROTATE(RenderInfo, (material)(texture_size)(bottom_left)(bottom_right)(top_right)(top_left));
  57. DEFINE_ROTATE_NAME(simple_rotate, RenderInfo, (material)(texture_size)(center)(object_size));
  58. DEFINE_PROXY_NAME( RenderInfo_position_proxy, RenderInfo, (center)(object_size) );
  59. DEFINE_PROXY_CONVERSION( RenderInfo, (material)(texture_size)(center)(object_size), (center)(object_size) );
  60. class rotate_TestSuite : public CxxTest::TestSuite {
  61. public:
  62. void test_proxy1() const {
  63. RenderInfo_rotate rdata;
  64. RenderInfo info{{5, 6}, {1, 1}};
  65. rdata.push_back(info);
  66. RenderInfo_proxy proxy = rdata.get(0);
  67. TS_ASSERT_EQUALS(info.texture_size, proxy.texture_size);
  68. TS_ASSERT_EQUALS(info.bottom_left, proxy.bottom_left);
  69. TS_ASSERT_EQUALS(info.bottom_right, proxy.bottom_right);
  70. TS_ASSERT_EQUALS(info.top_left, proxy.top_left);
  71. TS_ASSERT_EQUALS(info.top_right, proxy.top_right);
  72. }
  73. void test_proxy2() const {
  74. simple_rotate rdata;
  75. RenderInfo info{{5, 6}, {1, 1}};
  76. rdata.push_back(info);
  77. simple_rotate_proxy proxy = rdata.get(0);
  78. TS_ASSERT_EQUALS(info.texture_size, proxy.texture_size);
  79. TS_ASSERT_EQUALS(info.center, proxy.center);
  80. TS_ASSERT_EQUALS(info.object_size, proxy.object_size);
  81. }
  82. void test_datamodel_center() const {
  83. simple_rotate rdata;
  84. RenderInfo infos[3] = {{{1,2},{1,1}},{{0,3},{1,1}},{{4,3.2f},{1,1}}};
  85. rdata.push_back(infos[0]);
  86. rdata.push_back(infos[1]);
  87. rdata.push_back(infos[2]);
  88. TS_ASSERT_EQUALS(rdata.get(access_t(RenderInfo, center){}), (std::vector<Point>{{1,2},{0,3},{4,3.2f}}));
  89. }
  90. void test_datamodel_multiproxy() const {
  91. simple_rotate rdata;
  92. RenderInfo info{{5, 6}, {1, 1}};
  93. rdata.push_back(info);
  94. RenderInfo_position_proxy proxy = rdata.getp<rotate_members(RenderInfo, (center)(object_size))>(0);
  95. TS_ASSERT_EQUALS( sizeof(proxy), sizeof(Point&)+sizeof(Dimension&) );
  96. TS_ASSERT_EQUALS(info.center, proxy.center);
  97. TS_ASSERT_EQUALS(info.object_size, proxy.object_size);
  98. }
  99. private:
  100. };