Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
Views.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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)
77  {
78 
79  }
80 
81  virtual void onScreenSizeChange(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  , m_localViewport(0.0f, 0.0f, 1.0f, 1.0f)
106  {
107 
108  }
109 
110  /**
111  * @brief Construct the view from a rectangle
112  *
113  * @param rect Rectangle defining the zone to display
114  */
115  explicit FitView(const RectF& rect)
116  : AdaptativeView(rect)
117  , m_localViewport(0.0f, 0.0f, 1.0f, 1.0f)
118  {
119 
120  }
121 
122  /**
123  * @brief Construct the view from its center and size
124  *
125  * @param center Center of the zone to display
126  * @param size Size of the zone to display
127  */
128  FitView(Vector2f center, Vector2f size)
130  , m_localViewport(0.0f, 0.0f, 1.0f, 1.0f)
131  {
132 
133  }
134 
135  virtual void onScreenSizeChange(Vector2u screenSize) override;
136 
137  protected:
138  virtual void onSizeChange(Vector2f size) override;
139  virtual void onViewportChange(const RectF& viewport) override;
140 
141  void updateView();
142 
143  private:
144  Vector2u m_localScreenSize;
145  RectF m_localViewport;
146  };
147 
148  /**
149  * @ingroup graphics
150  * @brief Fill view
151  *
152  * This view keeps the aspect ratio of the world, but it will always fill
153  * the whole screen which might result in parts of the world being cut off.
154  *
155  * ![Fill view](@ref fillview.png)
156  *
157  * @sa gf::AdaptativeView
158  */
159  class GF_API FillView : public AdaptativeView {
160  public:
161  /**
162  * @brief Default constructor
163  *
164  * This constructor creates a default view of @f$(0, 0, 1000, 1000)@f$.
165  */
168  {
169 
170  }
171 
172  /**
173  * @brief Construct the view from a rectangle
174  *
175  * @param rect Rectangle defining the zone to display
176  */
177  explicit FillView(const RectF& rect)
178  : AdaptativeView(rect)
180  {
181 
182  }
183 
184  /**
185  * @brief Construct the view from its center and size
186  *
187  * @param center Center of the zone to display
188  * @param size Size of the zone to display
189  */
190  FillView(Vector2f center, Vector2f size)
192  , m_localSize(size)
193  {
194 
195  }
196 
197  virtual void onScreenSizeChange(Vector2u screenSize) override;
198 
199  protected:
200  virtual void onSizeChange(Vector2f size) override;
201  virtual void onViewportChange(const RectF& viewport) override;
202 
203  void updateView();
204 
205  private:
206  Vector2f m_localSize;
207  Vector2u m_localScreenSize;
208  };
209 
210  /**
211  * @ingroup graphics
212  * @brief Extend view
213  *
214  * This view keeps the world aspect ratio without black bars by extending
215  * the world in one direction. The world is first scaled to fit within the
216  * viewport, then the shorter dimension is lengthened to fill the viewport.
217  *
218  * ![Extend view](@ref extendview.png)
219  *
220  * @sa gf::AdaptativeView
221  */
223  public:
224  /**
225  * @brief Default constructor
226  *
227  * This constructor creates a default view of @f$(0, 0, 1000, 1000)@f$.
228  */
231  {
232 
233  }
234 
235  /**
236  * @brief Construct the view from a rectangle
237  *
238  * @param rect Rectangle defining the zone to display
239  */
240  explicit ExtendView(const RectF& rect)
241  : AdaptativeView(rect)
243  {
244 
245  }
246 
247  /**
248  * @brief Construct the view from its center and size
249  *
250  * @param center Center of the zone to display
251  * @param size Size of the zone to display
252  */
253  ExtendView(Vector2f center, Vector2f size)
255  , m_localSize(size)
256  {
257 
258  }
259 
260  virtual void onScreenSizeChange(Vector2u screenSize) override;
261 
262  protected:
263  virtual void onSizeChange(Vector2f size) override;
264  virtual void onViewportChange(const RectF& viewport) override;
265 
266  void updateView();
267 
268  private:
269  Vector2f m_localSize;
270  Vector2u m_localScreenSize;
271  };
272 
273 
274  /**
275  * @ingroup graphics
276  * @brief Locked view
277  *
278  * This view keeps the world size constant and add black bars if the world
279  * is smaller than the screen size or make a zoom in the center of the
280  * world if the world is bigger than the screen size.
281  *
282  * ![Locked view](@ref lockedview.png)
283  *
284  * @sa gf::AdaptativeView
285  */
287  public:
288  /**
289  * @brief Default constructor
290  *
291  * This constructor creates a default view of @f$(0, 0, 1000, 1000)@f$.
292  */
295  , m_localViewport(0.0f, 0.0f, 1.0f, 1.0f)
296  {
297 
298  }
299 
300  /**
301  * @brief Construct the view from a rectangle
302  *
303  * @param rect Rectangle defining the zone to display
304  */
305  explicit LockedView(const RectF& rect)
306  : AdaptativeView(rect)
308  , m_localViewport(0.0f, 0.0f, 1.0f, 1.0f)
309  {
310 
311  }
312 
313  /**
314  * @brief Construct the view from its center and size
315  *
316  * @param center Center of the zone to display
317  * @param size Size of the zone to display
318  */
319  LockedView(Vector2f center, Vector2f size)
321  , m_localSize(size)
322  , m_localViewport(0.0f, 0.0f, 1.0f, 1.0f)
323  {
324 
325  }
326 
327  virtual void onScreenSizeChange(Vector2u screenSize) override;
328 
329  protected:
330  virtual void onSizeChange(Vector2f size) override;
331  virtual void onViewportChange(const RectF& viewport) override;
332 
333  void updateView();
334 
335  private:
336  Vector2f m_localSize;
337  Vector2u m_localScreenSize;
338  RectF m_localViewport;
339  };
340 
341  /**
342  * @ingroup graphics
343  * @brief Screen view
344  *
345  * This view will always match the window size which means that no scaling
346  * happens and no black bars appear. As a disadvantage this means that the
347  * gameplay might change, because a player with a bigger screen might see
348  * more of the game, than a player with a smaller screen size.
349  *
350  * This view can be used to display [HUD](https://en.wikipedia.org/wiki/HUD_%28video_gaming%29).
351  *
352  * ![Screen view](@ref screenview.png)
353  *
354  * @sa gf::AdaptativeView
355  */
357  public:
358  virtual void onScreenSizeChange(Vector2u screenSize) override;
359  virtual void onViewportChange(const RectF& viewport) override;
360 
361  void updateView();
362 
363  private:
364  Vector2u m_localScreenSize;
365  };
366 
367 #ifndef DOXYGEN_SHOULD_SKIP_THIS
368 }
369 #endif
370 }
371 
372 #endif // GF_VIEWS_H
FitView(Vector2f center, Vector2f size)
Construct the view from its center and size.
Definition: Views.h:128
FitView()
Default constructor.
Definition: Views.h:103
FillView(Vector2f center, Vector2f size)
Construct the view from its center and size.
Definition: Views.h:190
AdaptativeView(const RectF &rect)
Construct the view from a rectangle.
Definition: View.h:401
virtual void onViewportChange(const RectF &viewport) override
Callback when the viewport has just been changed.
Screen view.
Definition: Views.h:356
virtual void onViewportChange(const RectF &viewport) override
Callback when the viewport has just been changed.
Fit view.
Definition: Views.h:96
virtual void onScreenSizeChange(Vector2u screenSize) override
Callback when the screen has just been resized.
StretchView()
Default constructor.
Definition: Views.h:52
FitView(const RectF &rect)
Construct the view from a rectangle.
Definition: Views.h:115
LockedView()
Default constructor.
Definition: Views.h:293
void updateView()
StretchView(const RectF &rect)
Construct the view from a rectangle.
Definition: Views.h:63
void updateView()
ExtendView()
Default constructor.
Definition: Views.h:229
Fill view.
Definition: Views.h:159
virtual void onScreenSizeChange(Vector2u screenSize) override
Callback when the screen has just been resized.
virtual void onViewportChange(const RectF &viewport) override
Callback when the viewport has just been changed.
LockedView(Vector2f center, Vector2f size)
Construct the view from its center and size.
Definition: Views.h:319
virtual void onSizeChange(Vector2f size) 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:253
The namespace for gf classes.
Definition: Action.h:34
ExtendView(const RectF &rect)
Construct the view from a rectangle.
Definition: Views.h:240
Extend view.
Definition: Views.h:222
virtual void onScreenSizeChange(Vector2u screenSize) override
Callback when the screen has just been resized.
virtual void onScreenSizeChange(Vector2u screenSize) override
Callback when the screen has just been resized.
LockedView(const RectF &rect)
Construct the view from a rectangle.
Definition: Views.h:305
virtual void onViewportChange(const RectF &viewport) override
Callback when the viewport has just been changed.
virtual void onSizeChange(Vector2f size) override
Callback when the world has just been resized.
virtual void onScreenSizeChange(Vector2u screenSize) override
Callback when the screen has just been resized.
AdaptativeView()
Default constructor.
Definition: View.h:390
virtual void onViewportChange(const RectF &viewport) override
Callback when the viewport has just been changed.
FillView(const RectF &rect)
Construct the view from a rectangle.
Definition: Views.h:177
virtual void onSizeChange(Vector2f size) override
Callback when the world has just been resized.
#define GF_API
Definition: Portability.h:35
Adaptative view.
Definition: View.h:383
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:166
virtual void onSizeChange(Vector2f size) override
Callback when the world has just been resized.
Locked view.
Definition: Views.h:286
virtual void onScreenSizeChange(Vector2u screenSize) override
Callback when the screen has just been resized.