]> git.localhorst.tv Git - blank.git/commitdiff
basic message state
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 2 Oct 2015 13:19:58 +0000 (15:19 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 2 Oct 2015 13:19:58 +0000 (15:19 +0200)
not very versatile, but oh well

src/app/Environment.hpp
src/app/MessageState.cpp [new file with mode: 0644]
src/app/MessageState.hpp [new file with mode: 0644]
src/app/runtime.cpp
src/client/client.cpp

index 97f4a7ec54434e839dfaba76c66095a80bb17cf7..f3aa4b5fe92ae3599bafb400aa9a593d9f292b65 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "Assets.hpp"
 #include "FrameCounter.hpp"
+#include "MessageState.hpp"
 #include "StateControl.hpp"
 #include "../audio/Audio.hpp"
 #include "../graphics/Viewport.hpp"
@@ -53,9 +54,13 @@ struct Environment
 
        Keymap keymap;
 
+       MessageState msg_state;
+
 
        Environment(Window &win, const Config &);
 
+       void ShowMessage(const char *);
+
 };
 
 }
diff --git a/src/app/MessageState.cpp b/src/app/MessageState.cpp
new file mode 100644 (file)
index 0000000..53ed741
--- /dev/null
@@ -0,0 +1,45 @@
+#include "MessageState.hpp"
+
+#include "Environment.hpp"
+
+
+namespace blank {
+
+MessageState::MessageState(Environment &env)
+: env(env) {
+       message.Position(glm::vec3(0.0f), Gravity::CENTER);
+       message.Hide();
+       press_key.Position(glm::vec3(0.0f, env.assets.large_ui_font.LineSkip(), 0.0f), Gravity::CENTER);
+       press_key.Set(env.assets.small_ui_font, "press any key to continue");
+       press_key.Show();
+}
+
+void MessageState::SetMessage(const char *msg) {
+       message.Set(env.assets.large_ui_font, msg);
+       message.Show();
+}
+
+void MessageState::ClearMessage() {
+       message.Hide();
+}
+
+void MessageState::Handle(const SDL_Event &e) {
+       if (e.type == SDL_KEYDOWN) {
+               env.state.Pop();
+       }
+}
+
+void MessageState::Update(int dt) {
+
+}
+
+void MessageState::Render(Viewport &viewport) {
+       if (message.Visible()) {
+               message.Render(viewport);
+       }
+       if (press_key.Visible()) {
+               press_key.Render(viewport);
+       }
+}
+
+}
diff --git a/src/app/MessageState.hpp b/src/app/MessageState.hpp
new file mode 100644 (file)
index 0000000..29c8cb6
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef BLANK_APP_MESSAGESTATE_HPP_
+#define BLANK_APP_MESSAGESTATE_HPP_
+
+#include "State.hpp"
+
+#include "../ui/FixedText.hpp"
+
+
+namespace blank {
+
+class Environment;
+
+class MessageState
+: public State {
+
+public:
+       explicit MessageState(Environment &);
+
+       void SetMessage(const char *);
+       void ClearMessage();
+
+       void Handle(const SDL_Event &) override;
+       void Update(int dt) override;
+       void Render(Viewport &) override;
+
+private:
+       Environment &env;
+       FixedText message;
+       FixedText press_key;
+
+};
+
+}
+
+#endif
index 7a489e6e21e542d1cdf843685ca34e6c048cbb6e..780a063fc1c3b27e1f15599dd47c32bfcbc37393 100644 (file)
@@ -135,7 +135,8 @@ Environment::Environment(Window &win, const Config &config)
 , audio()
 , viewport()
 , window(win)
-, keymap() {
+, keymap()
+, msg_state(*this) {
        viewport.Clear();
        window.Flip();
        keymap.LoadDefault();
@@ -150,6 +151,12 @@ Environment::Environment(Window &win, const Config &config)
        }
 }
 
+void Environment::ShowMessage(const char *msg) {
+       cout << msg << endl;
+       msg_state.SetMessage(msg);
+       state.Push(&msg_state);
+}
+
 
 Runtime::Runtime() noexcept
 : name("blank")
index e2702245a228fa1ba0d4e39c3e66c2d252dbc92b..7f58dfaca0a5e23486bb37c7411b424a883da87d 100644 (file)
@@ -337,9 +337,8 @@ void MasterState::OnPacketLost(uint16_t id) {
 
 void MasterState::OnTimeout() {
        if (client.GetConnection().Closed()) {
-               // TODO: push disconnected message
-               cout << "connection timed out" << endl;
                Quit();
+               env.ShowMessage("connection timed out");
        }
 }
 
@@ -367,20 +366,19 @@ void MasterState::On(const Packet::Join &pack) {
 }
 
 void MasterState::On(const Packet::Part &pack) {
+       Quit();
        if (state) {
                // kicked
-               cout << "kicked by server" << endl;
+               env.ShowMessage("kicked by server");
        } else {
                // join refused
-               cout << "login refused by server" << endl;
+               env.ShowMessage("login refused by server");
        }
-       Quit();
 }
 
 void MasterState::On(const Packet::SpawnEntity &pack) {
        if (!state) {
                cout << "got entity spawn before world was created" << endl;
-               Quit();
                return;
        }
        uint32_t entity_id;
@@ -401,7 +399,6 @@ void MasterState::On(const Packet::SpawnEntity &pack) {
 void MasterState::On(const Packet::DespawnEntity &pack) {
        if (!state) {
                cout << "got entity despawn before world was created" << endl;
-               Quit();
                return;
        }
        uint32_t entity_id;
@@ -419,7 +416,6 @@ void MasterState::On(const Packet::DespawnEntity &pack) {
 void MasterState::On(const Packet::EntityUpdate &pack) {
        if (!state) {
                cout << "got entity update before world was created" << endl;
-               Quit();
                return;
        }
 
@@ -474,7 +470,6 @@ void MasterState::ClearEntity(uint32_t entity_id) {
 void MasterState::On(const Packet::PlayerCorrection &pack) {
        if (!state) {
                cout << "got player correction without a player :S" << endl;
-               Quit();
                return;
        }
        uint16_t pack_seq;