For some reason, when I make a new sf::Thread and launch it, it stops my main thread from continuing :/
Here is my code:
The Server constructor makes a new thread as shown below
#include "Server.h"
#include "Client.h"
#include <SFML/Graphics.hpp>
int main()
{
sf::Text inputText; // text input by the user
std::string input;
bool hasChosen = false;
sf::RenderWindow window(sf::VideoMode(800, 200), "Input", sf::Style::Titlebar);
while (window.isOpen())
{
sf::Event e;
while (window.pollEvent(e))
{
if (e.type == sf::Event::KeyPressed)
{
if (e.key.code == sf::Keyboard::Return)
{
}
else if (e.key.code == sf::Keyboard::Escape)
{
window.close();
}
else if (e.key.code == sf::Keyboard::S)
{
if (!hasChosen)
{
hasChosen = true;
Server server(8080); // start server
std::cout << "Test";
}
}
else if (e.key.code == sf::Keyboard::C)
{
if (!hasChosen)
{
hasChosen = true;
Client client(sf::IpAddress::getLocalAddress(), 8080);
}
}
}
}
window.clear();
window.draw(inputText);
window.display();
}
Note my Server Class constructor:
Server::Server(int port)
{
listener.listen(port);
selector.add(listener);
done = false;
sf::Thread serverThread(&Server::startServer, this);
serverThread.launch();
listener.close(); // close the server when we are done
}