Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::render::Camera Class Reference

The part of the world shown on screen: where it looks, how close, within which bounds. More...

#include <aegis2d/render/camera.hpp>

Public Member Functions

 Camera (Vec2 view)
 Creates a camera looking at the middle of a view-sized world, at zoom 1.
 
Vec2 view () const noexcept
 Size of the view on screen.
 
void follow (Vec2 target) noexcept
 Looks at a point, or as close to it as the bounds allow.
 
Vec2 center () const noexcept
 World point at the middle of the view.
 
void set_zoom (float zoom)
 Changes the zoom, keeping the center within the bounds.
 
float zoom () const noexcept
 Screen pixels per world pixel.
 
void set_bounds (const std::optional< Rect > &bounds)
 Limits the view to an area of the world, or lets it go anywhere.
 
const std::optional< Rect > & bounds () const noexcept
 The area the view stays within.
 
void set_shake (Vec2 offset) noexcept
 Moves the picture on screen without moving the camera, for screen shake.
 
Vec2 shake () const noexcept
 The shake of this frame.
 
Rect visible () const noexcept
 World area shown, without the shake, for culling.
 
Vec2 to_screen (Vec2 world) const noexcept
 Where a world point appears on screen.
 
Vec2 to_world (Vec2 screen) const noexcept
 Which world point is under a screen point, such as the mouse.
 
Sprite to_screen (const Sprite &sprite) const noexcept
 A sprite of the world, placed and scaled on screen.
 

Detailed Description

The part of the world shown on screen: where it looks, how close, within which bounds.

World and screen are both in logical pixels from the top-left corner. The camera shows the world point center() at the middle of a view of a fixed size on screen, zoom() screen pixels per world pixel; with bounds, the view never shows anything outside them. Give it to the renderer (IRenderer::set_camera()), which places the sprites drawn after; draw the HUD without it.

render::Camera camera{{1280.0F, 720.0F}};
camera.set_bounds(render::Rect{.width = 4000.0F, .height = 720.0F});
...
camera.follow(player_position); // each frame
camera.set_shake(effects.offset()); // ScreenEffects shake the camera
renderer.set_camera(camera);
... draw the world, in world coordinates ...
renderer.set_camera(std::nullopt);
... draw the HUD, in screen coordinates ...
The part of the world shown on screen: where it looks, how close, within which bounds.
Definition camera.hpp:31
void set_bounds(const std::optional< Rect > &bounds)
Limits the view to an area of the world, or lets it go anywhere.
Axis-aligned rectangle.
Definition renderer.hpp:20
float width
Width, positive.
Definition renderer.hpp:26

Constructor & Destructor Documentation

◆ Camera()

aegis2d::render::Camera::Camera ( Vec2  view)
explicit

Creates a camera looking at the middle of a view-sized world, at zoom 1.

Parameters
viewSize of the view on screen, in logical pixels, such as the logical screen.
Exceptions
std::invalid_argumentif a component of view is not above 0 or not finite.

Member Function Documentation

◆ bounds()

const std::optional< Rect > & aegis2d::render::Camera::bounds ( ) const
noexcept

The area the view stays within.

Returns
The value given to set_bounds().

◆ center()

Vec2 aegis2d::render::Camera::center ( ) const
noexcept

World point at the middle of the view.

Returns
The last point followed, kept within the bounds.

◆ follow()

void aegis2d::render::Camera::follow ( Vec2  target)
noexcept

Looks at a point, or as close to it as the bounds allow.

Called each frame with the player's position, the view scrolls with the player and stops at the edges of the world. On an axis where the bounds are smaller than the visible area, the view stays centered on them.

Parameters
targetWorld point to show at the middle of the view.

◆ set_bounds()

void aegis2d::render::Camera::set_bounds ( const std::optional< Rect > &  bounds)

Limits the view to an area of the world, or lets it go anywhere.

Parameters
boundsWorld area the view stays within; nothing for no limit, the default.
Exceptions
std::invalid_argumentif a dimension of bounds is not above 0, or a value is not finite.

◆ set_shake()

void aegis2d::render::Camera::set_shake ( Vec2  offset)
noexcept

Moves the picture on screen without moving the camera, for screen shake.

Parameters
offsetScreen offset of this frame, such as ScreenEffects::offset(); zero to stop.

◆ set_zoom()

void aegis2d::render::Camera::set_zoom ( float  zoom)

Changes the zoom, keeping the center within the bounds.

Parameters
zoomScreen pixels per world pixel: 2 shows the world twice as large.
Exceptions
std::invalid_argumentif zoom is not above 0 or not finite.

◆ shake()

Vec2 aegis2d::render::Camera::shake ( ) const
noexcept

The shake of this frame.

Returns
The offset given to set_shake(); zero by default.

◆ to_screen() [1/2]

Sprite aegis2d::render::Camera::to_screen ( const Sprite &  sprite) const
noexcept

A sprite of the world, placed and scaled on screen.

Parameters
spriteSprite in world coordinates.
Returns
The same sprite at to_screen() of its corner, its size times zoom().

◆ to_screen() [2/2]

Vec2 aegis2d::render::Camera::to_screen ( Vec2  world) const
noexcept

Where a world point appears on screen.

Parameters
worldPoint in the world.
Returns
Its screen position, shake included.

◆ to_world()

Vec2 aegis2d::render::Camera::to_world ( Vec2  screen) const
noexcept

Which world point is under a screen point, such as the mouse.

Parameters
screenPoint on screen.
Returns
The world point that to_screen() places there.

◆ view()

Vec2 aegis2d::render::Camera::view ( ) const
noexcept

Size of the view on screen.

Returns
The size given to the constructor.

◆ visible()

Rect aegis2d::render::Camera::visible ( ) const
noexcept

World area shown, without the shake, for culling.

Returns
The rectangle centered on center(), of view() / zoom().

◆ zoom()

float aegis2d::render::Camera::zoom ( ) const
noexcept

Screen pixels per world pixel.

Returns
The zoom given to set_zoom(); 1 by default.

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