vector2d.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _VECTOR2D_H
  23. #define _VECTOR2D_H
  24. #ifndef _VECTOR_H_
  25. #include "vector.h"
  26. #endif
  27. //-------------------------------------------------------------------------------------
  28. // Adds accessors for using vector as a 2d structure
  29. // Vector2d class inheriting from vector
  30. //-------------------------------------------------------------------------------------
  31. template <class T>
  32. class Vector2d : public Vector<T>
  33. {
  34. protected:
  35. U32 mWidth;
  36. U32 mHeight;
  37. using Vector<T>::mArray;
  38. using Vector<T>::mElementCount;
  39. using Vector<T>::mArraySize;
  40. using Vector<T>::reserve;
  41. public:
  42. Vector2d( const U32 initialWidth = 0, const U32 initialHeight = 0 )
  43. {
  44. mArray = 0;
  45. mElementCount = 0;
  46. mArraySize = 0;
  47. mWidth = initialWidth;
  48. mHeight = initialHeight;
  49. if(initialWidth && initialHeight)
  50. reserve( initialWidth * initialHeight );
  51. }
  52. //---------------------------------------------------------
  53. U32 width()
  54. {
  55. return mWidth;
  56. }
  57. //---------------------------------------------------------
  58. U32 height()
  59. {
  60. return mHeight;
  61. }
  62. //---------------------------------------------------------
  63. bool resize( const U32 width, const U32 height )
  64. {
  65. reserve( width * height );
  66. mWidth = width;
  67. mHeight = height;
  68. return true;
  69. }
  70. //---------------------------------------------------------
  71. T& get( const U32 indexX, const U32 indexY )
  72. {
  73. U32 index = ( indexY * mWidth ) + indexX;
  74. return mArray[index];
  75. }
  76. //---------------------------------------------------------
  77. T& get( const Vector2 v )
  78. {
  79. U32 index = ( v.mY * mWidth ) + v.mX;
  80. return mArray[index];
  81. }
  82. };
  83. #endif