Network programming in C++ with SFML

My assignment for network programming in 3rd year was to create a program which simulated a world with a number of movable objects, where multiple machines could interact with the world through a network.

I implemented this using the SFML framework and library, which I found much easier to use over Winsock. The SFML library also gave me an easy way to render a world.

The program uses a client-server based architecture, where many clients can connect to a singular server through both a local or wide area network. Messages between the clients and server are sent using a datagram transport – holding several different variables.

The variables being sent are:

  • Vector2 – for the players position
  • Boolean – whether the player is moving or not
  • Enum – Direction the player is moving in
  • Float – Velocity in the Y direction

To keep a consistent view of the game world, a prediction technique is used to update each players position. Using the three variables; the boolean that determines whether the player is moving or not, enum that determines the direction the player is moving in, and the float for the players Y velocity, the client program will always update the other players position based on the most recent packet that has been received. If a packet is lost, the prediction will keep movement constant, and if no packets are lost, the prediction is not needed and is overridden.

Here’s a short gif of the game being played over LAN:

ezgif-1390280178

Leave a comment