Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ime::Camera Class Reference

A 2D camera that defines what region is shown on screen in a Scene. More...

#include <Camera.h>

Inheritance diagram for ime::Camera:
ime::Object

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...
 
Cameraoperator= (const Camera &)=delete
 Copy assignment operator. More...
 
 Camera (Camera &&)=delete
 Move constructor. More...
 
Cameraoperator= (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 &centre)
 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...
 
GameObjectgetFollowTarget () 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 Vector2fgetTargetFollowOffset () 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...
 

Detailed Description

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

Definition at line 61 of file Camera.h.

Member Typedef Documentation

◆ Ptr

using ime::Object::Ptr = std::unique_ptr<Object>
inherited

Unique object pointer.

Definition at line 42 of file Object.h.

Member Enumeration Documentation

◆ OnWinResize

enum class ime::Camera::OnWinResize
strong

Defines how the camera response to a window resize event.

Enumerator
Stretch 

This option squeezes/stretches the camera to match the new size of the window.

Letterbox 

This option scales the camera while maintaining its aspect ratio.

If the aspect ratio of the camera and that of the window do not match, black bars will appear vertically (to the left and right of the camera) or horizontally (above and below the camera)

MaintainSize 

This option updates the size of the camera to that of the window without squeezing, stretching or scaling.

This means that you will see more or less of the scene depending on the new size of the window

Definition at line 66 of file Camera.h.

Constructor & Destructor Documentation

◆ Camera() [1/2]

ime::Camera::Camera ( const Camera )
delete

Copy constructor.

◆ Camera() [2/2]

ime::Camera::Camera ( Camera &&  )
delete

Move constructor.

◆ ~Camera()

ime::Camera::~Camera ( )
override

Destructor.

Member Function Documentation

◆ emitChange()

void ime::Object::emitChange ( const Property property)
protectedinherited

Dispatch a property change event.

Parameters
propertyThe property that changed

This function will invoke all the event listeners of the specified property

See also
emit

◆ emitDestruction()

void ime::Object::emitDestruction ( )
protectedinherited

Emit a destruction event.

Note
This function must be the first statement in the definition of a destructor to avoid undefined behavior. In addition, note that destruction listeners are invoked once. Therefore, multiple classes in a hierarchy may call this function but the class that makes the call first will be the one that invokes the destruction listeners

◆ getBounds()

FloatRect ime::Camera::getBounds ( ) const

Get the camera bounds.

Returns
The bounds of the camera

◆ getCenter()

Vector2f ime::Camera::getCenter ( ) const

Get the centre of the camera.

Returns
The centre of the camera

◆ getClassName()

std::string ime::Camera::getClassName ( ) const
overridevirtual

Get the name of this class.

Returns
The name of this class

Implements ime::Object.

◆ getClassType()

virtual std::string ime::Object::getClassType ( ) const
virtualinherited

Get the name of the direct parent of an object instance.

Returns
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

auto rectangle = ime::RectangleShape(); // RectangleShape is derived from Shape
std::cout << rectangle->getClassName(); // Prints "RectangleShape"
std::cout << rectangle->getClassType(); // Prints "Shape"
A 2D shape having four sides and four corners (90 degree angles)
See also
getClassName

Reimplemented in ime::audio::Audio, ime::GameObject, ime::GridMover, ime::Collider, ime::Joint, ime::Drawable, ime::Shape, and ime::SpriteImage.

◆ getFollowTarget()

GameObject * ime::Camera::getFollowTarget ( ) const

Get that game object followed by the camera.

Returns
The game object followed by the camera or a nullptr if the camera is not following any game object
See also
startFollow

◆ getObjectId()

unsigned int ime::Object::getObjectId ( ) const
inherited

Get the unique id of the object.

Returns
The unique id of the object

Note that each instance of ime::Object has a unique id

See also
setTag

◆ getOutlineColour()

Colour ime::Camera::getOutlineColour ( ) const

Get the outline colour.

Returns
The outline colour
See also
setOutlineColour

◆ getOutlineThickness()

float ime::Camera::getOutlineThickness ( ) const

Get the outline thickness of the camera.

Returns
The outline thickness
See also
setOutlineThickness

◆ getRotation()

float ime::Camera::getRotation ( ) const

Get the current orientation of the camera.

Returns
The rotation of the camera in degrees

◆ getSize()

Vector2f ime::Camera::getSize ( ) const

Get the size of the camera.

Returns
The size of the camera

◆ getTag()

const std::string & ime::Object::getTag ( ) const
inherited

Get the tag assigned to the object.

Returns
The tag of the object
See also
setTag

◆ getTargetFollowOffset()

const Vector2f & ime::Camera::getTargetFollowOffset ( ) const

Get the camera's follow target offset.

Returns
Th camera's follow target offset

◆ getViewport()

FloatRect ime::Camera::getViewport ( ) const

Get the target viewport of the rectangle of the camera.

Returns
The viewport rectangle, expressed as a factor of the target size
See also
setViewPort

◆ getWindowResizeResponse()

OnWinResize ime::Camera::getWindowResizeResponse ( ) const

Get the cameras response to a window resize event.

Returns
The window resize response
See also
setWindowResizeResponse

◆ isDrawable()

bool ime::Camera::isDrawable ( ) const

Check if the camera is rendered by the scene.

Returns
True if its contents are rendered, otherwise false
See also
setDrawable

◆ isEventListenerSuspended()

bool ime::Object::isEventListenerSuspended ( int  id) const
inherited

Check if an event listener is suspended or not.

Parameters
idThe identification number of the listener to be checked
Returns
True if suspended, otherwise false

This function also returns false if the specified event listener does not exist

See also
suspendedEventListener

◆ isFollowingTarget()

bool ime::Camera::isFollowingTarget ( ) const

Check if the camera is following a game object or not.

Returns
True if the camera is following a game object, otherwise false
See also
startFollow

◆ isSameObjectAs()

bool ime::Object::isSameObjectAs ( const Object other) const
inherited

Check if another object is the same instance as this object.

Parameters
otherThe object to compare against this object
Returns
True if other is the same instance as this object, otherwise false

◆ move() [1/2]

void ime::Camera::move ( const Vector2f offset)

Move the camera relative to its current position.

Parameters
offsetThe move offset

◆ move() [2/2]

void ime::Camera::move ( float  offsetX,
float  offsetY 
)

Move the camera relative to its current position.

Parameters
offsetXThe value of the horizontal move offset
offsetYThe value of the vertical move offset

◆ onDestruction()

int ime::Object::onDestruction ( const Callback<> &  callback)
inherited

Add a destruction listener.

Parameters
callbackFunction to be executed when the object is destroyed
Returns
The unique id of the destruction listener

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

Warning
It's not advised to call virtual functions in the destruction callback as some parts of the object may have already been destroyed by the time the callback is invoked. In such an event, the behavior is undefined
See also
removeEventListener

◆ onPropertyChange() [1/2]

int ime::Object::onPropertyChange ( const Callback< Property > &  callback,
bool  oneTime = false 
)
inherited

Add an event listener to any property change event.

Parameters
callbackThe function to be executed when any property changes
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The unique id of the event listener

When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event

See also
onPropertyChange(const std::string&, const ime::Callback<ime::Property>&)

◆ onPropertyChange() [2/2]

int ime::Object::onPropertyChange ( const std::string &  property,
const Callback< Property > &  callback,
bool  oneTime = false 
)
inherited

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to add an event listener to
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

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.

// Prints the tag of the player object to the console everytime it changes
player.onPropertyChange("tag", [](const Property& property) {
cout << "New tag: " << property.getValue<std::string>() << endl;
});
...
//Sets tag = "player1" and invokes event listener(s)
player.setTag("player1");
See also
unsubscribe and onPropertyChange(const ime::Callback<ime::Property>&)

◆ operator=() [1/2]

Camera & ime::Camera::operator= ( Camera &&  )
delete

Move assignment operator.

◆ operator=() [2/2]

Camera & ime::Camera::operator= ( const Camera )
delete

Copy assignment operator.

◆ removeEventListener() [1/2]

bool ime::Object::removeEventListener ( const std::string &  event,
int  id 
)
inherited

Remove an event listener from an event.

Parameters
eventThe name of the event to remove an event listener from
idThe unique id of the event listener to be removed
Returns
True if the event listener was removed or false if the event or the event listener is does not exist
// Display the tag of the object to console every time it changes
auto tagChangeId = object.onPropertyChange("tag", [](ime::Property tag) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the tag of the object when it changes
object.removeEventListener("tag", tagChangeId);
Class that can store a value of any type.
Definition: Property.h:38

◆ removeEventListener() [2/2]

bool ime::Object::removeEventListener ( int  id)
inherited

Remove an event listener.

Parameters
idThe id of the event listener to be removed
Returns
True if the event listener was removed or false if no such handler exists

◆ reset()

void ime::Camera::reset ( const FloatRect rectangle)

Reset the camera to the given rectangle.

Parameters
rectangleRectangle defining the zone to display
Note
This function resets the rotation of the camera to 0 degrees

◆ resetToDefault()

void ime::Camera::resetToDefault ( )

Reset the camera to its default settings.

◆ rotate()

void ime::Camera::rotate ( float  angle)

Rotate the camera relative to its current orientation.

Parameters
angleThe angle to rotate the camera by in degrees
See also
setRotation

◆ setCenter() [1/2]

void ime::Camera::setCenter ( const Vector2f centre)

Set the centre of the camera.

Parameters
centreThe new centre of the camera

◆ setCenter() [2/2]

void ime::Camera::setCenter ( float  x,
float  y 
)

Set the centre of the camera.

Parameters
xX coordinate of the new centre
yY coordinate of the new centre

◆ setDrawable()

void ime::Camera::setDrawable ( bool  drawable)

Set whether or not the scene should render the camera.

Parameters
drawableTrue to render the camera, otherwise false

By default, the camera is drawable

See also
isDrawable

◆ setOutlineColour()

void ime::Camera::setOutlineColour ( const Colour colour)

Set the outline colour.

Parameters
colourThe 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

See also
setOutlineThickness, getOutlineColour

◆ setOutlineThickness()

void ime::Camera::setOutlineThickness ( float  thickness)

Set the outline thickness.

Parameters
thicknessThe 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

See also
setOutlineColour, getOutlineThickness

◆ setRotation()

void ime::Camera::setRotation ( float  angle)

Set the orientation of the camera.

Parameters
angleThe orientation to set in degrees

By default, the rotation is 0 degrees

◆ setSize() [1/2]

void ime::Camera::setSize ( const Vector2f size)

Set the size of the camera.

Parameters
sizeThe new size of the camera

By default, the camera is the same size as the render window

◆ setSize() [2/2]

void ime::Camera::setSize ( float  width,
float  height 
)

Set the size of the camera.

Parameters
widthThe new width of the camera
heightThe new height of the camera

By default, the camera is the same size as the render window

◆ setTag()

void ime::Object::setTag ( const std::string &  tag)
inherited

Assign the object an alias.

Parameters
tagThe 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

See also
getObjectId

◆ setTargetFollowOffset()

void ime::Camera::setTargetFollowOffset ( const Vector2f offset)

Set the camera's follow offset from the targets position.

Parameters
offsetThe 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

◆ setViewport()

void ime::Camera::setViewport ( const FloatRect viewport)

Set the target viewport.

Parameters
viewportThe 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

◆ setWindowResizeResponse()

void ime::Camera::setWindowResizeResponse ( OnWinResize  response)

Set how the camera responds to the window when it is resized.

Parameters
responseThe on resize event response

By default, the response is ime::Camera::onWinResize::Stretch

See also
getWindowResizeResponse

◆ startFollow()

void ime::Camera::startFollow ( GameObject gameObject,
const Vector2f offset = {0, 0} 
)

Set the camera to follow a GameObject.

Parameters
gameObjectThe game object to be followed
offsetThe offset of the camera from the game objects position
  • 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 camera will keep the game object at its centre

Warning
gameObject must not be a nullptr, otherwise undefined behavior
See also
stopFollow, setFollowOffset

◆ stopFollow()

void ime::Camera::stopFollow ( )

Stop the camera from following a game object.

See also
setFollow

◆ suspendedEventListener()

void ime::Object::suspendedEventListener ( int  id,
bool  suspend 
)
inherited

Pause or resume execution of an event listener.

Parameters
idThe event listeners unique identification number
suspendTrue to suspend/pause or false to unsuspend/resume
See also
isEventListenerSuspended

◆ windowCoordToWorldCoord()

Vector2f ime::Camera::windowCoordToWorldCoord ( const Vector2i point) const

Convert a window coordinate to a world coordinate.

Parameters
pointThe window coordinate to be converted
Returns
The converted point in world coordinates

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)

◆ worldCoordToWindowCoord()

Vector2i ime::Camera::worldCoordToWindowCoord ( const Vector2f point) const

Convert a world coordinate to a window coordinate.

Parameters
pointThe world point ot be converted
Returns
The converted point in window coordinates

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)

◆ zoom()

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.

Parameters
zoomPosPosition to zoom at (leave unspecified to zoom at center)
factorThe zoom factor to apply

Resizing the view simulates a zoom, as the zone displayed on screen grows or shrinks.

factor is a multiplier:

  • 1 keeps the size unchanged
  • > 1 makes the view bigger (objects appear smaller)
  • < 1 makes the view smaller (objects appear bigger)

By default the camera zooms in or out at its centre

See also
setSize

◆ zoomIn()

void ime::Camera::zoomIn ( float  factor,
const Vector2i zoomPos = {std::numeric_limits< int >::max(), std::numeric_limits< int >::max()} 
)

Zoom the camera in.

Parameters
zoomPosPosition to zoom in at (leave unspecified to zoom at center)
factorThe 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

See also
zoomOut, setCenter

◆ zoomOut()

void ime::Camera::zoomOut ( float  factor,
const Vector2i zoomPos = {std::numeric_limits< int >::max(), std::numeric_limits< int >::max()} 
)

Zoom the camera out.

Parameters
zoomPosPosition to zoom out at (leave unspecified to zoom at center)
factorThe 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

See also
zoomIn, setCenter

Member Data Documentation

◆ eventEmitter_

EventEmitter ime::Object::eventEmitter_
protectedinherited

Event dispatcher.

Definition at line 289 of file Object.h.


The documentation for this class was generated from the following file: