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
#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
////////////////////////////////////////////////////////////
// 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
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
}
}