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

Author Topic: Help with input and sprites  (Read 1922 times)

0 Members and 1 Guest are viewing this topic.

BlurrOfDeath

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Help with input and sprites
« on: April 24, 2014, 09:13:51 am »
I've recently picked up SFML and I'm trying to make a simple tic tac toe game, but I cannot figure out how to take input from the keyboard, and draw a sprite that stays there even after the key isn't pressed.
I know its probably very simple, but I cannot figure out how to do it at all. At the moment, the cross is only drawn when the key is down, and gets removed once it is not pressed.
Some help please!
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
    sf::RenderWindow window (sf::VideoMode(600,600), "TIC TAC TOE");

    sf::Texture Grid_Texture;
    sf::Texture Cross_Texture;

    Grid_Texture.loadFromFile("Grid.png");
    Cross_Texture.loadFromFile("Cross.png");

    sf::Sprite Grid;
    sf::Sprite Cross;

    Grid.setTexture(Grid_Texture);
    Cross.setTexture(Cross_Texture);

    Cross.setPosition(-200,-200);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color::Black);
        window.draw(Grid);
        window.display();

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Numpad7))
        {
            Cross.setPosition(0,0);
            window.draw(Grid);
            window.draw(Cross);
            window.display();
        }
    }
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Help with input and sprites
« Reply #1 on: April 24, 2014, 09:19:55 am »
The usual approach is an event loop where you handle keys. Real-time input is only needed if you want to check the current state, not state changes.

Multiple calls to display() are not meaningful -- please read the tutorials about 2D rendering and event handling carefully. There are some important points that you have missed in your code.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Help with input and sprites
« Reply #2 on: April 24, 2014, 09:22:59 am »
Why should it stay? You only call the draw() method inside the if block. That means that the sprite is only drawn when the key is pressed. You want something like a boolean that keeps track if the key has been pressed in the past:

bool drawSprite = false;
Cross.setPosition(0,0);
// ...
while(window.isOpen())
{
        // .. poll the events
        while (window.pollEvent(event))
        {
                if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Numpad7)
                {
                        drawSprite = true; // only draw it if the key has been pressed
                }
        }       // ... end polling events

        window.clear(sf::Color::Black);
        window.draw(Grid);

        if (drawSprite)
        {
                window.draw(Cross);
        }

        window.display();
}
 


EDIT: Nexus was faster ;)
« Last Edit: April 24, 2014, 09:24:44 am by MadMartin »

BlurrOfDeath

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Help with input and sprites
« Reply #3 on: April 24, 2014, 09:51:00 am »
Oh, I understand, thanks so much! :)

 

anything