Huge refactoring v3
parent
8398c6ebb5
commit
54b1ffa94f
|
@ -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;
|
||||
|
|
|
@ -178,45 +178,33 @@ GLfloat* Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D& c
|
|||
std::vector<Triangle>& 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());
|
||||
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 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)),
|
||||
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 + 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 + 1] = (GLfloat)triangles[i][0].y();
|
||||
geometry[stride + 2] = (GLfloat)triangles[i][0].z();
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -10,24 +10,19 @@
|
|||
#include "../utils/Log.h"
|
||||
#include "../Consts.h"
|
||||
|
||||
ClientUDP::ClientUDP() : _lastBroadcast(-std::numeric_limits<double>::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");
|
||||
|
||||
|
|
|
@ -9,14 +9,13 @@
|
|||
#include "UDPSocket.h"
|
||||
#include <memory>
|
||||
|
||||
class ClientUDP
|
||||
{
|
||||
class ClientUDP {
|
||||
protected:
|
||||
UDPSocket _socket;
|
||||
double _lastBroadcast;
|
||||
bool _working;
|
||||
sf::Uint16 _port;
|
||||
sf::IpAddress _ip;
|
||||
double _lastBroadcast = -std::numeric_limits<double>::max();
|
||||
bool _working = false;
|
||||
sf::Uint16 _port{};
|
||||
sf::IpAddress _ip{};
|
||||
|
||||
bool process();
|
||||
bool timeout(sf::Uint16 id);
|
||||
|
|
|
@ -5,11 +5,9 @@
|
|||
#include "ServerUDP.h"
|
||||
#include "MsgType.h"
|
||||
#include "../utils/Log.h"
|
||||
#include <cmath>
|
||||
|
||||
ServerUDP::ServerUDP() : _lastBroadcast(-std::numeric_limits<double>::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) {
|
||||
|
|
|
@ -12,12 +12,11 @@
|
|||
#include <memory>
|
||||
#include <set>
|
||||
|
||||
class ServerUDP
|
||||
{
|
||||
class ServerUDP {
|
||||
protected:
|
||||
UDPSocket _socket;
|
||||
double _lastBroadcast;
|
||||
bool _working;
|
||||
double _lastBroadcast = -std::numeric_limits<double>::max();
|
||||
bool _working = false;
|
||||
|
||||
bool process();
|
||||
bool timeout(sf::Uint16 id);
|
||||
|
|
Loading…
Reference in New Issue