Public Types | Public Member Functions | Protected Member Functions | Friends | 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 ime::utility::NonCopyable

Public Types

using Ptr = std::shared_ptr< Object >
 Shared object pointer. More...
 

Public Member Functions

 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 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)
 Resize the camera rectangle relative to its current size. 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...
 
 ~Camera () override
 Destructor. More...
 
void setTag (const std::string &tag)
 Assign the object an alias. More...
 
const std::string & getTag () const
 Get the alias of the object. More...
 
unsigned int getObjectId () const
 Get the 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)
 Add an event listener to a specific property change event. More...
 
void onPropertyChange (const Callback< Property > &callback)
 Add an event listener to a property change event. More...
 
int onEvent (const std::string &event, const Callback<> &callback)
 Add an event listener to an event. More...
 
bool unsubscribe (const std::string &event, int id)
 Remove an event listener from an event. More...
 
int onDestruction (const Callback<> &callback)
 Add a destruction listener. More...
 
bool removeDestructionListener (int id)
 Remove a destruction listener form the object. More...
 
bool operator== (const Object &rhs) const
 Check if two objects are the same object or not. More...
 
bool operator!= (const Object &rhs) const
 Check if two objects are not the same object. More...
 

Protected Member Functions

void emitChange (const Property &property)
 Dispatch a property change event. More...
 
void emit (const std::string &event)
 Dispatch an action event. More...
 

Friends

class Scene
 Needs access to constructor. 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 accessible within the scene. Each scene instance has its own camera

Definition at line 54 of file Camera.h.

Member Typedef Documentation

◆ Ptr

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

Shared object pointer.

Definition at line 45 of file Object.h.

Constructor & Destructor Documentation

◆ Camera()

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

Move constructor.

◆ ~Camera()

ime::Camera::~Camera ( )
override

Destructor.

Member Function Documentation

◆ emit()

void ime::Object::emit ( const std::string &  event)
protectedinherited

Dispatch an action event.

Parameters
eventThe name of the event to be dispatched

This function will invoke all event listeners of the specified event. The function should be used for events that represent an action, rather than those that represent a property change (Use emitChange for that)

See also
emitChange

◆ 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

◆ 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 Object which also serve as base classes for other Objects. 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"
See also
getClassName

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

◆ getObjectId()

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

Get the id of the object.

Returns
The id of the object

Each object has a unique id

◆ 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 alias of the object.

Returns
The alias of the object
See also
setTag and getObjectId

◆ 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

◆ 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 id of the destruction listener

Note that an object may have more than one destruction listeners, however, you have to keep the returned id if you may want to remove the callback at a later time

Warning
The callback is called when the object is destroyed. Do not try to access the object in the callback. Doing so is undefined behavior
See also
removeDestructionListener

◆ onEvent()

int ime::Object::onEvent ( const std::string &  event,
const Callback<> &  callback 
)
inherited

Add an event listener to an event.

Parameters
eventThe name of the event to add an an event listener to
callbackThe function to be executed when the event takes place
Returns
The unique identification number of the event listener

Unlike onPropertyChange, this function registers event listeners to events that occur when something happens to the object, or when the object does something (action events). Usually the name of the event/action is the name of the function:

// Add event listeners to the object
object.onEvent("attachRigidBody", [] {
std::cout << "Rigid body attached to object << std::endl;
});
object.onEvent("removeRigidBody", [] {
std::cout << "Rigid body removed from object << std::endl;
});
// Invokes event listener(s)
object.attachRigidBody(body);
object.removeRigidBody();
See also
onPropertyChange and unsubscribe

◆ onPropertyChange() [1/2]

void ime::Object::onPropertyChange ( const Callback< Property > &  callback)
inherited

Add an event listener to a property change event.

Parameters
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

Note that only one callback function may be registered with this function. This means that adding a new event listener overwrites the previous event listener. To remove the callback, pass a nullptr as an argument. The function may be useful if you want to write the logic for property changes in one function.

See also
onPropertyChange(std::string, Callback)

◆ onPropertyChange() [2/2]

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

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to listen for
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, for the setTag function, the property that the function modifies is Tag.

Note that 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");

Unlike onPropertyChange(const Callback&) you can add multiple event listeners to the same property using this function. However you must store the unique id of the event listener if you wish to remove it at a later time

See also
unsubscribe and onPropertyChange(Callback)

◆ operator!=()

bool ime::Object::operator!= ( const Object rhs) const
inherited

Check if two objects are not the same object.

Parameters
rhsObject to compare against this object
Returns
True if rhs is NOT the same object as this object, otherwise false

Two objects are different from each other if they have different object id's

See also
getObjectId and operator==

◆ operator=()

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

Move assignment operator.

◆ operator==()

bool ime::Object::operator== ( const Object rhs) const
inherited

Check if two objects are the same object or not.

Parameters
rhsObject to compare against this object
Returns
True if rhs is the same object as this object, otherwise false

Two objects are the same object if they have the same object id. Recall that each object instance has a unique id

See also
getObjectId and operator!=

◆ removeDestructionListener()

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

Remove a destruction listener form the object.

Parameters
Theid of the destruction listener to be removed
Returns
True if the destruction listener was removed or false if the listener with the given id does not exist

◆ 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

◆ 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
nameThe alias of the object

This function is useful if you want to refer to the object by tag instead of its id. Unlike an object id, multiple objects may have the same tag.

By default, the tag is an empty string

◆ 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

◆ unsubscribe()

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

Remove an event listener from an event.

Parameters
eventThe name of the event to remove 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 name of the object to console every time it changes
auto nameChangeId = object.onPropertyChange("name", [](Property name) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the name of the object when it changes
object.unsubscribe("name", id);

◆ 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)

Resize the camera rectangle relative to its current size.

Parameters
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)
See also
setSize

Friends And Related Function Documentation

◆ Scene

friend class Scene
friend

Needs access to constructor.

Definition at line 244 of file Camera.h.


The documentation for this class was generated from the following file:
ime::RectangleShape
A 2D shape having four sides and four corners (90 degree angles)
Definition: RectangleShape.h:36