src/Engine.cc
author terom
Wed, 17 Dec 2008 00:40:22 +0000
changeset 381 9b35bc329d23
parent 380 d193dd1d8a7e
child 389 e74c1820fbd2
permissions -rw-r--r--
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint

#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) {
    NetworkEndpoint listen_addr(listen_port);

    // create the server
    net_server = new NetworkServer(game_state, listen_addr);
}

void Engine::setupNetworkClient (const std::string &connect_host, const std::string &connect_port) {
    // connect_to
    NetworkEndpoint 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);
}