I'm trying to learn to use sfml's networking to make a chat program.I got this to work in console so I decided to do it with a window and Text. I was planning on having an array of Text that displays the received messages. This is called "mess".Drawing the array to screen using a for loop results in the window crashing.This however doesn't happen for the first index of mess. I tried limiting the for loop to just 0 but this still results in a crash.by the way,the typing input does draw to screen with no problem.
#include <iostream>
#include <SFML\Network.hpp>
#include "main.h"
using namespace std;
#include <SFML\Graphics.hpp>
using namespace sf;
RenderWindow window(VideoMode(960,540),"");
int main()
{
int currentline;
Font font;
font.loadFromFile("C:\\Windows\\Fonts\\comic.ttf");
Text typing;
Text mess[4000];
for(int x=0;x<4000;x++)
{
mess[x].setFont(font);
mess[x].setCharacterSize(24);
mess[x].setFillColor(Color::Blue);
}
typing.setFont(font);
typing.setCharacterSize(24);
typing.setFillColor(Color::Blue);
Clock clock;
IpAddress ip=IpAddress::getLocalAddress();
string text="Connected to:";
char buffer[2000];
size_t received;
TcpSocket socket;
char connectiontype,mode;
cout<<"Enter(s) for server,Enter (c) for client.\n";
cin>>connectiontype;
if(connectiontype=='s')
{
TcpListener listener;
listener.listen(5300);
listener.accept(socket);
text+="Server";
}
else if(connectiontype=='c')
{
socket.connect(ip,5300);
text +="Client";
}
mess[0].setString(text);
string input;
while(window.isOpen())
{
if(Keyboard::isKeyPressed(Keyboard::Return))
{
string typed=typing.getString();
socket.send('~'+typed.c_str(),typed.length()+1);
typing.setString("");
input="";
}
socket.receive(buffer,sizeof(buffer),received);
if(buffer[0]=='~')
{
mess[currentline].setString(string(buffer));
buffer[0]='';
currentline++;
}
Event e;
while(window.pollEvent(e))
{
if(e.type==Event::Closed)
window.close();
if(e.type==Event::TextEntered)
{
input+=e.text.unicode;
typing.setString(input);
}
}
window.clear(Color::Cyan);
window.draw(mess[0]);
//window.draw(mess[1]);
for(int x=0;x<currentline;x++)
{
mess[x].setPosition(0,mess[x].getCharacterSize()*currentline);
window.draw(mess[x]);
}
typing.setPosition(0,mess[0].getCharacterSize()*(currentline+1));
window.draw(typing);
window.display();
}
return 0;
}