spacebox/src/Node.cpp

162 lines
3.3 KiB
C++

/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#include "Game.hpp"
#include "Node.hpp"
Node::Node() : Node(nullptr) {}
Node::Node(Node* parent) : parent(parent)
{
SDL_Log("Constructing node %s", get_branch_as_string().c_str());
}
void Node::set_parent(Node* other)
{
parent = other;
}
bool Node::is_active() const
{
return active;
}
const Configuration& Node::configuration() const
{
return get_root()->configuration();
}
Configuration& Node::configuration()
{
return get_root()->configuration();
}
const sb::Delegate& Node::delegate() const
{
return get_root()->delegate();
}
sb::Delegate& Node::delegate()
{
return get_root()->delegate();
}
sb::Delegate& Node::get_delegate()
{
return delegate();
}
const sb::Display& Node::get_display() const
{
return get_root()->display;
}
const SDL_Renderer* Node::get_renderer() const
{
return get_root()->get_renderer();
}
SDL_Renderer* Node::get_renderer()
{
return get_root()->get_renderer();
}
const SDL_Window* Node::window() const
{
return get_root()->window();
}
SDL_Window* Node::window()
{
return get_root()->window();
}
const Input& Node::get_input() const
{
return get_root()->get_input();
}
Input& Node::get_input()
{
return get_root()->get_input();
}
Audio& Node::get_audio()
{
return get_root()->get_audio();
}
const Game* Node::get_root() const
{
const Node* r = this;
while (r->parent != NULL)
{
r = r->parent;
}
return dynamic_cast<const Game*>(r);
}
/* Get the window dimensions in pixels as a box object. If invert_y is set, the bottom left will be (0, 0).
* Otherwise, it the top left will be (0, 0). */
Box Node::window_box(bool invert_y)
{
return get_display().window_box(invert_y);
}
void Node::suppress_input()
{
get_root()->get_input().suppress();
}
void Node::suppress_input_temporarily(float length)
{
suppress_input();
if (length == 0.0f)
{
length = configuration()["input"]["default-unsuppress-delay"];
}
get_root()->get_input().unsuppress_animation.play_once(length);
}
void Node::unsuppress_input()
{
get_root()->get_input().unsuppress();
}
const std::string Node::get_branch_as_string() const
{
const Node* current = this;
std::stringstream branch;
while (current != nullptr)
{
branch << current->class_name() << " @ " << current;
if (current->parent != nullptr)
{
branch << " -> ";
}
current = current->parent;
}
return branch.str();
}
Node::~Node()
{
/* This logging call will segfault because it requires access to the configuration which may be deleted already:
*
* SDL_Log("Destroying Node %s", get_branch_as_string().c_str());
*/
/* This will cause problems for Delegates being destroyed but calling unsubscribe, among other undefined behavior
* issues (?)
*
* get_delegate().unsubscribe(this);
*/
}