I'm enjoying with SFML and i was thinking about FPS and bullets. So i thought:
-player fires unknown number of bullets;
-all bullets must be destroyed or game will go in memory leak;
So i was learning about dynamic sprite on screen and deleting them after X coords reach App.Width/2.
My code works fine (i spent 2h to make it work), but my question is, there is better way to manage this problem or my code is already optimized?
#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>
int main()
{
sf::RenderWindow App(sf::VideoMode(800,600,32),"SFML AnimTest",sf::Style::Titlebar | sf::Style::Close);
App.SetFramerateLimit(300);
sf::Event event;
sf::Texture bulletTexture;
bulletTexture.LoadFromFile("data/img/fire.png");
sf::Sprite *bulletSpritePtr=NULL;
std::list<sf::Sprite *> bulletList;
std::list<sf::Sprite *>::const_iterator bulletIterator;
float speed = 400.0f;
while(App.IsOpen()){
float FPS = App.GetFrameTime() / 1000.f;
if (App.PollEvent(event))
{
if (event.Type == sf::Event::Closed || (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape))
App.Close();
if(event.Type == sf::Event::MouseButtonPressed && event.MouseButton.Button == sf::Mouse::Left){
bulletSpritePtr= new sf::Sprite(bulletTexture);
bulletSpritePtr->SetPosition(sf::Mouse::GetPosition(App).x,sf::Mouse::GetPosition(App).y);
bulletList.push_back(bulletSpritePtr);
bulletSpritePtr=NULL;
}
}
//MOVE BULLETS
if(!bulletList.empty()){
for (bulletIterator = bulletList.begin(); bulletIterator != bulletList.end(); bulletIterator++){
(**bulletIterator).Move(speed * FPS, 0);
if((**bulletIterator).GetPosition().x > App.GetWidth()/2)
bulletSpritePtr=*bulletIterator;
}
}
//DELETE ALL BULLETS > APP.WITH / 2
if(bulletSpritePtr!=NULL){
bulletList.remove(bulletSpritePtr);
bulletSpritePtr=NULL;
}
App.Clear(sf::Color::Black);
if (!bulletList.empty())
for(bulletIterator = bulletList.begin(); bulletIterator != bulletList.end(); bulletIterator++)
App.Draw(**bulletIterator);
App.Display();
}
return EXIT_SUCCESS;
}