types.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. //========================================================================
  3. // types.h : Defines some basic types, replacing the need for atltypes.h
  4. // or project specific include files that are used frequently, but
  5. // are changed infrequently
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. #include <math.h>
  40. //---------------------------------------------------------------------------------------------------------------------
  41. // This class represents a single point in 2D space
  42. //---------------------------------------------------------------------------------------------------------------------
  43. class Point
  44. {
  45. public:
  46. long x, y;
  47. // construction
  48. Point(void) { x = y = 0; }
  49. Point(const long newX, const long newY) { x = newX; y = newY; }
  50. Point(const Point& newPoint) { x = newPoint.x; y = newPoint.y; }
  51. Point(const Point* pNewPoint) { x = pNewPoint->x; y = pNewPoint->y; }
  52. // assignment
  53. Point& operator=(const Point& newPoint) { x = newPoint.x; y = newPoint.y; return (*this); }
  54. Point& operator=(const Point* newPoint) { x = newPoint->x; y = newPoint->y; return (*this); }
  55. // addition/subtraction
  56. Point& operator+=(const Point& newPoint) { x += newPoint.x; y += newPoint.y; return (*this); }
  57. Point& operator-=(const Point& newPoint) { x -= newPoint.x; y -= newPoint.y; return (*this); }
  58. Point& operator+=(const Point* newPoint) { x += newPoint->x; y += newPoint->y; return (*this); }
  59. Point& operator-=(const Point* newPoint) { x -= newPoint->x; y -= newPoint->y; return (*this); }
  60. Point& operator+(const Point& other) { Point temp(this); temp += other; temp += other; return (*this); }
  61. Point& operator-(const Point& other) { Point temp(this); temp -= other; temp -= other; return (*this); }
  62. // comparison
  63. bool operator==(const Point& other) const { return ((x == other.x) && (y == other.y)); }
  64. bool operator!=(const Point& other) const { return ((x != other.x) || (y != other.y)); }
  65. // accessors (needed for Lua)
  66. long GetX() const { return x; }
  67. long GetY() const { return y; }
  68. void SetX(const long newX) { x = newX; }
  69. void SetY(const long newY) { y = newY; }
  70. void Set(const long newX, const long newY) { x = newX; y = newY; }
  71. float Length() const { return sqrt((float)(x*x+y*y)); }
  72. };
  73. inline Point operator-(const Point& left, const Point& right) { Point temp(left); temp -= right; return temp; }