Hey guys!
Im trying to draw a sprite within a list of components, within a list of gameobjects.
These are custom classes I have written.
How I'm trying to draw the sprite:
for (std::list<GameObject*>::iterator it = gameObjects.begin(); it != gameObjects.end(); it++)
{
std::list<Component*> compList = it._Ptr->_Myval->GetAllComponents();
for (std::list<Component*>::iterator ita = compList.begin(); ita != compList.end(); ita++)
{
Graphic *g = static_cast<Graphic*>(ita._Ptr->_Myval);
if (g)
{
sf::Sprite sprite = g->GetDrawable();
if (g->GetDrawable().getTexture() == nullptr)
{
std::cout << "No texture has been assigned" << std::endl;
}
_window.draw(sprite);
}
else
{
std::cout << "Casting Failed" << std::endl;
}
}
GameObject Logics (simplefied):
std::list<Component*> components;
void ECE::GameObject::AddComponent(Component & c)
{
components.emplace_back(&c);
}
std::list<Component*> ECE::GameObject::GetAllComponents()
{
return components;
}
And the Graphic class logics:
sf::Sprite _sprite;
sf::Sprite ECE::Graphic::GetDrawable()
{
return _sprite;
}
And finally adding the Graphic to the gameobject:
ECE::Graphic _G_myGraphic;
TestObject::TestObject()
{
_G_myGraphic.SetTexture("Game/GFX/mario.png");
_G_myGraphic.SetPostion(sf::Vector2f(100, 100));
AddComponent(_G_myGraphic);
}
And lastly adding the gameObject to the gameObject list:
void TestGame::Start()
{
AddGameObject(_GO_testObject);
std::cout << "Added gameObject" << std::endl;
}
void ECE::Game::AddGameObject(GameObject & go)
{
gameObjects.emplace_back(&go);
}
I can print any data from the sprite, but just drawing it seems to be a problem.
Any help would be appreciated!
Greetings!
Kyle