#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include <string>
#include <iostream>
#define PORT 2903
#define MAXPLAYER 10
#define SCALE 1
#define SCREENX 320*SCALE
#define SCREENY 180*SCALE
#define PLAYERX 16
#define PLAYERY 24
void init();
struct Entity{
std::string name;
int lvl;
int x;
int y;
int direction;
};
struct Login{
int id;
int mdp;
int out;
};
sf::RenderWindow window;
sf::Texture spr_player;
sf::Sprite obj_player;
sf::Packet& operator <<(sf::Packet& packet, const Entity& entity){return packet << entity.name << entity.lvl << entity.x << entity.y << entity.direction;}
sf::Packet& operator >>(sf::Packet& packet, Entity& entity){ return packet >> entity.name >> entity.lvl >> entity.x >> entity.y >> entity. direction;}
sf::Packet& operator <<(sf::Packet& packet, const Login& login){return packet << login.id << login.mdp << login.out;}
sf::Packet& operator >>(sf::Packet& packet, Login& login){ return packet >> login.id >> login.mdp >> login.out;}
Entity tmpPlayer[MAXPLAYER];
int main(int argc, char* argv[]){
init();
std::cout<<"Started..."<<std::endl;
sf::IpAddress ip=sf::IpAddress::getLocalAddress();
sf::TcpSocket socket;
Entity player;
//std::cin>>player.name;
player.name="Player";
player.lvl = 1;
player.x=0;
player.y=0;
player.direction=0;
socket.connect(ip, PORT);
while(window.isOpen()){
sf::Event event;
while(window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q)){player.x--;}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){player.x++;}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z)){player.y--;}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){player.y++;}
sf::Packet packet;
packet<<player;
socket.send(packet);
window.clear();
obj_player.setPosition(player.x*SCALE ,player.y*SCALE);
obj_player.setTextureRect(sf::IntRect(0*PLAYERX, player.direction*PLAYERY, PLAYERX, PLAYERY));
window.draw(obj_player);
window.display();
}
return 0;
}
void init(){
window.create(sf::VideoMode(SCREENX, SCREENY), "Client");
window.setFramerateLimit(60);
spr_player.loadFromFile("assets/spr_player.png");
obj_player.setTexture(spr_player);
obj_player.setScale(SCALE,SCALE);
}