1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| #include <iostream>
#include <vector>
#include <algorithm>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
using namespace std;
using namespace sf;
int main()
{
string id;
short int x = 0, y = 0;
cout << "Pseudo : ";
cin >> id;
TcpSocket socket;
int port = 2000;
socket.connect("192.168.0.25", port);
Packet sendPacket;
sendPacket << id;
socket.send(sendPacket);
bool state = true;
int i = 0;
RectangleShape me(Vector2f(20,20));
me.setFillColor(Color::Red);
vector<int> idPlayers;
vector<RectangleShape*>box;
int compteur = 0;
RenderWindow app(VideoMode(400,400,32),"SFML Multi Client test");
app.setFramerateLimit(60);
Clock clock;
socket.setBlocking(false);
while (app.isOpen())
{
Event event;
while (app.pollEvent(event))
{
if (event.type == Event::Closed)
app.close();
}
Packet send;
if (app.hasFocus())
{
if (Keyboard::isKeyPressed(Keyboard::Up))
{
y--;
send << id << x << y << state;
socket.send(send);
}
if (Keyboard::isKeyPressed(Keyboard::Down))
{
y++;
send << id << x << y << state;
socket.send(send);
}
if (Keyboard::isKeyPressed(Keyboard::Left))
{
x--;
send << id << x << y << state;
socket.send(send);
}
if (Keyboard::isKeyPressed(Keyboard::Right))
{
x++;
send << id << x << y << state;
socket.send(send);
}
if (Keyboard::isKeyPressed(Keyboard::Escape))
{
state = false;
app.close();
}
}
app.clear();
Packet packet;
socket.receive(packet);
string pseudo;
short int xe, ye;
bool state_S;
short int idNumber;
int sizeClients;
if (packet >> pseudo >> xe >> ye >> state_S >> sizeClients >> idNumber)
{
if (compteur<sizeClients-1)
{
i+=10;
RectangleShape *b = new RectangleShape(Vector2f(20,20));
b->setFillColor(Color::Red);
b->setPosition(xe+i, ye+i);
box.push_back(b);
compteur++;
}
box[idNumber]->setPosition(xe,ye);
cout << "Nbr objet box : " << box.size() << endl;
cout << "Recu : " << xe << " , " << ye << " de " << pseudo << endl;
cout << "Son numéro de client : " << idNumber << endl;
if (!state_S)
{
cout << pseudo << "s'est deconnecte." << endl;
}
}
for (int i = 0 ; i < box.size() ; i++)
{
app.draw(*box[i]);
}
me.setPosition(Vector2f(x, y));
app.draw(me);
app.display();
}
} |
Partager