SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
Rectangle.h
1#pragma once
2
3#include "spark/core/Application.h"
4#include "spark/core/components/Transform.h"
5
6#include "spark/math/Vector2.h"
7#include "spark/math/Vector4.h"
8#include "spark/rtti/HasRtti.h"
9
10namespace spark::core::components
11{
15 class Rectangle final : public Component
16 {
17 DECLARE_SPARK_RTTI(Rectangle, Component)
18
19 public:
20 math::Vector2<float> size = {50, 50};
21 math::Vector4<float> color = {1.f, 1.f, 1.f, 1.f};
22
23 public:
24 explicit Rectangle(GameObject* parent)
25 : Component(parent) {}
26
28 : Component(parent), size(std::move(size)), color(std::move(color)) {}
29
30 void render() const override
31 {
33
34 // Get the transform matrix for the rectangle
35 auto transform_matrix = gameObject()->transform()->matrix();
36
37 transform_matrix = glm::translate(transform_matrix, {size.x / 2.0f, size.y / 2.0f, 0.0f});
38 transform_matrix = glm::scale(transform_matrix, {size.x, size.y, 1.0f});
39
40 // Draw the rectangle
41 core::Application::Instance()->window().renderer().drawQuad(transform_matrix, color);
42 }
43 };
44}
45
46IMPLEMENT_SPARK_RTTI(spark::core::components::Rectangle)
static Application * Instance()
Gets the instance of the application.
Definition Application.cpp:53
Window & window()
Gets the window for the application.
Definition Application.cpp:120
A component that can be attached to a GameObject to provide additional functionality.
Definition Component.h:20
Component(GameObject *parent)
Instantiates a new Component.
Definition Component.cpp:7
GameObject * gameObject()
Gets the GameObject that the component is attached to.
Definition Component.cpp:19
virtual void render() const
Renders the component.
Definition Component.h:59
A GameObject is any object in the game. It contains a list of components that provides functionality ...
Definition GameObject.h:26
components::Transform * transform() const
Gets the transform component of the GameObject.
Definition GameObject.cpp:63
Renderer2D< render::vk::VulkanBackend > & renderer() const
Gets the renderer of the window.
Definition Window.cpp:469
A simple component to render a square/rectangle.
Definition Rectangle.h:16
void render() const override
Renders the component.
Definition Rectangle.h:30
glm::mat4 matrix() const
Gets the transformation matrix of the transform.
Definition Transform.h:40
A vector with two components.
Definition Vector2.h:13
A vector with four components.
Definition Vector4.h:13