src/Engine.cc
author Tero Marttila <terom@fixme.fi>
Wed, 21 Jan 2009 23:25:29 +0200
branchnew_graphics
changeset 413 7dddc163489a
parent 411 106aaf6eadfe
child 417 c503e0c6a740
permissions -rw-r--r--
drawing the GameView works

#include "Engine.hh"
#include "SinglePlayer.hh"
#include "Network/Reactor.hh"
#include "Config.hh"

#include <iostream>

Engine::Engine (const std::string resource_xml_path) : 
    terrain(NULL), game_state(NULL), graphics(NULL), net_server(NULL), net_client_connect(NULL),
    is_running(true), resources(resource_xml_path)
{
    
}

GameState& Engine::setupGame (Terrain *terrain) {
    // ensure this isn't called in inappropriate ways
    assert(!net_server);

    // remember the terrain
    this->terrain = terrain;

    // create the GameState
    game_state = new GameState(*terrain);

    // put graphics into GameView mode
    // XXX: this state is a mess, this is done in three places in weird ways
    // this depends on graphics not yet being set in client mode, but not in singleplayer mode
    if (graphics)
        graphics->displayGameView(*game_state, game_state->getLocalPlayer());
   
    return *game_state;
}
        
void Engine::setupGame (const TerrainConfig &config) {
    // proxy off to setupGame(Terrain *)
    setupGame(new Terrain(config));
}

void Engine::setupGraphics (const graphics::DisplayConfig &config) {
    // create the graphics
    graphics = new graphics::Graphics(*this, resources, config);
}

void Engine::setupNetworkServer (const std::string &listen_port) {
    NetworkEndpoint listen_addr(listen_port);
    
    assert(terrain && game_state);
    
    // create the server
    net_server = new NetworkServer(*game_state, listen_addr);

    // put graphics into GameView mode
    if (graphics)
        graphics->displayGameView(*game_state, NULL);
 
}

void Engine::setupNetworkClient (const std::string &connect_host, const std::string &connect_port) {
    // connect_to
    NetworkEndpoint connect_addr(connect_host, connect_port);

    // begin connecting, the client will callback us with setupGame once it's connected
    net_client_connect = new NetworkClientConnect(*this, connect_addr);
}

void Engine::setupSinglePlayer (void) {
    assert(terrain && game_state);

    // create player directly
 	LocalPlayer* lp = new SinglePlayer(*game_state);

    // add to gamestate
	game_state->setLocalPlayer(lp);

    // put graphics into GameView mode
    if (graphics)
        graphics->displayGameView(*game_state, lp);
 
}

void Engine::stop (void) {
    is_running = false;
}

void Engine::run (void) {
    // our NetworkReactor
    NetworkReactor *reactor = NetworkReactor::current;

    // timeout info
    timeval timeout;

    while (is_running) {
        // this does.... magical things
        CL_System::keep_alive();
        
        // setup our timeout to ENGINE_TIMEOUT_MS
        timeout.tv_sec = 0;
        timeout.tv_usec = ENGINE_TIMEOUT_MS * 1000;
       
        /*
         * Thursday came and went, I re-wrote clan-event.
         *
         * We use the NetworkReactor for sleeping, as it handles it effeciently even if we're not using network.
         */
        reactor->poll(&timeout);
    }
}

CL_ResourceManager* Engine::getResourceManager (void) {
    return &resources;
}

Logger Engine::log (enum LogLevel level, const char *type) {
    return Logger(level <= WARN ? std::cerr : std::cout, level, type);
}