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

Author Topic: Draw sprite using vector  (Read 9299 times)

0 Members and 1 Guest are viewing this topic.

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Draw sprite using vector
« on: December 29, 2010, 09:21:32 pm »
Hello!

I am trying to use std::vector stl too make a little shootergame. But I don´t know how to save a sprite to a vector.

I have
Code: [Select]

#include "Sprite.h"

Bullet::Bullet()
{
    x = 100;
    y = 100;
    if(!Image.LoadFromFile("bullet.jpg"))
    {
        //Error
    }
}

void Bullet::shoot()
{
    Bullet* bullet1 = new Bullet;
    bullet1->SetImage(Image);
    bullets.push_back(bullet1);
}

void Bullet::drawBullet()
{
    bullet.SetImage(Image);
}

void Bullet::generateBullet()
{
    Bullet* bullet = new Bullet;
    bullets.push_back(bullet);

}
void Bullet::draw(sf::RenderWindow& App)
{
    App.Draw(bullet);
}



But the I can´t save the file to the vector who are vector<Sprite*> bullets; in bullet class.

I want to App.Draw(bullets);

What do I need to do?

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Draw sprite using vector
« Reply #1 on: December 29, 2010, 11:37:33 pm »
If the vector is declared as vector<Sprite*>, it will expect Sprite* objects to be added.

If your Bullet class is not inherited from Sprite, it has no relation to Sprite class, therefore, it can't be inserted.

Possible fix:

When declaring Bullet class: class Bullet : public Sprite{..};

Another possible fix:

Instead of declaring the vector as vector<Sprite*>, declare it as vector<Bullet*>

To render everything, you can do something like this:

Code: [Select]
for(unsigned int i = 0; i < bullets.size(); i++){
          App.Draw(bullets[i]);
}


Note: it will only work if the object "bullets" is a Sprite, or inherits from Sprite.

Hope it helps ;D

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Draw sprite using vector
« Reply #2 on: December 29, 2010, 11:40:01 pm »
OK, let's see...

First of all, if you want to literally do
Code: [Select]
App.Draw(bullets);then bullets has to be a type of sf::Drawable.

But this line
Code: [Select]
vector<Sprite*> bullets;states that bullets is a vector of Sprite pointers.

To draw the sprites that those pointers (hopefully) point to, you would need to traverse the vector and draw them one by one. Something like this:
Code: [Select]
for( size_t i = 0; i < bullets.size(); ++i )
    App.Draw( *[i] );


There are also some other things that bugs me about the Bullet implementation code you showed:

The "shoot" method... When does a bullet ever shoot? It sounds like that functionality belongs in another class.

The "drawBullet" method... It doesn't actually draw anything - the "draw" method does that.

The "generateBullet" method... Seems like it tries to do some of the same stuff "shoot" does.


In general, that Bullet class seems really messed up, like it tries to be both a Bullet, and a BulletManager class.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Draw sprite using vector
« Reply #3 on: December 29, 2010, 11:56:00 pm »
Why is everyone using STL containers of owning pointers? Although there are cases where pointers are required (polymorphism for example), using them inside containers should definitely not be the default approach to store multiple objects. For that, it's just too error-prone and has too few advantages. You might take a look at this post of mine (scroll down) to see a more detailed explanation.

In your case, why don't you take a std::vector<Bullet>?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Draw sprite using vector
« Reply #4 on: December 30, 2010, 11:35:54 am »
Quote from: "DevilWithin"
If the vector is declared as vector<Sprite*>, it will expect Sprite* objects to be added.

If your Bullet class is not inherited from Sprite, it has no relation to Sprite class, therefore, it can't be inserted.

Possible fix:

When declaring Bullet class: class Bullet : public Sprite{..};

Another possible fix:

Instead of declaring the vector as vector<Sprite*>, declare it as vector<Bullet*>

To render everything, you can do something like this:

Code: [Select]
for(unsigned int i = 0; i < bullets.size(); i++){
          App.Draw(bullets[i]);
}


Note: it will only work if the object "bullets" is a Sprite, or inherits from Sprite.

Hope it helps ;D


How do I inherits a vector to a Sprite ? I have declared the bullet class: class Bullet : public sf::Sprite

and the vector to std::vector<Bullet> bullets; in the class.

But then drawing it just don´t make it with App.Draw(bullets)

do I have to declare it in some other way? or in the class method somewhere?

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Draw sprite using vector
« Reply #5 on: December 30, 2010, 01:41:00 pm »
I solved it.

Now it´s just that how to move the bullet/sprite;

Code: [Select]

void Bullet::generateBullet()
{
    Missile* sprite = new Missile;
    sprite->SetImage(Image);
    sprite->Resize(300, 200);
    move();
    bullets.push_back(sprite);
}
//Here is the problem... I can´t use the sprite above in the next method.

//x = 20; y = 0;
void Bullet::move()
{
    sprite.Move(x, y);
}

void Bullet::draw(sf::RenderWindow& App)
{
    for(unsigned int i = 0; i < bullets.size(); i++)
    {
        App.Draw(*bullets[i]);
    }
}


Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Draw sprite using vector
« Reply #6 on: December 30, 2010, 01:51:21 pm »
Code: [Select]
void Bullet::move(int x, int y)
{
    for(unsigned int i = 0; i < bullets.size();  i++){
        bullets[i]->Move(x, y);
    }
}


That should do, to move ALL the bullets : )

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Draw sprite using vector
« Reply #7 on: December 30, 2010, 01:57:06 pm »
Quote from: "DevilWithin"
Code: [Select]
void Bullet::move(int x, int y)
{
    for(unsigned int i = 0; i < bullets.size();  i++){
        bullets[i]->Move(x, y);
    }
}


That should do, to move ALL the bullets : )


THX a lot for a fast answer :D I belive that saved my day :D

 

anything