Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Views.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * 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
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_VIEWS_H
22 #define GF_VIEWS_H
23 
24 #include "Portability.h"
25 #include "View.h"
26 
27 namespace gf {
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 inline namespace v1 {
30 #endif
31 
32  /**
33  * @ingroup graphics
34  * @brief Stretch view
35  *
36  * This view assumes that the screen is always the same size as the world.
37  * The world will then be stretched to fit the screen. There are no black
38  * bars, but the aspect ratio may not be the same after the scaling took
39  * place.
40  *
41  * ![Stretch view](@ref stretchview.png)
42  *
43  * @sa gf::AdaptativeView
44  */
46  public:
47  /**
48  * @brief Default constructor
49  *
50  * This constructor creates a default view of @f$(0, 0, 1000, 1000)@f$.
51  */
54  {
55 
56  }
57 
58  /**
59  * @brief Construct the view from a rectangle
60  *
61  * @param rect Rectangle defining the zone to display
62  */
63  explicit StretchView(const RectF& rect)
64  : AdaptativeView(rect)
65  {
66 
67  }
68 
69  /**
70  * @brief Construct the view from its center and size
71  *
72  * @param center Center of the zone to display
73  * @param size Size of the zone to display
74  */
75  StretchView(Vector2f center, Vector2f size)
76  : AdaptativeView(center, size)
77  {
78 
79  }
80 
81  virtual void onScreenResize(Vector2u screenSize) override;
82  };
83 
84  /**
85  * @ingroup graphics
86  * @brief Fit view
87  *
88  * This view will always maintain the aspect ratio of the world, while
89  * scaling it as much as possible to fit the screen. One disadvantage
90  * with this strategy is that there may appear black bars.
91  *
92  * ![Fit view](@ref fitview.png)
93  *
94  * @sa gf::AdaptativeView
95  */
96  class GF_API FitView : public AdaptativeView {
97  public:
98  /**
99  * @brief Default constructor
100  *
101  * This constructor creates a default view of @f$(0, 0, 1000, 1000)@f$.
102  */
105  {
106 
107  }
108 
109  /**
110  * @brief Construct the view from a rectangle
111  *
112  * @param rect Rectangle defining the zone to display
113  */
114  explicit FitView(const RectF& rect)
115  : AdaptativeView(rect)
116  {
117 
118  }
119 
120  /**
121  * @brief Construct the view from its center and size
122  *
123  * @param center Center of the zone to display
124  * @param size Size of the zone to display
125  */
126  FitView(Vector2f center, Vector2f size)
127  : AdaptativeView(center, size)
128  {
129 
130  }
131 
132  virtual void onScreenResize(Vector2u screenSize) override;
133  };
134 
135  /**
136  * @ingroup graphics
137  * @brief Fill view
138  *
139  * This view keeps the aspect ratio of the world, but it will always fill
140  * the whole screen which might result in parts of the world being cut off.
141  *
142  * ![Fill view](@ref fillview.png)
143  *
144  * @sa gf::AdaptativeView
145  */
146  class GF_API FillView : public AdaptativeView {
147  public:
148  /**
149  * @brief Default constructor
150  *
151  * This constructor creates a default view of @f$(0, 0, 1000, 1000)@f$.
152  */
155  {
156 
157  }
158 
159  /**
160  * @brief Construct the view from a rectangle
161  *
162  * @param rect Rectangle defining the zone to display
163  */
164  explicit FillView(const RectF& rect)
165  : AdaptativeView(rect)
166  {
167  resizeWorld(getSize());
168  }
169 
170  /**
171  * @brief Construct the view from its center and size
172  *
173  * @param center Center of the zone to display
174  * @param size Size of the zone to display
175  */
176  FillView(Vector2f center, Vector2f size)
177  : AdaptativeView(center, size)
178  {
179  resizeWorld(getSize());
180  }
181 
182  virtual void onScreenResize(Vector2u screenSize) override;
183 
184  protected:
185  virtual void onWorldResize(Vector2f worldSize) override;
186 
187  private:
188  void resizeWorld(Vector2f worldSize);
189 
190  private:
191  Vector2f m_worldSize;
192  };
193 
194  /**
195  * @ingroup graphics
196  * @brief Extend view
197  *
198  * This view keeps the world aspect ratio without black bars by extending
199  * the world in one direction. The world is first scaled to fit within the
200  * viewport, then the shorter dimension is lengthened to fill the viewport.
201  *
202  * ![Extend view](@ref extendview.png)
203  *
204  * @sa gf::AdaptativeView
205  */
207  public:
208  /**
209  * @brief Default constructor
210  *
211  * This constructor creates a default view of @f$(0, 0, 1000, 1000)@f$.
212  */
215  {
216 
217  }
218 
219  /**
220  * @brief Construct the view from a rectangle
221  *
222  * @param rect Rectangle defining the zone to display
223  */
224  explicit ExtendView(const RectF& rect)
225  : AdaptativeView(rect)
226  {
227  resizeWorld(getSize());
228  }
229 
230  /**
231  * @brief Construct the view from its center and size
232  *
233  * @param center Center of the zone to display
234  * @param size Size of the zone to display
235  */
236  ExtendView(Vector2f center, Vector2f size)
237  : AdaptativeView(center, size)
238  {
239  resizeWorld(getSize());
240  }
241 
242  virtual void onScreenResize(Vector2u screenSize) override;
243 
244  protected:
245  virtual void onWorldResize(Vector2f worldSize) override;
246 
247  private:
248  void resizeWorld(Vector2f worldSize);
249 
250  private:
251  Vector2f m_worldSize;
252  };
253 
254  /**
255  * @ingroup graphics
256  * @brief Screen view
257  *
258  * This view will always match the window size which means that no scaling
259  * happens and no black bars appear. As a disadvantage this means that the
260  * gameplay might change, because a player with a bigger screen might see
261  * more of the game, than a player with a smaller screen size.
262  *
263  * This view can be used to display [HUD](https://en.wikipedia.org/wiki/HUD_%28video_gaming%29).
264  *
265  * ![Screen view](@ref screenview.png)
266  *
267  * @sa gf::AdaptativeView
268  */
270  public:
271  virtual void onScreenResize(Vector2u screenSize) override;
272  };
273 
274 #ifndef DOXYGEN_SHOULD_SKIP_THIS
275 }
276 #endif
277 }
278 
279 #endif // GF_VIEWS_H
FitView(Vector2f center, Vector2f size)
Construct the view from its center and size.
Definition: Views.h:126
FitView()
Default constructor.
Definition: Views.h:103
FillView(Vector2f center, Vector2f size)
Construct the view from its center and size.
Definition: Views.h:176
virtual void onScreenResize(Vector2u screenSize) override
Callback when the screen has just been resized.
AdaptativeView(const RectF &rect)
Construct the view from a rectangle.
Definition: View.h:363
Screen view.
Definition: Views.h:269
Fit view.
Definition: Views.h:96
StretchView()
Default constructor.
Definition: Views.h:52
FitView(const RectF &rect)
Construct the view from a rectangle.
Definition: Views.h:114
StretchView(const RectF &rect)
Construct the view from a rectangle.
Definition: Views.h:63
ExtendView()
Default constructor.
Definition: Views.h:213
Fill view.
Definition: Views.h:146
virtual void onWorldResize(Vector2f worldSize) override
Callback when the world has just been resized.
ExtendView(Vector2f center, Vector2f size)
Construct the view from its center and size.
Definition: Views.h:236
Definition: Action.h:34
ExtendView(const RectF &rect)
Construct the view from a rectangle.
Definition: Views.h:224
Extend view.
Definition: Views.h:206
Vector2f getSize() const
Get the size of the view.
Definition: View.h:162
virtual void onScreenResize(Vector2u screenSize) override
Callback when the screen has just been resized.
AdaptativeView()
Default constructor.
Definition: View.h:352
virtual void onWorldResize(Vector2f worldSize) override
Callback when the world has just been resized.
FillView(const RectF &rect)
Construct the view from a rectangle.
Definition: Views.h:164
virtual void onScreenResize(Vector2u screenSize) override
Callback when the screen has just been resized.
#define GF_API
Definition: Portability.h:35
Adaptative view.
Definition: View.h:345
StretchView(Vector2f center, Vector2f size)
Construct the view from its center and size.
Definition: Views.h:75
Stretch view.
Definition: Views.h:45
FillView()
Default constructor.
Definition: Views.h:153
virtual void onScreenResize(Vector2u screenSize) override
Callback when the screen has just been resized.
AdaptativeView(Vector2f center, Vector2f size)
Construct the view from its center and size.
Definition: View.h:375
virtual void onScreenResize(Vector2u screenSize) override
Callback when the screen has just been resized.