Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Whenever I receive a new message, my player stops drawing  (Read 2293 times)

0 Members and 1 Guest are viewing this topic.

Grimlen

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Whenever I receive a new message, my player stops drawing
« on: October 09, 2012, 11:09:47 pm »
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?

codecranker

  • Guest
Re: Whenever I receive a new message, my player stops drawing
« Reply #1 on: October 19, 2012, 04:06:42 am »
Few things:
- I dont remember exactly bu I read somewhere that rendering code should be a part of the main thread. this is an OS  limitation not the SFML limitation.
- Does readByte() blocks the execution when there is no data is available on the stream? If it doesn't block what does it return then? you should add a default case to your switch.
- I didnt really get the global variable player logic. You are adding the same player sprite with different position each time. doesn't it overwrite the original sprite considering the reference is same?

Also, on the optimization note, there is no need to load the same image again n again. load the image once in the beginning. Create a new sprite for every new player spawned using the same image. you will reduce the disk i/o  by about 100% :)