Netork control of the camera now works, slight hitch every few seconds though. Not sure how to track that down
This commit is contained in:
@@ -25,8 +25,6 @@ private:
|
||||
|
||||
void transpose_sf_events(std::list<sf::Event> event_queue);
|
||||
|
||||
// Network controller class
|
||||
|
||||
std::vector<sf::Keyboard::Key> held_keys;
|
||||
std::vector<sf::Mouse::Button> held_mouse_buttons;
|
||||
|
||||
@@ -36,7 +34,6 @@ private:
|
||||
private:
|
||||
|
||||
std::list<std::unique_ptr<vr::Event>> event_queue;
|
||||
|
||||
};
|
||||
|
||||
class WindowHandler : public VrEventSubscriber {
|
||||
|
||||
44
include/NetworkInput.h
Normal file
44
include/NetworkInput.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <SFML/Network.hpp>
|
||||
#include <thread>
|
||||
#include "Event.hpp"
|
||||
#include "Pub_Sub.h"
|
||||
#include <list>
|
||||
|
||||
struct CustomPacket {
|
||||
|
||||
char data[1024];
|
||||
int position = 0;
|
||||
int size = 12;
|
||||
|
||||
};
|
||||
|
||||
class NetworkInput : public VrEventPublisher {
|
||||
public:
|
||||
NetworkInput();
|
||||
|
||||
void listen_for_clients(int port);
|
||||
void stop_listening_for_clients();
|
||||
|
||||
void recieve_from_clients();
|
||||
void stop_recieving_from_clients();
|
||||
|
||||
void generate_events();
|
||||
void dispatch_events();
|
||||
|
||||
|
||||
private:
|
||||
|
||||
std::list<std::unique_ptr<vr::Event>> event_queue;
|
||||
|
||||
std::vector<sf::TcpSocket*> client_sockets;
|
||||
sf::SocketSelector socket_selector;
|
||||
|
||||
std::thread *client_listener_thread;
|
||||
std::thread *client_reciever_thread;
|
||||
|
||||
void threaded_client_listener(int port);
|
||||
void threaded_client_reciever();
|
||||
|
||||
sf::TcpListener listener;
|
||||
|
||||
};
|
||||
@@ -96,10 +96,9 @@ void Camera::recieve_event(VrEventPublisher* publisher, std::unique_ptr<vr::Even
|
||||
if (held_event->code == sf::Keyboard::LShift) {
|
||||
default_impulse = 0.01f;
|
||||
}
|
||||
if (held_event->code == sf::Keyboard::RShift) {
|
||||
else if (held_event->code == sf::Keyboard::RShift) {
|
||||
default_impulse = 1.0f;
|
||||
}
|
||||
|
||||
else if (held_event->code == sf::Keyboard::C) {
|
||||
look_at_center();
|
||||
}
|
||||
@@ -130,7 +129,7 @@ void Camera::recieve_event(VrEventPublisher* publisher, std::unique_ptr<vr::Even
|
||||
|
||||
vr::KeyPressed *key_event = static_cast<vr::KeyPressed*>(event.get());
|
||||
|
||||
if (key_event->code == sf::Keyboard::Y) {
|
||||
if (key_event->code == sf::Keyboard::M) {
|
||||
if (mouse_enabled)
|
||||
mouse_enabled = false;
|
||||
else
|
||||
@@ -156,6 +155,22 @@ void Camera::recieve_event(VrEventPublisher* publisher, std::unique_ptr<vr::Even
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (event->type == vr::Event::JoystickMoved) {
|
||||
|
||||
vr::JoystickMoved *joystick_event = static_cast<vr::JoystickMoved*>(event.get());
|
||||
|
||||
if (joystick_event->axis == sf::Joystick::Axis::X) {
|
||||
movement.x -= joystick_event->position / 5;
|
||||
//add_relative_impulse(Camera::DIRECTION::FORWARD, joystick_event->position);
|
||||
}
|
||||
else if (joystick_event->axis == sf::Joystick::Axis::Y) {
|
||||
movement.y += joystick_event->position / 5;
|
||||
//add_relative_impulse(Camera::DIRECTION::RIGHT, joystick_event->position);
|
||||
}
|
||||
//else if (joystick_event->axis == sf::Joystick::Axis::Z) {
|
||||
// add_relative_impulse(Camera::DIRECTION::DOWN, joystick_event->position);
|
||||
//}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
161
src/NetworkInput.cpp
Normal file
161
src/NetworkInput.cpp
Normal file
@@ -0,0 +1,161 @@
|
||||
#include "NetworkInput.h"
|
||||
#include <iostream>
|
||||
|
||||
NetworkInput::NetworkInput() {
|
||||
|
||||
}
|
||||
|
||||
void NetworkInput::listen_for_clients(int port) {
|
||||
//listener.listen(port);
|
||||
client_listener_thread = new std::thread(&NetworkInput::threaded_client_listener, this, port);
|
||||
}
|
||||
|
||||
void NetworkInput::stop_listening_for_clients() {
|
||||
delete client_listener_thread;
|
||||
}
|
||||
|
||||
void NetworkInput::recieve_from_clients()
|
||||
{
|
||||
//// Receive a message from the client
|
||||
//char buffer[1024];
|
||||
|
||||
//std::vector<CustomPacket> packets;
|
||||
|
||||
//sf::TcpSocket::Status status;
|
||||
|
||||
//do {
|
||||
|
||||
// std::size_t received = 0;
|
||||
// status = socket.receive(buffer, 1024, received);
|
||||
|
||||
// while (received < 12) {
|
||||
// std::size_t tack_on;
|
||||
// status = socket.receive(&buffer[received], 1024 - received, tack_on);
|
||||
// received += tack_on;
|
||||
// }
|
||||
|
||||
|
||||
// int position = 0;
|
||||
// while (position < received) {
|
||||
// CustomPacket p;
|
||||
// memcpy(p.data, &buffer[position], p.size);
|
||||
// packets.push_back(p);
|
||||
// position += p.size;
|
||||
// }
|
||||
|
||||
// std::cout << "packet_count = " << packets.size() << std::endl;
|
||||
|
||||
// int left_over = 12 - (position - received);
|
||||
// memcpy(buffer, &buffer[received - left_over], left_over);
|
||||
|
||||
//} while (status != sf::TcpSocket::Status::Disconnected);
|
||||
}
|
||||
|
||||
void NetworkInput::dispatch_events()
|
||||
{
|
||||
while (event_queue.size() != 0) {
|
||||
notify_subscribers(std::move(event_queue.front()));
|
||||
event_queue.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkInput::threaded_client_listener(int port) {
|
||||
|
||||
listener.listen(port);
|
||||
socket_selector.add(listener);
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Make the selector wait for data on any socket
|
||||
if (socket_selector.wait())
|
||||
{
|
||||
// Test the listener
|
||||
if (socket_selector.isReady(listener))
|
||||
{
|
||||
// The listener is ready: there is a pending connection
|
||||
sf::TcpSocket* client = new sf::TcpSocket;
|
||||
if (listener.accept(*client) == sf::Socket::Done)
|
||||
{
|
||||
// Add the new client to the clients list
|
||||
client_sockets.push_back(client);
|
||||
|
||||
// Add the new client to the selector so that we will
|
||||
// be notified when he sends something
|
||||
socket_selector.add(*client);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Error, we won't get a new connection, delete the socket
|
||||
delete client;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// The listener socket is not ready, test all other sockets (the clients)
|
||||
for (std::vector<sf::TcpSocket*>::iterator it = client_sockets.begin(); it != client_sockets.end(); ++it)
|
||||
{
|
||||
sf::TcpSocket& client = **it;
|
||||
if (socket_selector.isReady(client))
|
||||
{
|
||||
// Receive a message from the client
|
||||
char buffer[1024];
|
||||
|
||||
std::vector<CustomPacket> packets;
|
||||
|
||||
sf::TcpSocket::Status status;
|
||||
|
||||
do {
|
||||
|
||||
std::size_t received = 0;
|
||||
status = client.receive(buffer, 1024, received);
|
||||
|
||||
while (received < 12) {
|
||||
std::size_t tack_on;
|
||||
status = client.receive(&buffer[received], 1024 - received, tack_on);
|
||||
received += tack_on;
|
||||
}
|
||||
|
||||
|
||||
int position = 0;
|
||||
while (position < received) {
|
||||
CustomPacket p;
|
||||
memcpy(p.data, &buffer[position], p.size);
|
||||
packets.push_back(p);
|
||||
position += p.size;
|
||||
}
|
||||
|
||||
std::cout << "packet_count = " << packets.size() << std::endl;
|
||||
|
||||
int left_over = 12 - (position - received);
|
||||
memcpy(buffer, &buffer[received - left_over], left_over);
|
||||
|
||||
} while (status != sf::TcpSocket::Status::Done);
|
||||
|
||||
|
||||
for (auto i: packets) {
|
||||
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
|
||||
memcpy(&x, &i.data, sizeof(x));
|
||||
memcpy(&y, &i.data[4], sizeof(y));
|
||||
memcpy(&z, &i.data[8], sizeof(z));
|
||||
|
||||
event_queue.push_back(std::make_unique<vr::JoystickMoved>(vr::JoystickMoved(sf::Joystick::Axis::X, 0, x)));
|
||||
event_queue.push_back(std::make_unique<vr::JoystickMoved>(vr::JoystickMoved(sf::Joystick::Axis::Y, 0, y)));
|
||||
event_queue.push_back(std::make_unique<vr::JoystickMoved>(vr::JoystickMoved(sf::Joystick::Axis::Z, 0, z)));
|
||||
|
||||
std::cout << "X: " << x << " Y: " << y << " Z: " << z << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkInput::threaded_client_reciever()
|
||||
{
|
||||
|
||||
}
|
||||
61
src/main.cpp
61
src/main.cpp
@@ -36,6 +36,7 @@
|
||||
#include "Software_Caster.h"
|
||||
#include "Input.h"
|
||||
#include "Pub_Sub.h"
|
||||
#include "NetworkInput.h"
|
||||
|
||||
const int WINDOW_X = 1000;
|
||||
const int WINDOW_Y = 1000;
|
||||
@@ -65,64 +66,12 @@ sf::Texture window_texture;
|
||||
// Y: -1.57 is straight up
|
||||
// Y: 1.57 is straight down
|
||||
|
||||
struct CustomPacket {
|
||||
|
||||
char data[1024];
|
||||
int position = 0;
|
||||
int size = 12;
|
||||
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
|
||||
|
||||
// Create a listener to wait for incoming connections on port 55001
|
||||
sf::TcpListener listener;
|
||||
listener.listen(5000);
|
||||
|
||||
// Wait for a connection
|
||||
sf::TcpSocket socket;
|
||||
listener.accept(socket);
|
||||
std::cout << "New client connected: " << socket.getRemoteAddress() << std::endl;
|
||||
|
||||
// Receive a message from the client
|
||||
char buffer[1024];
|
||||
|
||||
std::vector<CustomPacket> packets;
|
||||
|
||||
sf::TcpSocket::Status status;
|
||||
|
||||
do {
|
||||
|
||||
std::size_t received = 0;
|
||||
status = socket.receive(buffer, 1024, received);
|
||||
|
||||
while (received < 12) {
|
||||
std::size_t tack_on;
|
||||
status = socket.receive(&buffer[received], 1024 - received, tack_on);
|
||||
received += tack_on;
|
||||
}
|
||||
|
||||
|
||||
int position = 0;
|
||||
while (position < received) {
|
||||
CustomPacket p;
|
||||
memcpy(p.data, &buffer[position], p.size);
|
||||
packets.push_back(p);
|
||||
position += p.size;
|
||||
}
|
||||
|
||||
std::cout << "packet_count = " << packets.size() << std::endl;
|
||||
|
||||
int left_over = 12 - (position - received);
|
||||
memcpy(buffer, &buffer[received - left_over], left_over);
|
||||
|
||||
} while (status != sf::TcpSocket::Status::Disconnected);
|
||||
|
||||
|
||||
|
||||
|
||||
NetworkInput ni;
|
||||
ni.listen_for_clients(5000);
|
||||
|
||||
|
||||
//Map _map(sf::Vector3i(0, 0, 0));
|
||||
@@ -229,6 +178,7 @@ int main() {
|
||||
camera->subscribe_to_publisher(&input_handler, vr::Event::EventType::KeyHeld);
|
||||
camera->subscribe_to_publisher(&input_handler, vr::Event::EventType::KeyPressed);
|
||||
camera->subscribe_to_publisher(&input_handler, vr::Event::EventType::MouseMoved);
|
||||
camera->subscribe_to_publisher(&ni, vr::Event::EventType::JoystickMoved);
|
||||
|
||||
WindowHandler win_hand(&window);
|
||||
win_hand.subscribe_to_publisher(&input_handler, vr::Event::EventType::Closed);
|
||||
@@ -240,6 +190,7 @@ int main() {
|
||||
input_handler.consume_sf_events(&window);
|
||||
input_handler.handle_held_keys();
|
||||
input_handler.dispatch_events();
|
||||
ni.dispatch_events();
|
||||
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::F11)) {
|
||||
@@ -274,7 +225,7 @@ int main() {
|
||||
light_vec.at(0).orbit_around_center(timer_accumulator += delta_time);
|
||||
}
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::M)) {
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num0)) {
|
||||
std::string path = "../assets/";
|
||||
std::string filename;
|
||||
std::getline(std::cin, filename);
|
||||
|
||||
Reference in New Issue
Block a user