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

Author Topic: Bullet and Gun problem  (Read 2766 times)

0 Members and 1 Guest are viewing this topic.

Njifra

  • Guest
Bullet and Gun problem
« on: February 18, 2014, 09:28:29 pm »
After few shots fired, my program crushes, and I get exception:
-"Unhandled exception at 0x64404e57 in R.I.P. 4.exe: 0xC0000005: Access violation reading location 0x040d1058."
or:
-"Bla, bla... Integer division with 0"

And sometimes my bullet becomes white square...

Please, help me :(

//main.cpp

...

                if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                {
                        if (pl.gun.clock.getElapsedTime().asSeconds() > 0.6f)
                        {
                                pl.gun.fireBullet(pl.rota);
                                pl.gun.bullets[pl.gun.bullets.size() - 1].sprite.setPosition(pl.sprite.getPosition().x, pl.sprite.getPosition().y);
                                pl.gun.clock.restart();
                                cout << pl.gun.bullets.size() << endl;
                        }
                }
...
 

//Game.h

class Bullet
{
public:
        sf::Texture texture;
        sf::Sprite sprite;
        float ang;
        bool exists;
        void check()
        {
                float sX = this->sprite.getPosition().x;
                float sY = this->sprite.getPosition().y;
                if (sX < 864 && sX > -64 && sY < 864 && sY > -64)
                {
                        exists = false;
                }
                else
                {
                        cout << "Owt 2" << endl;
                }
                cout << "Owt 1" << endl;
        }
        void update()
        {
                //sprite.setOrigin(sprite.getGlobalBounds().width / 2, sprite.getGlobalBounds().height / 2);
                //sprite.setRotation(angle);
                sprite.move((cos((ang - 90)*3.14159265/180)*2) , (sin((ang - 90)*3.14159265/180)*2));
        }
        void fire()
        {
                exists = true;
        }
        void setType(int bId)
        {
                if (bId == 0)
                {
                        texture.loadFromFile("bin/imgs/bullets/bullet_08.png", sf::IntRect(0, 0, 64, 64));
                        sprite.setTexture(texture);
                }
                else if (bId == 1)
                {
                        texture.loadFromFile("bin/imgs/bullets/bullet_08.png");
                        sprite.setTexture(texture);
                }
                else if (bId == 2)
                {
                        texture.loadFromFile("bin/imgs/bullets/bullet_02.png", sf::IntRect(0, 0, 8, 8));
                        sprite.setTexture(texture);
                }
                else if (bId == 3) //green shotgun
                {
                        texture.loadFromFile("bin/imgs/bullets/bullet_20.png", sf::IntRect(0, 0, 64, 64));
                        sprite.setTexture(texture);
                }
                else if (bId == 4) //bullet - machinegun
                {
                        texture.loadFromFile("bin/imgs/bullets/bullet_00.png", sf::IntRect(0, 0, 8, 8));
                        sprite.setTexture(texture);
                }
                else if (bId == 5) //Bazooka - granade
                {
                        texture.loadFromFile("bin/imgs/bullets/bullet_04.png", sf::IntRect(0, 0, 16, 16));
                        sprite.setTexture(texture);
                }
                else if (bId == 6) //Laser Gun - laser
                {
                        texture.loadFromFile("bin/imgs/bullets/bullet_14.png", sf::IntRect(0, 0, 32, 32));
                        sprite.setTexture(texture);
                }
                else if (bId == 7) //Double bazooka and machine gun
                {
                        texture.loadFromFile("bin/imgs/bullets/bullet_11.png", sf::IntRect(0, 0, 16, 16));
                        sprite.setTexture(texture);
                }
                else if (bId == 8) //Flamethrower
                {
                        texture.loadFromFile("bin/imgs/bullets/bullet_09.png", sf::IntRect(0, 0, 64, 64));
                        sprite.setTexture(texture);
                }
        }
};
class Gun
{
public:
        int maxBullets;
        int bulletsIn;
        sf::Clock clock;
        vector<Bullet> bullets;
        void reload()
        {
                bulletsIn = maxBullets;
        }
        void fireBullet(float angle)
        {
                Bullet b;
                b.fire();
                b.setType(1);
                b.ang = angle;
                bullets.push_back(b);
                bullets[bullets.size() - 1].fire();
                bullets[bullets.size() - 1].setType(1);
                bullets[bullets.size() - 1].ang = angle;
        }
        void drawBullets()
        {
                for (int i = 0; i < bullets.size(); i++)
                {
                        if (bullets[i].exists)
                        {
                                bullets[i].update();
                                window.draw(bullets[i].sprite);
                                /*if (sX < 864 && sX > -64 && sY < 864 && sY > -64)
                                {
                                        bullets[i].exists = false;
                                        cout << "Ow 1" << endl;
                                }*/

                        }
                }
        }
};
class Player
{
public:
        Gun gun;
        bool dir[4];
        int spriteId;
        int weaponId;
        int animationFrames;
        int currentFrame;
        sf::Clock animClock;
        float max_speed;
        float rota;
        sf::Texture texture[9][31];
        sf::Sprite sprite;
        sf::Vector2f speed;
        void init()
        {
                animClock.restart();
                this->animationFrames = 5;
                this->currentFrame = 0;
                this->max_speed = 0.6f;
                this->spriteId = 0;
                this->weaponId = 0;
                for (int wep_number = 1; wep_number < 10; wep_number++)
                {
                        for (int spr_number = 0; spr_number < 31; spr_number++)
                        {
                                texture[wep_number - 1][spr_number].setSmooth(true);
                                if (spr_number < 10)
                                {
                                        if (!texture[wep_number - 1][spr_number].loadFromFile("bin/imgs/hero/weapon" + convert::toString(wep_number - 1) + "/hero_0" + convert::toString(spr_number) + ".png", sf::IntRect(0, 0, 128, 128)))
                                        {

                                        }
                                }
                                else
                                {
                                        if (!texture[wep_number - 1][spr_number].loadFromFile("bin/imgs/hero/weapon" + convert::toString(wep_number - 1) + "/hero_" + convert::toString(spr_number) + ".png", sf::IntRect(0, 0, 128, 128)))
                                        {

                                        }
                                }
                        }
                }
                this->sprite.setTexture(this->texture[this->weaponId][this->spriteId]);
                this->sprite.setOrigin(this->sprite.getTextureRect().width / 2, this->sprite.getTextureRect().height / 2);
        }
        void setWeapon(int wId)
        {
                this->sprite.setTexture(this->texture[wId][this->spriteId]);
        }
        void setSprite(int sId)
        {
                this->sprite.setTexture(this->texture[this->weaponId][sId]);
        }
        void draw()
        {
                float PI = 3.14159265;
                sf::Vector2f mouse = (sf::Vector2f)sf::Mouse::getPosition(window);
                sf::Vector2f player= this->sprite.getPosition();
               
                float delta_y = player.y - mouse.y;
                float delta_x = player.x - mouse.x;

                float angle_deg = (atan2f(delta_y, delta_x)*180.0000)/PI;
                angle_deg -= 90;
                rota = angle_deg;
                this->sprite.setRotation(angle_deg);
               


                if (this->dir[0])
                {
                        if (this->speed.x > -max_speed)
                        {
                                this->speed.x -= accleration;
                        }
                        else
                        {
                                this->speed.x = 0;
                        }
                }

                if (this->dir[1])
                {
                       
                        if (this->speed.x < max_speed)
                        {
                                this->speed.x += accleration;
                        }
                        else
                        {
                                this->speed.x = 0;
                        }
                }

                if (!this->dir[1] && !this->dir[0])
                        speed.x = 0;
               
               
                if (this->dir[2])
                {
                        if (this->speed.y > -max_speed)
                        {
                                this->speed.y -= accleration;
                        }
                        else
                        {
                                this->speed.y = 0;
                        }
                }

                if (this->dir[3])
                {
                       
                        if (this->speed.y < max_speed)
                        {
                                this->speed.y += accleration;
                        }
                        else
                        {
                                this->speed.y = 0;
                        }
                }
                if (!this->dir[2] && !this->dir[3])
                        speed.y = 0;

                if (this->dir[0] || this->dir[1] ||this->dir[2] || this->dir[3])
                {
                        soundengine.playWalkSound();
                        if (this->animationFrames > this->currentFrame)
                        {
                                if (animClock.getElapsedTime().asSeconds() > 0.05f)
                                {
                                        this->setSprite(this->spriteId + currentFrame);
                                        currentFrame++;
                                        animClock.restart();
                                }
                        }
                        else
                        {
                                currentFrame = 0;
                                this->setSprite(this->spriteId + currentFrame);
                        }
                }
                this->sprite.move(speed.x, speed.y);
                window.draw(this->sprite);
                gun.drawBullets();
        }
} pl;
 

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Bullet and Gun problem
« Reply #1 on: February 18, 2014, 10:48:08 pm »
Debugger will show you where the access violation is occurring.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Bullet and Gun problem
« Reply #2 on: February 19, 2014, 01:16:38 pm »
Please read this post first.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Njifra

  • Guest
Re: Bullet and Gun problem
« Reply #3 on: February 19, 2014, 02:59:04 pm »
Meh, anyway... I fixed all bugs :D
But Now i have the other problem :(

I have bullet:

and I dont know how to make that black background transparent using SFML...
Please reply

« Last Edit: February 19, 2014, 03:13:15 pm by Njifra »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Bullet and Gun problem
« Reply #4 on: February 19, 2014, 03:55:42 pm »
Since you're dealing with gradual black tones rather than a single black color, that's rather difficult (either you change the pixel colors manually, or you render using specific blend modes or shaders).

I'd suggest to fix that using an external image manipulation program; simply use the alpha channel for the parts that are transparent.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Njifra

  • Guest
Re: Bullet and Gun problem
« Reply #5 on: February 20, 2014, 10:38:58 pm »
And how to make single color transparent? Btw, thanks

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Bullet and Gun problem
« Reply #6 on: February 20, 2014, 10:51:57 pm »
sf::Image::createMaskFromColor(). Therefore, to load a sf::Texture, you need to take the detour via sf::Image.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Njifra

  • Guest
Re: Bullet and Gun problem
« Reply #7 on: February 20, 2014, 11:03:37 pm »
Thanks.
You are awesome  ;)

 

anything