SFML - pollEvent双击鼠标点击

时间:2015-01-13 01:09:43

标签: c++ loops sfml

我的主要是这样的:

#include <SFML/Graphics.hpp>
#include <iostream>
#include "okno.h"

using namespace sf;

int main()
{
    // Create the main window

    RenderWindow app(VideoMode::getDesktopMode(), "Okno" ,Style::Fullscreen);
    app.setKeyRepeatEnabled(false);
    okno aplikacja(app);


    // Start the game loop
    while (app.isOpen())
    {
        // Process events
        Event event;
        event.type=Event::JoystickButtonPressed;
      event.mouseButton.button = Mouse::Right;
        while (app.pollEvent(event))
        {

            // Close window : exit
            if (event.type == Event::Closed)
                app.close();

            if( event.type == Event::KeyPressed && event.key.code == Keyboard::Escape )
                app.close();
        }
        aplikacja.click_przyciski(event);







     aplikacja.obsluga_przyciskow();
      event.type=Event::JoystickButtonPressed;
      event.mouseButton.button = Mouse::Right;

     aplikacja.rysuj();
     aplikacja.menu=true;
       event.type=Event::JoystickButtonPressed;
      event.mouseButton.button = Mouse::Right;

            //sleep(seconds(5));

        // Update the window
        //app.display();
    }

    return EXIT_SUCCESS;
}

和类okno.cpp一样:

#include "okno.h"

#include <iostream>

using namespace std;
int wyswietlanie =0;
okno::okno(RenderWindow &app): window(app)
{
    textures[0].loadFromFile("grafika/tlo.png");
    textures[1].loadFromFile("grafika/logo.png");
    textures[2].loadFromFile("grafika/siatka.png");
    textures[3].loadFromFile("grafika/button_start.png");
    textures[4].loadFromFile("grafika/button_informacje.png");
    textures[5].loadFromFile("grafika/button_wyjscie.png");
    textures[6].loadFromFile("grafika/button_cofnij.png");
    czcionka.loadFromFile("czcionki/czcionka_1.ttf");
    for (int j=0;j<i;j++)
        sprites[j].setTexture(textures[j]);
        float x,y;
        x=window.getView().getSize().x;
        y=window.getView().getSize().y;
        tekst.setString("Projekt wykonal : \n    Wojciech Sorota.");
        tekst.setCharacterSize(30);
        tekst.setPosition(x/10,y/10);
        tekst.setColor(sf::Color::Red);
        tekst.setFont(czcionka);
        sprites[0].setScale(x/sprites[0].getTextureRect().width,y/sprites[0].getTextureRect().height);
        sprites[1].setPosition(x/2 - sprites[1].getTextureRect().width/2,y/2 - sprites[1].getTextureRect().height/2);
        sprites[3].setPosition(x/10,y/10);
        sprites[2].setScale(x/2/sprites[2].getTextureRect().width,y/2/sprites[2].getTextureRect().height);
        sprites[2].setPosition(x/8,y/4);
        sprites[4].setPosition( x/10+ sprites[3].getPosition().x + x/ 15,y/10 );
        sprites[5].setPosition(  x/10 +sprites[4].getPosition().x +x/15,y/10);
        sprites[6].setPosition(x/10 + tekst.getPosition().x + tekst.getGlobalBounds().width,y/10);
        menu=true;
}
okno::~okno()
{}
void okno::rysuj()
{
      this->rysuj_intro();

      this->rysuj_menu();

}
void okno::start_gra()
{

}
void okno::click_przyciski(Event &event)
{
    if(event.type == Event::MouseButtonReleased && event.mouseButton.button == Mouse::Left)

{

        if(click_sprite(sprites[3]))
            start_gra();

        else if(click_sprite(sprites[5]))
                    window.close();
                  else if( click_sprite(sprites[4]))
        wyswietl_info(event);

}

}
void okno::wyswietl_info(Event &event)
{
{

    while(1)
        {
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && click_sprite(sprites[6]))
    {
          event.type=Event::JoystickButtonPressed;
          event.mouseButton.button = Mouse::Right;
    return;

    }


     window.clear();
         window.draw(sprites[0]);
         window.draw(sprites[2]);
         window.draw(sprites[6]);

         window.draw(tekst);
         obsluga_przyciskow();
         window.display();

}
}}
bool okno::click_sprite(Sprite a)
{
    // transform the mouse position from window coordinates to world coordinates
    sf::Vector2f mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));
    // retrieve the bounding box of the sprite
    sf::FloatRect bounds = a.getGlobalBounds();
    // hit test
    if (bounds.contains(mouse))
    {
        return true;
    }
    return false;
}
void okno::obluga_cofnij()
{

}
void okno::obsluga_przyciskow()
{

if(mysz_nad_sprite(sprites[3]))
        sprites[3].setColor(sf::Color(100,100,100));
            else
                 sprites[3].setColor(sf::Color(255,255,255));

if(mysz_nad_sprite(sprites[4]))
        sprites[4].setColor(sf::Color(100,100,100));
            else
                 sprites[4].setColor(sf::Color(255,255,255));
if(mysz_nad_sprite(sprites[5]))
        sprites[5].setColor(sf::Color(100,100,100));
            else
                 sprites[5].setColor(sf::Color(255,255,255));
if(mysz_nad_sprite(sprites[6]))
        sprites[6].setColor(sf::Color(100,100,100));
            else
                 sprites[6].setColor(sf::Color(255,255,255));

}
bool okno::mysz_nad_sprite(Sprite a)
{
    sf::Vector2f mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));
    if (a.getGlobalBounds().contains(mouse))
        return true;
    return false;
}
void okno::rysuj_intro()
{
        if (wyswietlanie == 0)
            {
                for ( int n=0;n<=254;n++)
                {
                    window.clear();
                    sprites[0].setColor(sf::Color(255, 255, 255, n));
                    sprites[1].setColor(sf::Color(255,255,255,n));
                    sleep(milliseconds(n/15));
                    window.draw(sprites[0]);
                    window.draw(sprites[1]);
                    window.display();
                }

                for (int  n=254;n>=0;n--)
                {
                    window.clear();
                    sprites[1].setColor(sf::Color(255, 255, 255, n));
                    sleep(milliseconds(n/15));
                    window.draw(sprites[0]);
                    window.draw(sprites[1]);
                    window.display();
                }
                wyswietlanie++;
            }


}

  void okno::rysuj_menu()
  {
       window.clear();

       if (wyswietlanie==1)
           for ( int n=0;n<=254;n++)
               {
                    window.clear();
                    sprites[2].setColor(sf::Color(255,255,255,n));
                    window.draw(sprites[0]);
                    window.draw(sprites[2]);
                    window.draw(sprites[3]);
                    window.draw(sprites[4]);
                    window.draw(sprites[5]);
                    window.display();
                    if (n==254)
                    wyswietlanie++;
                    window.clear();
                }

         window.draw(sprites[0]);
         window.draw(sprites[2]);
         window.draw(sprites[3]);
         window.draw(sprites[4]);
         window.draw(sprites[5]);
         window.display();
}

我的问题是为什么在我点击精灵[6]然后我回到循环[pollEvent]然后pollEvent返回最后一个事件,但最后一个事件是mouseclick soo然后它在mouseclick上运行脚本如何保护我的函数为此?

1 个答案:

答案 0 :(得分:0)

SFML事件处理

事件应该像这样处理:

sf::Event event;

while (app.pollEvent(event))
{
    // handle each event you want
    switch (event.type) 
    {
    case sf::Event::Closed:
        closed(event);
        Exit();
        break;
    case sf::Event::KeyPressed:
        keyPressed(event);
        break;
    case sf::Event::KeyReleased:
        keyReleased(event);
        break;
    case sf::Event::MouseButtonPressed:
        mouseButtonPressed(event);
        break;
    case sf::Event::MouseButtonReleased:
        mouseButtonReleased(event);
        break;
    case sf::Event::MouseMoved:
        mouseMoved(event);
        break;
    /* ...many more events exist */
    default:
        cerr << "unhandle event of type: " << event.type << endl;
        break;

    }

}

然后,为每个要处理的事件设置一个函数。

void mouseMoved(const sf::Event& event)
{
    // do something with the mouse event
    mRelMousePos.x = event.mouseMove.x - mMousePos.x;
    mRelMousePos.y = event.mouseMove.y - mMousePos.y;
    mMousePos.x = event.mouseMove.x;
    mMousePos.y = event.mouseMove.y;
}

正如@Hiura所提到的,您无法为sf::event分配事件类型,然后在pollEvent方法中重复使用它,更确切地说,它更多这里有问题的event.mouseButton.button = Mouse::Right;部分。这部分是因为sf::Event类将其事件参数(mouseButton,密钥等)存储在union中,部分原因是因为它不是用于sf的用途: :活动类。

您的代码

请用英文编码,您永远不会知道何时需要共享一段代码。而且我不是这样说的,因为我是一个以自我为中心的英语人士,事实上,我是法国加拿大人。

现在,让我们来看看你提供的代码。

    // Process events
    Event event;

    // these are problematic, you shouldn't do that.
    //event.type=Event::JoystickButtonPressed;
    //event.mouseButton.button = Mouse::Right;

    while (app.pollEvent(event))
    {
        // using a switch or else if here would lower code repetition
        // and useless check.

        // Since both cases do the same thing, you could have just merge them.
        // Close window : exit
        if ((event.type == Event::Closed) 
           || (event.type == Event::KeyPressed 
               && event.key.code == Keyboard::Escape ))
        {
            app.close();
        }
        else if (event.type == Event::MouseButtonReleased 
                  && event.mouseButton.button == Mouse::Left)
        {
            // this should really go here
            aplikacja.click_button(event); // thanks Google Translation
        }

    }

    // Why? Why do you send the last event from your event loop to that?
    //aplikacja.click_przyciski(event);

这里有一些不必要的调整只是为了好玩:

bool okno::click_sprite(Sprite sprite) // remember, meaningful names
{
    // transform the mouse position from window coordinates to world coordinates
    sf::Vector2f mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));

    // see how it's clearer and I just removed 7-8 LOC and the comments
    // are now unnecessary here. The if clause was redundant since the contains
    // method already returns a bool value.
    return sprite.getGlobalBounds().contains(mouse);
}

我不打算浏览整个代码,但您接近使用SFML的应用程序。有一些很好的教程,SFML源代码非常清楚,有关如何使用SFML的每个部分的更多信息,请参阅SFML Github repoSFML game dev book