src/Engine.cc
author terom
Tue, 16 Dec 2008 23:21:26 +0000
changeset 380 d193dd1d8a7e
parent 378 5589abf5e61b
child 381 9b35bc329d23
permissions -rw-r--r--
new NetworkReactor, fix everything to actually work

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

#include <iostream>

Engine::Engine (const std::string resource_xml_path) : 
    is_running(true), resources(resource_xml_path)
{
    
}

void Engine::setupGraphics (void) {
    // create the graphics
    graphics = new Graphics(*this, game_state);
}

void Engine::setupNetworkServer (const std::string &listen_port) {
    // create the server
    net_server = new NetworkServer(game_state, listen_port);
}

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

    // create the client
    net_client = new NetworkClient(*this, game_state, connect_addr);
}

void Engine::setupSinglePlayer (void) {
    // create player directly
 	LocalPlayer* lp = new SinglePlayer(game_state);

    // add to gamestate
	game_state.setLocalPlayer(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);
}