Advertisement
Guest User

UDP

a guest
Jun 9th, 2011
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.57 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <SFML/System.hpp>
  3. #include <SFML/Graphics.hpp>
  4. #include <SFML/Network.hpp>
  5. #include <iostream>
  6. using namespace std;
  7. using sf::SocketUDP;
  8. using sf::IPAddress;
  9. using sf::Packet;
  10.  
  11. class Input
  12. {
  13. public:
  14.     Input():
  15.       left(false), right(false), up(false), down(false)
  16.       {}
  17.     bool left;
  18.     bool right;
  19.     bool up;
  20.     bool down;
  21.     void update(const sf::Input& input);
  22. };
  23.  
  24. void Input::update(const sf::Input& input)
  25. {
  26.     left = input.IsKeyDown(sf::Key::Left);
  27.     right = input.IsKeyDown(sf::Key::Right);
  28.     up = input.IsKeyDown(sf::Key::Up);
  29.     down = input.IsKeyDown(sf::Key::Down);
  30. }
  31.  
  32. class Player
  33. {
  34. public:
  35.     Player(float x, float y, float width, float height, float xSpeed, float ySpeed):
  36.       xPos(x), yPos(y), width(width), height(height), xSpeed(xSpeed), ySpeed(ySpeed)
  37.       {}
  38.     Input input;
  39.     float xPos;
  40.     float yPos;
  41.     float height;
  42.     float width;
  43.     float xSpeed;
  44.     float ySpeed;
  45.     void move(float delta);
  46.     void draw(sf::RenderWindow & app);
  47. };
  48.  
  49. void Player::draw(sf::RenderWindow & app)
  50. {
  51.     glBegin(GL_QUADS);
  52.     glVertex2f(xPos, yPos);
  53.     glVertex2f(xPos + width, yPos);
  54.     glVertex2f(xPos + width, yPos + height);
  55.     glVertex2f(xPos, yPos + height);
  56.     glEnd();
  57. }
  58.  
  59. void Player::move(float delta)
  60. {
  61.     int xDir = 0;
  62.     int yDir = 0;
  63.     if(input.left ^ input.right)
  64.     {
  65.         if(input.left)
  66.             xDir = -1;
  67.         else
  68.             xDir = 1;
  69.     }
  70.     if(input.up ^ input.down)
  71.     {
  72.         if(input.up)
  73.             yDir = 1;
  74.         else
  75.             yDir = -1;
  76.     }
  77.     xPos += xDir * xSpeed * delta;
  78.     yPos += yDir * ySpeed * delta;
  79. }
  80.  
  81. sf::Packet& operator>>(sf::Packet& packet, Input& inp)
  82. {
  83.     packet >> inp.left >> inp.right >> inp.up >> inp.down;
  84.     return packet;
  85. }
  86.  
  87. sf::Packet& operator<<(sf::Packet& packet, const Input& inp)
  88. {
  89.     packet << inp.left << inp.right << inp.up << inp.down;
  90.     return packet;
  91. }
  92.  
  93. class Game
  94. {
  95. public:
  96.     Game();
  97.     int run();
  98. private:
  99.     void handleEvents();
  100.     void update(float delta);
  101.     void updateGameState(float delta);
  102.     void initNetwork();
  103.     void receiveClientInput();
  104.     void sendClientInput();
  105.     void sendGameState();
  106.     void receiveAndSyncGameState();
  107.  
  108.     Player myServerPlayer;
  109.     Player myClientPlayer;
  110.     bool myIsServer;
  111.     bool myIsConnected;
  112.     sf::IPAddress myIPAddress;
  113.     sf::SocketUDP mySocket;
  114.     sf::RenderWindow * myWindow;
  115.     static const unsigned short PORT;
  116.     static const float UPDATE_RATE;
  117. };
  118.  
  119. const unsigned short Game::PORT = 3456;
  120. const float Game::UPDATE_RATE = 1.f/20; //TODO different rates for update and networking
  121.  
  122. Game::Game():
  123. myServerPlayer(-1, -1, 0.2f, 0.2f, 0.35f, 0.35f),
  124. myClientPlayer(0.5f, 0.5f, 0.2f, 0.2f, 0.35f, 0.35f),
  125. myIsServer(false),
  126. myWindow(0),
  127. myIsConnected(false)
  128. {
  129.  
  130. }
  131.  
  132. void Game::initNetwork()
  133. {
  134.     while(true)
  135.     {
  136.         cout << "Server mode(s) or Client mode (c)? ";
  137.         string str;
  138.         cin >> str;
  139.         if(str == "s")
  140.         {
  141.             myIsServer = true;
  142.             break;
  143.         }
  144.         else if(str == "c")
  145.         {
  146.             myIsServer = false;
  147.             break;
  148.         }
  149.     }
  150.     if(!mySocket.Bind(PORT))
  151.     {
  152.         cerr << "Error binding socket to port << PORT";
  153.     }
  154.     cout << "Enter ip: ";
  155.     string line;
  156.     cin >> line;
  157.     myIPAddress = IPAddress(line);
  158.     mySocket.SetBlocking(false);
  159.     myIsConnected = true;
  160. }
  161.  
  162. void Game::handleEvents()
  163. {
  164.     sf::Event event;
  165.     while (myWindow->GetEvent(event))
  166.     {
  167.         switch(event.Type)
  168.         {
  169.         case sf::Event::Closed:
  170.             myWindow->Close();
  171.             break;
  172.         case sf::Event::Resized:
  173.             glViewport(0, 0, event.Size.Width, event.Size.Height);
  174.             break;
  175.         default:
  176.             break;
  177.         }
  178.     }
  179. }
  180.  
  181. int Game::run()
  182. {
  183.     initNetwork();
  184.     myWindow = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "SFML Window");
  185.     float restTime = 0;
  186.     while(myWindow->IsOpened())
  187.     {
  188.         handleEvents();
  189.         restTime += myWindow->GetFrameTime();
  190.         while(restTime > UPDATE_RATE)
  191.         {
  192.             restTime -= UPDATE_RATE;
  193.             update(UPDATE_RATE);
  194.         }
  195.         glClear(GL_COLOR_BUFFER_BIT);
  196.         myClientPlayer.draw(*myWindow);
  197.         myServerPlayer.draw(*myWindow);
  198.         myWindow->Display();
  199.     }
  200.     return 0;
  201. }
  202.  
  203. void Game::sendClientInput()
  204. {
  205.     sf::Packet toSend;
  206.     toSend << myClientPlayer.input;
  207.     unsigned short port = PORT;
  208.     sf::Socket::Status status = mySocket.Send(toSend, myIPAddress, port);
  209.     switch(status)
  210.     {
  211.     case sf::Socket::Disconnected:
  212.         cout << "Disconnected" << endl;
  213.         myIsConnected = false;
  214.         break;
  215.     case sf::Socket::Error:
  216.         cout << "Error sending input" << endl;
  217.         break;
  218.     case sf::Socket::Done:
  219.         break;
  220.     case sf::Socket::NotReady:
  221.         //try again next time;
  222.         break;
  223.     }
  224. }
  225.  
  226. void Game::receiveClientInput()
  227. {
  228.     sf::Packet toReceive;
  229.     unsigned short port = PORT;
  230.     sf::Socket::Status status = mySocket.Receive(toReceive, myIPAddress, port);
  231.     switch(status)
  232.     {
  233.     case sf::Socket::Disconnected:
  234.         cout << "Disconnected" << endl;
  235.         myIsConnected = false;
  236.         break;
  237.     case sf::Socket::Error:
  238.         cout << "Error receiving" << endl;
  239.         break;
  240.     case sf::Socket::Done:
  241.         toReceive >> myClientPlayer.input;
  242.         break;
  243.     case sf::Socket::NotReady:
  244.         //try again next time;
  245.         break;
  246.     }
  247. }
  248.  
  249. void Game::sendGameState()
  250. {
  251.     sf::Packet toSend;
  252.     toSend << myServerPlayer.xPos << myServerPlayer.yPos << myServerPlayer.input
  253.         << myClientPlayer.xPos << myClientPlayer.yPos;
  254.     sf::Socket::Status status = mySocket.Send(toSend, myIPAddress, PORT);
  255.     switch(status)
  256.     {
  257.     case sf::Socket::Disconnected:
  258.         cout << "Disconnected" << endl;
  259.         myIsConnected = false;
  260.         break;
  261.     case sf::Socket::Error:
  262.         cout << "Error sending" << endl;
  263.         break;
  264.     case sf::Socket::Done:
  265.         break;
  266.     case sf::Socket::NotReady:
  267.         //try again next time;
  268.         break;
  269.     }
  270. }
  271.  
  272. void Game::receiveAndSyncGameState()
  273. {
  274.     sf::Packet toReceive;
  275.     unsigned short port = PORT;
  276.     sf::Socket::Status status = mySocket.Receive(toReceive, myIPAddress, port);
  277.     switch(status)
  278.     {
  279.     case sf::Socket::Disconnected:
  280.         cout << "Disconnected" << endl;
  281.         myIsConnected = false;
  282.         break;
  283.     case sf::Socket::Error:
  284.         cout << "Error receiving game state" << endl;
  285.         break;
  286.     case sf::Socket::Done:
  287.         toReceive >> myServerPlayer.xPos >> myServerPlayer.yPos >> myServerPlayer.input
  288.         >> myClientPlayer.xPos >> myClientPlayer.yPos;
  289.         //no interpolation yet
  290.         break;
  291.     case sf::Socket::NotReady:
  292.         //try again next time;
  293.         break;
  294.     }
  295. }
  296.  
  297. void Game::updateGameState(float delta)
  298. {
  299.     myClientPlayer.move(delta);
  300.     myServerPlayer.move(delta);
  301. }
  302.  
  303. void Game::update(float delta)
  304. {
  305.     if(myIsServer)
  306.     {
  307.         myServerPlayer.input.update(myWindow->GetInput());
  308.         if(myIsConnected)
  309.         {
  310.             receiveClientInput();
  311.             updateGameState(delta);
  312.             sendGameState();
  313.         }
  314.     }
  315.     else
  316.     {
  317.         myClientPlayer.input.update(myWindow->GetInput());
  318.         if(myIsConnected)
  319.         {
  320.             sendClientInput();
  321.             receiveAndSyncGameState();
  322.         }
  323.     }
  324. }
  325.  
  326. int main()
  327. {
  328.     Game game;
  329.     return game.run();
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement