From: Daniel Karbach Date: Fri, 2 Oct 2015 13:19:58 +0000 (+0200) Subject: basic message state X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=78a290bd642c5578c9dd17481c8164ff50889ca2;p=blank.git basic message state not very versatile, but oh well --- diff --git a/src/app/Environment.hpp b/src/app/Environment.hpp index 97f4a7e..f3aa4b5 100644 --- a/src/app/Environment.hpp +++ b/src/app/Environment.hpp @@ -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 index 0000000..53ed741 --- /dev/null +++ b/src/app/MessageState.cpp @@ -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 index 0000000..29c8cb6 --- /dev/null +++ b/src/app/MessageState.hpp @@ -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 diff --git a/src/app/runtime.cpp b/src/app/runtime.cpp index 7a489e6..780a063 100644 --- a/src/app/runtime.cpp +++ b/src/app/runtime.cpp @@ -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") diff --git a/src/client/client.cpp b/src/client/client.cpp index e270224..7f58dfa 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -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;