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();
}