field.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2000 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. #ifndef HEADER_FLEXLAY_FIELD_HPP
  17. #define HEADER_FLEXLAY_FIELD_HPP
  18. #include <vector>
  19. #include <assert.h>
  20. template<class T>
  21. class Field
  22. {
  23. private:
  24. int width;
  25. int height;
  26. std::vector<T> vec;
  27. public:
  28. typedef typename std::vector<T>::iterator iterator;
  29. Field()
  30. : width(0), height(0)
  31. {
  32. }
  33. Field (int w, int h)
  34. : width (w), height (h), vec (width * height)
  35. {
  36. }
  37. Field(const Field<T>& copy)
  38. : width(copy.width), height(copy.height), vec(copy.vec)
  39. {
  40. }
  41. /** Creates a new field out of a subsection from an already excisting one
  42. * @param pos_x The position of the old field in the new resized one
  43. * @param pos_y The position of the old field in the new resized one */
  44. Field(const Field<T>& arg_field, int w, int h, int pos_x, int pos_y)
  45. : width (w), height (h), vec (width * height)
  46. {
  47. int start_x = std::max(0, -pos_x);
  48. int start_y = std::max(0, -pos_y);
  49. int end_x = std::min(arg_field.get_width(), get_width() - pos_x);
  50. int end_y = std::min(arg_field.get_height(), get_height() - pos_y);
  51. for(int y = start_y; y < end_y; ++y)
  52. for(int x = start_x; x < end_x; ++x)
  53. at(pos_x + x, pos_y + y) = arg_field.at(x, y);
  54. }
  55. Field<T>& operator=(const Field<T>& copy)
  56. {
  57. if (this != &copy)
  58. {
  59. width = copy.width;
  60. height = copy.height;
  61. vec = copy.vec;
  62. }
  63. return *this;
  64. }
  65. const T& operator[] (int i) const {
  66. return vec[i];
  67. }
  68. T& operator[] (int i) {
  69. return vec[i];
  70. }
  71. T& operator() (int x, int y)
  72. {
  73. assert (x >= 0 || x < (int) width || y >= 0 || y < (int) height);
  74. return vec [width*y + x];
  75. }
  76. const T& operator() (int x, int y) const
  77. {
  78. assert (x >= 0 || x < (int) width || y >= 0 || y < (int) height);
  79. return vec [width*y + x];
  80. }
  81. inline const T& at (int x, int y) const {
  82. return vec [width*y + x];
  83. }
  84. inline T& at (int x, int y) {
  85. return (*this) (x, y);
  86. }
  87. /** Resize a field to a new size
  88. * @param pos_x The position of the old field in the new resized one
  89. * @param pos_y The position of the old field in the new resized one
  90. **/
  91. void resize(int w, int h, int pos_x = 0, int pos_y = 0)
  92. {
  93. *this = Field<T>(*this, w, h, pos_x, pos_y);
  94. }
  95. void clear()
  96. {
  97. width = 0;
  98. height = 0;
  99. vec.clear();
  100. }
  101. std::vector<T>& get_data() { return vec; }
  102. void set_data(const std::vector<T>& d) {
  103. for(typename std::vector<T>::size_type i = 0; i < vec.size() && i < d.size(); ++i)
  104. vec[i] = d[i];
  105. }
  106. iterator begin () { return vec.begin (); }
  107. iterator end () { return vec.end (); }
  108. int size() const { return vec.size(); }
  109. int get_width () const { return width; }
  110. int get_height () const { return height; }
  111. };
  112. #endif
  113. /* EOF */