Huge refactoring v3

master
Vectozavr 2021-10-30 19:59:55 +07:00
parent 8398c6ebb5
commit 54b1ffa94f
6 changed files with 51 additions and 71 deletions

View File

@ -10,10 +10,10 @@ using namespace std;
int main() { int main() {
Shooter game; 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(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); //game.create(3072, 1920, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen);
return 0; return 0;

View File

@ -178,45 +178,33 @@ GLfloat* Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D& c
std::vector<Triangle>& triangles = mesh->triangles(); std::vector<Triangle>& triangles = mesh->triangles();
auto* geometry = (GLfloat*)malloc(7*3*triangles.size()*sizeof(GLfloat)); auto* geometry = (GLfloat*)malloc(7*3*triangles.size()*sizeof(GLfloat));
Matrix4x4 model = mesh->model();
for(int i = 0; i < triangles.size(); i++) { for(int i = 0; i < triangles.size(); i++) {
int stride = 21*i; int stride = 21*i;
double dot = triangles[i].norm().dot((mesh->model()*Vec3D(triangles[i][0]) - cameraPosition).normalized()); 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());
sf::Color color = triangles[i].color(); sf::Color color = triangles[i].color();
sf::Color ambientColor = sf::Color((sf::Uint8)(color.r * (0.3 * std::abs(dot) + 0.7)), 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) + 0.7)), (sf::Uint8)(color.g * (0.3 * std::abs(dot[k]) + 0.7)),
(sf::Uint8)(color.b * (0.3 * std::abs(dot) + 0.7)), (sf::Uint8)(color.b * (0.3 * std::abs(dot[k]) + 0.7)),
(sf::Uint8)color.a); (sf::Uint8)color.a);
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 + 0] = (GLfloat)triangles[i][0].x(); geometry[stride + 7*k + 3] = (GLfloat)ambientColor[k].r/255.0f;
geometry[stride + 1] = (GLfloat)triangles[i][0].y(); geometry[stride + 7*k + 4] = (GLfloat)ambientColor[k].g/255.0f;
geometry[stride + 2] = (GLfloat)triangles[i][0].z(); geometry[stride + 7*k + 5] = (GLfloat)ambientColor[k].b/255.0f;
geometry[stride + 7*k + 6] = (GLfloat)ambientColor[k].a/255.0f;
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] = (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;
} }
return geometry; return geometry;
} }

View File

@ -10,24 +10,19 @@
#include "../utils/Log.h" #include "../utils/Log.h"
#include "../Consts.h" #include "../Consts.h"
ClientUDP::ClientUDP() : _lastBroadcast(-std::numeric_limits<double>::max()), _working(false) ClientUDP::ClientUDP() {
{ _socket.setTimeoutCallback([this](sf::Uint16 id) {return ClientUDP::timeout(id); } );
// TODO: replace this with lambda
_socket.setTimeoutCallback(std::bind(&ClientUDP::timeout, this, std::placeholders::_1));
} }
bool ClientUDP::connected() const bool ClientUDP::connected() const {
{
return _socket.ownId(); return _socket.ownId();
} }
bool ClientUDP::isWorking() const bool ClientUDP::isWorking() const {
{
return _working; return _working;
} }
void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port) void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port) {
{
_ip = ip; _ip = ip;
_port = port; _port = port;
sf::Packet packet; sf::Packet packet;
@ -39,12 +34,12 @@ void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port)
Log::log("ClientUDP::connect(): connecting to the server..."); Log::log("ClientUDP::connect(): connecting to the server...");
} }
void ClientUDP::update() void ClientUDP::update() {
{ if (!isWorking()) {
if (!isWorking())
return; return;
}
while (isWorking() && process()); while (isWorking() && process()) {}
// Send new client information to server // Send new client information to server
if (Time::time() - _lastBroadcast > 1.0 / Consts::NETWORK_WORLD_UPDATE_RATE && connected()) { if (Time::time() - _lastBroadcast > 1.0 / Consts::NETWORK_WORLD_UPDATE_RATE && connected()) {
@ -56,8 +51,7 @@ void ClientUDP::update()
_socket.update(); _socket.update();
} }
void ClientUDP::disconnect() void ClientUDP::disconnect() {
{
sf::Packet packet; sf::Packet packet;
packet << MsgType::Disconnect << _socket.ownId(); packet << MsgType::Disconnect << _socket.ownId();
_socket.send(packet, _socket.serverId()); _socket.send(packet, _socket.serverId());
@ -68,30 +62,31 @@ void ClientUDP::disconnect()
processDisconnected(); processDisconnected();
} }
bool ClientUDP::timeout(sf::Uint16 id) bool ClientUDP::timeout(sf::Uint16 id) {
{
Log::log("ClientUDP::timeout(): timeout from the server."); Log::log("ClientUDP::timeout(): timeout from the server.");
if (id != _socket.serverId()) if (id != _socket.serverId()) {
return true; return true;
}
disconnect(); disconnect();
return false; return false;
} }
// Recive and process message. // Recive and process message.
// Returns true, if some message was received. // Returns true, if some message was received.
bool ClientUDP::process() bool ClientUDP::process() {
{
sf::Packet packet; sf::Packet packet;
sf::Uint16 senderId; sf::Uint16 senderId;
sf::Uint16 targetId; sf::Uint16 targetId;
MsgType type = _socket.receive(packet, senderId); MsgType type = _socket.receive(packet, senderId);
if (type == MsgType::Empty) if (type == MsgType::Empty) {
return false; return false;
if (!connected() && type != MsgType::Init) }
if (!connected() && type != MsgType::Init) {
return true; return true;
}
switch (type) { switch (type) {
// here we process any operations based on msg type // here we process any operations based on msg type
@ -115,8 +110,9 @@ bool ClientUDP::process()
break; break;
case MsgType::Disconnect: case MsgType::Disconnect:
packet >> targetId; packet >> targetId;
if (targetId == _socket.ownId()) if (targetId == _socket.ownId()) {
disconnect(); disconnect();
}
Log::log("ClientUDP::process(): client Id = " + std::to_string(targetId) + " disconnected from the server"); Log::log("ClientUDP::process(): client Id = " + std::to_string(targetId) + " disconnected from the server");

View File

@ -9,14 +9,13 @@
#include "UDPSocket.h" #include "UDPSocket.h"
#include <memory> #include <memory>
class ClientUDP class ClientUDP {
{
protected: protected:
UDPSocket _socket; UDPSocket _socket;
double _lastBroadcast; double _lastBroadcast = -std::numeric_limits<double>::max();
bool _working; bool _working = false;
sf::Uint16 _port; sf::Uint16 _port{};
sf::IpAddress _ip; sf::IpAddress _ip{};
bool process(); bool process();
bool timeout(sf::Uint16 id); bool timeout(sf::Uint16 id);

View File

@ -5,11 +5,9 @@
#include "ServerUDP.h" #include "ServerUDP.h"
#include "MsgType.h" #include "MsgType.h"
#include "../utils/Log.h" #include "../utils/Log.h"
#include <cmath>
ServerUDP::ServerUDP() : _lastBroadcast(-std::numeric_limits<double>::max()), _working(false) { ServerUDP::ServerUDP() {
// TODO: replace this with lambda: _socket.setTimeoutCallback([this](sf::Uint16 playerId) { return timeout(playerId); });
_socket.setTimeoutCallback(std::bind(&ServerUDP::timeout, this, std::placeholders::_1));
} }
bool ServerUDP::isWorking() const { bool ServerUDP::isWorking() const {
@ -33,7 +31,7 @@ void ServerUDP::update() {
return; return;
} }
while (process()); while (process()) {}
// World state broadcast // World state broadcast
if (Time::time() - _lastBroadcast > 1.0 / Consts::NETWORK_WORLD_UPDATE_RATE) { if (Time::time() - _lastBroadcast > 1.0 / Consts::NETWORK_WORLD_UPDATE_RATE) {

View File

@ -12,12 +12,11 @@
#include <memory> #include <memory>
#include <set> #include <set>
class ServerUDP class ServerUDP {
{
protected: protected:
UDPSocket _socket; UDPSocket _socket;
double _lastBroadcast; double _lastBroadcast = -std::numeric_limits<double>::max();
bool _working; bool _working = false;
bool process(); bool process();
bool timeout(sf::Uint16 id); bool timeout(sf::Uint16 id);