tile_brush.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <ClanLib/Core/Math/rect.h>
  17. #include "tile_brush.hpp"
  18. TileBrush::TileBrush()
  19. {
  20. opaque = false;
  21. }
  22. TileBrush::TileBrush(const Field<int>& f, int w, int h, int pos_x, int pos_y)
  23. : data(f, w, h, pos_x, pos_y)
  24. {
  25. opaque = false;
  26. }
  27. TileBrush::TileBrush(int w, int h)
  28. : data(w, h)
  29. {
  30. opaque = false;
  31. }
  32. void
  33. TileBrush::auto_crop()
  34. {
  35. CL_Rect rect(CL_Point(0, 0), CL_Size(0, 0));
  36. for(int y = 0; y < get_height(); ++y)
  37. for(int x = 0; x < get_width(); ++x)
  38. if (at(x, y) != 0)
  39. {
  40. rect.top = y;
  41. goto bottom;
  42. }
  43. bottom:
  44. for(int y = get_height()-1; y >= 0; --y)
  45. for(int x = 0; x < get_width(); ++x)
  46. if (at(x, y) != 0)
  47. {
  48. rect.bottom = y + 1;
  49. goto left;
  50. }
  51. left:
  52. for(int x = 0; x < get_width(); ++x)
  53. for(int y = 0; y < get_height(); ++y)
  54. if (at(x, y) != 0)
  55. {
  56. rect.left = x;
  57. goto right;
  58. }
  59. right:
  60. for(int x = get_width() - 1; x >= 0; --x)
  61. for(int y = 0; y < get_height(); ++y)
  62. if (at(x, y) != 0)
  63. {
  64. rect.right = x + 1;
  65. goto end;
  66. }
  67. end:
  68. if (rect.get_width() != 0)
  69. resize(rect.get_width(), rect.get_height(),
  70. -rect.left, -rect.top);
  71. else
  72. {
  73. (*this) = TileBrush(1, 1);
  74. at(0, 0) = 0;
  75. set_opaque();
  76. }
  77. }
  78. void
  79. TileBrush::set_data(const std::vector<int>& data_)
  80. {
  81. return data.set_data(data_);
  82. }
  83. std::vector<int>
  84. TileBrush::get_data()
  85. {
  86. return data.get_data();
  87. }
  88. /* EOF */