changeset 140:a83fff870983

Client: prepare state, #712 Add update and draw function in states to allow future development of predefined states.
author David Demelier <markand@malikania.fr>
date Wed, 27 Sep 2017 21:02:41 +0200
parents b80d37e71b87
children 2cce1729b6da
files libclient/malikania/client/client.cpp libclient/malikania/client/client.hpp libclient/malikania/client/state.hpp
diffstat 3 files changed, 60 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libclient/malikania/client/client.cpp	Wed Sep 27 20:34:59 2017 +0200
+++ b/libclient/malikania/client/client.cpp	Wed Sep 27 21:02:41 2017 +0200
@@ -43,6 +43,11 @@
     connection_.do_connect(*this, std::move(host), port);
 }
 
+void client::set_state(std::unique_ptr<state> state)
+{
+    state_next_ = std::move(state);
+}
+
 void client::handle_key_down(const key_event& ev)
 {
     window_.handle_key_down(ev);
@@ -86,6 +91,9 @@
 
 void client::run_one()
 {
+    if (state_next_)
+        state_ = std::move(state_next_);
+
     window_.poll(*this);
 }
 
--- a/libclient/malikania/client/client.hpp	Wed Sep 27 20:34:59 2017 +0200
+++ b/libclient/malikania/client/client.hpp	Wed Sep 27 21:02:41 2017 +0200
@@ -49,6 +49,7 @@
     mlk::client::connection& connection_;
     mlk::client::window& window_;
     std::unique_ptr<state> state_;
+    std::unique_ptr<state> state_next_;
 
 public:
     /**
@@ -72,19 +73,39 @@
      *
      * \return the connection
      */
+    inline const mlk::client::connection& connection() const noexcept
+    {
+        return connection_;
+    }
+
+    /**
+     * Overloaded function.
+     *
+     * \return the connection
+     */
     inline mlk::client::connection& connection() noexcept
     {
         return connection_;
     }
 
     /**
-     * Get the connection object.
+     * Get the window.
      *
-     * \return the connection
+     * \return the window
      */
-    inline const mlk::client::connection& connection() const noexcept
+    inline const mlk::client::window& window() const noexcept
     {
-        return connection_;
+        return window_;
+    }
+
+    /**
+     * Overloaded function.
+     *
+     * \return the window
+     */
+    inline mlk::client::window& window() noexcept
+    {
+        return window_;
     }
 
     /**
@@ -98,6 +119,13 @@
     void connect(std::string host, std::uint16_t port);
 
     /**
+     * Set the client state.
+     *
+     * \param state the state
+     */
+    void set_state(std::unique_ptr<state>);
+
+    /**
      * Run the client until the game window is closed.
      */
     void run();
--- a/libclient/malikania/client/state.hpp	Wed Sep 27 20:34:59 2017 +0200
+++ b/libclient/malikania/client/state.hpp	Wed Sep 27 21:02:41 2017 +0200
@@ -46,6 +46,26 @@
      * Virtual destructor defaulted.
      */
     virtual ~state() noexcept = default;
+
+    /**
+     * Update the state.
+     *
+     * \param clt the client
+     */
+    virtual void update(client& clt)
+    {
+        (void)clt;
+    }
+
+    /**
+     * Draw the state.
+     *
+     * \param clt the client
+     */
+    virtual void draw(client& clt)
+    {
+        (void)clt;
+    }
 };
 
 } // !client