Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
View.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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 * Part of this file comes from SFML, with the same license:
22 * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23 */
24#ifndef GF_VIEW_H
25#define GF_VIEW_H
26
27#include "GraphicsApi.h"
28#include "Matrix.h"
29#include "Rect.h"
30#include "Vector.h"
31
32namespace gf {
33#ifndef DOXYGEN_SHOULD_SKIP_THIS
34inline namespace v1 {
35#endif
36 struct Event;
37 class RenderTarget;
38
94 class GF_GRAPHICS_API View {
95 public:
102
108 explicit View(const RectF& rect);
109
116 View(Vector2f center, Vector2f size);
117
121 virtual ~View();
122
129
137 void setCenter(Vector2f center) {
138 m_center = center;
139 }
140
149 return m_center;
150 }
151
159 void setSize(Vector2f size) {
160 m_size = size;
161 onSizeChange(m_size);
162 }
163
172 return m_size;
173 }
174
184 void setRotation(float angle) {
185 m_rotation = angle;
186 }
187
195 float getRotation() const {
196 return m_rotation;
197 }
198
219 void setViewport(const RectF& viewport);
220
228 const RectF& getViewport() const {
229 return m_viewport;
230 }
231
241 void reset(const RectF& rect);
242
250 void move(Vector2f offset);
251
259 void rotate(float angle);
260
275 void zoom(float factor);
276
295 void zoom(float factor, Vector2f fixed);
296
307
318
319 protected:
329 m_size = size;
330 }
331
339 virtual void onSizeChange(Vector2f size);
340
349 void setViewportNoCallback(const RectF& viewport);
350
356 virtual void onViewportChange(const RectF& viewport);
357
358 private:
359 Vector2f m_center;
360 Vector2f m_size;
361 float m_rotation;
362 RectF m_viewport;
363 };
364
365
390 class GF_GRAPHICS_API AdaptativeView : public View {
391 public:
398 : View()
399 {
400
401 }
402
408 explicit AdaptativeView(const RectF& rect)
409 : View(rect)
410 {
411
412 }
413
421 : View(center, size)
422 {
423
424 }
425
431 void setInitialFramebufferSize(Vector2i framebufferSize);
432
440 [[deprecated("You should use setInitialFramebufferSize() instead")]]
442 setInitialFramebufferSize(screenSize);
443 }
444
450 virtual void onFramebufferSizeChange(Vector2i framebufferSize) = 0;
451
452 };
453
458 class GF_GRAPHICS_API ZoomingViewAdaptor {
459 public:
466 ZoomingViewAdaptor(const RenderTarget& target, View& view);
467
473 void processEvent(const Event& event);
474
475 private:
476 const RenderTarget& m_target;
477 View& m_view;
478 Vector2i m_mousePosition;
479
480 enum class State {
481 Stationary,
482 Moving,
483 };
484
485 State m_state;
486 };
487
488#ifndef DOXYGEN_SHOULD_SKIP_THIS
489}
490#endif
491}
492
493#endif // GF_VIEW_H
Adaptative view.
Definition: View.h:390
AdaptativeView(const RectF &rect)
Construct the view from a rectangle.
Definition: View.h:408
AdaptativeView(Vector2f center, Vector2f size)
Construct the view from its center and size.
Definition: View.h:420
void setInitialFramebufferSize(Vector2i framebufferSize)
Set the initial framebuffer size.
void setInitialScreenSize(Vector2i screenSize)
Set the initial screen size.
Definition: View.h:441
AdaptativeView()
Default constructor.
Definition: View.h:397
virtual void onFramebufferSizeChange(Vector2i framebufferSize)=0
Callback when the framebuffer has just been resized.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
2D camera that defines what region is shown on framebuffer
Definition: View.h:94
void setViewport(const RectF &viewport)
Set the target viewport.
void rotate(float angle)
Rotate the view relatively to its current orientation.
void reset(const RectF &rect)
Reset the view to the given rectangle.
void setSizeNoCallback(Vector2f size)
Set the world size, without calling onSizeChange()
Definition: View.h:328
View(const RectF &rect)
Construct the view from a rectangle.
virtual void onSizeChange(Vector2f size)
Callback when the world has just been resized.
void zoom(float factor)
Resize the view rectangle relatively to its current size.
void setSize(Vector2f size)
Set the size of the view.
Definition: View.h:159
Matrix3f getInverseTransform() const
Get the inverse projection transform of the view.
RectF getBounds() const
Get the non-rotated bounds.
virtual void onViewportChange(const RectF &viewport)
Callback when the viewport has just been changed.
void setCenter(Vector2f center)
Set the center of the view.
Definition: View.h:137
Vector2f getCenter() const
Get the center of the view.
Definition: View.h:148
void setRotation(float angle)
Set the orientation of the view.
Definition: View.h:184
void move(Vector2f offset)
Move the view relatively to its current position.
void setViewportNoCallback(const RectF &viewport)
Set the viewport, without calling onViewportChange()
float getRotation() const
Get the current orientation of the view.
Definition: View.h:195
const RectF & getViewport() const
Get the target viewport rectangle of the view.
Definition: View.h:228
View(Vector2f center, Vector2f size)
Construct the view from its center and size.
Vector2f getSize() const
Get the size of the view.
Definition: View.h:171
View()
Default constructor.
virtual ~View()
Destructor.
void zoom(float factor, Vector2f fixed)
Resize the view rectangle relatively to its current size and a fixed point.
Matrix3f getTransform() const
Get the projection transform of the view.
A view adaptor for zooming/moving with the mouse.
Definition: View.h:458
void processEvent(const Event &event)
Update the original view thanks to the event.
ZoomingViewAdaptor(const RenderTarget &target, View &view)
Constructor.
GF_CORE_API float angle(Direction direction)
Get an angle from a direction.
@ Event
An event is pending on the sockets.
The namespace for gf classes.
Defines a system event and its parameters.
Definition: Event.h:224