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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - suzzhuzz

Pages: [1]
1
General / Re: Few newbie questions regarding sfml
« on: April 14, 2015, 10:20:45 am »
Yes, you can't erase it in the running iteration (at least not without updating the iterator). This is a C++ problem and has nothing to do with SFML, as such you're in the wrong forum. I recommend reading a bit more about the STL and iterators, knowing how to use them is absolutely fundamental.

Senpai....it worked...

vector<rock>::iterator show = rockmem.begin();
        while (show!=rockmem.end())
        {
                if (bo.sprite.getGlobalBounds().intersects(show->spr.getGlobalBounds()))
                {
                        show = rockmem.erase(show);
                }
                else
                {
                        show._Ptr->draw(mywindow);
                        show++;
                }
        }


2
General / Re: Few newbie questions regarding sfml
« on: April 13, 2015, 05:50:04 pm »
Store the rocks in a data structure, for example a STL container like std::vector.

Then, it's simple: instantiating a rock means inserting a new object, destroying one means erasing the object from the container. Every frame, you iterate through all the existing rocks in order to draw them and perform game logic updates.

assigned

vector<rock> rockmem;


created
rock* n = new rock(sf::Vector2f(xpos[rand() % 10],0.0f));
rockmem.push_back(*n);


rendering part

vector<rock>::iterator show = rockmem.begin();
        while (show!=rockmem.end())
        {
                if (bo.sprite.getGlobalBounds().intersects(show._Ptr->spr.getGlobalBounds()))
                {
                        rockmem.erase(show);
                        cout << "Removed" << rockmem.size() << endl;
                }
                else
                {
                        show._Ptr->draw(mywindow);
                        show++;
                }
               
        }

i dont get error and the rocks get drawn perfectly, but if i add collision, to make it erase i get the following error

Debug Assertion Failed!

Program: C:\Windows\system32\MSVCP120D.dll
File: c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector
Line: 240

Expression: vector iterators incompatible

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

3
General / Re: Few newbie questions regarding sfml
« on: April 13, 2015, 12:32:02 pm »
Store the rocks in a data structure, for example a STL container like std::vector.

Then, it's simple: instantiating a rock means inserting a new object, destroying one means erasing the object from the container. Every frame, you iterate through all the existing rocks in order to draw them and perform game logic updates.

ty,will try it...

4
General / Few newbie questions regarding sfml
« on: April 11, 2015, 09:44:53 pm »
how to destroy an object when it collides?
if (bo.sprite.getGlobalBounds().intersects(ro.spr.getGlobalBounds()))
        {
                cout << "Collides" << endl;
        }

How to instantiate an object, in this case i want to summon rocks, i do it in this manner

//rock class
void rock::create(sf::Vector2f pos)
{
        tex.loadFromFile("Images/meteorBig.png");
        spr.setTexture(tex);
        spr.setPosition(sf::Vector2f(pos.x, pos.y));
        std::cout << pos.x << std::endl;
}

void rock::draw(sf::RenderWindow &window)
{
        spr.move(0.0f, +.05f);
        window.draw(spr);
}
//Main Class

ro.create(pos);
ro.draw(window);
 
but with above i code i get the rock generated properly,but when i call it again, the previous rock goes invisible. how to make window draw as many rocks as generated.

5
General / How do i render a bullet?
« on: April 05, 2015, 10:56:14 am »
hello, i am very new to SFML and c++, the problem i have is when i click my left mouse,the bullet functions works and creates a bullet at the position, but bullet is cleared since my render() clears it,how to i make my window draw the bullet without clearing it.
here is the code..
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;

float tempnum = 0.0f;

class Game
{
public:
                Game();
        void Run();
        sf::RenderWindow mywindow;
       
private:
        void Process();
        void Update();
        void Renderer();
        void PlayerMovement();
        void ShootBullet();
        void Laser(sf::Vector2f pos);

private:
        sf::Texture PlayerIdle,PlayerR,PlayerL,PlayerB;
        sf::Sprite PlayerSprite,PlayerBullet;
        sf::FloatRect PlayerCol,RectCol;
        sf::RectangleShape CheckRect;
        sf::Font arial;
        sf::Text EndMsg;
};


Game::Game()
        : mywindow(sf::VideoMode(1280,720),"Space Droids")
{
        if(!PlayerIdle.loadFromFile("Images/player.png"))
        {
                cout<<"Error Loading Image"<<endl;
        }
        if(!PlayerR.loadFromFile("Images/playerRight.png"))
        {
                cout<<"Error Loading Image"<<endl;     
        }
        if(!PlayerL.loadFromFile("Images/playerLeft.png"))
        {
                cout<<"Error Loading Image"<<endl;     
        }
        if(!PlayerB.loadFromFile("Images/playerBullet.png"))
        {
                cout<<"Error Loading Image"<<endl;     
        }
        PlayerIdle.setSmooth(true);
        PlayerR.setSmooth(true);
        PlayerL.setSmooth(true);
        PlayerB.setSmooth(true);
        PlayerSprite.setTexture(PlayerIdle);
        PlayerSprite.setScale(sf::Vector2f(1.3f, 1.3f));
        PlayerSprite.setPosition(sf::Vector2f(580, 600));
        PlayerCol = PlayerSprite.getGlobalBounds();
        CheckRect.setSize(sf::Vector2f(100,100));
        CheckRect.setFillColor(sf::Color::White);
        CheckRect.setPosition(sf::Vector2f(300,100));
        RectCol= CheckRect.getGlobalBounds();
        arial.loadFromFile("Fonts/arial.ttf");
        EndMsg.setFont(arial);
        EndMsg.setCharacterSize(30);
        EndMsg.setColor(sf::Color::Green);
        EndMsg.setPosition(sf::Vector2f(500,400));
       
       
}

void Game::Laser(sf::Vector2f pos)
{
        sf::Texture BulletT;
        sf::Sprite BulletS;
        BulletT.loadFromFile("Images/laserRed.png");
        BulletS.setTexture(BulletT);
        BulletS.setPosition(sf::Vector2f(pos.x+37,pos.y-40));
        mywindow.draw(BulletS);
        mywindow.display();
}

void Game::PlayerMovement()
{

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
                PlayerSprite.move(-5.0f,0.0f);
                PlayerCol.left-= 5.0f;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
                PlayerSprite.move(+5.0f,0.0f);
                PlayerCol.left+= 5.0f;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
                PlayerSprite.move(0.0f,-5.0f);
                PlayerCol.top-= 5.0f;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
                PlayerSprite.move(0.0f,+5.0f);
                PlayerCol.top+= 5.0f;
        }
        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
                ShootBullet();
        }
}

void Game::ShootBullet()
{
        cout<<"Pressed "<<tempnum<<endl;
        Laser(PlayerSprite.getPosition());
        tempnum+=10.0f;
}

void Game::Update()
{
        if(PlayerCol.intersects(RectCol))
        {
                cout<<"Collides"<<endl;
                EndMsg.setString("Game Over");
        }
}

void Game::Renderer()
{
        mywindow.clear();
        mywindow.draw(PlayerSprite);
        mywindow.draw(PlayerBullet);
        mywindow.draw(CheckRect);
        mywindow.draw(EndMsg);
        mywindow.display();
}

void Game::Process()
{
          sf::Event event;
        while (mywindow.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                mywindow.close();

                        PlayerMovement();
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        {
                                PlayerSprite.setTexture(PlayerR);
                               
                        }
                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        {
                                PlayerSprite.setTexture(PlayerL);
                               
                        }
                        else
                        {
                                PlayerSprite.setTexture(PlayerIdle);
                        }

        }
}

void Game::Run()
{
        while(mywindow.isOpen())
        {
                Process();
                Update();
                Renderer();
        }
}

int main()
{
        Game game;
        game.Run();
       
}

Pages: [1]
anything