SFML community forums

Help => Window => Topic started by: vankraster on June 12, 2014, 10:00:47 am

Title: Drawing sprite while passing ByRef RenderWindow
Post by: vankraster on June 12, 2014, 10:00:47 am
The problem is that if I pass the RenderWindow ByRef to function I don't see the bullet but if I create the bullet in main.cpp I see the bullet, on debug i see that the programs enter in BULLET DRAWING and generates NO error, here is the piece of code:
in Main I have
while (window.isOpen())
        {
                ControlKeyPress(&window, &event);  

                window.clear();
                window.draw(game.getBACKSprite());
                window.draw(me.retPlane());

                me.DrawBullets(&window);
 
                window.display();
        }
 
The draw function is :
void MyPlayer::DrawBullets(sf::RenderWindow *window){
        int usB = 0;
        for (int i = 0; i < 6; i++){
                if (!this->bullet[i].isDead()){
                        window->draw(this->bullet[i].retBullet());  // BULLET DRAWING
                        usB++;
                }
        }
        this->usingBullet = usB;
};

 
sf::Sprite Bullet::retBullet(){
        if (!this->dead){
                if (this->posX <= 800 - this->MoveVelocity)
                        this->posX += MoveVelocity;
                else
                        this->dead = true;

                this->bulletSprite.setPosition((float)this->posX, (float)this->posY);
        }
        return this->bulletSprite;
};
 

Title: Re: Drawing sprite while passing ByRef RenderWindow
Post by: Jesper Juhl on June 12, 2014, 11:16:46 am
A few random comments:

Why are you explicitly dereferencing "this" everywhere?
Why do you have a ";" after your function definitions?
You should generally prefer C++ style casts (static_cast<> and friends) over C style casts.
Why are you passing "window" by pointer to draw bullets? You don't check if it is null, so the function assumes it is always valid, so why not pass by reference?
Title: Re: Drawing sprite while passing ByRef RenderWindow
Post by: select_this on June 12, 2014, 11:35:13 am
Why are you explicitly dereferencing "this" everywhere?

That's fairly common if you've come from a language such as PHP where this is not automatically dereferenced.
Title: Re: Drawing sprite while passing ByRef RenderWindow
Post by: Strelok on June 12, 2014, 12:01:38 pm
sf::RenderWindow* window; //pointer to sf::RenderWindow
sf::RenderWindow& window; //reference to sf::RenderWindow
 
http://en.wikipedia.org/wiki/Reference_%28C++%29#Relationship_to_pointers (http://en.wikipedia.org/wiki/Reference_%28C++%29#Relationship_to_pointers)
Title: Re: Drawing sprite while passing ByRef RenderWindow
Post by: Laurent on June 12, 2014, 12:04:51 pm
How is the bullet sprite's texture created and stored?
Title: Re: Drawing sprite while passing ByRef RenderWindow
Post by: vankraster on June 13, 2014, 02:23:32 pm
The Bullet contructor is :

Bullet::Bullet(){
        this->MoveVelocity = 5;
        this->posX = 0;
        this->posY = 0;
        this->dead = true;

        if (this->bulletTexture.loadFromFile("img\\bullet\\bullet.png"))
        {
                this->bulletTexture.setSmooth(true);
                this->bulletTexture.setRepeated(true);

                this->bulletSprite.setTexture(this->bulletTexture);
                //this->bulletSprite.setScale(0.7F, 0.7F);
        }
}            


Bullet::Bullet(int x, int y){
        this->MoveVelocity = 5;
        this->posX = x;
        this->posY = y;
        this->dead = false;
        if (this->bulletTexture.loadFromFile("img\\bullet\\bullet.png"))
        {

                this->bulletTexture.setSmooth(true);
                this->bulletTexture.setRepeated(true);

                this->bulletSprite.setTexture(this->bulletTexture);
                this->bulletSprite.setColor(sf::Color::Red);
                //this->bulletSprite.setScale(0.7F, 0.7F);
        }
};

 
//BULLET piece of HEADER:

#ifndef Bullet_H
#define Bullet_H

class Bullet{
public:
        Bullet();
        Bullet(int x, int y);
        sf::Sprite retBullet();
        void Explode();
        void SetPos(int x, int y);
        bool isDead();
protected:
private:
        sf::Texture bulletTexture;
        sf::Sprite bulletSprite;

        int posX;
        int posY;

        int MoveVelocity;

        bool dead;
};
#endif

 

it is initialized and stored well, on debug i don't receive error  on texture or on sprite it seems that the procedure draws it correctly but i don't see it


P.S.
@Jesper Juhl   @select_this  I am a C# programmer
Title: Re: Drawing sprite while passing ByRef RenderWindow
Post by: Strelok on June 13, 2014, 04:48:43 pm
I know this is off topic but if you can use C++11 features this
Bullet::Bullet() : Bullet(0, 0, true/*this is not mandatory*/) {}
Bullet::Bullet(int x, int y, bool isDead = true)
    : MoveVelocity(5)
    , posX(x)
    , poxY(y)
    , dead(isDead)
{
    if (bulletTexture.loadFromFile("img\\bullet\\bullet.png"))
    {
        bulletTexture.setSmooth(true);
        bulletTexture.setRepeated(true);
        bulletSprite.setTexture(bulletTexture);
        if(!isDead)
            bulletSprite.setColor(sf::Color::Red);
        //bulletSprite.setScale(0.7F, 0.7F);
    }
}
 
will save you some duplicated code