summaryrefslogtreecommitdiffstats
path: root/src/Game.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2020-07-30 01:34:25 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2020-07-30 01:34:25 +0200
commit3f9ce9f54d342bfaba1f41bb0d6b18aa9cf11a43 (patch)
treec33a330ab7b4bc704b7b3c978c354cba0683d729 /src/Game.cpp
parentImproved uncompressed chunks decoding (diff)
downloadAltCraft-3f9ce9f54d342bfaba1f41bb0d6b18aa9cf11a43.tar
AltCraft-3f9ce9f54d342bfaba1f41bb0d6b18aa9cf11a43.tar.gz
AltCraft-3f9ce9f54d342bfaba1f41bb0d6b18aa9cf11a43.tar.bz2
AltCraft-3f9ce9f54d342bfaba1f41bb0d6b18aa9cf11a43.tar.lz
AltCraft-3f9ce9f54d342bfaba1f41bb0d6b18aa9cf11a43.tar.xz
AltCraft-3f9ce9f54d342bfaba1f41bb0d6b18aa9cf11a43.tar.zst
AltCraft-3f9ce9f54d342bfaba1f41bb0d6b18aa9cf11a43.zip
Diffstat (limited to 'src/Game.cpp')
-rw-r--r--src/Game.cpp54
1 files changed, 42 insertions, 12 deletions
diff --git a/src/Game.cpp b/src/Game.cpp
index c69ff5d..f0266d4 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -18,35 +18,61 @@ std::unique_ptr<Render> render;
std::unique_ptr<LoopExecutionTimeController> timer;
EventListener listener;
-void InitEvents() {
- /*
- * Network Events
- */
+std::thread connThread;
+std::unique_ptr<NetworkClient> connNc;
+std::unique_ptr<GameState> connGs;
+
+void ConnectionThreadExec() {
+ EventListener connListener;
- listener.RegisterHandler("ConnectToServer", [](const Event & eventData) {
+ bool connRun = true;
+
+ connListener.RegisterHandler("ConnectToServer", [](const Event& eventData) {
auto data = eventData.get <std::tuple<std::string, unsigned short, std::string>>(); //address,port,username
if (std::get<0>(data) == "" || std::get<1>(data) == 0)
LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT";
- if (nc != nullptr) {
+ if (connNc != nullptr) {
LOG(ERROR) << "Already connected";
return;
}
LOG(INFO) << "Connecting to server at address " + std::get<0>(data) + ":" + std::to_string(std::get<1>(data)) + " as " + std::get<2>(data);
- PUSH_EVENT("Connecting",0);
- gs = std::make_unique<GameState>();
+ PUSH_EVENT("Connecting", 0);
+ connGs = std::make_unique<GameState>();
try {
- nc = std::make_unique<NetworkClient>(std::get<0>(data),
+ connNc = std::make_unique<NetworkClient>(std::get<0>(data),
std::get<1>(data),
std::get<2>(data));
- } catch (std::exception &e) {
+ }
+ catch (std::exception& e) {
LOG(WARNING) << "Connection failed";
PUSH_EVENT("ConnectionFailed", std::string(e.what()));
- gs.reset();
+ connGs.reset();
return;
}
LOG(INFO) << "Connected to server";
PUSH_EVENT("ConnectionSuccessfull", 0);
- });
+ });
+
+ connListener.RegisterHandler("Exit", [&](const Event&) {
+ connRun = false;
+ });
+
+ LoopExecutionTimeController timer(std::chrono::milliseconds(50));
+ while (connRun) {
+ connListener.HandleAllEvents();
+ timer.Update();
+ }
+}
+
+void InitEvents() {
+ /*
+ * Network Events
+ */
+
+ listener.RegisterHandler("ConnectionSuccessfull", [](const Event&) {
+ nc = std::move(connNc);
+ gs = std::move(connGs);
+ });
listener.RegisterHandler("Disconnect", [](const Event& eventData) {
auto data = eventData.get<std::string>();
@@ -186,6 +212,8 @@ void RunGame() {
render = std::make_unique<Render>(900, 480, "AltCraft");
+ connThread = std::thread(ConnectionThreadExec);
+
SetState(State::MainMenu);
while (isRunning) {
@@ -212,6 +240,8 @@ void RunGame() {
}
render.reset();
+
+ connThread.join();
}
State GetState() {