summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/GlobalState.cpp46
-rw-r--r--src/Render.cpp156
-rw-r--r--src/Render.hpp9
3 files changed, 94 insertions, 117 deletions
diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp
index 57b42ea..a9f3d4e 100644
--- a/src/GlobalState.cpp
+++ b/src/GlobalState.cpp
@@ -6,12 +6,10 @@
#include "DebugInfo.hpp"
#include "Event.hpp"
-
//Global game variables
std::unique_ptr<NetworkClient> nc;
std::unique_ptr<GameState> gs;
std::unique_ptr<Render> render;
-std::thread threadGs;
bool isRunning;
bool isPhysRunning;
EventListener listener;
@@ -64,11 +62,6 @@ void InitEvents() {
PUSH_EVENT("Disconnect", data);
});
- listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) {
- std::shared_ptr<Packet> packet = eventData.get<std::shared_ptr<Packet>>();
- gs->UpdatePacket(packet);
- });
-
/*
* GameState Events
*/
@@ -156,6 +149,11 @@ void PhysExec() {
gs->HandleRotation(std::get<0>(data),std::get<1>(data));
});
+ listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) {
+ std::shared_ptr<Packet> packet = eventData.get<std::shared_ptr<Packet>>();
+ gs->UpdatePacket(packet);
+ });
+
LoopExecutionTimeController timer(std::chrono::milliseconds(8));
while (isPhysRunning) {
@@ -182,39 +180,17 @@ void PhysExec() {
}
}
-void GsExec() {
- el::Helpers::setThreadName("Game");
- LoopExecutionTimeController timer(std::chrono::milliseconds(16));
-
- while (isRunning) {
- try {
- while (nc && gs) {
- listener.HandleAllEvents();
- }
- } catch (std::exception &e) {
- PUSH_EVENT("NetworkClientException", e.what());
- }
-
- listener.HandleAllEvents();
-
- timer.Update();
- }
- if (isPhysRunning) {
- isPhysRunning = false;
- threadPhys.join();
- }
- nc.reset();
- gs.reset();
-}
-
void GlobalState::Exec() {
render = std::make_unique<Render>(900, 480, "AltCraft");
isRunning = true;
InitEvents();
- threadGs = std::thread(&GsExec);
- render->ExecuteRenderLoop();
+ GlobalState::SetState(State::MainMenu);
+ while (isRunning) {
+ render->Update();
+ listener.HandleAllEvents();
+ }
+ PUSH_EVENT("Exit", 0);
isRunning = false;
- threadGs.join();
render.reset();
}
diff --git a/src/Render.cpp b/src/Render.cpp
index 5aa38d7..6c5cc61 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -14,6 +14,8 @@
#include "RendererWorld.hpp"
Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) : timer(std::chrono::milliseconds(16)) {
+ InitEvents();
+
InitSdl(windowWidth, windowHeight, windowTitle);
glCheckError();
InitGlew();
@@ -141,7 +143,7 @@ void Render::HandleEvents() {
switch (event.type) {
case SDL_QUIT:
LOG(INFO) << "Received close event by window closing";
- isRunning = false;
+ PUSH_EVENT("Exit",0);
break;
case SDL_WINDOWEVENT: {
switch (event.window.event) {
@@ -179,7 +181,7 @@ void Render::HandleEvents() {
break;
case State::MainMenu:
LOG(INFO) << "Received close event by esc";
- isRunning = false;
+ PUSH_EVENT("Exit",0);
break;
}
break;
@@ -242,81 +244,16 @@ void Render::SetMouseCapture(bool IsCaptured) {
}
}
-void Render::ExecuteRenderLoop() {
- EventListener listener;
-
- listener.RegisterHandler("ConnectionSuccessfull", [this](const Event&) {
- stateString = "Logging in...";
- });
-
- listener.RegisterHandler("PlayerConnected", [this](const Event&) {
- stateString = "Loading terrain...";
- world = std::make_unique<RendererWorld>(GlobalState::GetGameState());
- });
-
- listener.RegisterHandler("RemoveLoadingScreen", [this](const Event&) {
- stateString = "Playing";
- renderWorld = true;
- GlobalState::SetState(State::Playing);
- glClearColor(0, 0, 0, 1.0f);
- });
-
- listener.RegisterHandler("ConnectionFailed", [this](const Event& eventData) {
- stateString = "Connection failed: " + eventData.get <std::string>();
- renderWorld = false;
- world.reset();
- GlobalState::SetState(State::MainMenu);
- glClearColor(0.8, 0.8, 0.8, 1.0f);
- });
-
- listener.RegisterHandler("Disconnected", [this](const Event& eventData) {
- stateString = "Disconnected: " + eventData.get<std::string>();
- renderWorld = false;
- world.reset();
- GlobalState::SetState(State::MainMenu);
- glClearColor(0.8, 0.8, 0.8, 1.0f);
- });
-
- listener.RegisterHandler("Connecting", [this](const Event&) {
- stateString = "Connecting to the server...";
- GlobalState::SetState(State::Loading);
- });
-
- listener.RegisterHandler("ChatMessageReceived", [this](const Event& eventData) {
- auto data = eventData.get<std::tuple<Chat, unsigned char>>();
- std::string msg = "(" + std::to_string((int)std::get<1>(data)) + ") " + std::get<0>(data).text;
- chatMessages.push_back(msg);
- });
-
- listener.RegisterHandler("StateUpdated", [this](const Event& eventData) {
- switch (GlobalState::GetState()) {
- case State::Playing:
- SetMouseCapture(true);
- break;
- case State::InitialLoading:
- case State::MainMenu:
- case State::Loading:
- case State::Paused:
- case State::Inventory:
- case State::Chat:
- SetMouseCapture(false);
- break;
- }
- });
-
- GlobalState::SetState(State::MainMenu);
-
- while (isRunning) {
- HandleEvents();
- if (HasFocus && GlobalState::GetState() == State::Playing) UpdateKeyboard();
- if (isMouseCaptured) HandleMouseCapture();
- glCheckError();
-
- RenderFrame();
- listener.HandleAllEvents();
- timer.Update();
- }
- PUSH_EVENT("Exit", 0);
+void Render::Update() {
+
+ HandleEvents();
+ if (HasFocus && GlobalState::GetState() == State::Playing) UpdateKeyboard();
+ if (isMouseCaptured) HandleMouseCapture();
+ glCheckError();
+
+ RenderFrame();
+ listener.HandleAllEvents();
+ timer.Update();
}
void Render::RenderGui() {
@@ -364,7 +301,7 @@ void Render::RenderGui() {
ImGui::InputInt("Port", &port);
ImGui::Separator();
if (ImGui::Button("Exit"))
- isRunning = false;
+ PUSH_EVENT("Exit",0);
ImGui::End();
break;
}
@@ -531,4 +468,65 @@ void Render::RenderGui() {
}
ImGui::Render();
-} \ No newline at end of file
+}
+
+void Render::InitEvents() {
+ listener.RegisterHandler("ConnectionSuccessfull", [this](const Event&) {
+ stateString = "Logging in...";
+ });
+
+ listener.RegisterHandler("PlayerConnected", [this](const Event&) {
+ stateString = "Loading terrain...";
+ world = std::make_unique<RendererWorld>(GlobalState::GetGameState());
+ });
+
+ listener.RegisterHandler("RemoveLoadingScreen", [this](const Event&) {
+ stateString = "Playing";
+ renderWorld = true;
+ GlobalState::SetState(State::Playing);
+ glClearColor(0, 0, 0, 1.0f);
+ });
+
+ listener.RegisterHandler("ConnectionFailed", [this](const Event& eventData) {
+ stateString = "Connection failed: " + eventData.get <std::string>();
+ renderWorld = false;
+ world.reset();
+ GlobalState::SetState(State::MainMenu);
+ glClearColor(0.8, 0.8, 0.8, 1.0f);
+ });
+
+ listener.RegisterHandler("Disconnected", [this](const Event& eventData) {
+ stateString = "Disconnected: " + eventData.get<std::string>();
+ renderWorld = false;
+ world.reset();
+ GlobalState::SetState(State::MainMenu);
+ glClearColor(0.8, 0.8, 0.8, 1.0f);
+ });
+
+ listener.RegisterHandler("Connecting", [this](const Event&) {
+ stateString = "Connecting to the server...";
+ GlobalState::SetState(State::Loading);
+ });
+
+ listener.RegisterHandler("ChatMessageReceived", [this](const Event& eventData) {
+ auto data = eventData.get<std::tuple<Chat, unsigned char>>();
+ std::string msg = "(" + std::to_string((int)std::get<1>(data)) + ") " + std::get<0>(data).text;
+ chatMessages.push_back(msg);
+ });
+
+ listener.RegisterHandler("StateUpdated", [this](const Event& eventData) {
+ switch (GlobalState::GetState()) {
+ case State::Playing:
+ SetMouseCapture(true);
+ break;
+ case State::InitialLoading:
+ case State::MainMenu:
+ case State::Loading:
+ case State::Paused:
+ case State::Inventory:
+ case State::Chat:
+ SetMouseCapture(false);
+ break;
+ }
+ });
+}
diff --git a/src/Render.hpp b/src/Render.hpp
index 7d3af39..3367100 100644
--- a/src/Render.hpp
+++ b/src/Render.hpp
@@ -9,6 +9,7 @@
#include "Utility.hpp"
#include "Renderer.hpp"
+#include "Event.hpp"
class RendererWorld;
@@ -17,7 +18,6 @@ class Render {
SDL_GLContext glContext;
bool renderGui = false;
- bool isRunning = true;
bool isMouseCaptured = false;
int prevMouseX, prevMouseY;
float mouseXDelta, mouseYDelta;
@@ -30,7 +30,7 @@ class Render {
float sensetivity = 0.1f;
bool isWireframe = false;
std::vector<std::string> chatMessages;
-
+ EventListener listener;
std::string stateString;
void SetMouseCapture(bool IsCaptured);
@@ -50,9 +50,12 @@ class Render {
void UpdateKeyboard();
void RenderGui();
+
+ void InitEvents();
+
public:
Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle);
~Render();
- void ExecuteRenderLoop();
+ void Update();
}; \ No newline at end of file