Here's how my client/server works:
Client presses the LEFT key:
Sends byte: 0
Server reads for a byte:
Gets: 0
Decided to execute code:
Create a new player and insert player into array
Later on in server on another thread:
Draw players using window.draw(), iterating through vector of players
Client presses the RIGHT key:
Sends byte: 1
Server reads for a byte:
Get: 1
Decided to execute code:
Show the IP of the player that sent this message
//Up to this point, the server will stop drawing the player
//In a very short instant upon each new message being received, the server will draw a white rectangle //(the size of my player) and then draw the black background. Sometimes I even catch a glimpse of my player sprite, but it goes away.
I'm not sure why, after I receive byte: 1, the server stops drawing my player...
main.cpp:/************************************************************
SERVER
************************************************************/
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
using namespace sf;
#include "ServPlayer.h"
#include "Sockets.h"
vector<ServPlayer> players;
bool checkIpAndPort(Data d){
for (vector<ServPlayer>::iterator itr = players.begin(); itr != players.end(); ++itr) {
if(itr->ip == d.sender && itr->port == d.port){
return true;
}
}
return false;
}
void netThread(void *userData){
bool running = *(bool*) userData;
sf::IpAddress sender;
// Create the UDP socket
sf::UdpSocket socket;
// Bind it (listen) to the port 4567
if (!socket.bind(4567)){
// Error...
}
while(running){
Data d = readbyte(&socket);
char b = d.byte;
switch(b)
{
case 0:
{
//Find out if the IP address is not in
if(!checkIpAndPort(d)){
ServPlayer p(d.sender, d.port);
players.push_back(p);
cout << "New Player: " << d.sender << " " << d.port << endl;
}
else{
cout << "Hello!" << endl;
}
break;
}
case 1:
{
for (vector<ServPlayer>::iterator itr = players.begin(); itr != players.end(); ++itr) {
if(itr->ip == d.sender && itr->port == d.port){
ServPlayer p = *itr;
cout << "Test: " << p.ip.toString() << endl;
}
}
}
break;
}
}
}
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
bool running = true;
sf::Thread thread(&netThread, &running);
thread.launch();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed){
running = false;
window.close();
}
}
window.clear();
vector<ServPlayer>::iterator itr;
for ( itr = players.begin(); itr != players.end(); ++itr )
window.draw(itr->spr_player);
window.display();
}
running = false;
return 0;
}
ServPlayer.cpp:#include "ServPlayer.h"
#include <cstdlib>
#include <iostream>
ServPlayer::ServPlayer(){
}
ServPlayer::ServPlayer(IpAddress ip, unsigned short port){
this->ip = ip;
this->port = port;
/* initialize random seed: */
srand ( time(NULL) );
/* generate secret number: */
int num = rand() %10 + 1;
std::cout << num << std::endl;
if(!img_player.loadFromFile("Images\\spr_player.png")){
//Error
}
spr_player.setTexture(img_player);
spr_player.setPosition(50 + (50+num), 50 + (50+num));
spr_player.setOrigin(img_player.getSize().x/2, img_player.getSize().y/2);
}
Is it possibly because:
- I'm using players as a
global variable?- I'm using a different thread?