...
 
Commits (5)
......@@ -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
......
0 0.5 0.5
6 0.25 0.5
6 0.25 0.65
6 0.25 0.35
16 0.75 0.5
22 0.5 0.25
19 0.5 0.75
......@@ -2,20 +2,40 @@
#include <SFML/Graphics.hpp>
#include "world.hpp"
#include <sstream>
class Client{
public :
Client(std::string str);
void connect();
void send(std::string, int length);
private :
std::string ip;
sf::TcpSocket socket;
sf::Thread thread;
bool connected;
};
class Game{
public :
Game();
Game(std::string ip);
void run();
void update(sf::Time deltaTime);
void render();
void processEvents();
void streamEvents();
private :
sf::RenderWindow mWindow;
World *mWorld;
std::array<HUD*,2> mHUD;
sf::Time TimePerFrame;
std::set<sf::Keyboard::Key> pressed, released;
Client client;
};
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <set>
#include <iostream>
#include <sstream>
#define PORT 53000
class Input{
......@@ -8,13 +13,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 +20,22 @@ 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 :
void connect();
std::set<sf::Keyboard::Key> pressed;
sf::TcpListener listener;
sf::TcpSocket client;
sf::Thread thread;
bool listening, clienting;
};
......@@ -34,6 +34,8 @@ public :
int getKeys() const{return key_stack;}
int getDaggers() const{return dagger_stack;}
Input* getInput() const {return input;}
virtual void onCollideWith(SceneNode& collider);
private :
......
#include "../headers/game.hpp"
#include "../headers/HUD.hpp"
Game::Game() : mWindow(sf::VideoMode(1920, 1080), "LSV Fighters"),
mWorld(new World(mWindow)){
Game::Game(std::string str) : mWindow(sf::VideoMode(1920, 1080), "LSV Fighters"),
mWorld(new World(mWindow)), client(str){
mHUD[0] = new HUD(mWindow,mWorld->getHUDLocator(0), HUD::TL);
mHUD[1] = new HUD(mWindow,mWorld->getHUDLocator(1), HUD::TR);
TimePerFrame = sf::seconds(1.f/60.f);
......@@ -15,12 +15,27 @@ void Game::run(){
timeSinceLastUpdate += clock.restart();
for(;timeSinceLastUpdate > TimePerFrame; timeSinceLastUpdate -= TimePerFrame){
processEvents();
streamEvents();
update(TimePerFrame);
}
render();
}
}
void Game::streamEvents(){
std::stringstream isstr;
for(auto it : pressed){
isstr<<"P"<<it<<"\n";
}
for(auto it : released){
isstr<<"R"<<it<<"\n";
}
const char* data = isstr.str().c_str();
client.send(data, 1000);
pressed.clear();
released.clear();
}
void Game::processEvents(){
sf::Event event;
while(mWindow.pollEvent(event)){
......@@ -28,6 +43,14 @@ void Game::processEvents(){
case sf::Event::Closed :
mWindow.close();
break;
case sf::Event::KeyPressed :
pressed.insert(event.key.code);
released.erase(event.key.code);
break;
case sf::Event::KeyReleased :
released.insert(event.key.code);
pressed.erase(event.key.code);
break;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)
......@@ -54,3 +77,26 @@ void Game::render(){
mWindow.display();
}
Client::Client(std::string str) : ip(str), thread(&Client::connect, this){
connected = false;
thread.launch();
}
void Client::connect(){
sf::Socket::Status status = socket.connect(ip.c_str(), PORT, sf::seconds(5.f));
while(status != sf::Socket::Done){
status = socket.connect(ip.c_str(), PORT, sf::seconds(1.f));
sf::sleep(sf::seconds(1.f));
}
connected = true;
}
void Client::send(std::string msg, int length){
if(connected){
socket.send(msg.c_str(),length);
}
}
#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() : listening(false), clienting(false), thread(&TCPInput::update,this){
thread.launch();
}
void TCPInput::connect(){
listening = listening || listener.listen(PORT) == sf::Socket::Done;
clienting = clienting || listener.accept(client) == sf::Socket::Done;
}
void TCPInput::update(){
sf::TcpListener listener;
if (listener.listen(53000) != sf::Socket::Done){
std::cout << "err\n";
}
sf::TcpSocket client;
if (listener.accept(client) != sf::Socket::Done){
std::cout << "err2\n";
}
char data[1000];
std::size_t received;
while(true){
if (client.receive(data, 1000, received) != sf::Socket::Done){}
std::stringstream sstr; sstr << data;
std::string str; char c;
while(std::getline(sstr,str)){
std::stringstream osstr; osstr << str;
osstr >> c >> str;
if (c == 'P') press((sf::Keyboard::Key) std::stoi(str));
if (c == 'R') release((sf::Keyboard::Key) std::stoi(str));
}
sf::sleep(sf::seconds(0.01f));
}
/* connect();
std::cout << "t\n";
while(true){
char data[1000];
std::size_t received;
if(client.receive(data,1000,received) != sf::Socket::Done){
std::cout << "u\n";
std::cout << data;
}
sf::sleep(sf::seconds(1.f));
}*/
}
bool Input::isKeyPressed(sf::Keyboard::Key k){
return pressed.find(k) != pressed.end();
return false;
}
......
......@@ -3,9 +3,9 @@
#include <ctime>
int main(){
int main(int argc, char** argv){
srand(time(NULL));
Game game;
Game game(argv[1]);
game.run();
return 0;
}
......@@ -57,7 +57,8 @@ void Player::e_spell(int dir){
sf::Vector2f base(400.f,0.f);
SpeedPatern *sp = new SpeedPatern(base); sp->rotate(dir*90.f);
Orbital* dagger = new Orbital(Agent::Dagger, getTexHolder(),sp, Entity::Player,unsing_poison, 3);
Orbital* dagger = new Orbital(Agent::Dagger, getTexHolder(),sp,
Entity::Player,unsing_poison, 3);
unsing_poison = false;
dagger->rotate(dir*90.f);
getParent()->attachChild(dagger);
......
......@@ -7,7 +7,7 @@ World::World(sf::RenderWindow& window)
mCarte(mTextures,mWorldBounds,0.9f,0.9f,0.4f,20,this){
mRoom = mCarte.checkIn();
spawnPlayer(0,mRoom, new KeyInput());
spawnPlayer(1,mRoom, new Input());
spawnPlayer(1,mRoom, new TCPInput());
mSceneGraph.attachChild(mRoom);
makeData(0); makeData(1);
}
......