fix Engine <-> NetworkClient interaction new_graphics
authorTero Marttila <terom@fixme.fi>
Thu, 22 Jan 2009 02:47:53 +0200
branchnew_graphics
changeset 419 9cd4e54693b6
parent 418 194bc810a570
child 420 278020dcd9b7
fix Engine <-> NetworkClient interaction
src/Engine.cc
src/Engine.hh
src/Graphics/Graphics.hh
src/Network/Client.cc
--- a/src/Engine.cc	Thu Jan 22 02:38:33 2009 +0200
+++ b/src/Engine.cc	Thu Jan 22 02:47:53 2009 +0200
@@ -26,13 +26,14 @@
 
 Engine::Engine (const EngineConfig &config) : 
     terrain(NULL), game_state(NULL), graphics(NULL), net_server(NULL), net_client_connect(NULL),
+    game_view(NULL),
     is_running(true), resources(config.resource_path)
 {
     // update global log_level
     Engine::log_level = config.log_level;
 }
 
-GameState& Engine::setupGame (Terrain *terrain) {
+void Engine::setupGame (Terrain *terrain) {
     // ensure this isn't called in inappropriate ways
     assert(!net_server);
 
@@ -41,10 +42,6 @@
 
     // create the GameState
     game_state = new GameState(*terrain);
-    
-    // XXX: NetworkClient + GameView?
-
-    return *game_state;
 }
         
 void Engine::setupGame (const TerrainConfig &config) {
@@ -72,9 +69,9 @@
 void Engine::startGameView (LocalPlayer *player) {
 
 #if GRAPHICS_ENABLED    
-    assert(graphics);
+    assert(graphics && !game_view);
 
-    graphics->displayGameView(*game_state, player);
+    game_view = graphics->displayGameView(*game_state, player);
 
 #else
     (void) player;
@@ -138,7 +135,25 @@
     // put graphics into GameView mode
     if (graphics)
         startGameView(lp);
- 
+}
+        
+
+GameState& Engine::onNetworkClientConnected (Terrain *terrain) {
+    // setup the game
+    setupGame(terrain);
+
+    // start the GameView, but no LocalPlayer yet
+    startGameView(NULL);
+
+    // return GameState
+    return *game_state;
+}
+        
+void Engine::onNetworkClientPlayer (LocalPlayer *player) {
+    assert(game_view);
+
+    // set the GameView's player
+    game_view->setPlayer(player);
 }
 
 void Engine::stop (void) {
--- a/src/Engine.hh	Thu Jan 22 02:38:33 2009 +0200
+++ b/src/Engine.hh	Thu Jan 22 02:47:53 2009 +0200
@@ -10,10 +10,11 @@
 
 // forward-declare component pointer types
 // XXX: move to some kind of Components.hh file?
-namespace graphics { class Graphics; }
+namespace graphics { class Graphics; class GameView; }
 class NetworkServer;
 class NetworkClientConnect;
 class NetworkClient;
+class NetworkClientController;
 
 /**
  * This is the core class that glues all the other components together and runs the main loop
@@ -38,6 +39,9 @@
         /** Network client, currently unused */
         NetworkClient *net_client;
 
+        /** The GameView, if open */
+        graphics::GameView *game_view;
+
         /** Is the mainloop still running? */
         bool is_running;
         
@@ -53,10 +57,8 @@
 
         /**
          * Setup game world using the given terrain, returning the new GameState
-         *
-         * XXX: fix to return void
          */
-        GameState& setupGame (Terrain *terrain);
+        void setupGame (Terrain *terrain);
 
         /**
          * Setup game world using given terrain configuration
@@ -105,6 +107,20 @@
          */
         void startGameView (LocalPlayer *player);
 
+    protected:
+        friend class NetworkClientConnect;
+        friend class NetworkClientController;
+
+        /**
+         * Interface for NetworkClient to use once it has connected
+         */
+        GameState& onNetworkClientConnected (Terrain *terrain);
+
+        /**
+         * Interface for NetworkClient to use once it has set up the player
+         */
+        void onNetworkClientPlayer (LocalPlayer *player);
+
     public:
         /**
          * Get a pointer to our resource manager, please don't break it.
--- a/src/Graphics/Graphics.hh	Thu Jan 22 02:38:33 2009 +0200
+++ b/src/Graphics/Graphics.hh	Thu Jan 22 02:47:53 2009 +0200
@@ -52,12 +52,15 @@
     /**
      * Display a new GameView
      */
-    void displayGameView (GameState &state, LocalPlayer *player) {
+    GameView* displayGameView (GameState &state, LocalPlayer *player) {
         // allocate a new GameView
         GameView *view = new GameView(state, player);
 
         // assign it to the display
         display.setView(view);
+        
+        // return it
+        return view;
     }
 
 };
--- a/src/Network/Client.cc	Thu Jan 22 02:38:33 2009 +0200
+++ b/src/Network/Client.cc	Thu Jan 22 02:47:53 2009 +0200
@@ -57,7 +57,7 @@
 
 void NetworkClientConnect::connectDone (Terrain *terrain) {
     // pass Terrain to engine to create game
-    GameState &gs = engine.setupGame(terrain);
+    GameState &gs = engine.onNetworkClientConnected(terrain);
 
     // create our new NetworkClient object
     client = new NetworkClient(engine, gs, netsession, server);
@@ -119,7 +119,10 @@
     Engine::log(INFO, "client.on_server_hello") << this << ": pos=" << position;
 
     // create the LocalPlayer object
-    new NetworkClientLocalPlayer(client, obj_id, position);
+    NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(client, obj_id, position);
+
+    // pass it on to engine
+    client.engine.onNetworkClientPlayer(player);
 }
         
 void NetworkClientController::on_player_info (NetworkObjectID obj_id, NetworkPacketInput &pkt) {