From 54b1ffa94f581d73e8d61be52abe2aa89da1a2da Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Sat, 30 Oct 2021 19:59:55 +0700 Subject: [PATCH] Huge refactoring v3 --- Source.cpp | 4 +-- engine/Screen.cpp | 48 ++++++++++++++---------------------- engine/network/ClientUDP.cpp | 44 +++++++++++++++------------------ engine/network/ClientUDP.h | 11 ++++----- engine/network/ServerUDP.cpp | 8 +++--- engine/network/ServerUDP.h | 7 +++--- 6 files changed, 51 insertions(+), 71 deletions(-) diff --git a/Source.cpp b/Source.cpp index ac6f36f..9225478 100644 --- a/Source.cpp +++ b/Source.cpp @@ -10,10 +10,10 @@ using namespace std; int main() { Shooter game; - game.create(1280, 720, ShooterConsts::PROJECT_NAME); + //game.create(1280, 720, ShooterConsts::PROJECT_NAME); //game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); - //game.create(2048, 1152, ShooterConsts::PROJECT_NAME); + game.create(2048, 1152, ShooterConsts::PROJECT_NAME); //game.create(3072, 1920, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); return 0; diff --git a/engine/Screen.cpp b/engine/Screen.cpp index 596046a..7d151ba 100644 --- a/engine/Screen.cpp +++ b/engine/Screen.cpp @@ -178,45 +178,33 @@ GLfloat* Screen::glMeshToGLfloatArray(std::shared_ptr mesh, const Vec3D& c std::vector& triangles = mesh->triangles(); auto* geometry = (GLfloat*)malloc(7*3*triangles.size()*sizeof(GLfloat)); + Matrix4x4 model = mesh->model(); for(int i = 0; i < triangles.size(); i++) { int stride = 21*i; - double dot = triangles[i].norm().dot((mesh->model()*Vec3D(triangles[i][0]) - cameraPosition).normalized()); - sf::Color color = triangles[i].color(); - sf::Color ambientColor = sf::Color((sf::Uint8)(color.r * (0.3 * std::abs(dot) + 0.7)), - (sf::Uint8)(color.g * (0.3 * std::abs(dot) + 0.7)), - (sf::Uint8)(color.b * (0.3 * std::abs(dot) + 0.7)), - (sf::Uint8)color.a); + double dot[3]; + sf::Color ambientColor[3]; + for(int k = 0; k < 3; k++) { + dot[k] = triangles[i].norm().dot((model*Vec3D(triangles[i][k]) - cameraPosition).normalized()); - geometry[stride + 0] = (GLfloat)triangles[i][0].x(); - geometry[stride + 1] = (GLfloat)triangles[i][0].y(); - geometry[stride + 2] = (GLfloat)triangles[i][0].z(); + sf::Color color = triangles[i].color(); + ambientColor[k] = sf::Color((sf::Uint8)(color.r * (0.3 * std::abs(dot[k]) + 0.7)), + (sf::Uint8)(color.g * (0.3 * std::abs(dot[k]) + 0.7)), + (sf::Uint8)(color.b * (0.3 * std::abs(dot[k]) + 0.7)), + (sf::Uint8)color.a); - geometry[stride + 3] = (GLfloat)ambientColor.r/255.0f; - geometry[stride + 4] = (GLfloat)ambientColor.g/255.0f; - geometry[stride + 5] = (GLfloat)ambientColor.b/255.0f; - geometry[stride + 6] = (GLfloat)ambientColor.a/255.0f; + geometry[stride + 7*k + 0] = (GLfloat)triangles[i][k].x(); + geometry[stride + 7*k + 1] = (GLfloat)triangles[i][k].y(); + geometry[stride + 7*k + 2] = (GLfloat)triangles[i][k].z(); - geometry[stride + 7] = (GLfloat)triangles[i][1].x(); - geometry[stride + 8] = (GLfloat)triangles[i][1].y(); - geometry[stride + 9] = (GLfloat)triangles[i][1].z(); - - geometry[stride + 10] = (GLfloat)ambientColor.r/255.0f; - geometry[stride + 11] = (GLfloat)ambientColor.g/255.0f; - geometry[stride + 12] = (GLfloat)ambientColor.b/255.0f; - geometry[stride + 13] = (GLfloat)ambientColor.a/255.0f; - - geometry[stride + 14] = (GLfloat)triangles[i][2].x(); - geometry[stride + 15] = (GLfloat)triangles[i][2].y(); - geometry[stride + 16] = (GLfloat)triangles[i][2].z(); - - geometry[stride + 17] = (GLfloat)ambientColor.r/255.0f; - geometry[stride + 18] = (GLfloat)ambientColor.g/255.0f; - geometry[stride + 19] = (GLfloat)ambientColor.b/255.0f; - geometry[stride + 20] = (GLfloat)ambientColor.a/255.0f; + geometry[stride + 7*k + 3] = (GLfloat)ambientColor[k].r/255.0f; + geometry[stride + 7*k + 4] = (GLfloat)ambientColor[k].g/255.0f; + geometry[stride + 7*k + 5] = (GLfloat)ambientColor[k].b/255.0f; + geometry[stride + 7*k + 6] = (GLfloat)ambientColor[k].a/255.0f; + } } return geometry; } diff --git a/engine/network/ClientUDP.cpp b/engine/network/ClientUDP.cpp index 7fe0bb0..63938af 100644 --- a/engine/network/ClientUDP.cpp +++ b/engine/network/ClientUDP.cpp @@ -10,24 +10,19 @@ #include "../utils/Log.h" #include "../Consts.h" -ClientUDP::ClientUDP() : _lastBroadcast(-std::numeric_limits::max()), _working(false) -{ - // TODO: replace this with lambda - _socket.setTimeoutCallback(std::bind(&ClientUDP::timeout, this, std::placeholders::_1)); +ClientUDP::ClientUDP() { + _socket.setTimeoutCallback([this](sf::Uint16 id) {return ClientUDP::timeout(id); } ); } -bool ClientUDP::connected() const -{ +bool ClientUDP::connected() const { return _socket.ownId(); } -bool ClientUDP::isWorking() const -{ +bool ClientUDP::isWorking() const { return _working; } -void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port) -{ +void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port) { _ip = ip; _port = port; sf::Packet packet; @@ -39,12 +34,12 @@ void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port) Log::log("ClientUDP::connect(): connecting to the server..."); } -void ClientUDP::update() -{ - if (!isWorking()) +void ClientUDP::update() { + if (!isWorking()) { return; + } - while (isWorking() && process()); + while (isWorking() && process()) {} // Send new client information to server if (Time::time() - _lastBroadcast > 1.0 / Consts::NETWORK_WORLD_UPDATE_RATE && connected()) { @@ -56,8 +51,7 @@ void ClientUDP::update() _socket.update(); } -void ClientUDP::disconnect() -{ +void ClientUDP::disconnect() { sf::Packet packet; packet << MsgType::Disconnect << _socket.ownId(); _socket.send(packet, _socket.serverId()); @@ -68,30 +62,31 @@ void ClientUDP::disconnect() processDisconnected(); } -bool ClientUDP::timeout(sf::Uint16 id) -{ +bool ClientUDP::timeout(sf::Uint16 id) { Log::log("ClientUDP::timeout(): timeout from the server."); - if (id != _socket.serverId()) + if (id != _socket.serverId()) { return true; + } disconnect(); return false; } // Recive and process message. // Returns true, if some message was received. -bool ClientUDP::process() -{ +bool ClientUDP::process() { sf::Packet packet; sf::Uint16 senderId; sf::Uint16 targetId; MsgType type = _socket.receive(packet, senderId); - if (type == MsgType::Empty) + if (type == MsgType::Empty) { return false; - if (!connected() && type != MsgType::Init) + } + if (!connected() && type != MsgType::Init) { return true; + } switch (type) { // here we process any operations based on msg type @@ -115,8 +110,9 @@ bool ClientUDP::process() break; case MsgType::Disconnect: packet >> targetId; - if (targetId == _socket.ownId()) + if (targetId == _socket.ownId()) { disconnect(); + } Log::log("ClientUDP::process(): client Id = " + std::to_string(targetId) + " disconnected from the server"); diff --git a/engine/network/ClientUDP.h b/engine/network/ClientUDP.h index b9bf1d0..396f4ac 100644 --- a/engine/network/ClientUDP.h +++ b/engine/network/ClientUDP.h @@ -9,14 +9,13 @@ #include "UDPSocket.h" #include -class ClientUDP -{ +class ClientUDP { protected: UDPSocket _socket; - double _lastBroadcast; - bool _working; - sf::Uint16 _port; - sf::IpAddress _ip; + double _lastBroadcast = -std::numeric_limits::max(); + bool _working = false; + sf::Uint16 _port{}; + sf::IpAddress _ip{}; bool process(); bool timeout(sf::Uint16 id); diff --git a/engine/network/ServerUDP.cpp b/engine/network/ServerUDP.cpp index e0102e5..5170434 100644 --- a/engine/network/ServerUDP.cpp +++ b/engine/network/ServerUDP.cpp @@ -5,11 +5,9 @@ #include "ServerUDP.h" #include "MsgType.h" #include "../utils/Log.h" -#include -ServerUDP::ServerUDP() : _lastBroadcast(-std::numeric_limits::max()), _working(false) { - // TODO: replace this with lambda: - _socket.setTimeoutCallback(std::bind(&ServerUDP::timeout, this, std::placeholders::_1)); +ServerUDP::ServerUDP() { + _socket.setTimeoutCallback([this](sf::Uint16 playerId) { return timeout(playerId); }); } bool ServerUDP::isWorking() const { @@ -33,7 +31,7 @@ void ServerUDP::update() { return; } - while (process()); + while (process()) {} // World state broadcast if (Time::time() - _lastBroadcast > 1.0 / Consts::NETWORK_WORLD_UPDATE_RATE) { diff --git a/engine/network/ServerUDP.h b/engine/network/ServerUDP.h index 41f24d8..6e86683 100644 --- a/engine/network/ServerUDP.h +++ b/engine/network/ServerUDP.h @@ -12,12 +12,11 @@ #include #include -class ServerUDP -{ +class ServerUDP { protected: UDPSocket _socket; - double _lastBroadcast; - bool _working; + double _lastBroadcast = -std::numeric_limits::max(); + bool _working = false; bool process(); bool timeout(sf::Uint16 id);