src/Engine.hh
author morasa@smirgeline.hut.fi
Fri, 11 Sep 2009 16:45:04 +0300
branchnew-physics
changeset 447 fc9e4305fddf
parent 419 9cd4e54693b6
permissions -rw-r--r--
create new physics branch
#ifndef ENGINE_HH
#define ENGINE_HH

// forward-declare
class Engine;

#include "GameState.hh"
#include "Configuration.hh"
#include "Logger.hh"

// forward-declare component pointer types
// XXX: move to some kind of Components.hh file?
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
 */
class Engine {
    private:
        /** The game's terrain */
        Terrain *terrain;

        /** The game's state */
        GameState *game_state;
        
        /** The graphics+input component */
        graphics::Graphics *graphics;
        
        /** Network server */
        NetworkServer *net_server;

        /** Network client connector */
        NetworkClientConnect *net_client_connect;

        /** Network client, currently unused */
        NetworkClient *net_client;

        /** The GameView, if open */
        graphics::GameView *game_view;

        /** Is the mainloop still running? */
        bool is_running;
        
        /** Used to load ClanLib resources */
        CL_ResourceManager resources;

        /** *Global* log level */
        static LogLevel log_level;
    
    public:    
        // default constructor
        Engine (const EngineConfig &config);

        /**
         * Setup game world using the given terrain, returning the new GameState
         */
        void setupGame (Terrain *terrain);

        /**
         * Setup game world using given terrain configuration
         */
        void setupGame (const TerrainConfig &config);

        /**
         * Enable graphics. 
         *
         * Requires: GRAPHICS_ENABLED
         */
        void setupGraphics (const DisplayConfig &config);
        
        /**
         * Setup server, must call setupGame first
         *
         * Requires: NETWORK_ENABLED
         */
        void setupNetworkServer (const std::string &listen_port);

        /**
         * Setup client, do *not* call setupGame, configuration comes from the server
         *
         * Requires: NETWORK_ENABLED
         */
        void setupNetworkClient (const std::string &connect_host, const std::string &connect_port);

        /**
         * Setup singleplayer, must call setupGame first
         */
        void setupSinglePlayer (void);

        
        /**
         * Run the game main loop. This will not return until the game aborts due to an error, or someone calls stop().
         */
        void run (void);
        
        /**
         * Terminate the main loop, causing run() to return once this loop iteration is finished
         */
        void stop (void);

    private:
        /**
         * Puts graphics into GameView mode, using our GameState and the given player
         */
        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.
         *
         * Guaranteed to never be NULL
         */
        CL_ResourceManager* getResourceManager (void) { return &resources; }
        
        /**
         * Log output, see Logger for more info
         *
         * @see Logger
         */
        static inline Logger log (LogLevel level, const char *type) {
            return Logger(level <= WARN ? std::cerr : std::cout, level, type, log_level);
        }

};

#endif /* ENGINE_HH */