objmap_sprite_object.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "objmap_sprite_object.hpp"
  17. #include "objmap_object_impl.hpp"
  18. class ObjMapSpriteObjectImpl : public ObjMapObjectImpl
  19. {
  20. public:
  21. CL_Sprite sprite;
  22. void draw(CL_GraphicContext* gc);
  23. CL_Rectf get_bound_rect() const;
  24. ObjMapObject* duplicate(int handle_);
  25. };
  26. ObjMapSpriteObject::ObjMapSpriteObject()
  27. {
  28. }
  29. ObjMapSpriteObject::ObjMapSpriteObject(const CL_Sprite& sprite_,
  30. const CL_Pointf& pos_,
  31. const MetaData& data_)
  32. : impl(new ObjMapSpriteObjectImpl())
  33. {
  34. impl->pos = pos_;
  35. impl->data = data_;
  36. impl->sprite = sprite_;
  37. }
  38. void
  39. ObjMapSpriteObjectImpl::draw(CL_GraphicContext* gc)
  40. {
  41. sprite.draw(pos.x, pos.y, gc);
  42. }
  43. CL_Rectf
  44. ObjMapSpriteObjectImpl::get_bound_rect() const
  45. {
  46. CL_Point align = CL_Point(0, 0);
  47. CL_Origin origin_e;
  48. sprite.get_alignment(origin_e, align.x, align.y);
  49. CL_Point origin = calc_origin(origin_e, CL_Size(sprite.get_width(),
  50. sprite.get_height()));
  51. align.x = -align.x;
  52. // FIXME: This looks a bit hacky
  53. float scale_x, scale_y;
  54. sprite.get_scale(scale_x, scale_y);
  55. if (scale_x < 0)
  56. align.x += sprite.get_width();
  57. if (scale_y < 0)
  58. align.y += sprite.get_height();
  59. // if (scale_x > 1.0f && scale_y > 1.0f)
  60. // return CL_Rectf(pos - origin - align,
  61. // CL_Sizef(sprite.get_width() * scale_x, sprite.get_height() * scale_y));
  62. // else
  63. return CL_Rectf(pos - origin - align,
  64. CL_Sizef(sprite.get_width(), sprite.get_height()));
  65. }
  66. void
  67. ObjMapSpriteObject::flip_vertical()
  68. {
  69. float scale_x, scale_y;
  70. impl->sprite.get_scale(scale_x, scale_y);
  71. impl->sprite.set_scale(scale_x, -scale_y);
  72. if (scale_y < 0)
  73. impl->pos.y -= impl->sprite.get_height();
  74. else
  75. impl->pos.y += impl->sprite.get_height();
  76. }
  77. void
  78. ObjMapSpriteObject::flip_horizontal()
  79. {
  80. float scale_x, scale_y;
  81. impl->sprite.get_scale(scale_x, scale_y);
  82. impl->sprite.set_scale(-scale_x, scale_y);
  83. if (scale_x < 0)
  84. impl->pos.x -= impl->sprite.get_width();
  85. else
  86. impl->pos.x += impl->sprite.get_width();
  87. }
  88. void
  89. ObjMapSpriteObject::set_sprite(const CL_Sprite& sprite)
  90. {
  91. impl->sprite = sprite;
  92. }
  93. void
  94. ObjMapSpriteObject::set_rotate(float angle)
  95. {
  96. impl->sprite.set_angle(angle);
  97. }
  98. ObjMapObject
  99. ObjMapSpriteObject::to_object()
  100. {
  101. return ObjMapObject(std::shared_ptr<ObjMapObjectImpl>(impl));
  102. }
  103. /* EOF */