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

Author Topic: Drawing out objects in a grid shape!  (Read 3187 times)

0 Members and 2 Guests are viewing this topic.

haz

  • Newbie
  • *
  • Posts: 8
    • View Profile
Drawing out objects in a grid shape!
« on: December 04, 2015, 10:52:17 pm »
EDIT: Sorry if this was not the best place to post it! If someone could move it to a more appropriate area, that would be great!

Hey!

Could do with some help/guidance. I am trying to draw some objects that I have stored in a vector in a grid format.

I push back a number of objects in to a vector with no default position. Each object has a sprite & texture.

I then call a function which I want to go through the vector and put the objects in a grid like shape. But for some reason, it only displays the first object however when I look at the individual objects in the vector they're positions are different.

Some code below to help explain what I am doing.

Thanks for any help/guidance!

//Called at initialisation
        for (int i = 0; i < m_maxEnemies; i++)
        {
                m_vEnemies.push_back(new EnemyEntity());
        }

//Then I call the function below to try and manipulate their positions to where I want them to be.
        for (int i = 0; i < m_maxEnemies; i++)
        {
                m_vEnemies[i]->getEntitySprite().setPosition(10 + (i * 10), 0);
        }
//The above code seems to work when I look at the debug information however only the first enemy is displayed, a bit stuck here!

//I draw the objects from my main.cpp, each object has a flag to check whether it is active, by default when creating one instance of type 'EnemyEntity()' it is set to true so they should be all being drawn.
       
for (auto &enemy : enemyHandler->getVectorOfEnemies())
                {
                        if (enemy->isEntityActive())
                        {
                                m_renderWindow.draw(*enemy);
                        }
                }
 
« Last Edit: December 04, 2015, 10:55:12 pm by haz »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Drawing out objects in a grid shape!
« Reply #1 on: December 05, 2015, 12:14:16 am »
It's hard to tell what might be going wrong just by looking at the snippet of code you posted. Have you tried to use std::cout to print the positions of your enemies in the loop as you draw them? Does it print to the console as many times as you expect? Are the values what you expect?
« Last Edit: December 05, 2015, 12:19:36 am by Arcade »

haz

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Drawing out objects in a grid shape!
« Reply #2 on: December 05, 2015, 12:22:06 am »
Thanks for the reply Arcade!

Yeah so, I use std::cout and I can see that the 'X' positions of the object has been incremented by 20 for each one and the 'Y' value stays the same! If I alter the 'Y' position the positions update as expected however only the first element from the vector is displayed.

EDIT: I just re-read your comment, yes I just debugged in the draw loop and the positions are exactly the same as when doing the code below!

        for (int i = 0; i < m_vEnemies.size(); i++)
        {
                m_vEnemies[i]->getEntitySprite().setPosition(20 + (i * 10), 0);
                std::cout << m_vEnemies[i]->getEntityPosition().x << std::endl;
                std::cout << m_vEnemies[i]->getEntityPosition().y << std::endl;
        }
 
« Last Edit: December 05, 2015, 12:26:56 am by haz »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Drawing out objects in a grid shape!
« Reply #3 on: December 05, 2015, 12:27:07 am »
Can you move those print statements to be next to where you draw? Do you still get the values you expect?

for (auto &enemy : enemyHandler->getVectorOfEnemies())
{
   if (enemy->isEntityActive())
   {
      std::cout << enemy->getEntityPosition().x << std::endl;
      std::cout << enemy->getEntityPosition().y << std::endl;
      m_renderWindow.draw(*enemy);
   }

}
 


I'm assuming getEntityPosition() returns the sprite's position.

haz

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Drawing out objects in a grid shape!
« Reply #4 on: December 05, 2015, 12:27:52 am »
Haha sorry, yes I did, and the same values are being spat out.

And yes that returns the sprites position!

Edit: is this the best way to go about drawing objects in a grid?
« Last Edit: December 05, 2015, 12:35:03 am by haz »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Drawing out objects in a grid shape!
« Reply #5 on: December 05, 2015, 12:34:54 am »
Can you show how you are setting up your sprites and textures?

I'm also assuming EnemyEntity inherits from sf::Drawable. Can you show how you implemented the draw call?

haz

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Drawing out objects in a grid shape!
« Reply #6 on: December 05, 2015, 12:49:37 am »
Yep so, I have a base class called 'Entity' which inherits from sf::Drawable.

I have set up 'EnemyEntity' class pretty similar to the way I set up a 'Bullet' class which are also derived from Entity however in this case the functionality is completely different.

I set up the texture of the enemies in the handler. I have a 'Texturehandler' class which I can add and get textures. In the enemies class I have function that takes in a reference to a texture and the goes through the vector and adds them accordingly to each object.

Some code below to help explain better.

I tried this code below, this works and the enemies are drawn on the screen at random positions. So I am not sure what is causing them not to be drawn from the code shown in an earlier post.

               
m_vEnemies[i]->getEntitySprite().setPosition(rand() % m_screenWidth, rand() % m_screenHeight);
 

EnemyEntity::EnemyEntity()
        :Entity()
        , m_speed(0.1)
        , m_fireRate(0.1)
{
        setEntityType(eEnemy);
        getEntitySprite().setScale(0.1f, 0.1f);
        setEntityActive(true);
}

void EnemyHandler::setEnemyTexture(sf::Texture& enemyTexture)
{
        for (m_it = m_vEnemies.begin(); m_it != m_vEnemies.end(); m_it++)
        {
                EnemyEntity* pEnemy = *m_it;
                pEnemy->setEntityTexture(enemyTexture);
        }
}

//Base class draw function I have used in all of my other derived classes which works fine.
void Entity::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        target.draw(m_entitySprite, states);
}
 

 

anything