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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - PaulDubois

Pages: [1]
1
Hello, could someone explain to me why that happens please ?

My problem is as follow : If I press a key while my program is running then the input is only received once, which is what I want. However, if I press a key while I move my mouse over my SFML window then the input is repetead ~5-7 times in an instant.

Here is my main loop :

sf::RenderWindow window(sf::VideoMode(WIN_HEI, WIN_WID), "fenĂȘtre");
    if (!heroTexture.loadFromFile("res/hero_sheet.png"))
    {
        cout << "erreur de chargement";
    }

    heroSprite.setTexture(heroTexture);
    heroSprite.setTextureRect(IntRect(spriteSize * heroAnim.x, heroAnim.y * spriteSize, spriteSize, spriteSize));
    while (window.isOpen())
    {
       
       
        Event event;
       
        while (window.pollEvent(event))
        {
           
            CheckBtn();
            animPlayer();
        }
       
        input.InputHandler(event, window);
       
       
        window.clear();
        window.draw(heroSprite);
        window.display();
    }

    return 0;
}


Here is my class Input.h :


#ifndef INPUT_H
#define INPUT_H

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


using namespace sf;

class Input
{
        struct Button { bool left, right, up, down, attack, escape; };
        Button button;
public:
        Input();
        Button GetButton(void) const;
        void InputHandler(Event event, RenderWindow& window);

       
};

#endif


Input.cpp :
#include "input.h"

Input::Input()
{
    button.left = button.right = button.down = button.up = button.escape = button.attack = false;
}

void Input::InputHandler(Event event, RenderWindow& window)
{
    if (event.type == sf::Event::Closed)
    {
        window.close();
    }
    if (event.type == Event::KeyPressed)
    {
        switch (event.key.code)
        {
        case Keyboard::Escape:
            button.escape = true;
            break;
        case Keyboard::Up:
            button.up = true;
            break;
        case Keyboard::Down:
            button.down = true;
            break;
        case Keyboard::Left:
            button.left = true;
            break;
        case Keyboard::Right:
            button.right = true;
            break;
        default:
            break;
        }
    }
    if (event.type == Event::KeyReleased)
    {
        switch (event.key.code)
        {
        case Keyboard::Escape:
            button.escape = false;
            break;
        case Keyboard::Up:
            button.up = false;
            break;
        case Keyboard::Down:
            button.down = false;
            break;
        case Keyboard::Left:
            button.left = false;
            break;
        case Keyboard::Right:
            button.right = false;
            break;
        default:
            break;
        }

    }
    if (event.type == Event::MouseButtonPressed)
    {
        if (event.mouseButton.button == Mouse::Left)
        {
            button.attack = true;
        }
    }
    if (event.type == Event::MouseButtonReleased)
    {
        if (event.mouseButton.button == Mouse::Left)
        {
            button.attack = false;
        }
    }
   
   

}

Input::Button Input::GetButton(void) const
{
    return button;
}


And in my main is a fuction CheckBtn that simply assosciates an enumeration (the 4 directions) to corresponding sprites.



Pages: [1]