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

Author Topic: Window clear? Trying to shoot a bullet  (Read 2210 times)

0 Members and 1 Guest are viewing this topic.

lenbot

  • Newbie
  • *
  • Posts: 1
    • View Profile
Window clear? Trying to shoot a bullet
« on: July 06, 2014, 07:29:10 pm »
First I am new to SFML and C++ I used to program awhile ago so I am learning as is everybody and I know my code is not really great or formatted great so take it easy on me. My problem is this when I shoot my bullet instead of clearing for each new position of the image it just stretches across the screen  and slows down my program while left button is clicked. I want it to do what a bullet should do just shoot across the screen. Maybe it's my programing or maybe I am missing something in SFML Thanks!!

#include <SFML/Graphics.hpp>
#include <iostream>

int windowX = 800;
int windowY = 600;
float x = 0;
float y = 0;
sf::RenderWindow window(sf::VideoMode(windowX, windowY), "My window");
int controller(){
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
    // left key is pressed: move our character
 if (x <= 0){
 }else{
        x = x-.2;
}
       
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
    // left key is pressed: move our character
   if (x >= windowX -50){
 }else{
        x = x+.2;
 }

       
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
    // left key is pressed: move our character
    if (y >= windowY -40){
 }else{
        y = y+.2;
 }
       
       
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
    // left key is pressed: move our character
      if (y <= 0){
 }else{
                y = y-.2;
 }

       
}

return 0;
}



int main()
{
    // create the window
   

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
                window.clear(sf::Color::Blue);
                       
sf::Texture texture;
sf::Sprite sprite;
sprite.setPosition (x,y);
if (!texture.loadFromFile("image.png"))
{
    // error...
}
controller();
                bool drawbullet = false;
                float bulletx = 49;
                sf::Texture bullet1;
            sf::Sprite spritebullet1;
                if (!bullet1.loadFromFile("bullet.png"))
                {
                 
                }
       
       
                if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
                {
                        drawbullet = true;
                }
        if (drawbullet == true){
                 
                for (float i = 0;i <= windowX; i = i + 1){
                spritebullet1.setTexture(bullet1);
                spritebullet1.setPosition (i,0);
            window.draw(spritebullet1);
       
                }
                }
               
sprite.setTexture(texture);
window.draw(sprite);

        // end the current frame
        window.display();
    }

    return 0;
}

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Window clear? Trying to shoot a bullet
« Reply #1 on: July 06, 2014, 10:21:42 pm »
You're recreating the sprites and textures every frame, including reloading the textures from files.  You should do that once at the beginning of main() and then leave them alone in the game loop until you have a reason to change them (ie, when the player shoots you'll make a new sprite for that bullet, but it can use the existing bullet texture).

Also, global variables are bad.  Move them all into main().  And try to indent more consistently so your code is easier to read.

nomdeplume

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Window clear? Trying to shoot a bullet
« Reply #2 on: July 07, 2014, 12:56:31 pm »
SFML is doing exactly what you told it to do:
  • Clear the display buffer
  • If the mouse button is pressed: render 800 bullets along the top of the screen, each 1 pixel further along than the previous one
  • Render the player sprite
  • Display the new scene
The basic problem here is that you're running the entirety of the bullet animation within a single frame. What you need to do is set the position of the bullet the first time the mouse button is pressed and make a note that the bullet is now active (should be displayed). Then as long as the bullet is active, move the bullet in a similar manner to how you move the player position each frame, and then draw it ONCE at the new location (like with the player sprite). Once the bullet goes outside play, make a note that it's no longer active.