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

Author Topic: Few newbie questions regarding sfml  (Read 3647 times)

0 Members and 1 Guest are viewing this topic.

suzzhuzz

  • Newbie
  • *
  • Posts: 5
    • View Profile
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.
« Last Edit: April 11, 2015, 09:48:10 pm by suzzhuzz »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Few newbie questions regarding sfml
« Reply #1 on: April 12, 2015, 09:39:51 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MasterDeveloper

  • Newbie
  • *
  • Posts: 14
  • Forever, is not forever!
    • View Profile
    • Email
Re: Few newbie questions regarding sfml
« Reply #2 on: April 12, 2015, 10:34:36 pm »
use rand() function that should help you because it sets to random!
Born to be C++ Programmer!

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Few newbie questions regarding sfml
« Reply #3 on: April 12, 2015, 10:37:52 pm »
use rand() function that should help you because it sets to random!

What the heck, please shutup and stop posting all these totally useless posts that don't even relate to the thread. Not to mention you shouldn't even be using that function in modern code. I would give a link but there is no need to clutter this thread anymore - so do not reply, just read and understand.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

suzzhuzz

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Few newbie questions regarding sfml
« Reply #4 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...

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: Few newbie questions regarding sfml
« Reply #5 on: April 13, 2015, 12:52:16 pm »
use rand() function that should help you because it sets to random!

What the heck, please shutup and stop posting all these totally useless posts that don't even relate to the thread. Not to mention you shouldn't even be using that function in modern code. I would give a link but there is no need to clutter this thread anymore - so do not reply, just read and understand.

I'm not quite sure if this is even a real person, in any post where he posted, he posted random stuff which hardly made any sense(even out of thread context)

suzzhuzz

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Few newbie questions regarding sfml
« Reply #6 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Few newbie questions regarding sfml
« Reply #7 on: April 13, 2015, 06:00:09 pm »
assigned
vector<rock> rockmem;
You mean declared/defined? There's no assignment here.

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

i dont get error and the rocks get drawn perfectly, but if i add collision, to make it erase i get the following error
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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: Few newbie questions regarding sfml
« Reply #8 on: April 13, 2015, 06:01:48 pm »
Why do you create it with new? No need to do that. If you really want to use a pointer, use something like unique_ptr.

Also, the reason why it crashes it's because erase invalidate the iterator. You should look into the erase-remove idiom. It's all C++ stuff unrelated to SFML that you should know. You might want to grab a good book before continuing with SFML.
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

suzzhuzz

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Few newbie questions regarding sfml
« Reply #9 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++;
                }
        }