void *readdisp(void *s) { RenderWindow *window = RenderWindow::getInstance(); ProtocoleClient x; void *data; while (true) { if (!(data = Client::getNetwork()->read(sizeof(ANetwork::t_frame)))) { //Client Disconnected { static_cast<GamePanel*>(window->getPanels().top())->getLabels().at(2).getText().setColor(sf::Color::Red); static_cast<GamePanel*>(window->getPanels().top())->getLabels().at(2).getText().setString("Connection lost !"); static_cast<GamePanel*>(window->getPanels().top())->getLabels().at(2).getText().setOrigin(static_cast<GamePanel*>(window->getPanels().top())->getLabels().at(2).getText().getGlobalBounds().width / 2, static_cast<GamePanel*>(window->getPanels().top())->getLabels().at(2).getText().getGlobalBounds().height / 2); try { Client::getNetwork()->connect(RenderWindow::getInstance()->getSettings()->getIP()); } catch (const std::exception & e) { } } } else x.methodChecker(*reinterpret_cast<ANetwork::t_frame*>(data)); } return s; }
void RoomPanel::newPlayer(std::string &newUsername) { RenderWindow *window = RenderWindow::getInstance(); unsigned int i = static_cast<RoomPanel*>(window->getPanels().top())->getNbPlayers(); static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(i + 2).setString(newUsername); static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(i + 2).setOrigin(static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(i + 2).getText().getGlobalBounds().width / 2, static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(i + 2).getText().getGlobalBounds().height / 2); static_cast<RoomPanel*>(window->getPanels().top())->getBackgrounds().at(i + 1).setTexture(*(static_cast<RoomPanel*>(window->getPanels().top())->getTextures()).at(i + 1)); static_cast<RoomPanel*>(window->getPanels().top())->addNbPlayers(); }
void JoinPanel::setError(const std::string &error) { Sound *s = Client::getSound(); RenderWindow *window = RenderWindow::getInstance(); static_cast<JoinPanel*>(window->getPanels().top())->setAlpha(255); static_cast<JoinPanel*>(window->getPanels().top())->getLabels().at(4).setString(error); static_cast<JoinPanel*>(window->getPanels().top())->getLabels().at(4).setOrigin(static_cast<JoinPanel*>(window->getPanels().top())->getLabels().at(4).getText().getGlobalBounds().width / 2, static_cast<JoinPanel*>(window->getPanels().top())->getLabels().at(4).getText().getGlobalBounds().height / 2); static_cast<JoinPanel*>(window->getPanels().top())->getLabels().at(4).getText().setColor(sf::Color(255, 0, 0, static_cast<JoinPanel*>(window->getPanels().top())->getAlpha())); s->playSound("denied"); }
void RoomPanel::setSlider(int diff) { RenderWindow *window = RenderWindow::getInstance(); float x = 0; float xbase = (window->getSize()._x / 2) - (window->_ressources->_slide->getSize()._x / 2); switch (diff) { case 1: { x = window->_ressources->_sliderNormal->getSize()._x / 3; break; } case 2: { x = window->_ressources->_slide->getSize()._x / 2; break; } case 3: { x = window->_ressources->_slide->getSize()._x - window->_ressources->_sliderNormal->getSize()._x / 3; break; } default: { x = window->_ressources->_slide->getSize()._x / 2; break; } } static_cast<RoomPanel*>(window->getPanels().top())->getDifficulty()->setPosX(xbase + x); }
void RoomPanel::downloadComplete(std::string &usernameComplete) { RenderWindow *window = RenderWindow::getInstance(); std::size_t pos = usernameComplete.find("player"); unsigned int i = std::stoi(usernameComplete.substr(pos + 6)); static_cast<RoomPanel*>(window->getPanels().top())->getBackgrounds().at(i + 4).getSprite().setColor(sf::Color(255, 255, 255, 255)); i++; }
void RoomPanel::receiveFiles(int port, int nbrFiles) { std::this_thread::sleep_for(std::chrono::seconds(1)); for (int a = 0; a < nbrFiles; a++) { File file; file.receiveMe(RenderWindow::getInstance()->getSettings()->getIP(), port++, "./recv/", setFileProgression, NULL); } FileManager Toto("./recv/"); RenderWindow *window = RenderWindow::getInstance(); std::list<File *> list = Toto.getFileListByExtension("png"); for (std::list<File*>::iterator it = list.begin(); it != list.end(); ++it) { Texture *text = new Texture(); text->loadFromFile((*it)->getFullPath()); static_cast<RoomPanel*>(window->getPanels().top())->getReceived()->insert(std::make_pair((*it)->getBasename(),text)); } }
void RoomPanel::createPlayers() { RenderWindow *window = RenderWindow::getInstance(); unsigned int i = 0; std::string empty = ""; while (i < 4) { Text *username = new Text(); Sprite *blackShip = new Sprite; Player *player = new Player; player->setUsername(empty); player->setCurrentClient(false); blackShip->setTexture(*_spaceShipsTextures.at(0)); blackShip->setPosition(0.2 * (i + 1) * window->getSize()._x, 0.6 * window->getSize()._y); blackShip->getSprite().setOrigin(_spaceShipsTextures.at(0)->getSize()._x / 2, _spaceShipsTextures.at(0)->getSize()._y / 2); blackShip->scale(0.4); username->setString(player->getUsername()); username->setSize(40); username->setStyle(1); username->setOrigin(username->getText().getGlobalBounds().width / 2, username->getText().getGlobalBounds().height / 2); username->setPosition(Vector2(0.2 * (i + 1) * window->getSize()._x, 0.8 * window->getSize()._y)); username->setColor(Color::WHITE); window->getPanels().top()->getBackgrounds().push_back(*blackShip); window->getPanels().top()->getLabels().push_back(*username); _players.push_back(player); i++; } Text *roomID = new Text(); roomID->setString(_idRoom); roomID->setSize(60); roomID->setStyle(1); roomID->setOrigin(roomID->getText().getGlobalBounds().width / 2, roomID->getText().getGlobalBounds().height / 2); roomID->setPosition(Vector2(0.5 * window->getSize()._x, 0.03 * window->getSize()._y)); roomID->setFont(*((RenderWindow::getInstance())->_ressources->_fontSecond)); roomID->setColor(Color::WHITE); _labels.push_back(*roomID); _nbPlayers = 0; i = 0; while (i < 4) { Sprite *fireShip = new Sprite; fireShip->setTexture(*((RenderWindow::getInstance())->_ressources->_reactor)); fireShip->setPosition(220 + 0.2 * (i + 1) * window->getSize()._x, 0.765 * window->getSize()._y); fireShip->getSprite().setOrigin(_spaceShipsTextures.at(0)->getSize()._x / 2, _spaceShipsTextures.at(0)->getSize()._y / 2); fireShip->getSprite().setColor(sf::Color(255, 255, 255, 0)); window->getPanels().top()->getBackgrounds().push_back(*fireShip); i++; } }
void RoomPanel::playerLeft(std::vector<std::string> &vector) { RenderWindow *window = RenderWindow::getInstance(); unsigned int i = 0; while (i < (static_cast<RoomPanel*>(window->getPanels().top())->getPlayers().size())) { static_cast<RoomPanel*>(window->getPanels().top())->getPlayers().at(i)->setCurrentClient(false); static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(i + 2).setColor(Color::WHITE); i++; } std::size_t pos = vector.at(0).find("player"); unsigned int idToRemove = std::stoi(vector.at(0).substr(pos + 6)) - 1; if (idToRemove + 1 >= static_cast<RoomPanel*>(window->getPanels().top())->getNbPlayers()) { static_cast<RoomPanel*>(window->getPanels().top())->getBackgrounds().at(idToRemove + 1).setTexture(*(static_cast<RoomPanel*>(window->getPanels().top())->getTextures()).at(0)); static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(idToRemove + 2).setString(""); } pos = vector.at(1).find("player"); unsigned int idToChange = std::stoi(vector.at(1).substr(pos + 6)) - 1; static_cast<RoomPanel*>(window->getPanels().top())->getPlayers().at(idToChange)->setCurrentClient(true); static_cast<RoomPanel*>(window->getPanels().top())->getBackgrounds().at(idToChange + 1).setTexture(*(static_cast<RoomPanel*>(window->getPanels().top())->getTextures()).at(idToChange + 1)); static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(idToChange + 2).setString(vector.at(1)); i = static_cast<RoomPanel*>(window->getPanels().top())->getNbPlayers() - 1; while (i < 4) { static_cast<RoomPanel*>(window->getPanels().top())->getBackgrounds().at(i + 1).setTexture(*(static_cast<RoomPanel*>(window->getPanels().top())->getTextures()).at(0)); static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(i + 2).setString(""); i++; } switch (idToChange) { case 0: static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(idToChange + 2).setColor(Color::BLUE); break; case 1: static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(idToChange + 2).setColor(Color::RED); break; case 2: static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(idToChange + 2).setColor(Color::GREEN); break; case 3: static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(idToChange + 2).setColor(Color::YELLOW); break; default: static_cast<RoomPanel*>(window->getPanels().top())->getLabels().at(idToChange + 2).setColor(Color::WHITE); break; } static_cast<RoomPanel*>(window->getPanels().top())->minusNbPlayers(); }
void Client::Start() { //Creating Everything RenderWindow *window = RenderWindow::getInstance(); _network = new Network(); _UDPnetwork = new Network(); _sound = new Sound(); //Connecting to server _network->init(window->getSettings()->getPort(), ANetwork::TCP_MODE); std::cout << "Connecting to : " << window->getSettings()->getIP() << ":" << window->getSettings()->getPort() << std::endl; try { _network->connect(window->getSettings()->getIP()); } catch (const std::exception &e) { std::cerr << e.what() << std::endl; } //Sending Handshake ANetwork::t_frame sender = CreateRequest::create((unsigned char)C_HANDSHAKE, CRC::calcCRC("Bonjour 1.0"), 0, "Bonjour 1.0"); _network->write(sender); //Creating SF::window window->setWindow(sf::VideoMode(1920, 1080, 32), "R-Pint"); window->setFramerateLimit(60); window->clear(); //Creating Textures for splash screen Texture *splashScreenTexture = new Texture(); Sprite *splashScreen = new Sprite(); splashScreenTexture->loadFromFile("../common/misc/splash_screen.png"); splashScreen->setTexture(*splashScreenTexture); splashScreen->setPosition(0, 0); //Display Splash screen and loading Ressources window->draw(splashScreen->getSprite()); window->display(); window->_ressources = new Ressources(); window->getPanels().push(static_cast<StartPanel*>(PanelFactory::createPanel(PanelFactory::PanelType::START_PANEL))); window->getPanels().top()->setUserInterface(); //Adding & playing music for Menu _sound->registerSound("../common/misc/mouseHover.ogg", "hover"); _sound->registerSound("../common/misc/accessDenied.ogg", "denied"); _sound->registerSound("../common/misc/metalDoorOpen.ogg", "door"); _sound->registerMusic("../common/misc/laserSound.ogg", "riffle"); _sound->registerSound("../common/misc/rocketSound.ogg", "missile"); _sound->registerSound("../common/misc/megaLaser1.ogg", "laser"); _sound->registerMusic("../common/misc/menuMusic1.ogg", "mainMenu"); _sound->registerSound("../common/misc/explosion1.ogg", "explosion1"); _sound->registerSound("../common/misc/gameOver.ogg", "endGame"); _sound->registerMusic("../common/misc/GameMusicIntro.ogg", "gameIntro"); _sound->registerMusic("../common/misc/GameMusicLoop.ogg", "gameLoop"); _sound->registerSound("../common/misc/BimBamBoum.ogg", "logoSound"); _sound->playMusic("mainMenu"); //Threading the Read std::unique_ptr<AThread> t(new Thread(1)); char str1[] = ""; (void) str1; t->attach(&readdisp, (void *)str1); t->run(); //Main Loop while(window->isOpen()) { window->getPanels().top()->update(); window->getPanels().top()->render(); window->display(); sf::Event event; while (window->pollEvent(event)) { window->getPanels().top()->getInputManager().methodChecker(event); } } //Closing Thread and Window t->cancel(); _network->close(); }