0

I'm doing a game project with SFML and I encountered a small problem,

Here is my code:

#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
using namespace std;
using namespace sf;
int main() {

    sf::RenderWindow Window;
    Window.create(sf::VideoMode(490, 485), "My First Sfml Game");

    sf::Texture pTexture1;
    sf::Sprite playerImage;



    if (!pTexture1.loadFromFile("Data/dots.png"))
        std::cout << "error could not load player image" << std::endl;

    playerImage.setTexture(pTexture1);

    sf::Texture pTexture2;
    sf::Sprite lineImage;
    if (!pTexture2.loadFromFile("Data/line.png"))
        std::cout << "error could not load player image" << std::endl;
    lineImage.setTexture(pTexture2);

    int v;

    while (Window.isOpen()) {
        sf::Event event;
        while (Window.pollEvent(event)) {
            switch (event.type) {
            case sf::Event::Closed:
                Window.close();
                break;
            case Event::KeyPressed:
                if (event.key.code == Keyboard::Right)
                    lineImage.move(5, 0);
                else if (event.key.code == Keyboard::Left)
                    lineImage.move(-5, 0);
                else if (event.key.code == Keyboard::Up)
                    lineImage.move(0, -5);
                else if (event.key.code == Keyboard::Down)
                    lineImage.move(0, 5);
                else if (event.key.code == Keyboard::Space)
                    lineImage.setRotation(90);
                break;
            case Event::KeyReleased:
                if (event.key.code == Keyboard::Right)
                    lineImage.move(0, 0);
                else if (event.key.code == Keyboard::Left)
                    lineImage.move(-0, 0);
                else if (event.key.code == Keyboard::Up)
                    lineImage.move(0, -0);
                else if (event.key.code == Keyboard::Down)
                    lineImage.move(0, 0);
                else if (event.key.code == Keyboard::Space)
                    lineImage.setRotation(90);
                break;
            }
            if (event.type == Event::KeyReleased && event.key.code == Keyboard::Space)
                lineImage.setRotation(180);
        }


        Window.draw(playerImage);
        Window.draw(lineImage);
        Window.display();
    }
    return 0;
}

Images in the code here : postimg.org/gallery/71ufmoi/da508bc3/

the problem is that I need to rotate the lineImage when I click the spacebar but when I click space it rotate 90 then back again 90! I need to make it stable when I click spacebar it rotate 90 then when I click space again it rotate 90 to the original! Another problem I have is I'm doing dots game so, how can I make the logic of the line so I can let it between two points when I hit the enter key?

Rapptz
  • 20,807
  • 5
  • 72
  • 86
Omar Ahmed
  • 23
  • 1
  • 4

1 Answers1

3

I need to make it stable when I click spacebar it rotate 90 then when I click space again it rotate 90 to the original!

Instead of using setRotation you can use rotate and keep the angle in a variable so that your program :

You would do the rotation only when the key is released.

Also, consider using switch instead of a bunch of if-else. It will reduce the code duplication of event.key.code == and make your code more readable.

Note that your Window variable has an inconsistent name : all other variables start with a lower case but this one doesn't. It's not a big deal for such small piece of code but it's always a good practice to keep the same naming convention across the whole program.

So, to sum up, your code could look like this :

// includes...

int main(int, char**) {
    // window and texture loading...

    float rotationAngle = 90;

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            switch (event.type) {
                case Event::KeyReleased:
                    switch (event.key.code) {
                        case Keyboard::Space:
                            lineSprite.rotate(rotationAngle);
                            rotationAngle = - rotationAngle;
                            break;

                        // other cases...
                    }
                    break;

                // other cases...
            }
        }

        // drawing...
    }
}

NB: I also renamed lineImage to lineSprite to prevent any confusion with sf::Image and sf::Sprite.

EDIT :

Another problem I have is I'm doing dots game so, how can I make the logic of the line so I can let it between two points when I hit the enter key?

Instead of using textures I would rather use vertices. See the corresponding tutorial here

You would typically have a (dynamic) vertex array to draw the lines.

EDIT 2 :

Let's be more concreate and write a few lines of code.

You can declare the vertex array like this :

sf::VertexArray lines(sf::Lines);

At first it is empty, but you can easily add new lines when needed :

// the player connects dots d1 and d2
lines.append(sf::Vertex(d1.position));
lines.append(sf::Vertex(d2.position));

And, as always, you want to draw the lines to the window. You can achieve this like that :

window.draw(lines);

You can find the relevant documentation here :

Community
  • 1
  • 1
Hiura
  • 3,500
  • 2
  • 20
  • 39
  • Thanks alot it works :D and about the second prob i'm doing Dots game so i want to connect two dots with the lineSprite when the user hits enter , and the second user do the same till it forms a square , that is the problem what can i use to do that !? cuz i'm new at SFML ! thanks again for your help ^^ – Omar Ahmed May 04 '13 at 10:47
  • so my way isn't enough !? , i see it more simple than vertex arrays :D , srry but if you can clarify it more i will be grateful ^^ – Omar Ahmed May 06 '13 at 01:30
  • I added a few lines of code to describe more precisely what I said earlier. But maybe we're not talking about the same thing... – Hiura May 07 '13 at 06:40
  • I know vertex array , but in fact i don't know how to deal with two arrays of points and lines and make the logic or them , so im asking is there a way with textures easier than this ? , and if not can you explain how to make array of points with fixed size and array of lines and the logic that makes when there are complete square it add point to the score .. sorry for your time ^^ – Omar Ahmed May 08 '13 at 12:53
  • you should probably open a new question for that. – Hiura May 08 '13 at 13:44