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

Author Topic: Drawing sprite while passing ByRef RenderWindow  (Read 2486 times)

0 Members and 1 Guest are viewing this topic.

vankraster

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Drawing sprite while passing ByRef RenderWindow
« 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;
};
 


Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Drawing sprite while passing ByRef RenderWindow
« Reply #1 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?

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: Drawing sprite while passing ByRef RenderWindow
« Reply #2 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.
Follow me on Twitter, why don'tcha? @select_this

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Drawing sprite while passing ByRef RenderWindow
« Reply #3 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Drawing sprite while passing ByRef RenderWindow
« Reply #4 on: June 12, 2014, 12:04:51 pm »
How is the bullet sprite's texture created and stored?
Laurent Gomila - SFML developer

vankraster

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Drawing sprite while passing ByRef RenderWindow
« Reply #5 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
« Last Edit: June 13, 2014, 02:27:39 pm by vankraster »

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Drawing sprite while passing ByRef RenderWindow
« Reply #6 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
« Last Edit: June 13, 2014, 04:54:35 pm by Strelok »