Incremental Engine  1.0.6
A 2D Game Engine to create Idle Games
IncrementalEngine::Transform Class Reference

#include <Transform.h>

Public Member Functions

 Transform ()
 
 Transform (float a00, float a01, float a02, float a10, float a11, float a12, float a20, float a21, float a22)
 
const float * GetMatrix () const
 
Transform GetInverse () const
 
D3DXVECTOR2 TransformPoint (float x, float y) const
 
D3DXVECTOR2 TransformPoint (const D3DXVECTOR2 &point) const
 
FloatRect TransformRect (const FloatRect &rectangle) const
 
TransformCombine (const Transform &transform)
 
TransformTranslate (float x, float y)
 
TransformTranslate (const D3DXVECTOR2 &offset)
 
TransformRotate (float angle)
 
TransformRotate (float angle, float centerX, float centerY)
 
TransformRotate (float angle, const D3DXVECTOR2 &center)
 
TransformScale (float scaleX, float scaleY)
 
TransformScale (float scaleX, float scaleY, float centerX, float centerY)
 
TransformScale (const D3DXVECTOR2 &factors)
 
TransformScale (const D3DXVECTOR2 &factors, const D3DXVECTOR2 &center)
 

Static Public Attributes

static const Transform Identity
 

Private Attributes

float _matrix [16]
 

Detailed Description

Definition at line 41 of file Transform.h.

Constructor & Destructor Documentation

◆ Transform() [1/2]

IncrementalEngine::Transform::Transform ( )

Definition at line 36 of file Transform.cpp.

37  {
38  _matrix[0] = 1.f; _matrix[4] = 0.f; _matrix[8] = 0.f; _matrix[12] = 0.f;
39  _matrix[1] = 0.f; _matrix[5] = 1.f; _matrix[9] = 0.f; _matrix[13] = 0.f;
40  _matrix[2] = 0.f; _matrix[6] = 0.f; _matrix[10] = 1.f; _matrix[14] = 0.f;
41  _matrix[3] = 0.f; _matrix[7] = 0.f; _matrix[11] = 0.f; _matrix[15] = 1.f;
42  }

◆ Transform() [2/2]

IncrementalEngine::Transform::Transform ( float  a00,
float  a01,
float  a02,
float  a10,
float  a11,
float  a12,
float  a20,
float  a21,
float  a22 
)

Definition at line 44 of file Transform.cpp.

49  {
50  _matrix[0] = a00; _matrix[4] = a01; _matrix[8] = 0.f; _matrix[12] = a02;
51  _matrix[1] = a10; _matrix[5] = a11; _matrix[9] = 0.f; _matrix[13] = a12;
52  _matrix[2] = 0.f; _matrix[6] = 0.f; _matrix[10] = 1.f; _matrix[14] = 0.f;
53  _matrix[3] = a20; _matrix[7] = a21; _matrix[11] = 0.f; _matrix[15] = a22;
54  }

Member Function Documentation

◆ Combine()

Transform & IncrementalEngine::Transform::Combine ( const Transform transform)

Definition at line 125 of file Transform.cpp.

126  {
127  const float* a = _matrix;
128  const float* b = transform._matrix;
129 
130  *this = Transform(a[0] * b[0] + a[4] * b[1] + a[12] * b[3],
131  a[0] * b[4] + a[4] * b[5] + a[12] * b[7],
132  a[0] * b[12] + a[4] * b[13] + a[12] * b[15],
133  a[1] * b[0] + a[5] * b[1] + a[13] * b[3],
134  a[1] * b[4] + a[5] * b[5] + a[13] * b[7],
135  a[1] * b[12] + a[5] * b[13] + a[13] * b[15],
136  a[3] * b[0] + a[7] * b[1] + a[15] * b[3],
137  a[3] * b[4] + a[7] * b[5] + a[15] * b[7],
138  a[3] * b[12] + a[7] * b[13] + a[15] * b[15]);
139 
140  return *this;
141  }

◆ GetInverse()

Transform IncrementalEngine::Transform::GetInverse ( ) const

Definition at line 61 of file Transform.cpp.

62  {
63  float det = _matrix[0] * (_matrix[15] * _matrix[5] - _matrix[7] * _matrix[13]) -
64  _matrix[1] * (_matrix[15] * _matrix[4] - _matrix[7] * _matrix[12]) +
65  _matrix[3] * (_matrix[13] * _matrix[4] - _matrix[5] * _matrix[12]);
66 
67  if (det != 0.f)
68  {
69  return Transform( (_matrix[15] * _matrix[5] - _matrix[7] * _matrix[13]) / det,
70  -(_matrix[15] * _matrix[4] - _matrix[7] * _matrix[12]) / det,
71  (_matrix[13] * _matrix[4] - _matrix[5] * _matrix[12]) / det,
72  -(_matrix[15] * _matrix[1] - _matrix[3] * _matrix[13]) / det,
73  (_matrix[15] * _matrix[0] - _matrix[3] * _matrix[12]) / det,
74  -(_matrix[13] * _matrix[0] - _matrix[1] * _matrix[12]) / det,
75  (_matrix[7] * _matrix[1] - _matrix[3] * _matrix[5]) / det,
76  -(_matrix[7] * _matrix[0] - _matrix[3] * _matrix[4]) / det,
77  (_matrix[5] * _matrix[0] - _matrix[1] * _matrix[4]) / det
78  );
79  }
80  else
81  {
82  return Identity;
83  }
84  }

◆ GetMatrix()

const float * IncrementalEngine::Transform::GetMatrix ( ) const

Definition at line 56 of file Transform.cpp.

57  {
58  return _matrix;
59  }

◆ Rotate() [1/3]

Transform & IncrementalEngine::Transform::Rotate ( float  angle)

Definition at line 157 of file Transform.cpp.

158  {
159  float rad = angle * 3.141592654f / 180.f;
160  float cos = std::cos(rad);
161  float sin = std::sin(rad);
162 
163  Transform rotation(cos, -sin, 0,
164  sin, cos, 0,
165  0, 0, 1);
166 
167  return Combine(rotation);
168  }

◆ Rotate() [2/3]

Transform & IncrementalEngine::Transform::Rotate ( float  angle,
const D3DXVECTOR2 &  center 
)

Definition at line 183 of file Transform.cpp.

184  {
185  return Rotate(angle, center.x, center.y);
186  }

◆ Rotate() [3/3]

Transform & IncrementalEngine::Transform::Rotate ( float  angle,
float  centerX,
float  centerY 
)

Definition at line 170 of file Transform.cpp.

171  {
172  float rad = angle * 3.141592654f / 180.f;
173  float cos = std::cos(rad);
174  float sin = std::sin(rad);
175 
176  Transform rotation(cos, -sin, centerX * (1 - cos) + centerY * sin,
177  sin, cos, centerY * (1 - cos) - centerX * sin,
178  0, 0, 1);
179 
180  return Combine(rotation);
181  }

◆ Scale() [1/4]

Transform & IncrementalEngine::Transform::Scale ( const D3DXVECTOR2 &  factors)

Definition at line 206 of file Transform.cpp.

207  {
208  return Scale(factors.x, factors.y);
209  }

◆ Scale() [2/4]

Transform & IncrementalEngine::Transform::Scale ( const D3DXVECTOR2 &  factors,
const D3DXVECTOR2 &  center 
)

Definition at line 211 of file Transform.cpp.

212  {
213  return Scale(factors.x, factors.y, center.x, center.y);
214  }

◆ Scale() [3/4]

Transform & IncrementalEngine::Transform::Scale ( float  scaleX,
float  scaleY 
)

Definition at line 188 of file Transform.cpp.

189  {
190  Transform scaling(scaleX, 0, 0,
191  0, scaleY, 0,
192  0, 0, 1);
193 
194  return Combine(scaling);
195  }

◆ Scale() [4/4]

Transform & IncrementalEngine::Transform::Scale ( float  scaleX,
float  scaleY,
float  centerX,
float  centerY 
)

Definition at line 197 of file Transform.cpp.

198  {
199  Transform scaling(scaleX, 0, centerX * (1 - scaleX),
200  0, scaleY, centerY * (1 - scaleY),
201  0, 0, 1);
202 
203  return Combine(scaling);
204  }

◆ TransformPoint() [1/2]

D3DXVECTOR2 IncrementalEngine::Transform::TransformPoint ( const D3DXVECTOR2 &  point) const

Definition at line 94 of file Transform.cpp.

95  {
96  return TransformPoint(point.x, point.y);
97  }

◆ TransformPoint() [2/2]

D3DXVECTOR2 IncrementalEngine::Transform::TransformPoint ( float  x,
float  y 
) const

Definition at line 86 of file Transform.cpp.

87  {
88  return D3DXVECTOR2(
89  _matrix[0] * x + _matrix[4] * y + _matrix[12],
90  _matrix[1] * x + _matrix[5] * y + _matrix[13]
91  );
92  }

◆ TransformRect()

FloatRect IncrementalEngine::Transform::TransformRect ( const FloatRect rectangle) const

Definition at line 99 of file Transform.cpp.

100  {
101  const D3DXVECTOR2 points[] =
102  {
103  TransformPoint(rectangle.left(), rectangle.top()),
104  TransformPoint(rectangle.left(), rectangle.top() + rectangle.height()),
105  TransformPoint(rectangle.left() + rectangle.width(), rectangle.top()),
106  TransformPoint(rectangle.left() + rectangle.width(), rectangle.top() + rectangle.height())
107  };
108 
109  float left = points[0].x;
110  float top = points[0].y;
111  float right = points[0].x;
112  float bottom = points[0].y;
113 
114  for (int i = 1; i < 4; ++i)
115  {
116  if (points[i].x < left) left = points[i].x;
117  else if (points[i].x > right) right = points[i].x;
118  if (points[i].y < top) top = points[i].y;
119  else if (points[i].y > bottom) bottom = points[i].y;
120  }
121 
122  return FloatRect(left, top, right - left, bottom - top);
123  }

◆ Translate() [1/2]

Transform & IncrementalEngine::Transform::Translate ( const D3DXVECTOR2 &  offset)

Definition at line 152 of file Transform.cpp.

153  {
154  return Translate(offset.x, offset.y);
155  }

◆ Translate() [2/2]

Transform & IncrementalEngine::Transform::Translate ( float  x,
float  y 
)

Definition at line 143 of file Transform.cpp.

144  {
145  Transform translation(1, 0, x,
146  0, 1, y,
147  0, 0, 1);
148 
149  return Combine(translation);
150  }

Member Data Documentation

◆ _matrix

float IncrementalEngine::Transform::_matrix[16]
private

Definition at line 76 of file Transform.h.

◆ Identity

const Transform IncrementalEngine::Transform::Identity
static

Definition at line 44 of file Transform.h.

IncrementalEngine::Transform::Transform
Transform()
Definition: Transform.cpp:36
IncrementalEngine::Transform::Translate
Transform & Translate(float x, float y)
Definition: Transform.cpp:143
IncrementalEngine::Transform::_matrix
float _matrix[16]
Definition: Transform.h:76
IncrementalEngine::Transform::Identity
static const Transform Identity
Definition: Transform.h:44
IncrementalEngine::Transform::Rotate
Transform & Rotate(float angle)
Definition: Transform.cpp:157
IncrementalEngine::Transform::Scale
Transform & Scale(float scaleX, float scaleY)
Definition: Transform.cpp:188
IncrementalEngine::Transform::TransformPoint
D3DXVECTOR2 TransformPoint(float x, float y) const
Definition: Transform.cpp:86
IncrementalEngine::Transform::Combine
Transform & Combine(const Transform &transform)
Definition: Transform.cpp:125