From 7a01776f878f460b1f3fb0308a61bf9dc531c35b Mon Sep 17 00:00:00 2001 From: CREAsTIVE Date: Sat, 23 Jul 2022 02:47:19 +0500 Subject: [PATCH] chat in multiplayer 1.0 (use ENTER to write message) --- Shooter.cpp | 67 ++++++++++++++++++++------------------- engine/io/Screen.cpp | 10 ++++-- engine/io/Screen.h | 4 +++ network/Chat.cpp | 11 +++++-- network/Chat.h | 4 +-- network/ShooterClient.cpp | 2 +- network/ShooterServer.cpp | 2 +- 7 files changed, 60 insertions(+), 40 deletions(-) diff --git a/Shooter.cpp b/Shooter.cpp index a168489..b81e634 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -140,28 +140,6 @@ void Shooter::update() { if (!screen->hasFocus()) { return; } - - if (keyboard->isKeyTapped(sf::Keyboard::Escape)) { - inGame = !inGame; - screen->setMouseCursorVisible(!inGame); - } - - if (keyboard->isKeyTapped(sf::Keyboard::O)) { - setGlEnable(!glEnable()); - } - - if (keyboard->isKeyTapped(sf::Keyboard::Tab)) { - setDebugInfo(!showDebugInfo()); - } - - if (keyboard->isKeyTapped(sf::Keyboard::P)) { - screen->startRender(); - } - - if (keyboard->isKeyTapped(sf::Keyboard::L)) { - screen->stopRender(); - } - if (keyboard->isKeyTapped(sf::Keyboard::Enter)) { if (isTypingMessage) { client->sendMessage(message); @@ -169,19 +147,41 @@ void Shooter::update() { } isTypingMessage = !isTypingMessage; } + if (!isTypingMessage) { + if (keyboard->isKeyTapped(sf::Keyboard::Escape)) { + inGame = !inGame; + screen->setMouseCursorVisible(!inGame); + } + + if (keyboard->isKeyTapped(sf::Keyboard::O)) { + setGlEnable(!glEnable()); + } + + if (keyboard->isKeyTapped(sf::Keyboard::Tab)) { + setDebugInfo(!showDebugInfo()); + } + + if (keyboard->isKeyTapped(sf::Keyboard::P)) { + screen->startRender(); + } + + if (keyboard->isKeyTapped(sf::Keyboard::L)) { + screen->stopRender(); + } + } if (inGame) { screen->setTitle(ShooterConsts::PROJECT_NAME); if (isTypingMessage) { - string msg; - cin >> msg; - message += msg; - - client->sendMessage(message); - message = ""; - isTypingMessage = false; - - Log::log(chat->getChat()); + string symbols = screen->getInputSymbols(); + for (char s : symbols) { + if (s == (char)8) { + message = message.substr(0, message.size() - 1); + } + else { + message += s; + } + } } else { playerController->update(); @@ -203,7 +203,10 @@ void Shooter::update() { void Shooter::drawChat() { sf::Color chatColor = sf::Color(0, 0, 0, chat->update(Time::deltaTime())); string chatText = chat->getChat(); - screen->drawText(chatText, Vec2D{ 0, 0 }, 10, chatColor); + screen->drawText(chatText, Vec2D{ 0, (double)screen->height()/2 }, 20, chatColor); + if (isTypingMessage) { + screen->drawText(message, Vec2D{ (double)screen->width()/4, (double)screen->height() / 1.5 }, 40, sf::Color(0, 0, 0, 255)); + } } void Shooter::gui() { diff --git a/engine/io/Screen.cpp b/engine/io/Screen.cpp index 551065b..b8d2817 100644 --- a/engine/io/Screen.cpp +++ b/engine/io/Screen.cpp @@ -27,10 +27,16 @@ void Screen::open(int screenWidth, int screenHeight, const std::string &name, bo void Screen::display() { sf::Event event{}; + inputSymbols = ""; while (_window->pollEvent(event)) { if (event.type == sf::Event::Closed) { _window->close(); } + else if (event.type == sf::Event::TextEntered) { + if (event.text.unicode < 128) { + inputSymbols += static_cast(event.text.unicode); + } + } } std::string title = _title + " (" + std::to_string(Time::fps()) + " fps)"; @@ -61,7 +67,7 @@ void Screen::stopRender() { Log::log("Screen::stopRender(): stop recording the screen"); Log::log("Screen::stopRender(): start saving .png sequence"); std::string c = "rm film/png/*.png"; - popen(c.c_str(), "w"); + _popen(c.c_str(), "w"); int i = 0; for(; i < _renderSequence.size(); i++) { _renderSequence[i].copyToImage().saveToFile("film/png/" + std::to_string(i) + ".png"); @@ -72,7 +78,7 @@ void Screen::stopRender() { Log::log("Screen::stopRender(): start rendering final video"); // TODO: .png sequence looks better than a final video (poor clarity and desaturated colors) c = "ffmpeg -stats -r 60 -i film/png/%d.png -vcodec libx264 -crf 1 -pix_fmt yuv420p -frames " + std::to_string(i) + " film/mp4/" + std::to_string(_scene) + "_" + _title + "_" + std::to_string(rand()) + ".mp4"; - popen(c.c_str(), "w"); + _popen(c.c_str(), "w"); _scene++; _renderVideo = false; Log::log("Screen::stopRender(): finish rendering final video"); diff --git a/engine/io/Screen.h b/engine/io/Screen.h index 2faea27..fd309a9 100644 --- a/engine/io/Screen.h +++ b/engine/io/Screen.h @@ -27,6 +27,8 @@ private: sf::Color _background; + std::string inputSymbols; + const std::shared_ptr _window = std::make_shared(); public: void open(int screenWidth = Consts::STANDARD_SCREEN_WIDTH, int screenHeight = Consts::STANDARD_SCREEN_HEIGHT, @@ -75,6 +77,8 @@ public: void startRender(); void stopRender(); + + std::string getInputSymbols() { return inputSymbols; }; }; diff --git a/network/Chat.cpp b/network/Chat.cpp index 89f406c..06b59d8 100644 --- a/network/Chat.cpp +++ b/network/Chat.cpp @@ -2,13 +2,20 @@ #include #include void ChatManager::addNewMessage(std::string author, std::string message) { - hide = 3.0; + hide = 7.0; messages.push_back(message); authors.push_back(author); isChatUpdate = true; + if (messages.size() > 6) { + messages.erase(messages.begin()); + } } int ChatManager::update(double delta) { hide = std::max(hide-delta, 0.0); + if (hide < 0.0000001) { + messages.clear(); + authors.clear(); + } return std::min((int)(hide * 255.0), 255); } @@ -17,7 +24,7 @@ std::string ChatManager::getChat() { isChatUpdate = false; int size = messages.size(); chatStr = ""; - for (int messageIndex = messages.size()-1; messageIndex > 0 && chatStr.size() < 100; messageIndex--) + for (int messageIndex = messages.size()-1; messageIndex >= 0; messageIndex--) { chatStr += authors[messageIndex] + ": " + messages[messageIndex] + ";\n"; } diff --git a/network/Chat.h b/network/Chat.h index f48854f..f8ddf5b 100644 --- a/network/Chat.h +++ b/network/Chat.h @@ -9,8 +9,8 @@ class ChatManager final { private: std::vector messages; std::vector authors; - bool isChatUpdate = false; - std::string chatStr = "A: b\nC: D"; + bool isChatUpdate = true; + std::string chatStr = ""; double hide = 0.0; public: void addNewMessage(std::string author, std::string message); diff --git a/network/ShooterClient.cpp b/network/ShooterClient.cpp index be6833d..a902cb2 100644 --- a/network/ShooterClient.cpp +++ b/network/ShooterClient.cpp @@ -237,7 +237,7 @@ void ShooterClient::processCustomPacket(sf::Packet &packet) { case ShooterMsgType::newMessage: packet >> name >> message; - newMessage(name, message); + newMessage(message, name); break; default: Log::log("ShooterClient::processCustomPacket: unknown message type " + diff --git a/network/ShooterServer.cpp b/network/ShooterServer.cpp index 243f46c..2f593e0 100644 --- a/network/ShooterServer.cpp +++ b/network/ShooterServer.cpp @@ -145,7 +145,7 @@ void ShooterServer::processCustomPacket(sf::Packet &packet, sf::Uint16 senderId) case ShooterMsgType::newMessage: packet >> message; - sendPacket << MsgType::Custom << ShooterMsgType::ChangeWeapon << _players[senderId]->playerNickName() << message; + sendPacket << MsgType::Custom << ShooterMsgType::newMessage << _players[senderId]->playerNickName() << message; for (auto& player : _players) { if (player.first != senderId) { _socket.send(sendPacket, player.first);