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

Author Topic: Audio, network and threading.  (Read 5463 times)

0 Members and 1 Guest are viewing this topic.

ActionBoy

  • Newbie
  • *
  • Posts: 36
    • View Profile
Audio, network and threading.
« on: January 15, 2011, 08:29:34 pm »
A small game running just a simple TCP connection from the SFML 1.6 tutorial. The game runs the network code from a second Thread. Everything works fine until I add music. Then the network only receives or sends corrupt data.

I think(don't really have a clue) this has something to do with how I link the projekt but im not sure.

The current setup.
-lmingw32
-lopengl32
-lglu32
-lgdi32
-lsfml-audio
-lsfml-graphics
-lsfml-window
-lsfml-network
-lsfml-system

Any ideas?

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Audio, network and threading.
« Reply #1 on: January 17, 2011, 09:56:12 am »
Please post some simple code so we can recreate the problem.

ActionBoy

  • Newbie
  • *
  • Posts: 36
    • View Profile
Audio, network and threading.
« Reply #2 on: January 17, 2011, 10:03:44 pm »
Will do tomorrow (tuesday).

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Audio, network and threading.
« Reply #3 on: January 18, 2011, 12:21:10 am »
It's tuesday now (in New Zealand) :P

ActionBoy

  • Newbie
  • *
  • Posts: 36
    • View Profile
Audio, network and threading.
« Reply #4 on: January 18, 2011, 08:02:44 pm »
Now its tuesday in Sweden so I'll post:D This is the whole network code and some gamecode. The rendering, calc and input is basic. A sprite within a class together with a x,y,frame value. Calc just takes the x and y value and put it to the sprite.

I should clean up the gamecode some more before posting, but as you see it´s just stolen from the tutorials. I have rewritten the gamecode so much in trying to make it work that I lost myself in the code and everything just chaos, hince the pseudocode.


player
Code: [Select]

#ifndef GAME_PLAYER_H
#define GAME_PLAYER_H



#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include "game_convert.h"
#include "game_bullet.h"
using namespace std;





namespace game
{
class cPlayer
{
public:

    enum PlayerType
    {
        Chip       = 0,
        Dale        = 1
    };

    cPlayer(PlayerType tmpPlayerType);
    cBulletList mBulletList;
    Direction::Type mDirection;
    float mX;
    float mY;
    float mGravity;
    bool mJump;
    int mCounterRun;
    int mCounterRunDelay;
    sf::Image mImage[6];
    sf::Sprite mSprite;
    int nextFrame();
    bool mFlipX;
    bool mWalking;
};
int cPlayer::nextFrame()
{
    mCounterRunDelay++;
    if (mCounterRunDelay>10)
    {
        mCounterRun++;
        if (mCounterRun>3)
        {
            mCounterRun=0;
        }
        mCounterRunDelay=0;
    }
}

cPlayer::cPlayer(PlayerType tmpPlayerType)
{
    if(tmpPlayerType == Chip)
    {
        cout<<"Create Chip"<<endl;
        mImage[0].LoadFromFile("chip/run1.png");
        mImage[0].SetSmooth(false);
        mImage[1].LoadFromFile("chip/run2.png");
        mImage[1].SetSmooth(false);
        mImage[2].LoadFromFile("chip/run3.png");
        mImage[2].SetSmooth(false);
        mImage[3].LoadFromFile("chip/run2.png");
        mImage[3].SetSmooth(false);
        mImage[4].LoadFromFile("chip/stand.png");
        mImage[4].SetSmooth(false);

        mImage[5].LoadFromFile("chip/jump.png");
        mImage[5].SetSmooth(false);
    }
    else
    {
        cout<<"Create Dale"<<endl;
        mImage[0].LoadFromFile("dale/run1.png");
        mImage[0].SetSmooth(false);
        mImage[1].LoadFromFile("dale/run2.png");
        mImage[1].SetSmooth(false);
        mImage[2].LoadFromFile("dale/run3.png");
        mImage[2].SetSmooth(false);
        mImage[3].LoadFromFile("dale/run2.png");
        mImage[3].SetSmooth(false);
        mImage[4].LoadFromFile("dale/stand.png");
        mImage[4].SetSmooth(false);


        mImage[5].LoadFromFile("dale/jump.png");
        mImage[5].SetSmooth(false);
    }

    mSprite.SetImage(mImage[0]);
    mSprite.SetCenter(9,24);
    mCounterRun = 0;
    mFlipX = false;
    mX = 9;
    mY = 24;
    mWalking = false;
}

class cPlayerList
{
public:
    vector<cPlayer> mPlayer;
    int addPlayer(cPlayer::PlayerType type);
};

int cPlayerList::addPlayer(cPlayer::PlayerType type)
{
    mPlayer.push_back(cPlayer(type));
}
}

#endif



network code
Code: [Select]


////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Network.hpp>
#include <iostream>
#include <vector>

string mStringNetwork;

int mCounterNetwork = 0;
struct Character
{
    float mX;
    float mY;
    int mCounterRun;
    bool mFlipX;
};

sf::Packet& operator <<(sf::Packet& Packet, const Character& C)
{
    return Packet << C.mX << C.mY<< C.mCounterRun << C.mFlipX;
}

sf::Packet& operator >>(sf::Packet& Packet, Character& C)
{
    return Packet >> C.mX >> C.mY >> C.mCounterRun>> C.mFlipX;
}


////////////////////////////////////////////////////////////
/// Our custom packet type, handling encryption and
/// decryption of data
///
////////////////////////////////////////////////////////////
class MyEncryptedPacket : public sf::Packet
{
private :

    ////////////////////////////////////////////////////////////
    /// Called before the packet is sent to the network
    ///
    /// \param DataSize : Variable to fill with the size of data to send
    ///
    /// \return Pointer to the array of bytes to send
    ///
    ////////////////////////////////////////////////////////////
    virtual const char* OnSend(std::size_t& DataSize)
    {
        // Copy the internal data of the packet into our destination buffer
        myBuffer.assign(GetData(), GetData() + GetDataSize());

        // Encrypt (powerful algorithm : add 1 to each character !)
        for (std::vector<char>::iterator i = myBuffer.begin(); i != myBuffer.end(); ++i)
            *i += 1;

        // Return the size of encrypted data, and a pointer to the buffer containing it
        DataSize = myBuffer.size();
        return &myBuffer[0];
    }

    ////////////////////////////////////////////////////////////
    /// Called after the packet has been received from the network
    ///
    /// \param Data :     Pointer to the array of received bytes
    /// \param DataSize : Size of the array of bytes
    ///
    ////////////////////////////////////////////////////////////
    virtual void OnReceive(const char* Data, std::size_t DataSize)
    {
        // Copy the received data into our destination buffer
        myBuffer.assign(Data, Data + DataSize);

        // Decrypt data using our powerful algorithm
        for (std::vector<char>::iterator i = myBuffer.begin(); i != myBuffer.end(); ++i)
            *i -= 1;

        // Fill the packet with the decrypted data
        Append(&myBuffer[0], myBuffer.size());
    }

    std::vector<char> myBuffer;
};










const unsigned short Port = 2435;
bool mBoolServer =false;
////////////////////////////////////////////////////////////
/// Create a client and connect it to a running server
///
////////////////////////////////////////////////////////////
sf::IPAddress ServerAddress;
sf::SocketTCP Client;

void initClient()
{
    do
    {
        std::cout << "Type address or name of the server to connect to : ";
        std::cin  >> ServerAddress;
    }
    while (!ServerAddress.IsValid());

    // Create a TCP socket for communicating with server


    // Connect to the specified server
    if (Client.Connect(Port, ServerAddress) != sf::Socket::Done)
        return;
    std::cout << "Connected to server " << ServerAddress << std::endl;
}

void DoClientTCP()
{


    // Receive a message from the client
    mStringNetwork = "Receive";
    sf::Packet RegularPacket;
    if (Client.Receive(RegularPacket) != sf::Socket::Done)
        return;

    Character C1;
    if (RegularPacket >> C1)
    {
        //     std::cout << "Character received from the server (regular packet) : " << std::endl;
        //     std::cout << C1.mX << ", " << C1.mY<< std::endl;
        mPlayer.mPlayer[0].mX           = C1.mX;
        mPlayer.mPlayer[0].mY           = C1.mY;
        mPlayer.mPlayer[0].mCounterRun  = C1.mCounterRun;

        mPlayer.mPlayer[0].mSprite.FlipX(C1.mFlipX);
    }

    mStringNetwork = "Send";
    Character C2 = {mPlayer.mPlayer[1].mX,mPlayer.mPlayer[1].mY,mPlayer.mPlayer[1].mCounterRun,mPlayer.mPlayer[1].mFlipX};
    sf::Packet RegularPacket2;
    RegularPacket2 << C2;
    if (Client.Send(RegularPacket2) != sf::Socket::Done)
        return;
}


////////////////////////////////////////////////////////////
/// Launch a server and wait for incoming connections
///
////////////////////////////////////////////////////////////
sf::SocketTCP Server;
sf::IPAddress ClientAddress;

void initServer()
{
    // Listen to a port for incoming connections
    if (!Server.Listen(Port))
        return;
    std::cout << "Server is listening to port " << Port << ", waiting for connections... " << std::endl;

    // Wait for a connection

    Server.Accept(Client, &ClientAddress);
    std::cout << "Client connected : " << ClientAddress << std::endl;
}

void DoServerTCP()
{
    // Create a TCP socket for communicating with clients


    mStringNetwork = "Send";
    Character C1 = {mPlayer.mPlayer[0].mX,mPlayer.mPlayer[0].mY,mPlayer.mPlayer[0].mCounterRun,mPlayer.mPlayer[0].mFlipX};
    sf::Packet RegularPacket;
    RegularPacket << C1;
    if (Client.Send(RegularPacket) != sf::Socket::Done)
        return;


    mStringNetwork = "Receive";
    sf::Packet RegularPacket2;
    if (Client.Receive(RegularPacket2) != sf::Socket::Done)
        return;

    Character C2;
    if (RegularPacket2 >> C2)
    {
        //     std::cout << "Character received from the server (regular packet) : " << std::endl;
        //   std::cout << C2.mX << ", " << C2.mY << std::endl;
        mPlayer.mPlayer[1].mX           = C2.mX;
        mPlayer.mPlayer[1].mY           = C2.mY;
        mPlayer.mPlayer[1].mCounterRun  = C2.mCounterRun;

        mPlayer.mPlayer[1].mSprite.FlipX(C2.mFlipX);

    }

}


void networkInit(bool tmpBool)
{
    if (tmpBool == true)
    {
        initServer();
        mBoolServer = true;
    }
    else
    {
        initClient();
        mBoolServer = false;
    }
}

void networkUpdate()
{
    if (mBoolServer == true)
    {
        DoServerTCP();
    }
    else
    {
        DoClientTCP();
    }
    mCounterNetwork++;
}

void networkClose()
{
    if (mBoolServer == true)
    {
        Client.Close();
        Server.Close();
    }
    else
    {
        Client.Close();
    }
}



main.cpp
Code: [Select]

void ThreadFunction(void* UserData)
{
    while (1 > 0)
    {
        networkUpdate();
    }
}
int main()
{

//set if your server or not

//start the thread
sf::Thread Thread2(&ThreadFunction);
Thread2.Launch();

//music
sf::Music Music;
if (!Music.OpenFromFile("theme.ogg"))
 return EXIT_FAILURE;
Music.SetLoop(true);
Music.Play();


while(App.IsOpened())
{
        //Input
       //Calc
      //render
}

}

 

anything