Commit 46c7f15e authored by pa's avatar pa

Add TCP listener to input

parent 39b897e3
......@@ -8,6 +8,7 @@ HEADERS=$(wildcard $(HEADERS_DIR)*.cpp)
PARAMS= -lsfml-graphics
PARAMS+= -lsfml-window
PARAMS+= -lsfml-system
PARAMS+= -lsfml-network
all : dir lsv-fighters
......
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <set>
#include <iostream>
class Input{
......@@ -8,13 +10,6 @@ class Input{
virtual bool isKeyPressed(sf::Keyboard::Key k);
virtual void update();
protected :
void press(sf::Keyboard::Key k);
void release(sf::Keyboard::Key k);
private :
std::set<sf::Keyboard::Key> pressed;
};
class KeyInput : public Input {
......@@ -22,3 +17,19 @@ class KeyInput : public Input {
virtual bool isKeyPressed(sf::Keyboard::Key k);
virtual void update();
};
class TCPInput : public Input{
public :
TCPInput();
virtual bool isKeyPressed(sf::Keyboard::Key k);
virtual void update();
protected :
void press(sf::Keyboard::Key k);
void release(sf::Keyboard::Key k);
private :
std::set<sf::Keyboard::Key> pressed;
sf::TcpListener listener;
sf::TcpSocket client;
};
#include "../headers/input.hpp"
void Input::press(sf::Keyboard::Key k){
void TCPInput::press(sf::Keyboard::Key k){
pressed.insert(k);
}
void Input::release(sf::Keyboard::Key k){
void TCPInput::release(sf::Keyboard::Key k){
pressed.erase(k);
}
void Input::update(){}
bool TCPInput::isKeyPressed(sf::Keyboard::Key k){
return pressed.find(k) != pressed.end();
}
void Input::update(){
}
TCPInput::TCPInput(){
client.setBlocking(false);
if(listener.listen(53000) != sf::Socket::Done)
std::cout << "Wesh, j'ai pas réussi à établir la co\n";
if(listener.accept(client) != sf::Socket::Done)
std::cout << "Wesh, j'ai pas réussi à établir la co\n";
}
void TCPInput::update(){
char data[100];
std::size_t received;
while(client.receive(data,100,received) != sf::Socket::Done){
if (data[0] == 'P') press((sf::Keyboard::Key) atoi(data+1));
if (data[0] == 'R') release((sf::Keyboard::Key) atoi(data+1));
}
}
bool Input::isKeyPressed(sf::Keyboard::Key k){
return pressed.find(k) != pressed.end();
return false;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment