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

Author Topic: deleting a class made up of SFML images  (Read 1468 times)

0 Members and 1 Guest are viewing this topic.

codyjae

  • Newbie
  • *
  • Posts: 2
    • View Profile
deleting a class made up of SFML images
« on: June 25, 2014, 09:47:00 pm »
Hey, new to SFML but not to C/C++. 
So here is my issue, I created a linked list (manualy) to test what i could do right off the bat.  Originally it was a program to produce various circle shapes that bounce off the walls, basically an infinite amount of balls.  Now I made a function to start deleting them to go back to having 0 circles bumping around.  I used a pointer to iterate through the chain of them (the chain was also used to see where each was and to move them slightly everytime before a new window was rendered).  Then the last one in the chain was set to be deleted.  This caused a seg fault, so i ended up just removing the item from the chain and ignoring it for the rest of its existence.  And that is how i deleted it. 

I recently created a Galaga-like game using a self-made bullets class containing a circle shape for each.  Now to stop that initial problem i put them into a deque (using push_front()).  Now same issue arrised when i tried to erase them from the deque as they exited the window.  Although its small, I do feel like there are an endless amount of bullets/balls floating around outside the window as i cannot delete them.  Is there a solution to this? or is RAII going to make it irrelevant

Thanks ahead of time

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: deleting a class made up of SFML images
« Reply #1 on: June 25, 2014, 10:46:38 pm »
In general, don't handcraft solutions if the existing alternative (here the STL) does its job well. Use containers and iterators from the standard library. And yes, avoid new/delete and use RAII.

Furthermore, your problem description is very vague and not really related to SFML...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

codyjae

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: deleting a class made up of SFML images
« Reply #2 on: June 26, 2014, 09:58:05 pm »
Thank you, and i apologize for the vagueness.

How, though, do you use RAII?  I have more or less made a small scale version of what i was hoping to do, and it gets laggy very fast with all the circles going around, even though only a few of them are actually relevant.  being able to kill off the ones not being used would be awesome, but i have no idea how.  I can attach my .cpp file (very simple to read as most of my code is in functions on .h).
 
int main(int argc, char* argv[])
{
        int enemycount;
        std::cout << "How many enemies?" << std::endl;
        std::cin >> enemycount;
        user player_user;
        sf::RenderWindow window(sf::VideoMode(800,600,10), "Shooter Jaynt");
        window.setVerticalSyncEnabled(true);
        window.setMouseCursorVisible(false);
        player_user.reloadtime = 1;
        player_user.alive = 1;
        std::vector <ai*> enemy; //contains all enemies (circles that shoot small circles)
        for(int i = 0; i < enemycount; i++)
        {
                ai* tmp;
                tmp = new ai(400,(rand() % 80));
                enemy.push_back(tmp);
        }
        while(window.isOpen())
        {      
                for(int i = 0; i < enemycount; i++)
                {
                        //this checks for collisions, causes a great deal of lag but i have no idea any other way of doing it
                        for(int p = 0; p < player_user.bullets_active.size(); p++)
                        {
                                if(player_user.bullets_active[p].bullet.getGlobalBounds().intersects(enemy[i]->player.getGlobalBounds())){
                                        enemy[i]->alive = 0;
                                        enemy[i]->player.move(700,-20);
                                        player_user.bullets_active[p].alive = 0;
                                        player_user.bullets_active[p].bullet.move(700,-20);
                                }
                        }


                        enemy[i]->reloadtimef();
                        enemy[i]->shoot_bullets(player_user);
                        enemy[i]->move_bullets();
                        enemy[i]->move_player();
                        enemy[i]->move_target(player_user);
                }

                player_user.reloadtimef();
                player_user.check_for_shoot();
                player_user.checkforexit(window);
                player_user.move_bullets(window);
                player_user.move_target_right(window);
                player_user.move_target_left(window);
                player_user.move_player_right(window);
                player_user.move_player_left(window);

                window.clear(sf::Color(10,10,10));
               
                //display enemy bullets
                for(int i = 0; i < enemycount; i++){
                        if(enemy[i]->alive == 1){
                        enemy[i]->displaybullets(window);
                        window.draw(enemy[i]->player);
                        }
                }
                player_user.displaybullets(window);

                window.draw(player_user.player);
                window.draw(player_user.target);
                window.display();

        }
        return 0;
}

I guess another thing, is there a smarter way of doing this?  Again new to SFML and this forum so feel free to yell at me if i post something overly ignorant

Thanks ahead of time


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: deleting a class made up of SFML images
« Reply #3 on: June 26, 2014, 11:16:31 pm »
How, though, do you use RAII?
Maybe you should read about the general concept on Wikipedia in order to fully understand it. I've also written an article that reflects my personal opinion.

Concerning your code, you should replace std::vector<ai*> with std::vector<ai>. Then you could use iterators or the range-based for loop for iterations -- don't use indices. And why are you using numbers for the boolean variable alive? The literals true and false exist for good reasons.

To remove outdated elements, the erase-remove idiom comes in often handy.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything