#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <sstream>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <math.h>
#include "objects.cpp"
int main()
{
short scale_res = 3;
// Okno gry
sf::RenderWindow window;
window.create(sf::VideoMode( (304*scale_res), (240*scale_res) ), "game", (sf::Style::Titlebar, sf::Style::Close));
window.setMouseCursorVisible(false);
// Tekstury
sf::Texture interface;
interface.loadFromFile("graphics/interface.png");
interface.setSmooth(false);
// Sprity
sf::Sprite ithing;
ithing.setTexture(interface);
ithing.setScale(sf::Vector2f( (1.0f*scale_res), (1.0f*scale_res)));
sf::RectangleShape rectangle(sf::Vector2f(16*scale_res, 16*scale_res));
// Widok
sf::View view(sf::FloatRect(0, 0, 304*scale_res, 240*scale_res));
window.setView(view);
// Zmienne gry
sf::Clock clock;
short x = 160;
short y = 120;
bool server = false;
bool iclient = false;
bool connect = false;
sf::TcpSocket socket;
socket.setBlocking(false);
sf::TcpListener listener;
listener.setBlocking(false);
sf::TcpSocket client;
client.setBlocking(false);
sf::Socket::Status status;
// Pętla gry
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::P))
server = true;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::L)){
iclient = true;
connect = true;
}
}
// Rysowanie na ekranie
if(clock.getElapsedTime().asSeconds() >= 0.025) { // 40 FPS
// Czyszczenie ekranu
window.clear(sf::Color(48,196,0,255));
// Łączenie
if(iclient && connect){
status = socket.connect("localhost", 1203);
connect = false;
}
// Serwer
if(server){
listener.listen(1203);
listener.accept(client);
}
// Ruch postaci
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))x--;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))x++;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))y--;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))y++;
// Rysowanie postaci
rectangle.setOutlineThickness(scale_res);
rectangle.setSize(sf::Vector2f(16*scale_res, 16*scale_res));
rectangle.setPosition(x*scale_res, y*scale_res);
rectangle.setOutlineColor(sf::Color(0,0,0,255));
rectangle.setFillColor(sf::Color(255,0,0,255));
window.draw(rectangle);
// Rysowanie kursora
ithing.setTextureRect(sf::IntRect(0, 0, 14, 14));
ithing.setPosition(sf::Vector2f(sf::Mouse::getPosition(window).x,sf::Mouse::getPosition(window).y));
window.draw(ithing);
// Resetowanie zegara
clock.restart();
// Wyświetlanie wszystkiego
window.display();
}
}
return 0;
}