JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
Drawable.hpp
Go to the documentation of this file.
1// Copyright 2012-2025 Jan Niklas Hasse <jhasse@bixense.com>
2// For conditions of distribution and use, see copyright notice in LICENSE.txt
5#pragma once
6
7#include "Vec2.hpp"
8
9namespace jngl {
10
11class Sprite;
12class Text;
13
15class Drawable {
16public:
17 friend class Sprite;
18 friend class Text;
19
20 Drawable();
21 Drawable(const Drawable&) = default;
22 Drawable& operator=(const Drawable&) = default;
23 Drawable(Drawable&&) = default;
24 Drawable& operator=(Drawable&&) = default;
25 virtual ~Drawable();
26
28 virtual void step() = 0;
29
31 virtual void draw() const = 0;
32
34 virtual void setPos(double x, double y);
35
37 template <class Vect> void setPos(Vect p) {
38 setPos(p.x, p.y);
39 }
40
43
45 virtual void setCenter(double x, double y);
46
48 template <class Vect> void setCenter(Vect c) {
49 setCenter(c.x, c.y);
50 }
51
53 double getLeft() const;
54 void setLeft(double x);
55
57 double getTop() const;
58 void setTop(double y);
59
61 double getRight() const;
62 void setRight(double x);
63
65 double getBottom() const;
66 void setBottom(double y);
67
68 double getX() const;
69 void setX(double);
70
71 double getY() const;
72 void setY(double);
73
75 Vec2 getSize() const;
76
78 float getWidth() const;
79
81 float getHeight() const;
82
84 void drawBoundingBox() const;
85
87 bool contains(jngl::Vec2 point) const;
88
89protected:
91 void setWidth(float);
92
94 void setHeight(float);
95
96 Vec2 position;
97
98private:
100 float width = 0;
101 float height = 0;
102};
103
105template <class Box> bool contains(const Box& box, const Vec2 point) {
106 return (box.getX() <= point.x && point.x < box.getX() + box.getWidth() &&
107 box.getY() <= point.y && point.y < box.getY() + box.getHeight());
108}
109
110} // namespace jngl
Contains jngl::Vec2 class.
Base class for drawable objects with a position and a rectangle size.
Definition Drawable.hpp:15
double getBottom() const
Returns the distance from the bottom of the screen.
virtual void draw() const =0
Called when drawing a frame.
double getTop() const
Returns the distance from the top of the screen.
void setWidth(float)
Override width (in screen coordinates)
virtual void setPos(double x, double y)
Sets the position of the top-left of the Drawable.
void setCenter(Vect c)
Centers the Sprite at c (e.g. jngl::Vec2)
Definition Drawable.hpp:48
bool contains(jngl::Vec2 point) const
Returns whether point is inside the bounding box.
void drawBoundingBox() const
Draws a red box around the Drawable.
virtual void step()=0
Advance object's state.
float getWidth() const
Returns the width in screen coordinates.
double getRight() const
Returns the distance from the right side of the screen.
jngl::Vec2 getCenter() const
Returns the position of the center of the Drawable.
float getHeight() const
Returns the height in screen coordinates.
Vec2 getSize() const
Returns {width, height} in screen coordinates.
virtual void setCenter(double x, double y)
Centers the Sprite at (x, y)
void setHeight(float)
Override height (in screen coordinates)
void setPos(Vect p)
Sets the position of the top-left of the Drawable.
Definition Drawable.hpp:37
double getLeft() const
Returns the distance from the left side of the screen.
Higher-level representation of an image.
Definition Sprite.hpp:26
Rectangle shaped text block.
Definition text.hpp:23
Two-dimensional vector.
Definition Vec2.hpp:36
double y
y component
Definition Vec2.hpp:48
double x
x component
Definition Vec2.hpp:45
JNGL's main namespace.
bool contains(const Box &box, const Vec2 point)
Pass any class that implements getX(), getY(), getWidth() and getHeight()
Definition Drawable.hpp:105