A 2D camera that defines what region is shown on screen in a Scene. More...
#include <Camera.h>
Public Types | |
enum class | OnWinResize { Stretch , Letterbox , MaintainSize } |
Defines how the camera response to a window resize event. More... | |
using | Ptr = std::unique_ptr< Object > |
Unique object pointer. More... | |
Public Member Functions | |
Camera (priv::RenderTarget &window) | |
Camera (const Camera &)=delete | |
Copy constructor. More... | |
Camera & | operator= (const Camera &)=delete |
Copy assignment operator. More... | |
Camera (Camera &&)=delete | |
Move constructor. More... | |
Camera & | operator= (Camera &&)=delete |
Move assignment operator. More... | |
std::string | getClassName () const override |
Get the name of this class. More... | |
void | setCenter (float x, float y) |
Set the centre of the camera. More... | |
void | setCenter (const Vector2f ¢re) |
Set the centre of the camera. More... | |
Vector2f | getCenter () const |
Get the centre of the camera. More... | |
void | setSize (float width, float height) |
Set the size of the camera. More... | |
void | setSize (const Vector2f &size) |
Set the size of the camera. More... | |
Vector2f | getSize () const |
Get the size of the camera. More... | |
void | setRotation (float angle) |
Set the orientation of the camera. More... | |
float | getRotation () const |
Get the current orientation of the camera. More... | |
void | setViewport (const FloatRect &viewport) |
Set the target viewport. More... | |
FloatRect | getViewport () const |
Get the target viewport of the rectangle of the camera. More... | |
void | setWindowResizeResponse (OnWinResize response) |
Set how the camera responds to the window when it is resized. More... | |
OnWinResize | getWindowResizeResponse () const |
Get the cameras response to a window resize event. More... | |
void | setDrawable (bool drawable) |
Set whether or not the scene should render the camera. More... | |
bool | isDrawable () const |
Check if the camera is rendered by the scene. More... | |
void | setOutlineThickness (float thickness) |
Set the outline thickness. More... | |
float | getOutlineThickness () const |
Get the outline thickness of the camera. More... | |
void | setOutlineColour (const Colour &colour) |
Set the outline colour. More... | |
Colour | getOutlineColour () const |
Get the outline colour. More... | |
FloatRect | getBounds () const |
Get the camera bounds. More... | |
void | reset (const FloatRect &rectangle) |
Reset the camera to the given rectangle. More... | |
void | resetToDefault () |
Reset the camera to its default settings. More... | |
void | move (float offsetX, float offsetY) |
Move the camera relative to its current position. More... | |
void | move (const Vector2f &offset) |
Move the camera relative to its current position. More... | |
void | rotate (float angle) |
Rotate the camera relative to its current orientation. More... | |
void | zoom (float factor, const Vector2i &zoomPos={std::numeric_limits< int >::max(), std::numeric_limits< int >::max()}) |
Resize the camera rectangle relative to its current size. More... | |
void | zoomIn (float factor, const Vector2i &zoomPos={std::numeric_limits< int >::max(), std::numeric_limits< int >::max()}) |
Zoom the camera in. More... | |
void | zoomOut (float factor, const Vector2i &zoomPos={std::numeric_limits< int >::max(), std::numeric_limits< int >::max()}) |
Zoom the camera out. More... | |
Vector2f | windowCoordToWorldCoord (const Vector2i &point) const |
Convert a window coordinate to a world coordinate. More... | |
Vector2i | worldCoordToWindowCoord (const Vector2f &point) const |
Convert a world coordinate to a window coordinate. More... | |
void | startFollow (GameObject *gameObject, const Vector2f &offset={0, 0}) |
Set the camera to follow a GameObject. More... | |
void | stopFollow () |
Stop the camera from following a game object. More... | |
bool | isFollowingTarget () const |
Check if the camera is following a game object or not. More... | |
GameObject * | getFollowTarget () const |
Get that game object followed by the camera. More... | |
void | setTargetFollowOffset (const Vector2f &offset) |
Set the camera's follow offset from the targets position. More... | |
const Vector2f & | getTargetFollowOffset () const |
Get the camera's follow target offset. More... | |
void | setInternalView (std::any view) |
std::any | getInternalView () |
~Camera () override | |
Destructor. More... | |
void | setTag (const std::string &tag) |
Assign the object an alias. More... | |
const std::string & | getTag () const |
Get the tag assigned to the object. More... | |
unsigned int | getObjectId () const |
Get the unique id of the object. More... | |
virtual std::string | getClassType () const |
Get the name of the direct parent of an object instance. More... | |
int | onPropertyChange (const std::string &property, const Callback< Property > &callback, bool oneTime=false) |
Add an event listener to a specific property change event. More... | |
int | onPropertyChange (const Callback< Property > &callback, bool oneTime=false) |
Add an event listener to any property change event. More... | |
void | suspendedEventListener (int id, bool suspend) |
Pause or resume execution of an event listener. More... | |
bool | isEventListenerSuspended (int id) const |
Check if an event listener is suspended or not. More... | |
bool | removeEventListener (const std::string &event, int id) |
Remove an event listener from an event. More... | |
bool | removeEventListener (int id) |
Remove an event listener. More... | |
int | onDestruction (const Callback<> &callback) |
Add a destruction listener. More... | |
bool | isSameObjectAs (const Object &other) const |
Check if another object is the same instance as this object. More... | |
Protected Member Functions | |
void | emitChange (const Property &property) |
Dispatch a property change event. More... | |
void | emitDestruction () |
Emit a destruction event. More... | |
Protected Attributes | |
EventEmitter | eventEmitter_ |
Event dispatcher. More... | |
A 2D camera that defines what region is shown on screen in a Scene.
A camera is composed of a source rectangle, which defines what part of the Scene is shown, and a target viewport, which defines where the contents of the source rectangle will be displayed on the render Window.
The viewport allows to map the scene to a custom part of the render window, and can be used for split-screen or for displaying a minimap, for example. If the source rectangle doesn't have the same size as the viewport, its contents will be stretched to fit in
You do not instantiate a camera directly, it is created when you instantiate a scene and it is accessed with ime::Scene::getCamera or ime::Scene::getCameras
|
inherited |
|
strong |
Defines how the camera response to a window resize event.
|
delete |
Copy constructor.
|
delete |
Move constructor.
|
override |
Destructor.
|
protectedinherited |
Dispatch a property change event.
property | The property that changed |
This function will invoke all the event listeners of the specified property
|
protectedinherited |
Emit a destruction event.
FloatRect ime::Camera::getBounds | ( | ) | const |
Get the camera bounds.
Vector2f ime::Camera::getCenter | ( | ) | const |
Get the centre of the camera.
|
overridevirtual |
|
virtualinherited |
Get the name of the direct parent of an object instance.
In contrast to getClassName() which returns the name of the concrete class, this function returns the name of the concrete class's base class. This function is implemented by all derived classes of ime::Object which also serve as base classes. For classes whose direct parent is this class, this function will return the name of this class
Reimplemented in ime::audio::Audio, ime::GameObject, ime::GridMover, ime::Collider, ime::Joint, ime::Drawable, ime::Shape, and ime::SpriteImage.
GameObject * ime::Camera::getFollowTarget | ( | ) | const |
Get that game object followed by the camera.
|
inherited |
Get the unique id of the object.
Note that each instance of ime::Object has a unique id
Colour ime::Camera::getOutlineColour | ( | ) | const |
float ime::Camera::getOutlineThickness | ( | ) | const |
float ime::Camera::getRotation | ( | ) | const |
Get the current orientation of the camera.
Vector2f ime::Camera::getSize | ( | ) | const |
Get the size of the camera.
|
inherited |
const Vector2f & ime::Camera::getTargetFollowOffset | ( | ) | const |
Get the camera's follow target offset.
FloatRect ime::Camera::getViewport | ( | ) | const |
Get the target viewport of the rectangle of the camera.
OnWinResize ime::Camera::getWindowResizeResponse | ( | ) | const |
Get the cameras response to a window resize event.
bool ime::Camera::isDrawable | ( | ) | const |
Check if the camera is rendered by the scene.
|
inherited |
Check if an event listener is suspended or not.
id | The identification number of the listener to be checked |
This function also returns false if the specified event listener does not exist
bool ime::Camera::isFollowingTarget | ( | ) | const |
Check if the camera is following a game object or not.
|
inherited |
Check if another object is the same instance as this object.
other | The object to compare against this object |
void ime::Camera::move | ( | const Vector2f & | offset | ) |
Move the camera relative to its current position.
offset | The move offset |
void ime::Camera::move | ( | float | offsetX, |
float | offsetY | ||
) |
Move the camera relative to its current position.
offsetX | The value of the horizontal move offset |
offsetY | The value of the vertical move offset |
|
inherited |
Add a destruction listener.
callback | Function to be executed when the object is destroyed |
The destruction listener is called when the object reaches the end of its lifetime. Note that an object may have multiple destruction listeners registered to it
|
inherited |
Add an event listener to any property change event.
callback | The function to be executed when any property changes |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event
|
inherited |
Add an event listener to a specific property change event.
property | The name of the property to add an event listener to |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
callback | The function to be executed when the property changes |
A property change event is triggered by any function that begins with set, where the the text after set is the name of the property. For example, the setTag() function, modifies the tag property of the object, thus will generate a "tag" change event each time it is called
Note that multiple event listeners may be registered to the same property change event. In addition, when adding a property change event listener, the name of the property must be in lowercase.
|
inherited |
Remove an event listener from an event.
event | The name of the event to remove an event listener from |
id | The unique id of the event listener to be removed |
|
inherited |
Remove an event listener.
id | The id of the event listener to be removed |
void ime::Camera::reset | ( | const FloatRect & | rectangle | ) |
Reset the camera to the given rectangle.
rectangle | Rectangle defining the zone to display |
void ime::Camera::resetToDefault | ( | ) |
Reset the camera to its default settings.
void ime::Camera::rotate | ( | float | angle | ) |
Rotate the camera relative to its current orientation.
angle | The angle to rotate the camera by in degrees |
void ime::Camera::setCenter | ( | const Vector2f & | centre | ) |
Set the centre of the camera.
centre | The new centre of the camera |
void ime::Camera::setCenter | ( | float | x, |
float | y | ||
) |
Set the centre of the camera.
x | X coordinate of the new centre |
y | Y coordinate of the new centre |
void ime::Camera::setDrawable | ( | bool | drawable | ) |
Set whether or not the scene should render the camera.
drawable | True to render the camera, otherwise false |
By default, the camera is drawable
void ime::Camera::setOutlineColour | ( | const Colour & | colour | ) |
Set the outline colour.
colour | The new outline colour |
The outline colour together with the outline thickness enables visualization of the camera bounds
By default the outline colour is ime::Colour::Transparent
void ime::Camera::setOutlineThickness | ( | float | thickness | ) |
Set the outline thickness.
thickness | The new outline thickness (must be >= 0) |
The outline thickness together with the outline colour enables visualization if the camera bounds
By default, the outline thickness is 1
void ime::Camera::setRotation | ( | float | angle | ) |
Set the orientation of the camera.
angle | The orientation to set in degrees |
By default, the rotation is 0 degrees
void ime::Camera::setSize | ( | const Vector2f & | size | ) |
Set the size of the camera.
size | The new size of the camera |
By default, the camera is the same size as the render window
void ime::Camera::setSize | ( | float | width, |
float | height | ||
) |
Set the size of the camera.
width | The new width of the camera |
height | The new height of the camera |
By default, the camera is the same size as the render window
|
inherited |
Assign the object an alias.
tag | The alias of the object |
This function is useful if you want to refer to the object by a tag instead of its object id. Unlike an object id, multiple objects may have the same tag
By default, the tag is an empty string
void ime::Camera::setTargetFollowOffset | ( | const Vector2f & | offset | ) |
Set the camera's follow offset from the targets position.
offset | The new follow offset |
A positive x offset places the camera to the right of the target, while a negative x offset places the camera to the left of the target. Similarly, a positive y offset places the camera below the target while a negative y offset places the camera above the target
By default the offset is Vector2f{0, 0} which means that the camera keeps the game object at its centre
void ime::Camera::setViewport | ( | const FloatRect & | viewport | ) |
Set the target viewport.
viewport | The new viewport rectangle |
The viewport is the rectangle into which the contents of the camera are displayed, expressed as a factor (between 0 and 1) of the size of the RenderWindow. For example, a view which takes the left side of the target would be defined with View.setViewport(sf::FloatRect(0, 0, 0.5, 1)).
By default, a camera has a viewport which covers the entire render window
void ime::Camera::setWindowResizeResponse | ( | OnWinResize | response | ) |
Set how the camera responds to the window when it is resized.
response | The on resize event response |
By default, the response is ime::Camera::onWinResize::Stretch
void ime::Camera::startFollow | ( | GameObject * | gameObject, |
const Vector2f & | offset = {0, 0} |
||
) |
Set the camera to follow a GameObject.
gameObject | The game object to be followed |
offset | The offset of the camera from the game objects position |
By default, the camera will keep the game object at its centre
void ime::Camera::stopFollow | ( | ) |
Stop the camera from following a game object.
|
inherited |
Pause or resume execution of an event listener.
id | The event listeners unique identification number |
suspend | True to suspend/pause or false to unsuspend/resume |
Convert a window coordinate to a world coordinate.
point | The window coordinate to be converted |
Initially, both coordinate systems (world coordinates and window pixels) match perfectly. But if you move or rotate the camera or resize the render Window then a mismatch occurs, i.e. a point located at (10, 50) on the render Window may map to the point (150, 75) in your 2D world – if the camera is translated by (140, 25)
Convert a world coordinate to a window coordinate.
point | The world point ot be converted |
Initially, both coordinate systems (world coordinates and window pixels) match perfectly. But if you move or rotate the camera or resize the render Window then a mismatch occurs, i.e. a point located at (10, 50) on the render Window may map to the point (150, 75) in your 2D world – if the camera is translated by (140, 25)
void ime::Camera::zoom | ( | float | factor, |
const Vector2i & | zoomPos = {std::numeric_limits< int >::max(), std::numeric_limits< int >::max()} |
||
) |
Resize the camera rectangle relative to its current size.
zoomPos | Position to zoom at (leave unspecified to zoom at center) |
factor | The zoom factor to apply |
Resizing the view simulates a zoom, as the zone displayed on screen grows or shrinks.
factor is a multiplier:
By default the camera zooms in or out at its centre
void ime::Camera::zoomIn | ( | float | factor, |
const Vector2i & | zoomPos = {std::numeric_limits< int >::max(), std::numeric_limits< int >::max()} |
||
) |
Zoom the camera in.
zoomPos | Position to zoom in at (leave unspecified to zoom at center) |
factor | The zoom factor |
The factor argument must be greater than 1 in order to zoom out, values equal to 1 or less than are ignored
By default, the camera zooms out from its centre
void ime::Camera::zoomOut | ( | float | factor, |
const Vector2i & | zoomPos = {std::numeric_limits< int >::max(), std::numeric_limits< int >::max()} |
||
) |
Zoom the camera out.
zoomPos | Position to zoom out at (leave unspecified to zoom at center) |
factor | The zoom out factor |
The factor argument must be greater than 1 in order to zoom out, values equal to 1 or less than are ignored
By default, the camera zooms in at its centre
|
protectedinherited |