Chat program with C++ and SFML
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
This is my client code:
(My server is very similar at the moment but I will upload it below this one just in case somebody wants to run it)
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <thread>
int main()
sf::RenderWindow main_win(sf::VideoMode(800, 800), "Chatcy!");
main_win.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width/2 -400, sf::VideoMode::getDesktopMode().height/2 -400));
main_win.setVerticalSyncEnabled(true);
//chatcy logo
sf::Texture chatcy;
chatcy.loadFromFile("image_chatcy.png");
sf::Sprite logo;
logo.setTexture(chatcy); //200x800
//font
sf::Font font;
font.loadFromFile("FreeSerif.ttf");
//name
sf::String name("Client");
name += ": ";
//text input area and background
sf::RectangleShape in_background(sf::Vector2f(800, 100));
in_background.setFillColor(sf::Color::Black);
in_background.setPosition(0, 700);
sf::Text inText;
inText.setFont(font);
inText.setPosition(0, 700);
inText.setCharacterSize(24);
sf::String input(name);
inText.setString(input);
//text output area
sf::Text outText;
outText.setFont(font);
outText.setCharacterSize(24);
outText.setPosition(0, 200);
sf::String output;
//networking
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("31.216.109.207", 55552);
if(status != sf::Socket::Done)
outText.setString("nCannot connect to the server!n");
return -1;
else
outText.setString("nYou have been connected to the server!n");
socket.setBlocking(false);
//Packet for incoming msg
sf::Packet in_packet;
while(main_win.isOpen())
sf::Event event;
while(main_win.pollEvent(event))
switch (event.type)
case sf::Event::Closed: main_win.close(); break;
case sf::Event::TextEntered:
if(event.text.unicode != 'b' && event.text.unicode != 'n' && event.text.unicode != 'r')
input += event.text.unicode;
inText.setString(input);
if(inText.findCharacterPos(1000).x >= 780) //1000<msg length so I get the pos of last ch.
input += 'n';
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) && input.getSize()>name.getSize()) //2nd par to protect name
input.erase(input.getSize()-1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
output += 'n' + input;
outText.setString(output);
//send msg
sf::Packet out_packet;
out_packet << input;
while(socket.send(out_packet) == sf::Socket::Partial)
socket.send(out_packet);
input.clear();
input += name;
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
break;
case sf::Event::MouseWheelScrolled:
//-7 for speed and to go the same way with scroll (-)
outText.move(0, event.mouseWheelScroll.delta*-7);
break;
//read msg
if(socket.receive(in_packet) == sf::Socket::Done)
sf::String msg;
in_packet >> msg;
output += 'n' + msg;
outText.setString(output);
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
//check if connected
if(socket.getRemoteAddress() == sf::IpAddress::None)
output += "nConnection has been terminated. Goodbye!n";
outText.setString(output);
main_win.clear(sf::Color::Black);
main_win.draw(outText);
main_win.draw(in_background);
main_win.draw(logo);
main_win.draw(inText);
main_win.display();
I need a way to put this mess in an order. I would like to find an object oriented way to make the client more efficient and then I will use the classes to make an actually server! (I will post another question for the server I guess :P )
Server:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
int main()
sf::RenderWindow main_win(sf::VideoMode(800, 800), "Chatcy server");
main_win.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width/2 -400, sf::VideoMode::getDesktopMode().height/2 -400));
main_win.setVerticalSyncEnabled(true);
//chatcy logo
sf::Texture chatcy;
chatcy.loadFromFile("image_chatcy.png");
sf::Sprite logo;
logo.setTexture(chatcy); //200x800
//font
sf::Font font;
font.loadFromFile("FreeSerif.ttf");
//name
sf::String name("George");
name += ": ";
//text input area and background
sf::RectangleShape in_background(sf::Vector2f(800, 100));
in_background.setFillColor(sf::Color::Black);
in_background.setPosition(0, 700);
sf::Text inText;
inText.setFont(font);
inText.setPosition(0, 700);
inText.setCharacterSize(24);
sf::String input(name);
inText.setString(input);
//text output area
sf::Text outText;
outText.setFont(font);
outText.setCharacterSize(24);
outText.setPosition(0, 200);
sf::String output;
//networking
sf::TcpListener listener;
if(listener.listen(55552) == sf::Socket::Done)
std::cout << "Server is up and running!n";
sf::TcpSocket client;
listener.accept(client);
client.setBlocking(false); //after we connect
//packet to put messages in
sf::Packet in_packet;
//sound notification
sf::SoundBuffer buffer;
buffer.loadFromFile("bell_magic.wav");
sf::Sound sound;
sound.setBuffer(buffer);
sound.play(); //plays after a client is connected
outText.setString("nClient has been connected!n");
while(main_win.isOpen())
sf::Event event;
while(main_win.pollEvent(event))
switch (event.type)
case sf::Event::Closed: main_win.close(); break;
case sf::Event::TextEntered:
if(event.text.unicode != 'b' && event.text.unicode != 'n' && event.text.unicode != 'r')
input += event.text.unicode;
inText.setString(input);
if(inText.findCharacterPos(1000).x >= 780) //1000<msg length so I get the pos of last ch.
input += 'n';
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) && input.getSize()>name.getSize()) //2nd par to protect name
input.erase(input.getSize()-1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
output += 'n' + input;
outText.setString(output);
//send msg
sf::Packet out_packet;
out_packet << input;
while(client.send(out_packet) == sf::Socket::Partial)
client.send(out_packet);
input.clear();
input += name;
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
break;
case sf::Event::MouseWheelScrolled:
//-7 for speed and to go the same way with scroll (-)
outText.move(0, event.mouseWheelScroll.delta*-7);
break;
//read msg
if(client.receive(in_packet) == sf::Socket::Done)
sf::String msg;
in_packet >> msg;
output += 'n' + msg;
outText.setString(output);
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
//check if connected
if(client.getRemoteAddress() == sf::IpAddress::None)
output += "nConnection has been terminated. Goodbye!n";
outText.setString(output);
main_win.clear(sf::Color::Black);
main_win.draw(outText);
main_win.draw(in_background);
main_win.draw(logo);
main_win.draw(inText);
main_win.display();
You may ignore the sound or put your own if you are going to run it. The image is a cosmetic logo. Also note that background
is used so that text behind it is not visible.
c++ object-oriented chat client sfml
add a comment |Â
up vote
2
down vote
favorite
This is my client code:
(My server is very similar at the moment but I will upload it below this one just in case somebody wants to run it)
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <thread>
int main()
sf::RenderWindow main_win(sf::VideoMode(800, 800), "Chatcy!");
main_win.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width/2 -400, sf::VideoMode::getDesktopMode().height/2 -400));
main_win.setVerticalSyncEnabled(true);
//chatcy logo
sf::Texture chatcy;
chatcy.loadFromFile("image_chatcy.png");
sf::Sprite logo;
logo.setTexture(chatcy); //200x800
//font
sf::Font font;
font.loadFromFile("FreeSerif.ttf");
//name
sf::String name("Client");
name += ": ";
//text input area and background
sf::RectangleShape in_background(sf::Vector2f(800, 100));
in_background.setFillColor(sf::Color::Black);
in_background.setPosition(0, 700);
sf::Text inText;
inText.setFont(font);
inText.setPosition(0, 700);
inText.setCharacterSize(24);
sf::String input(name);
inText.setString(input);
//text output area
sf::Text outText;
outText.setFont(font);
outText.setCharacterSize(24);
outText.setPosition(0, 200);
sf::String output;
//networking
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("31.216.109.207", 55552);
if(status != sf::Socket::Done)
outText.setString("nCannot connect to the server!n");
return -1;
else
outText.setString("nYou have been connected to the server!n");
socket.setBlocking(false);
//Packet for incoming msg
sf::Packet in_packet;
while(main_win.isOpen())
sf::Event event;
while(main_win.pollEvent(event))
switch (event.type)
case sf::Event::Closed: main_win.close(); break;
case sf::Event::TextEntered:
if(event.text.unicode != 'b' && event.text.unicode != 'n' && event.text.unicode != 'r')
input += event.text.unicode;
inText.setString(input);
if(inText.findCharacterPos(1000).x >= 780) //1000<msg length so I get the pos of last ch.
input += 'n';
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) && input.getSize()>name.getSize()) //2nd par to protect name
input.erase(input.getSize()-1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
output += 'n' + input;
outText.setString(output);
//send msg
sf::Packet out_packet;
out_packet << input;
while(socket.send(out_packet) == sf::Socket::Partial)
socket.send(out_packet);
input.clear();
input += name;
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
break;
case sf::Event::MouseWheelScrolled:
//-7 for speed and to go the same way with scroll (-)
outText.move(0, event.mouseWheelScroll.delta*-7);
break;
//read msg
if(socket.receive(in_packet) == sf::Socket::Done)
sf::String msg;
in_packet >> msg;
output += 'n' + msg;
outText.setString(output);
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
//check if connected
if(socket.getRemoteAddress() == sf::IpAddress::None)
output += "nConnection has been terminated. Goodbye!n";
outText.setString(output);
main_win.clear(sf::Color::Black);
main_win.draw(outText);
main_win.draw(in_background);
main_win.draw(logo);
main_win.draw(inText);
main_win.display();
I need a way to put this mess in an order. I would like to find an object oriented way to make the client more efficient and then I will use the classes to make an actually server! (I will post another question for the server I guess :P )
Server:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
int main()
sf::RenderWindow main_win(sf::VideoMode(800, 800), "Chatcy server");
main_win.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width/2 -400, sf::VideoMode::getDesktopMode().height/2 -400));
main_win.setVerticalSyncEnabled(true);
//chatcy logo
sf::Texture chatcy;
chatcy.loadFromFile("image_chatcy.png");
sf::Sprite logo;
logo.setTexture(chatcy); //200x800
//font
sf::Font font;
font.loadFromFile("FreeSerif.ttf");
//name
sf::String name("George");
name += ": ";
//text input area and background
sf::RectangleShape in_background(sf::Vector2f(800, 100));
in_background.setFillColor(sf::Color::Black);
in_background.setPosition(0, 700);
sf::Text inText;
inText.setFont(font);
inText.setPosition(0, 700);
inText.setCharacterSize(24);
sf::String input(name);
inText.setString(input);
//text output area
sf::Text outText;
outText.setFont(font);
outText.setCharacterSize(24);
outText.setPosition(0, 200);
sf::String output;
//networking
sf::TcpListener listener;
if(listener.listen(55552) == sf::Socket::Done)
std::cout << "Server is up and running!n";
sf::TcpSocket client;
listener.accept(client);
client.setBlocking(false); //after we connect
//packet to put messages in
sf::Packet in_packet;
//sound notification
sf::SoundBuffer buffer;
buffer.loadFromFile("bell_magic.wav");
sf::Sound sound;
sound.setBuffer(buffer);
sound.play(); //plays after a client is connected
outText.setString("nClient has been connected!n");
while(main_win.isOpen())
sf::Event event;
while(main_win.pollEvent(event))
switch (event.type)
case sf::Event::Closed: main_win.close(); break;
case sf::Event::TextEntered:
if(event.text.unicode != 'b' && event.text.unicode != 'n' && event.text.unicode != 'r')
input += event.text.unicode;
inText.setString(input);
if(inText.findCharacterPos(1000).x >= 780) //1000<msg length so I get the pos of last ch.
input += 'n';
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) && input.getSize()>name.getSize()) //2nd par to protect name
input.erase(input.getSize()-1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
output += 'n' + input;
outText.setString(output);
//send msg
sf::Packet out_packet;
out_packet << input;
while(client.send(out_packet) == sf::Socket::Partial)
client.send(out_packet);
input.clear();
input += name;
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
break;
case sf::Event::MouseWheelScrolled:
//-7 for speed and to go the same way with scroll (-)
outText.move(0, event.mouseWheelScroll.delta*-7);
break;
//read msg
if(client.receive(in_packet) == sf::Socket::Done)
sf::String msg;
in_packet >> msg;
output += 'n' + msg;
outText.setString(output);
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
//check if connected
if(client.getRemoteAddress() == sf::IpAddress::None)
output += "nConnection has been terminated. Goodbye!n";
outText.setString(output);
main_win.clear(sf::Color::Black);
main_win.draw(outText);
main_win.draw(in_background);
main_win.draw(logo);
main_win.draw(inText);
main_win.display();
You may ignore the sound or put your own if you are going to run it. The image is a cosmetic logo. Also note that background
is used so that text behind it is not visible.
c++ object-oriented chat client sfml
1
Have you researched any chat protocols? rfc2812
â Martin York
Jan 17 at 8:15
I wasn't even aware of their existence! :P Time to do some research :D
â alienCY
Jan 17 at 15:13
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This is my client code:
(My server is very similar at the moment but I will upload it below this one just in case somebody wants to run it)
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <thread>
int main()
sf::RenderWindow main_win(sf::VideoMode(800, 800), "Chatcy!");
main_win.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width/2 -400, sf::VideoMode::getDesktopMode().height/2 -400));
main_win.setVerticalSyncEnabled(true);
//chatcy logo
sf::Texture chatcy;
chatcy.loadFromFile("image_chatcy.png");
sf::Sprite logo;
logo.setTexture(chatcy); //200x800
//font
sf::Font font;
font.loadFromFile("FreeSerif.ttf");
//name
sf::String name("Client");
name += ": ";
//text input area and background
sf::RectangleShape in_background(sf::Vector2f(800, 100));
in_background.setFillColor(sf::Color::Black);
in_background.setPosition(0, 700);
sf::Text inText;
inText.setFont(font);
inText.setPosition(0, 700);
inText.setCharacterSize(24);
sf::String input(name);
inText.setString(input);
//text output area
sf::Text outText;
outText.setFont(font);
outText.setCharacterSize(24);
outText.setPosition(0, 200);
sf::String output;
//networking
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("31.216.109.207", 55552);
if(status != sf::Socket::Done)
outText.setString("nCannot connect to the server!n");
return -1;
else
outText.setString("nYou have been connected to the server!n");
socket.setBlocking(false);
//Packet for incoming msg
sf::Packet in_packet;
while(main_win.isOpen())
sf::Event event;
while(main_win.pollEvent(event))
switch (event.type)
case sf::Event::Closed: main_win.close(); break;
case sf::Event::TextEntered:
if(event.text.unicode != 'b' && event.text.unicode != 'n' && event.text.unicode != 'r')
input += event.text.unicode;
inText.setString(input);
if(inText.findCharacterPos(1000).x >= 780) //1000<msg length so I get the pos of last ch.
input += 'n';
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) && input.getSize()>name.getSize()) //2nd par to protect name
input.erase(input.getSize()-1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
output += 'n' + input;
outText.setString(output);
//send msg
sf::Packet out_packet;
out_packet << input;
while(socket.send(out_packet) == sf::Socket::Partial)
socket.send(out_packet);
input.clear();
input += name;
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
break;
case sf::Event::MouseWheelScrolled:
//-7 for speed and to go the same way with scroll (-)
outText.move(0, event.mouseWheelScroll.delta*-7);
break;
//read msg
if(socket.receive(in_packet) == sf::Socket::Done)
sf::String msg;
in_packet >> msg;
output += 'n' + msg;
outText.setString(output);
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
//check if connected
if(socket.getRemoteAddress() == sf::IpAddress::None)
output += "nConnection has been terminated. Goodbye!n";
outText.setString(output);
main_win.clear(sf::Color::Black);
main_win.draw(outText);
main_win.draw(in_background);
main_win.draw(logo);
main_win.draw(inText);
main_win.display();
I need a way to put this mess in an order. I would like to find an object oriented way to make the client more efficient and then I will use the classes to make an actually server! (I will post another question for the server I guess :P )
Server:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
int main()
sf::RenderWindow main_win(sf::VideoMode(800, 800), "Chatcy server");
main_win.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width/2 -400, sf::VideoMode::getDesktopMode().height/2 -400));
main_win.setVerticalSyncEnabled(true);
//chatcy logo
sf::Texture chatcy;
chatcy.loadFromFile("image_chatcy.png");
sf::Sprite logo;
logo.setTexture(chatcy); //200x800
//font
sf::Font font;
font.loadFromFile("FreeSerif.ttf");
//name
sf::String name("George");
name += ": ";
//text input area and background
sf::RectangleShape in_background(sf::Vector2f(800, 100));
in_background.setFillColor(sf::Color::Black);
in_background.setPosition(0, 700);
sf::Text inText;
inText.setFont(font);
inText.setPosition(0, 700);
inText.setCharacterSize(24);
sf::String input(name);
inText.setString(input);
//text output area
sf::Text outText;
outText.setFont(font);
outText.setCharacterSize(24);
outText.setPosition(0, 200);
sf::String output;
//networking
sf::TcpListener listener;
if(listener.listen(55552) == sf::Socket::Done)
std::cout << "Server is up and running!n";
sf::TcpSocket client;
listener.accept(client);
client.setBlocking(false); //after we connect
//packet to put messages in
sf::Packet in_packet;
//sound notification
sf::SoundBuffer buffer;
buffer.loadFromFile("bell_magic.wav");
sf::Sound sound;
sound.setBuffer(buffer);
sound.play(); //plays after a client is connected
outText.setString("nClient has been connected!n");
while(main_win.isOpen())
sf::Event event;
while(main_win.pollEvent(event))
switch (event.type)
case sf::Event::Closed: main_win.close(); break;
case sf::Event::TextEntered:
if(event.text.unicode != 'b' && event.text.unicode != 'n' && event.text.unicode != 'r')
input += event.text.unicode;
inText.setString(input);
if(inText.findCharacterPos(1000).x >= 780) //1000<msg length so I get the pos of last ch.
input += 'n';
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) && input.getSize()>name.getSize()) //2nd par to protect name
input.erase(input.getSize()-1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
output += 'n' + input;
outText.setString(output);
//send msg
sf::Packet out_packet;
out_packet << input;
while(client.send(out_packet) == sf::Socket::Partial)
client.send(out_packet);
input.clear();
input += name;
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
break;
case sf::Event::MouseWheelScrolled:
//-7 for speed and to go the same way with scroll (-)
outText.move(0, event.mouseWheelScroll.delta*-7);
break;
//read msg
if(client.receive(in_packet) == sf::Socket::Done)
sf::String msg;
in_packet >> msg;
output += 'n' + msg;
outText.setString(output);
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
//check if connected
if(client.getRemoteAddress() == sf::IpAddress::None)
output += "nConnection has been terminated. Goodbye!n";
outText.setString(output);
main_win.clear(sf::Color::Black);
main_win.draw(outText);
main_win.draw(in_background);
main_win.draw(logo);
main_win.draw(inText);
main_win.display();
You may ignore the sound or put your own if you are going to run it. The image is a cosmetic logo. Also note that background
is used so that text behind it is not visible.
c++ object-oriented chat client sfml
This is my client code:
(My server is very similar at the moment but I will upload it below this one just in case somebody wants to run it)
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <thread>
int main()
sf::RenderWindow main_win(sf::VideoMode(800, 800), "Chatcy!");
main_win.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width/2 -400, sf::VideoMode::getDesktopMode().height/2 -400));
main_win.setVerticalSyncEnabled(true);
//chatcy logo
sf::Texture chatcy;
chatcy.loadFromFile("image_chatcy.png");
sf::Sprite logo;
logo.setTexture(chatcy); //200x800
//font
sf::Font font;
font.loadFromFile("FreeSerif.ttf");
//name
sf::String name("Client");
name += ": ";
//text input area and background
sf::RectangleShape in_background(sf::Vector2f(800, 100));
in_background.setFillColor(sf::Color::Black);
in_background.setPosition(0, 700);
sf::Text inText;
inText.setFont(font);
inText.setPosition(0, 700);
inText.setCharacterSize(24);
sf::String input(name);
inText.setString(input);
//text output area
sf::Text outText;
outText.setFont(font);
outText.setCharacterSize(24);
outText.setPosition(0, 200);
sf::String output;
//networking
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("31.216.109.207", 55552);
if(status != sf::Socket::Done)
outText.setString("nCannot connect to the server!n");
return -1;
else
outText.setString("nYou have been connected to the server!n");
socket.setBlocking(false);
//Packet for incoming msg
sf::Packet in_packet;
while(main_win.isOpen())
sf::Event event;
while(main_win.pollEvent(event))
switch (event.type)
case sf::Event::Closed: main_win.close(); break;
case sf::Event::TextEntered:
if(event.text.unicode != 'b' && event.text.unicode != 'n' && event.text.unicode != 'r')
input += event.text.unicode;
inText.setString(input);
if(inText.findCharacterPos(1000).x >= 780) //1000<msg length so I get the pos of last ch.
input += 'n';
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) && input.getSize()>name.getSize()) //2nd par to protect name
input.erase(input.getSize()-1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
output += 'n' + input;
outText.setString(output);
//send msg
sf::Packet out_packet;
out_packet << input;
while(socket.send(out_packet) == sf::Socket::Partial)
socket.send(out_packet);
input.clear();
input += name;
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
break;
case sf::Event::MouseWheelScrolled:
//-7 for speed and to go the same way with scroll (-)
outText.move(0, event.mouseWheelScroll.delta*-7);
break;
//read msg
if(socket.receive(in_packet) == sf::Socket::Done)
sf::String msg;
in_packet >> msg;
output += 'n' + msg;
outText.setString(output);
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
//check if connected
if(socket.getRemoteAddress() == sf::IpAddress::None)
output += "nConnection has been terminated. Goodbye!n";
outText.setString(output);
main_win.clear(sf::Color::Black);
main_win.draw(outText);
main_win.draw(in_background);
main_win.draw(logo);
main_win.draw(inText);
main_win.display();
I need a way to put this mess in an order. I would like to find an object oriented way to make the client more efficient and then I will use the classes to make an actually server! (I will post another question for the server I guess :P )
Server:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
int main()
sf::RenderWindow main_win(sf::VideoMode(800, 800), "Chatcy server");
main_win.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width/2 -400, sf::VideoMode::getDesktopMode().height/2 -400));
main_win.setVerticalSyncEnabled(true);
//chatcy logo
sf::Texture chatcy;
chatcy.loadFromFile("image_chatcy.png");
sf::Sprite logo;
logo.setTexture(chatcy); //200x800
//font
sf::Font font;
font.loadFromFile("FreeSerif.ttf");
//name
sf::String name("George");
name += ": ";
//text input area and background
sf::RectangleShape in_background(sf::Vector2f(800, 100));
in_background.setFillColor(sf::Color::Black);
in_background.setPosition(0, 700);
sf::Text inText;
inText.setFont(font);
inText.setPosition(0, 700);
inText.setCharacterSize(24);
sf::String input(name);
inText.setString(input);
//text output area
sf::Text outText;
outText.setFont(font);
outText.setCharacterSize(24);
outText.setPosition(0, 200);
sf::String output;
//networking
sf::TcpListener listener;
if(listener.listen(55552) == sf::Socket::Done)
std::cout << "Server is up and running!n";
sf::TcpSocket client;
listener.accept(client);
client.setBlocking(false); //after we connect
//packet to put messages in
sf::Packet in_packet;
//sound notification
sf::SoundBuffer buffer;
buffer.loadFromFile("bell_magic.wav");
sf::Sound sound;
sound.setBuffer(buffer);
sound.play(); //plays after a client is connected
outText.setString("nClient has been connected!n");
while(main_win.isOpen())
sf::Event event;
while(main_win.pollEvent(event))
switch (event.type)
case sf::Event::Closed: main_win.close(); break;
case sf::Event::TextEntered:
if(event.text.unicode != 'b' && event.text.unicode != 'n' && event.text.unicode != 'r')
input += event.text.unicode;
inText.setString(input);
if(inText.findCharacterPos(1000).x >= 780) //1000<msg length so I get the pos of last ch.
input += 'n';
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) && input.getSize()>name.getSize()) //2nd par to protect name
input.erase(input.getSize()-1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
output += 'n' + input;
outText.setString(output);
//send msg
sf::Packet out_packet;
out_packet << input;
while(client.send(out_packet) == sf::Socket::Partial)
client.send(out_packet);
input.clear();
input += name;
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
break;
case sf::Event::MouseWheelScrolled:
//-7 for speed and to go the same way with scroll (-)
outText.move(0, event.mouseWheelScroll.delta*-7);
break;
//read msg
if(client.receive(in_packet) == sf::Socket::Done)
sf::String msg;
in_packet >> msg;
output += 'n' + msg;
outText.setString(output);
//autoscroll
if(outText.findCharacterPos(outText.getString().getSize()).y >= 680)
outText.move(0, -33);
//check if connected
if(client.getRemoteAddress() == sf::IpAddress::None)
output += "nConnection has been terminated. Goodbye!n";
outText.setString(output);
main_win.clear(sf::Color::Black);
main_win.draw(outText);
main_win.draw(in_background);
main_win.draw(logo);
main_win.draw(inText);
main_win.display();
You may ignore the sound or put your own if you are going to run it. The image is a cosmetic logo. Also note that background
is used so that text behind it is not visible.
c++ object-oriented chat client sfml
edited Jan 16 at 20:05
asked Jan 16 at 20:01
alienCY
1287
1287
1
Have you researched any chat protocols? rfc2812
â Martin York
Jan 17 at 8:15
I wasn't even aware of their existence! :P Time to do some research :D
â alienCY
Jan 17 at 15:13
add a comment |Â
1
Have you researched any chat protocols? rfc2812
â Martin York
Jan 17 at 8:15
I wasn't even aware of their existence! :P Time to do some research :D
â alienCY
Jan 17 at 15:13
1
1
Have you researched any chat protocols? rfc2812
â Martin York
Jan 17 at 8:15
Have you researched any chat protocols? rfc2812
â Martin York
Jan 17 at 8:15
I wasn't even aware of their existence! :P Time to do some research :D
â alienCY
Jan 17 at 15:13
I wasn't even aware of their existence! :P Time to do some research :D
â alienCY
Jan 17 at 15:13
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185249%2fchat-program-with-c-and-sfml%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Have you researched any chat protocols? rfc2812
â Martin York
Jan 17 at 8:15
I wasn't even aware of their existence! :P Time to do some research :D
â alienCY
Jan 17 at 15:13