浏览代码

Import network player

Previously known as "points en réseau"
DricomDragon 4 年之前
父节点
当前提交
772cec2b12
共有 3 个文件被更改,包括 228 次插入0 次删除
  1. 68 0
      sdl1/networkPlayers/Client.cpp
  2. 54 0
      sdl1/networkPlayers/Client.h
  3. 106 0
      sdl1/networkPlayers/main.cpp

+ 68 - 0
sdl1/networkPlayers/Client.cpp

@@ -0,0 +1,68 @@
+#include "Client.h"
+
+Client::Client(std::string IP, int port): m_IP(IP), m_port(port)
+{
+    m_csock.recsize = sizeof(m_csock.sin);
+    std::cout << "-°*,-Programme Client-,*°-" << std::endl;
+    std::cout << "Initialisation..." << std::endl;
+    #ifdef WIN32
+        m_erreur = WSAStartup(MAKEWORD(2,2), &m_WSAData);
+    #else
+        m_erreur = 0;
+    #endif // WIN32
+}
+
+Client::~Client()
+{
+    std::cout << "Fermeture de la socket client" << std::endl;
+    closesocket(m_csock.sock);
+    std::cout << "Fermeture du client termine" << std::endl;
+    #ifdef WIN32
+        WSACleanup();
+    #endif // WIN32
+}
+
+
+
+bool Client::rendreUtilisable()
+{
+    if(!m_erreur)
+    {
+        m_csock.sock = socket(AF_INET,SOCK_STREAM, 0);
+        if(m_csock.sock != INVALID_SOCKET)
+        {
+            std::cout << "La socket " << m_csock.sock << " est maintenant ouverte en mode TCP/IP" << std::endl;
+            m_csock.sin.sin_addr.s_addr = inet_addr(m_IP.c_str());
+            m_csock.sin.sin_family = AF_INET;
+            m_csock.sin.sin_port = htons(m_port);
+
+            if(connect(m_csock.sock, (SOCKADDR*)&m_csock.sin, m_csock.recsize) != SOCKET_ERROR)
+            {
+                std::cout << "Connection a " << inet_ntoa(m_csock.sin.sin_addr) << " sur le port " << htons(m_csock.sin.sin_port) << " reussie" << std::endl;
+            }
+            else
+                return false;
+        }
+        else
+            return false;
+    }
+    else
+        return false;
+    return true;
+}
+
+void Client::envoyer(void* donnee, int tailleByte)
+{
+    if(tailleByte != 0)
+        send(m_csock.sock, (char*)donnee, tailleByte, 0);
+    else
+        send(m_csock.sock, (char*)donnee, sizeof(donnee), 0);
+}
+
+void Client::recevoir(void* donnee, int tailleByte)
+{
+    if(tailleByte != 0)
+        recv(m_csock.sock, (char*)donnee, tailleByte, 0);
+    else
+        recv(m_csock.sock, (char*)donnee, sizeof(donnee), 0);
+}

+ 54 - 0
sdl1/networkPlayers/Client.h

@@ -0,0 +1,54 @@
+#ifndef CLIENT_H_INCLUDED
+#define CLIENT_H_INCLUDED
+
+#ifdef WIN32
+    #include <winsock2.h>
+    typedef int socklen_t;
+#elif defined (linux)
+    #include <sys/types.h>
+    #include <sys/socket.h>
+    #include <netinet/in.h>
+    #include <arpa/inet.h>
+    #include <unistd.h>
+
+    #define INVALID_SOCKET -1
+    #define SOCKET_ERROR -1
+    #define closesocket(s) close (s)
+
+    typedef int SOCKET;
+    typedef struct sockaddr_in SOCKADDR_IN;
+    typedef struct sockaddr SOCKADDR;
+
+#endif // WIN32
+#include <iostream>
+
+struct Sock
+{
+    SOCKADDR_IN sin;
+    SOCKET sock;
+    socklen_t recsize;
+};
+
+class Client
+{
+public:
+    Client(std::string IP, int port);
+    ~Client();
+
+    bool rendreUtilisable();
+    void envoyer(void* donnee, int tailleByte);
+    void recevoir(void* donnee, int tailleByte);
+
+private:
+    #ifdef WIN32
+    WSADATA m_WSAData;
+    #endif
+
+    Sock m_csock;
+
+    std::string m_IP;
+    int m_port;
+    int m_erreur;
+};
+
+#endif // CLIENT_H_INCLUDED

+ 106 - 0
sdl1/networkPlayers/main.cpp

@@ -0,0 +1,106 @@
+#include <iostream>
+#include <SDL.h>
+#undef main
+#include "Client.h"
+
+struct Attributs
+{
+    int x;
+    int y;
+};
+
+int main ( int argc, char** argv )
+{
+    /// [1] Démarrage
+    // [1.1] Démarrages SDL
+    if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
+    {
+        std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
+        return 1;
+    }
+
+    // [1.2] Préparation de fermeture
+    atexit(SDL_Quit);
+
+    // [1.3] Para-fenêtre
+    SDL_WM_SetCaption("Points en réseau", 0);
+
+    /// [2] Préparation des composants
+    // [2.1] Préparation du réseau
+    Client lan("192.168.1.87", 7856);
+    if (!lan.rendreUtilisable())
+    {
+        std::cout << "Problème d'initialisation des connexions." << std::endl;
+        return 1;
+    }
+
+    // [2.2] Préparation de la fenêtre
+    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32,
+                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
+    if ( !screen )
+    {
+        std::cout << "Unable to set 640x480 video: " << SDL_GetError() << std::endl;
+        return 1;
+    }
+
+    // [2.3] Préparation des points
+    Attributs blocMoi;
+    SDL_Rect posMoi;
+    Attributs blocAutre;
+    SDL_Rect posAutre;
+    blocMoi.x = blocMoi.y = blocAutre.x = blocAutre.y = 100;
+    SDL_Surface* imgMoi = SDL_CreateRGBSurface(SDL_HWSURFACE, 20, 20, 32, 0, 0, 0, 0);
+    SDL_Surface* imgAutre = SDL_CreateRGBSurface(SDL_HWSURFACE, 20, 20, 32, 0, 0, 0, 0);
+    SDL_FillRect(imgMoi, 0, SDL_MapRGB(imgMoi->format, 0, 0, 200));
+    SDL_FillRect(imgAutre, 0, SDL_MapRGB(imgMoi->format, 200, 0, 0));
+
+    /// [3] Boucle principale
+    bool done = false;
+    while (!done)
+    {
+        // [3.1] Gestion évènements
+        SDL_Event event;
+        while (SDL_PollEvent(&event))
+        {
+            switch (event.type)
+            {
+            case SDL_QUIT:
+                done = true;
+                break;
+            case SDL_KEYDOWN:
+                if (event.key.keysym.sym == SDLK_ESCAPE)
+                    done = true;
+                break;
+            case SDL_MOUSEMOTION:
+                blocMoi.x = event.motion.x;
+                blocMoi.y = event.motion.y;
+                break;
+            } // end switch event type
+        } // end of message processing
+
+        // [3.2] Réseau
+        lan.envoyer(&blocMoi, sizeof(Attributs));
+        lan.recevoir(&blocAutre, sizeof(Attributs));
+
+        // [3.3] Calculs
+        posMoi.x = blocMoi.x;
+        posMoi.y = blocMoi.y;
+        posAutre.x = blocAutre.x;
+        posAutre.y = blocAutre.y;
+
+        // [3.3] Dessin des composants
+        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 130, 120, 140));
+
+        SDL_BlitSurface(imgAutre, 0, screen, &posAutre);
+        SDL_BlitSurface(imgMoi, 0, screen, &posMoi);
+
+        SDL_Flip(screen);
+    } //fin bcl principale
+
+    ///[4] Destruction des composants
+    SDL_FreeSurface(screen);
+
+
+    std::cout << "Aucune erreur détectée." << std::endl;
+    return 0;
+}