Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Drawing a sprite from within a list, within a list  (Read 1256 times)

0 Members and 1 Guest are viewing this topic.

kyle_michiels

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Drawing a sprite from within a list, within a list
« on: November 07, 2017, 07:11:43 pm »
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
« Last Edit: November 07, 2017, 07:14:07 pm by kyle_michiels »

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Drawing a sprite from within a list, within a list
« Reply #1 on: November 14, 2017, 06:32:57 am »
What happens when you compile? texture-less sprite? empty space where it should be? error message?

seems to me like this
sf::Sprite sprite = g->GetDrawable();
doesn't properly copy the sprite, but I'm no expert.

 

anything