SFML网络问题

时间:2014-05-25 20:12:21

标签: c++ sfml

我是SFML网络的小伙子,我认为我做错了,我经常会遇到这段代码的错误。我想做的就是发送位置的数据包然后在另一个窗口更新它们,这是我到目前为止所拥有的。

#include <iostream>
#include <string>

#include "player.h"
#include "gameAssets.h"
#include "zombie.h"

using namespace std;


sf::Vector2u size(1000, 800);

bool focused = true;

sf::Vector2f newPosition, oldPosition;

//Multiplayer code.
//sf::Thread* thread = 0;
char choice;
sf::Mutex globalMutex;
sf::TcpSocket socket;
sf::IpAddress ip;


void sendandReceiveData(){
    //Server half
    sf::Packet packetSendX, packetSendY;

    packetSendX << oldPosition.x;
    packetSendY << oldPosition.y;

    socket.send(packetSendX);
    socket.send(packetSendY);

    sf::Packet packetReceiveX, packetReceiveY;

    //Client half
    socket.receive(packetReceiveX);
    socket.receive(packetReceiveY);

    packetReceiveX >> newPosition.x;
    packetReceiveY >> newPosition.y;

    cout << "Other Players X: " << newPosition.x << endl;
    cout << "Other Players Y: " << newPosition.y << endl;
}

int main(){

    sf::RenderWindow window(sf::VideoMode(size.x, size.y), "Zombie Defence", sf::Style::Titlebar | sf::Style::Close);

    //Limited fps so we dont need deltaTime...
    window.setFramerateLimit(60);
    window.setVerticalSyncEnabled(true);

    player player;
    zombie zombie;
    gameAssets gameAssets;


    player.initialize();
    zombie.initialize();
    gameAssets.initialize();

    gameAssets.loadContent();
    zombie.loadContent();
    player.loadContent();

    //Multiplayer code.
    system("CLS");
    cout << "Multiplayer Dev Build 0.1" << endl;
    cout << " " << endl;
    cout << "Enter (S) for server or (C) for client." << endl;
    cin >> choice;

    if (choice == 'S'){
        sf::TcpListener listener;
        listener.setBlocking(false);
        listener.listen(5000);
        listener.accept(socket);
        cout << "New Client Connected: " << socket.getRemoteAddress() << endl;

    }
    else if (choice == 'C'){
        cin >> ip;

        if (socket.connect(ip, 5000) == sf::Socket::Done){
            cout << "Connected to server" << endl;
        }
    }

    while (window.isOpen()){

        sf::View view = player.getView();
        sf::Vector2f playerPosition = player.getPosition();

        sf::Event event;

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

            if (event.type == sf::Event::GainedFocus){
                focused = 1;
            }
            else if (event.type == sf::Event::LostFocus){
                focused = 0;
            }
        }

        //Only update if window is focused...
        if (focused){
            //Updating
            player.update(window);

            zombie.followPlayer(playerPosition);
            zombie.update(window);

            window.setView(view);
        }

        oldPosition = player.getPosition();
        sendandReceiveData();

        window.clear();
        gameAssets.draw(window);
        zombie.draw(window);
        player.draw(window);
        window.display();
    }
    return 0;
}

如果有人能帮我解决我做错的事情,我们将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:1)

我修好了。我所要做的只是添加packetReceiveX.clear();y相同的内容来解决问题。