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-2022 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
12class Drawable {
13public:
14 Drawable();
15 Drawable(const Drawable&) = default;
16 Drawable& operator=(const Drawable&) = default;
17 Drawable(Drawable&&) = default;
18 Drawable& operator=(Drawable&&) = default;
19 virtual ~Drawable();
20
22 virtual void step() = 0;
23
25 virtual void draw() const = 0;
26
28 virtual void setPos(double x, double y);
29
31 template <class Vect> void setPos(Vect p) {
32 setPos(p.x, p.y);
33 }
34
37
39 virtual void setCenter(double x, double y);
40
42 template <class Vect> void setCenter(Vect c) {
43 setCenter(c.x, c.y);
44 }
45
47 double getLeft() const;
48 void setLeft(double x);
49
51 double getTop() const;
52 void setTop(double y);
53
55 double getRight() const;
56 void setRight(double x);
57
59 double getBottom() const;
60 void setBottom(double y);
61
62 double getX() const;
63 void setX(double);
64
65 double getY() const;
66 void setY(double);
67
69 Vec2 getSize() const;
70
72 float getWidth() const;
73
75 float getHeight() const;
76
78 void drawBoundingBox() const;
79
81 bool contains(jngl::Vec2 point) const;
82
83protected:
84 Vec2 position;
85 float width = 0;
86 float height = 0;
87};
88
90template <class Box> bool contains(const Box& box, const Vec2 point) {
91 return (box.getX() <= point.x && point.x < box.getX() + box.getWidth() &&
92 box.getY() <= point.y && point.y < box.getY() + box.getHeight());
93}
94
95} // namespace jngl
Contains jngl::Vec2 class.
Base class for drawable objects with a position and a rectangle size.
Definition Drawable.hpp:12
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.
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:42
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 setPos(Vect p)
Sets the position of the top-left of the Drawable.
Definition Drawable.hpp:31
double getLeft() const
Returns the distance from the left side of the screen.
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:90