View.cpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
26 // Headers
28 #include <SFML/Graphics/View.hpp>
29 #include <algorithm>
30 
31 
32 namespace sf
33 {
37 View::View(const FloatRect& ViewRect)
38 {
39  SetFromRect(ViewRect);
40 }
41 
42 
46 View::View(const sf::Vector2f& Center, const sf::Vector2f& HalfSize) :
47 myCenter (Center),
48 myHalfSize (HalfSize),
49 myNeedUpdate(true)
50 {
51 
52 }
53 
54 
58 void View::SetCenter(float X, float Y)
59 {
60  myCenter.x = X;
61  myCenter.y = Y;
62  myNeedUpdate = true;
63 }
64 
65 
69 void View::SetCenter(const sf::Vector2f& Center)
70 {
71  SetCenter(Center.x, Center.y);
72 }
73 
74 
78 void View::SetHalfSize(float HalfWidth, float HalfHeight)
79 {
80  myHalfSize.x = HalfWidth;
81  myHalfSize.y = HalfHeight;
82  myNeedUpdate = true;
83 }
84 
85 
89 void View::SetHalfSize(const sf::Vector2f& HalfSize)
90 {
91  SetHalfSize(HalfSize.x, HalfSize.y);
92 }
93 
94 
98 void View::SetFromRect(const FloatRect& ViewRect)
99 {
100  SetCenter( (ViewRect.Right + ViewRect.Left) / 2, (ViewRect.Bottom + ViewRect.Top) / 2);
101  SetHalfSize((ViewRect.Right - ViewRect.Left) / 2, (ViewRect.Bottom - ViewRect.Top) / 2);
102 }
103 
104 
109 {
110  return myCenter;
111 }
112 
113 
118 {
119  return myHalfSize;
120 }
121 
122 
127 {
128  // Recompute it if needed
129  if (myNeedUpdate)
130  const_cast<View*>(this)->RecomputeMatrix();
131 
132  return myRect;
133 }
134 
135 
139 void View::Move(float OffsetX, float OffsetY)
140 {
141  myCenter.x += OffsetX;
142  myCenter.y += OffsetY;
143  myNeedUpdate = true;
144 }
145 
146 
150 void View::Move(const sf::Vector2f& Offset)
151 {
152  Move(Offset.x, Offset.y);
153 }
154 
155 
159 void View::Zoom(float Factor)
160 {
161  if (Factor != 0)
162  {
163  myHalfSize /= Factor;
164  myNeedUpdate = true;
165  }
166 }
167 
168 
172 const Matrix3& View::GetMatrix() const
173 {
174  // Recompute the matrix if needed
175  if (myNeedUpdate)
176  const_cast<View*>(this)->RecomputeMatrix();
177 
178  return myMatrix;
179 }
180 
181 
185 void View::RecomputeMatrix()
186 {
187  // Compute the 4 corners of the view
188  float Left = myCenter.x - myHalfSize.x;
189  float Top = myCenter.y - myHalfSize.y;
190  float Right = myCenter.x + myHalfSize.x;
191  float Bottom = myCenter.y + myHalfSize.y;
192 
193  // Update the view rectangle - be careful, reversed views are allowed !
194  myRect.Left = std::min(Left, Right);
195  myRect.Top = std::min(Top, Bottom);
196  myRect.Right = std::max(Left, Right);
197  myRect.Bottom = std::max(Top, Bottom);
198 
199  // Update the projection matrix
200  myMatrix(0, 0) = 2.f / (Right - Left);
201  myMatrix(1, 1) = 2.f / (Top - Bottom);
202  myMatrix(0, 2) = (Left + Right) / (Left - Right);
203  myMatrix(1, 2) = (Bottom + Top) / (Bottom - Top);
204 
205  myNeedUpdate = false;
206 }
207 
208 } // namespace sf
View(const FloatRect &ViewRect=FloatRect(0, 0, 1000, 1000))
Construct the view from a rectangle.
Definition: View.cpp:37
This class defines a view (position, size, etc.) ; you can consider it as a 2D camera.
Definition: View.hpp:45
T x
X coordinate of the vector.
Definition: Vector2.hpp:59
const sf::Vector2f & GetCenter() const
Get the center of the view.
Definition: View.cpp:108
T Right
Right coordinate of the rectangle.
Definition: Rect.hpp:114
void SetCenter(float X, float Y)
Change the center of the view (take 2 values)
Definition: View.cpp:58
void SetFromRect(const FloatRect &ViewRect)
Rebuild the view from a rectangle.
Definition: View.cpp:98
const sf::Vector2f & GetHalfSize() const
Get the half-size of the view.
Definition: View.cpp:117
void Move(float OffsetX, float OffsetY)
Move the view (take 2 values)
Definition: View.cpp:139
Utility class to manipulate 3x3 matrices representing 2D transformations.
Definition: Matrix3.hpp:42
void Zoom(float Factor)
Resize the view rectangle to simulate a zoom / unzoom effect.
Definition: View.cpp:159
const sf::FloatRect & GetRect() const
Get the bounding rectangle of the view.
Definition: View.cpp:126
void SetHalfSize(float HalfWidth, float HalfHeight)
Change the half-size of the view (take 2 values)
Definition: View.cpp:78
T Bottom
Bottom coordinate of the rectangle.
Definition: Rect.hpp:115
T y
Y coordinate of the vector.
Definition: Vector2.hpp:60
T Top
Top coordinate of the rectangle.
Definition: Rect.hpp:113
T Left
Left coordinate of the rectangle.
Definition: Rect.hpp:112