Skip to content

Shapes


Overview

Shapes are classes that all inherit from the Geometry interface. Shapes serve as the base for any copyable or drawable element on the screen, so learning them is essential.


Usage

To start, we'll use the Shape class, contained in the header "remake2d/shape.hpp", whose class is templated on a number of points, and whose constructor takes two parameters:

template<size_t POINT_COUNT> requires (POINT_COUNT > (u8)point::min && POINT_COUNT <= (u8)point::max)
Shape(const Vec2d& center, const Dim2d& size);
  • POINT_COUNT : number of sides of the shape.
  • center : the shape's center.
  • size : the shape's size/diameters.

Unlike Window, shapes are positioned directly from their center. The size represents the shape's vertical and horizontal diameter. Indeed, shapes can be irregular on X and Y. POINT_COUNT itself determines the number of sides of the shape. Let's declare a shape named triangle with derived type Triangle:

rmk::Triangle triangle({200, 200}, {50, 50});

Numbers limit point are reprenseted by following enumeration:

enum class point : u8 {
    min = 0,
    max = 50
};

Methods

The Geometry class has several pure virtual methods that Shape inherits from. Among these, we have:

virtual u8 points(void)              const noexcept = 0; // number of points
virtual Dim2d size(void)             const noexcept = 0; // shape size
virtual Vec2d center(void)           const noexcept = 0; // center position
virtual const Vec2d* pointsPos(void) const noexcept = 0; // raw points array

virtual void move(const Vec2d& center) noexcept = 0;     // translate shape
virtual void rotate(f32 angle) noexcept = 0;             // rotate shape (radians)
virtual void scale(const Fact2d& scaling) noexcept = 0;  // scale shape
virtual void resize(const Dim2d& size) noexcept = 0;     // resize shape
virtual void transform(const Vec2d& center, f32 angle, const Fact2d& scaling) noexcept = 0; // translate + rotate + scale

template<IsShape S> S  as(void) const noexcept;                    // cast to another shape type
virtual bool           hasIntersected(const Geometry& other) const noexcept = 0; // collision test

Drawing a shape

Now we can display our triangle on screen thanks to the draw method of the Window class and the color method of Geometry :

void  Geometry::color(Color color) noexcept; // set color
Color Geometry::color(void)  const noexcept; // get current color

void Window::draw(const Printable& object) noexcept;
  • color : draw color
  • object : drawable object

Your question is probably this:

The object parameter is of type Printable, but my figure is of type Triangle! Furthermore, Geometry does not have any color methods!

This is perfectly normal, and the answer is simply that the geometry class inherits from the Printable trait class, which makes it drawable on the screen. Note that the color method actually belongs to the Printable class, but this will be covered in another lesson.

Let's place ourselves in the game loop and write:

triangle.color(rmk::color::red); // set draw color to red

//In render loop
win.draw(triangle);
#include <remake2d/window.hpp>
#include <remake2d/shape.hpp>
#include <remake2d/loop.hpp>

int main(void) {
    rmk::Window win;
    rmk::Triangle triangle({200, 200}, {50, 50});
    triangle.color(rmk::color::red);

    rmk::loop.execute(win, [&](void) {
        win.draw(triangle);
    });

    rmk::loop.update();
}

drew triangle

It is also possible to fill the shape with the fill method, similar to draw:

void fill(const Printable& object) noexcept;
win.fill(triangle);

filled triangle

Transparency

It is possible to make shapes somewhat transparent. By reducing the alpha value of Color, we also reduce the opacity of the shape drawn on screen.

example:

without transparency:

triangle.color(rmk::color::red);
win.clear(rmk::color::green);
win.fill(triangle);

opaque triangle

with transparency:

triangle.color({255, 0, 0, 128}); // semi-transparent red triangle
win.clear(rmk::color::green);
win.fill(triangle);

semi-transparent triangle

You can change the transparency mode via the blendMode method of the Window class, which can take five distinct values:

namespace window {

enum class blendmode : u8 {
    none,
    normal,
    add,
    mod,
    mul
};

}
  • none : disable transparency on the window
  • normal : default transparency mode
  • add : additive mode with the background color
  • mod : modulate mode with the background color
  • mul : multiply mode with the background color

example:

triangle.color({255, 0, 0, 128});
win.blendMode(rmk::window::blendmode::add);

win.clear(rmk::color::green);
win.fill(triangle);

additive semi-transparent triangle


Shape types

RE:MAKE 2D offers several type aliases and specialized types for shapes:

// Alias
using Line      = Shape<2>;   // line segment
using Diamond   = Shape<4>;   // diamond shape
using Hexagon  = Shape<6>;   // hexagon
using Ellipse   = Shape<36>;  // ellipse approximation

// Derived types
class Point     : public Shape<1>;  // single point
class Triangle  : public Shape<3>;  // triangle
class Rectangle : public Shape<4>;  // axis-aligned rectangle (optimized build)
class Square    : public Rectangle; // uniform rectangle
class Circle    : public Ellipse;   // circle (w == h enforced)

Info

For the Square and Circle types, only the transform method is overridden to take just the width into account. The constructors of the Square and Circle types take an f32 rather than a Dim2d in their constructor.


Property

Type Copiable Movable Bases and Traits
Geometry Yes Yes Followable and Printable
Shape Yes Yes Geometry
Shape derived Yes Yes Geometry

Previous chapter Next chapter