chat in multiplayer 1.0

(use ENTER to write message)
master
CREAsTIVE 2022-07-23 02:47:19 +05:00
parent fcee9637aa
commit 7a01776f87
7 changed files with 60 additions and 40 deletions

View File

@ -140,7 +140,14 @@ void Shooter::update() {
if (!screen->hasFocus()) { if (!screen->hasFocus()) {
return; return;
} }
if (keyboard->isKeyTapped(sf::Keyboard::Enter)) {
if (isTypingMessage) {
client->sendMessage(message);
message = "";
}
isTypingMessage = !isTypingMessage;
}
if (!isTypingMessage) {
if (keyboard->isKeyTapped(sf::Keyboard::Escape)) { if (keyboard->isKeyTapped(sf::Keyboard::Escape)) {
inGame = !inGame; inGame = !inGame;
screen->setMouseCursorVisible(!inGame); screen->setMouseCursorVisible(!inGame);
@ -161,27 +168,20 @@ void Shooter::update() {
if (keyboard->isKeyTapped(sf::Keyboard::L)) { if (keyboard->isKeyTapped(sf::Keyboard::L)) {
screen->stopRender(); screen->stopRender();
} }
if (keyboard->isKeyTapped(sf::Keyboard::Enter)) {
if (isTypingMessage) {
client->sendMessage(message);
message = "";
}
isTypingMessage = !isTypingMessage;
} }
if (inGame) { if (inGame) {
screen->setTitle(ShooterConsts::PROJECT_NAME); screen->setTitle(ShooterConsts::PROJECT_NAME);
if (isTypingMessage) { if (isTypingMessage) {
string msg; string symbols = screen->getInputSymbols();
cin >> msg; for (char s : symbols) {
message += msg; if (s == (char)8) {
message = message.substr(0, message.size() - 1);
client->sendMessage(message); }
message = ""; else {
isTypingMessage = false; message += s;
}
Log::log(chat->getChat()); }
} }
else { else {
playerController->update(); playerController->update();
@ -203,7 +203,10 @@ void Shooter::update() {
void Shooter::drawChat() { void Shooter::drawChat() {
sf::Color chatColor = sf::Color(0, 0, 0, chat->update(Time::deltaTime())); sf::Color chatColor = sf::Color(0, 0, 0, chat->update(Time::deltaTime()));
string chatText = chat->getChat(); 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() { void Shooter::gui() {

View File

@ -27,10 +27,16 @@ void Screen::open(int screenWidth, int screenHeight, const std::string &name, bo
void Screen::display() { void Screen::display() {
sf::Event event{}; sf::Event event{};
inputSymbols = "";
while (_window->pollEvent(event)) { while (_window->pollEvent(event)) {
if (event.type == sf::Event::Closed) { if (event.type == sf::Event::Closed) {
_window->close(); _window->close();
} }
else if (event.type == sf::Event::TextEntered) {
if (event.text.unicode < 128) {
inputSymbols += static_cast<char>(event.text.unicode);
}
}
} }
std::string title = _title + " (" + std::to_string(Time::fps()) + " fps)"; 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(): stop recording the screen");
Log::log("Screen::stopRender(): start saving .png sequence"); Log::log("Screen::stopRender(): start saving .png sequence");
std::string c = "rm film/png/*.png"; std::string c = "rm film/png/*.png";
popen(c.c_str(), "w"); _popen(c.c_str(), "w");
int i = 0; int i = 0;
for(; i < _renderSequence.size(); i++) { for(; i < _renderSequence.size(); i++) {
_renderSequence[i].copyToImage().saveToFile("film/png/" + std::to_string(i) + ".png"); _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"); Log::log("Screen::stopRender(): start rendering final video");
// TODO: .png sequence looks better than a final video (poor clarity and desaturated colors) // 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"; 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++; _scene++;
_renderVideo = false; _renderVideo = false;
Log::log("Screen::stopRender(): finish rendering final video"); Log::log("Screen::stopRender(): finish rendering final video");

View File

@ -27,6 +27,8 @@ private:
sf::Color _background; sf::Color _background;
std::string inputSymbols;
const std::shared_ptr<sf::RenderWindow> _window = std::make_shared<sf::RenderWindow>(); const std::shared_ptr<sf::RenderWindow> _window = std::make_shared<sf::RenderWindow>();
public: public:
void open(int screenWidth = Consts::STANDARD_SCREEN_WIDTH, int screenHeight = Consts::STANDARD_SCREEN_HEIGHT, void open(int screenWidth = Consts::STANDARD_SCREEN_WIDTH, int screenHeight = Consts::STANDARD_SCREEN_HEIGHT,
@ -75,6 +77,8 @@ public:
void startRender(); void startRender();
void stopRender(); void stopRender();
std::string getInputSymbols() { return inputSymbols; };
}; };

View File

@ -2,13 +2,20 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
void ChatManager::addNewMessage(std::string author, std::string message) { void ChatManager::addNewMessage(std::string author, std::string message) {
hide = 3.0; hide = 7.0;
messages.push_back(message); messages.push_back(message);
authors.push_back(author); authors.push_back(author);
isChatUpdate = true; isChatUpdate = true;
if (messages.size() > 6) {
messages.erase(messages.begin());
}
} }
int ChatManager::update(double delta) { int ChatManager::update(double delta) {
hide = std::max(hide-delta, 0.0); hide = std::max(hide-delta, 0.0);
if (hide < 0.0000001) {
messages.clear();
authors.clear();
}
return std::min((int)(hide * 255.0), 255); return std::min((int)(hide * 255.0), 255);
} }
@ -17,7 +24,7 @@ std::string ChatManager::getChat() {
isChatUpdate = false; isChatUpdate = false;
int size = messages.size(); int size = messages.size();
chatStr = ""; 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"; chatStr += authors[messageIndex] + ": " + messages[messageIndex] + ";\n";
} }

View File

@ -9,8 +9,8 @@ class ChatManager final {
private: private:
std::vector<std::string> messages; std::vector<std::string> messages;
std::vector<std::string> authors; std::vector<std::string> authors;
bool isChatUpdate = false; bool isChatUpdate = true;
std::string chatStr = "A: b\nC: D"; std::string chatStr = "";
double hide = 0.0; double hide = 0.0;
public: public:
void addNewMessage(std::string author, std::string message); void addNewMessage(std::string author, std::string message);

View File

@ -237,7 +237,7 @@ void ShooterClient::processCustomPacket(sf::Packet &packet) {
case ShooterMsgType::newMessage: case ShooterMsgType::newMessage:
packet >> name >> message; packet >> name >> message;
newMessage(name, message); newMessage(message, name);
break; break;
default: default:
Log::log("ShooterClient::processCustomPacket: unknown message type " + Log::log("ShooterClient::processCustomPacket: unknown message type " +

View File

@ -145,7 +145,7 @@ void ShooterServer::processCustomPacket(sf::Packet &packet, sf::Uint16 senderId)
case ShooterMsgType::newMessage: case ShooterMsgType::newMessage:
packet >> message; packet >> message;
sendPacket << MsgType::Custom << ShooterMsgType::ChangeWeapon << _players[senderId]->playerNickName() << message; sendPacket << MsgType::Custom << ShooterMsgType::newMessage << _players[senderId]->playerNickName() << message;
for (auto& player : _players) { for (auto& player : _players) {
if (player.first != senderId) { if (player.first != senderId) {
_socket.send(sendPacket, player.first); _socket.send(sendPacket, player.first);