Skip to content

Viewport

Viewports are extensions of windows that allow restricting rendering to a specific area of the screen.


Overview

Viewports are represented by the Window::Viewport structure. They allow delimiting the rendering zone to a defined portion of a window.

By default, every object is rendered within the global viewport of the window, whose origin is the top-left corner:

(0, 0)  _ _ _ _ _ _ _ _ _ _ _ _ _ (x)
       |
       |
       |
       |
       |
       |
       |
      (y)

Viewports allow restricting this area to a defined portion:

(0, 0)  _ _ _ _ _ _ _ _ _ _ _ _ _ (x)
       |
       |
       |     (0, 0) _ _ _ _ (x')
       |           |
       |           |
       |          (y')
       |
      (y)

Their constructor is as follows:

Viewport(void);
Viewport(const Area& zone);
Viewport(const Area& zone, Camera& camera);
  • zone — rendering area of the viewport
  • camera — camera attached to the viewport

In this section, only the constructor taking an Area parameter will be covered. Camera usage will be addressed in the tilemap section.


Usage

Viewports are created and managed through the following Window methods:

void addViewport(std::string_view tag, Viewport vp) noexcept; // register a named viewport
void removeViewport(std::string_view tag)           noexcept; // delete a named viewport
void useViewport(std::string_view tag)              noexcept; // activate a viewport for subsequent draws
void resetViewport(void)                            noexcept; // restore the default full-window viewport
  • tag — unique identifier of the viewport

For example:

rmk::Window win;
rmk::Rectangle rect(0, 200); // position (0, 0) — size 200x200
rmk::Window::Viewport vp(rmk::Area(500, 100, 100, 100));

win.addViewport("view", vp);

win.fill(rect, rmk::color::red);
win.fill(rect, rmk::color::cyan, "view");

Rectangle drew on viewport

The useViewport method sets a default active viewport, used when no identifier is passed to the window's rendering methods. resetViewport deactivates it and restores the global viewport.


Previous chapter Next chapter