diff --git a/SQCSim-common/define.h b/SQCSim-common/define.h index 45a7cbb..bafb39e 100644 --- a/SQCSim-common/define.h +++ b/SQCSim-common/define.h @@ -12,6 +12,7 @@ #define CHUNK_SIZE_Z 4 #define MAX_SELECTION_DISTANCE 5 #define SEED 12345 +#define COUNTDOWN 300 #define WORLD_SIZE_X 64 #define WORLD_SIZE_Y 64 diff --git a/SQCSim2021/SQCSim2021.vcxproj b/SQCSim2021/SQCSim2021.vcxproj index ab87f31..9cac270 100644 --- a/SQCSim2021/SQCSim2021.vcxproj +++ b/SQCSim2021/SQCSim2021.vcxproj @@ -38,7 +38,6 @@ - @@ -62,6 +61,11 @@ + + + {ee91ab12-4225-4a4d-931d-69d72f6d91fb} + + {A21FD938-1FEA-4687-AB86-0EABAC30877B} Win32Proj diff --git a/SQCSim2021/SQCSim2021.vcxproj.filters b/SQCSim2021/SQCSim2021.vcxproj.filters index 8d96ac0..60aa0a8 100644 --- a/SQCSim2021/SQCSim2021.vcxproj.filters +++ b/SQCSim2021/SQCSim2021.vcxproj.filters @@ -38,9 +38,6 @@ Fichiers d%27en-tête - - Fichiers d%27en-tête - Fichiers d%27en-tête diff --git a/SQCSim2021/audio.h b/SQCSim2021/audio.h index 9e8e3be..315bfae 100644 --- a/SQCSim2021/audio.h +++ b/SQCSim2021/audio.h @@ -4,7 +4,7 @@ #include #include #include "define.h" -#include "vector3.h" +#include "../SQCSim-common/vector3.h" class Audio { private: diff --git a/SQCSim2021/connector.cpp b/SQCSim2021/connector.cpp index 0bdaa56..4ccc9a0 100644 --- a/SQCSim2021/connector.cpp +++ b/SQCSim2021/connector.cpp @@ -1,18 +1,51 @@ #include "connector.h" -Connector::Connector() -{ -} +Connector::Connector() {} -Connector::~Connector() -{ -} +Connector::~Connector() {} + +int Connector::Init() { +#ifdef _WIN32 + if (WSAStartup(MAKEWORD(2, 2), &m_wsaData) != 0) { /* Initialisation de l'environnement reseau (Windows only) */ + std::cout << "Initialisation WinSock." << std::endl; + return 1; + } +#endif + + m_sock_udp = socket(AF_INET, SOCK_DGRAM, 0); + if (m_sock_udp == INVALID_SOCKET) { /* Creation du socket UDP */ + std::cout << "Creation Socket UDP." << std::endl; + return 2; + } + + m_sock_tcp = socket(AF_INET, SOCK_STREAM, 0); + if (m_sock_tcp == INVALID_SOCKET) { /* Creation du socket TCP */ + std::cout << "Creation Socket TCP." << std::endl; + return 3; + } + + /* Creation structure donnes descripteur du socket serveur */ + sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(SRV_PORT); + addr.sin_addr.s_addr = htonl(INADDR_ANY); + + if (bind(m_sock_udp, (sockaddr*)&addr, sizeof(addr)) != 0) { /* Associer le socket UDP au port */ + std::cout << "Association Socket UDP." << std::endl; + return 4; + } + + if (bind(m_sock_tcp, (sockaddr*)&addr, sizeof(addr)) != 0) { /* Associer le socket TCP au port */ + std::cout << "Association Socket TCP." << std::endl; + return 5; + } + + std::cout << "It is the voork!" << std::endl; -int Connector::Init(sockaddr_in srv_addr) { return 0; } -int Connector::Connect(std::string name) { +int Connector::Connect(sockaddr_in srv_addr, std::string name) { return 0; } diff --git a/SQCSim2021/connector.h b/SQCSim2021/connector.h index d2aee73..b9653ad 100644 --- a/SQCSim2021/connector.h +++ b/SQCSim2021/connector.h @@ -2,41 +2,26 @@ #define CONNECTOR_H__ #include "define.h" -#include "vector3.h" - -struct Input { // vers serveur - Timestamp timestamp; - UINT8 keys; // 0bFBLRJS__ - Vector3f direction; -}; - -struct Output { // autres joueurs du serveur - Timestamp timestamp; - UINT64 id = 0; - Vector3f position, direction; - bool is_shooting, is_jumping; -}; - -struct Sync { // du serveur - Timestamp timestamp; - UINT64 sid = 0; - Vector3f position; -}; +#include "../SQCSim-common/netprotocol.h" class Connector { public: Connector(); ~Connector(); - int Init(sockaddr_in srv_addr); - int Connect(std::string name); + int Init(); + int Connect(sockaddr_in srv_addr, std::string name); UINT64 getId() const; unsigned int getSeed() const; //void SendInput(); //int Sync(); private: - SOCKET m_sock = 0; +#ifdef _WIN32 + WSADATA m_wsaData; +#endif + SOCKET m_sock_udp = 0, + m_sock_tcp = 0; std::string m_name = ""; UINT64 m_sid = 0, m_tid = 0; diff --git a/SQCSim2021/define.h b/SQCSim2021/define.h index c26b096..533d6dc 100644 --- a/SQCSim2021/define.h +++ b/SQCSim2021/define.h @@ -1,11 +1,12 @@ -#ifndef DEFINE_H__ -#define DEFINE_H__ +#ifndef CLI_DEFINE_H__ +#define CLI_DEFINE_H__ #include #include #include #include #include +#include "../SQCSim-common/define.h" #ifdef _WIN32 #include @@ -14,21 +15,11 @@ #include #endif -#define NETWORK_TEST false + + +#define NETWORK_TEST true #define SRV_ADDR "127.0.0.1" -#define SRV_PORT 1025 -#define CLI_PORT 1026 - -#define CHUNK_SIZE_X 4 -#define CHUNK_SIZE_Y 64 -#define CHUNK_SIZE_Z 4 -#define MAX_SELECTION_DISTANCE 5 -#define BASE_WIDTH 640 -#define BASE_HEIGHT 480 -#define SEED 12345 - -#define WORLD_SIZE_X 64 -#define WORLD_SIZE_Y 64 +#define COUNTDOWN 300 #define FRAMES_RENDER_CHUNKS 1 #define FRAMES_UPDATE_CHUNKS 1 @@ -38,39 +29,8 @@ #define THREADS_UPDATE_CHUNKS 3 #define THREADS_DELETE_CHUNKS 3 -#define VIEW_DISTANCE 512 -#define TEXTURE_SIZE 512 -#define MAX_BULLETS 512 - -#ifdef _WIN32 - -#pragma comment(lib,"wsock32.lib") // Pour pouvoir faire fonctionner le linker sans le vcxproject - -#include -#include -#include - -#define popen _popen -#define pclose _pclose - -#else // Pas _WIN32 - -#include -#include -#include -#include -#include -#include - -#define SOCKET int -#define INVALID_SOCKET -1 -#define closesocket close - -#endif // _WIN32 - -typedef uint8_t BlockType; -enum BLOCK_TYPE { BTYPE_AIR, BTYPE_DIRT, BTYPE_GRASS, BTYPE_METAL, BTYPE_ICE, BTYPE_LAST }; -typedef std::chrono::system_clock::time_point Timestamp; +#define BASE_WIDTH 640 +#define BASE_HEIGHT 480 #define TEXTURE_PATH "./media/textures/" #define SHADER_PATH "./media/shaders/" diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index 3173dc0..bd618ae 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -55,11 +55,11 @@ void Engine::Init() { addr.sin_port = htons(SRV_PORT); addr.sin_addr.s_addr = inet_addr(srvaddr); - if (!m_conn->Init(addr)) { - if (m_conn->Connect(playname)) { + if (!m_conn.Init()) { + if (m_conn.Connect(addr, playname)) { // setup jeu en réseau. - seed = m_conn->getSeed(); - std::cout << "ID reçu du serveur: " << std::to_string(m_conn->getId()) << "!" << std::endl; + seed = m_conn.getSeed(); + std::cout << "ID reçu du serveur: " << std::to_string(m_conn.getId()) << "!" << std::endl; } else std::cout << "Erreur de connexion." << std::endl; } diff --git a/SQCSim2021/engine.h b/SQCSim2021/engine.h index faae19d..5ce52ec 100644 --- a/SQCSim2021/engine.h +++ b/SQCSim2021/engine.h @@ -49,7 +49,7 @@ private: void DrawHud(float elapsedTime, BlockType bloc); void PrintText(float x, float y, float scale, const std::string& t); - Connector* m_conn = nullptr; + Connector m_conn; Shader m_shader01; BlockInfo* m_blockinfo[BTYPE_LAST]; TextureAtlas m_textureAtlas = TextureAtlas(BTYPE_LAST); diff --git a/SQCSim2021/matrix4.h b/SQCSim2021/matrix4.h index a0d911a..bf5ac5b 100644 --- a/SQCSim2021/matrix4.h +++ b/SQCSim2021/matrix4.h @@ -6,7 +6,7 @@ #include #include "define.h" -#include "vector3.h" +#include "../SQCSim-common/vector3.h" #ifndef M_PI #define M_PI 3.14159265f diff --git a/SQCSim2021/player.h b/SQCSim2021/player.h index 3466d1a..239b550 100644 --- a/SQCSim2021/player.h +++ b/SQCSim2021/player.h @@ -1,6 +1,6 @@ #ifndef _PLAYER_H__ #define _PLAYER_H__ -#include "vector3.h" +#include "../SQCSim-common/vector3.h" #include "transformation.h" #include "audio.h" #include diff --git a/SQCSim2021/transformation.h b/SQCSim2021/transformation.h index d147a58..764a781 100644 --- a/SQCSim2021/transformation.h +++ b/SQCSim2021/transformation.h @@ -2,7 +2,7 @@ #define TRANSFORMATION_H__ #include "matrix4.h" -#include "vector3.h" +#include "../SQCSim-common/vector3.h" #include class Transformation diff --git a/SQCSim2021/vector3.h b/SQCSim2021/vector3.h deleted file mode 100644 index b191681..0000000 --- a/SQCSim2021/vector3.h +++ /dev/null @@ -1,219 +0,0 @@ -#ifndef VECTOR3_H__ -#define VECTOR3_H__ - -#include -#include - -template -class Vector3 -{ -public: - Vector3(); - Vector3(const T& x, const T& y, const T& z); - ~Vector3(); - - T Length() const; - void Normalize(); - void Zero(); - - T Dot(const Vector3& v) const; - Vector3 Cross(const Vector3& v) const; - - Vector3 operator+(const Vector3& v) const; - Vector3 operator-(const Vector3& v) const; - Vector3 operator-() const; - Vector3 operator+(const T& v) const; - Vector3 operator-(const T& v) const; - Vector3 operator/(const T& v) const; - Vector3 operator*(const T& v) const; - - Vector3& operator=(const Vector3& v); - - Vector3& operator+=(const Vector3& v); - Vector3& operator-=(const Vector3& v); - Vector3& operator+=(const T& v); - Vector3& operator-=(const T& v); - Vector3& operator/=(const T& v); - Vector3& operator*=(const T& v); - - bool operator==(const Vector3& v) const; - bool operator!=(const Vector3& v) const; - - void Afficher() const; - -public: - T x, y, z; -}; - -typedef Vector3 Vector3i; -typedef Vector3 Vector3f; - -template -inline std::ostream& operator<<(std::ostream& out, const Vector3& v) -{ - out << "[" << v.x << ", " << v.y << ", " << v.z << "]"; - return out; -} - - -template -Vector3::Vector3() -{ -} - -template -Vector3::Vector3(const T& x, const T& y, const T& z) : x(x), y(y), z(z) -{ -} - -template -Vector3::~Vector3() -{ -} - -template -T Vector3::Length() const -{ - return sqrt(x*x + y*y + z*z); -} - -template -void Vector3::Normalize() -{ - T len = Length(); - if (len != 0) - { - x /= len; - y /= len; - z /= len; - } -} - -template -void Vector3::Zero() -{ - x = y = z = 0; -} - -template -T Vector3::Dot(const Vector3& v) const -{ - return (x * v.x) + (y * v.y) + (z * v.z); -} - -template -Vector3 Vector3::Cross(const Vector3& v) const -{ - return Vector3( - y * v.z - v.y * z, - z * v.x - v.z * x, - x * v.y - v.x * y); -} - -template -Vector3 Vector3::operator+(const Vector3& v) const -{ - return Vector3(x + v.x, y + v.y, z + v.z); -} - -template -Vector3 Vector3::operator-(const Vector3& v) const -{ - return Vector3(x - v.x, y - v.y, z - v.z); -} - -template -Vector3 Vector3::operator-() const -{ - return Vector3(-x, -y, -z); -} - -template -Vector3 Vector3::operator+(const T& v) const -{ - return Vector3(x + v, y + v, z + v); -} - -template -Vector3 Vector3::operator-(const T& v) const -{ - return Vector3(x - v, y - v, z - v); -} - -template -Vector3 Vector3::operator/(const T& v) const -{ - return Vector3(x / v, y / v, z / v); -} - -template -Vector3 Vector3::operator*(const T& v) const -{ - return Vector3(x * v, y * v, z * v); -} - -template -Vector3& Vector3::operator=(const Vector3& v) -{ - x = v.x; - y = v.y; - z = v.z; - return *this; -} - -template -Vector3& Vector3::operator+=(const Vector3& v) -{ - return (*this = *this + v); -} - -template -Vector3& Vector3::operator-=(const Vector3& v) -{ - return (*this = *this - v); -} - -template -Vector3& Vector3::operator+=(const T& v) -{ - return (*this = *this + v); -} - -template -Vector3& Vector3::operator-=(const T& v) -{ - return (*this = *this - v); -} - -template -Vector3& Vector3::operator/=(const T& v) -{ - return (*this = *this / v); -} - -template -Vector3& Vector3::operator*=(const T& v) -{ - return (*this = *this * v); -} - -template -bool Vector3::operator==(const Vector3& v) const -{ - return (x == v.x && y == v.y && z == v.z); -} - -template -bool Vector3::operator!=(const Vector3& v) const -{ - return !(*this == v); -} - -template -void Vector3::Afficher() const -{ - std::cout << "[" << x << ", " << y << ", " << z << "]" << std::endl; -} - - -#endif // VECTOR3_H__ diff --git a/SQCSim2021/world.h b/SQCSim2021/world.h index 55140e3..486e2d2 100644 --- a/SQCSim2021/world.h +++ b/SQCSim2021/world.h @@ -8,7 +8,7 @@ #include "define.h" #include "chunk.h" #include "array2d.h" -#include "vector3.h" +#include "../SQCSim-common/vector3.h" #include "player.h" #include "transformation.h" #include "shader.h"