2017-04-14 10 views
0

간단한 SFML 게임을하고 있는데 udp 소켓을 사용하여 네트워크 통신을 원합니다. 하지만 문제는 소켓이받는 좌표를 사용하여 원의 위치를 ​​업데이트하려고하면 윈도우가 차단되고 응답하지 않는다는 것입니다. 아래 코드는 다음과 같습니다. 아무도 문제가 뭔지 알고 있습니까? 이 소켓의의 뭔가 하나에 수신 될 때까지 당신이 당신의 창에서 이벤트에 응답하지 않도록 매개 변수없이소켓을 사용하는 동안 창이 응답하지 않습니다. SFML

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

int posX=100,posY=220,x=5; 
sf::UdpSocket receiver; 
sf::SocketSelector selector; 

void changePosition(); 
void defineWindow(sf::RenderWindow &window); 
void drawCircle(sf::CircleShape &circle, sf::RenderWindow &window); 

int main() 
{ 
    receiver.bind(15000); 
    selector.add(receiver); 

    sf::RenderWindow window (sf::VideoMode(800,600), "Krugovi"); 

    defineWindow (window); 

    return 0; 
} 

void changePosition() 
{ 
    if (x>0 && posX+x>685) { 
    posX=685; 
    x=-x; 
    } 
    else if (x<0 && posX+x<15) { 
    posX=15; 
    x=-x; 
    } 
    else 
    posX=posX+x; 
} 

void defineWindow(sf::RenderWindow &window) 
{ 
    sf::CircleShape circle(50); 
    sf::Event event; 
    sf::Clock clock; 

    while (window.isOpen()) { 
    while(window.pollEvent(event)) { 
     if (event.type == sf::Event::KeyPressed) { 
     if (event.key.code == sf::Keyboard::Escape) 
      window.close(); 
     } 
     if (event.type==sf::Event::Closed) 
     window.close(); 
    } 

    window.clear(sf::Color::White); 

    char msg[5]; 
    size_t received; 
    sf::IpAddress ip; 
    unsigned short port; 

    std::string string; 

    if (selector.wait()) { 
     if(receiver.receive(msg,sizeof(msg),received,ip,port)==sf::UdpSocket::Done) { 
     posX=atoi(msg); 
     } 
    } 

    drawCircle(circle,window); 

    window.display(); 
    } 
} 

void drawCircle(sf::CircleShape &circle, sf::RenderWindow &window) 
{ 
    circle.setFillColor(sf::Color::Yellow); 
    circle.setOutlineThickness(15); 
    circle.setOutlineColor(sf::Color::Red); 
    circle.setPosition(posX,posY); 
    window.draw(circle); 
} 
+0

폴링을 계속할 수 있습니다. SFML에 익숙하지 않지만 문서의 어딘가에서 옵션을 활성화하는 방법을 찾아야합니다. 그런 다음 네트워크에서 수신 된 데이터없이 계속 실행되도록 코드를 수정해야합니다. – rlam12

답변

2

sf::SocketSelector::wait() 영원히 기다릴 것이다.

당신이 그것을 기다릴 수있는 시간을 통과하는 경우, 예를 sf::milliseconds(5)에 대한 다음 당신은 적절한 플래그를 사용하여 비 블로킹 소켓을 활성화해야합니다 이벤트

Relevent docs here