Skip to content

Commit

Permalink
Add convenience function for creating child object.
Browse files Browse the repository at this point in the history
  • Loading branch information
ASxa86 committed Jun 17, 2024
1 parent 883e5ea commit 83003d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
19 changes: 8 additions & 11 deletions app/aspire/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,30 @@ auto main() -> int

auto root = std::make_unique<aspire::scene::Node>();

auto rect = std::make_unique<aspire::scene::Rectangle>();
auto rect = root->createChild<aspire::scene::Rectangle>();
rect->setPosition({80, 80});
rect->setSize({64, 64});
rect->onFrameFixed(
[r = rect.get()]
[rect]
{
auto pos = r->getPosition();
auto pos = rect->getPosition();
pos.x += 0.01;
r->setPosition(pos);
rect->setPosition(pos);
});

auto subrect = std::make_unique<aspire::scene::Rectangle>();
auto subrect = rect->createChild<aspire::scene::Rectangle>();
subrect->setSize({16, 16});
subrect->setColor(sf::Color::Red);
subrect->setRotation(30);

subrect->onFrame(
[s = subrect.get()]
[subrect]
{
auto rot = s->getRotation();
auto rot = subrect->getRotation();
rot += 0.01;
s->setRotation(rot);
subrect->setRotation(rot);
});

rect->addChild(std::move(subrect));

root->addChild(std::move(rect));
window->setRootNode(std::move(root));

// Add the window last to ensure frame processing occurs last.
Expand Down
20 changes: 20 additions & 0 deletions include/aspire/core/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@

namespace aspire::core
{
class Object;

namespace concepts
{
template <typename T>
concept Object = std::is_base_of<aspire::core::Object, T>::value == true;
}

/// @brief This class defines the base object and how general memory manage will be handled within aspire.
class ASPIRE_CORE_EXPORT Object
{
Expand Down Expand Up @@ -40,6 +48,18 @@ namespace aspire::core
/// @param x The object to be added to this object.
auto addChild(std::unique_ptr<Object> x) -> void;

/// @brief Create the given object type as a child of this object and then returns the pointer to the allocated object.
/// @tparam T The object type to create.
/// @return The pointer to the allocated object.
template <concepts::Object T>
auto createChild()
{
auto child = std::make_unique<T>();
auto temp = child.get();
this->addChild(std::move(child));
return temp;
}

/// @brief Remove this object from its parent.
/// @return The pointer to this object handing ownership over to the caller.
[[nodiscard]] auto remove() -> std::unique_ptr<Object>;
Expand Down

0 comments on commit 83003d7

Please sign in to comment.