Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <vector>
- #include <iostream>
- #include "Bullet.h"
- #include "BulletPool.h"
- #include "Player.h"
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(640, 480), "Demo");
- // flags
- bool isFiring = false;
- // Define Objects:
- Player player(sf::Vector2f(50,50));
- BulletPool pool(Bullet(), 10); // create 10 bullets in the pool
- std::vector<Bullet> bullets;
- bullets.reserve(10); // reserve memory
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
- isFiring = true;
- }
- }
- window.clear();
- // fire bullet
- if(isFiring == true)
- {
- if(bullets.size() < 10) // make sure the bullets don't go over 10
- {
- Bullet &newBullet = pool.GetBullet(); // get a bullet from the pool by reference
- newBullet.SetPos(sf::Vector2f(player.GetX(), player.GetY()));
- bullets.push_back(newBullet); // add the bullet to the vector
- }
- isFiring = false;
- }
- // update bullet
- for( int i = bullets.size()-1; i >= 0; i--)
- {
- bullets[i].Draw(window);
- bullets[i].Fire(3);
- // if bullet goes past the screen
- if(bullets[i].GetRight() > 650)
- {
- bullets.erase(bullets.begin()); // delete element
- Bullet &oldBullet = bullets[i]; // get reference from old bullet
- pool.ReturnBullet(oldBullet); // pass the old bullet back to the pool by reference
- }
- }
- player.Draw(window);
- window.display();
- // debugg information making sure it doesn't create a new bullet that is over 10
- std::cout << "bullets in vector: " << bullets.size() << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement