src/Engine.cc
changeset 408 e6cfc44266af
parent 389 e74c1820fbd2
child 409 1a03ff151abc
equal deleted inserted replaced
407:443f6f7abcfb 408:e6cfc44266af
     5 #include "Config.hh"
     5 #include "Config.hh"
     6 
     6 
     7 #include <iostream>
     7 #include <iostream>
     8 
     8 
     9 Engine::Engine (const std::string resource_xml_path) : 
     9 Engine::Engine (const std::string resource_xml_path) : 
       
    10     terrain(NULL), game_state(NULL), graphics_config(NULL), graphics(NULL), net_server(NULL), net_client_connect(NULL),
    10     is_running(true), resources(resource_xml_path)
    11     is_running(true), resources(resource_xml_path)
    11 {
    12 {
    12     
    13     
    13 }
    14 }
    14 
    15 
    15 void Engine::setupGraphics (PixelCoordinate resolution, bool fullscreen) {
    16 GameState& Engine::setupGame (Terrain *terrain) {
       
    17     // ensure this isn't called in inappropriate ways
       
    18     assert(!net_server);
       
    19 
       
    20     // remember the terrain
       
    21     this->terrain = terrain;
       
    22 
       
    23     // create the GameState
       
    24     game_state = new GameState(*terrain);
       
    25     
       
    26     // start graphics?
       
    27     if (graphics_config)
       
    28         startGraphics();
       
    29     
       
    30     return *game_state;
       
    31 }
       
    32         
       
    33 GameState& Engine::setupGame (void) {
       
    34     // proxy off to setupGame(Terrain *)
       
    35     return setupGame(new Terrain(TERRAIN_WIDTH, TERRAIN_HEIGHT, TERRAIN_RANDOM_SEED));
       
    36 }
       
    37 
       
    38 void Engine::setupGraphics (const GraphicsConfiguration &config) {
       
    39     // store config
       
    40     graphics_config = &config;
       
    41     
       
    42     // start already?
       
    43     if (game_state)
       
    44         startGraphics();
       
    45 }
       
    46 
       
    47 void Engine::startGraphics (void) {
       
    48     // check state
       
    49     assert(game_state && graphics_config);
       
    50 
    16     // create the graphics
    51     // create the graphics
    17     graphics = new Graphics(*this, game_state, resolution, fullscreen);
    52     graphics = new Graphics(*this, *game_state, *graphics_config);
    18 }
    53 }
    19 
    54 
    20 void Engine::setupNetworkServer (const std::string &listen_port) {
    55 void Engine::setupNetworkServer (const std::string &listen_port) {
    21     NetworkEndpoint listen_addr(listen_port);
    56     NetworkEndpoint listen_addr(listen_port);
       
    57     
       
    58     // setup default game
       
    59     setupGame();
    22 
    60 
    23     // create the server
    61     // create the server
    24     net_server = new NetworkServer(game_state, listen_addr);
    62     net_server = new NetworkServer(*game_state, listen_addr);
    25 }
    63 }
    26 
    64 
    27 void Engine::setupNetworkClient (const std::string &connect_host, const std::string &connect_port) {
    65 void Engine::setupNetworkClient (const std::string &connect_host, const std::string &connect_port) {
    28     // connect_to
    66     // connect_to
    29     NetworkEndpoint connect_addr(connect_host, connect_port);
    67     NetworkEndpoint connect_addr(connect_host, connect_port);
    30 
    68 
    31     // create the client
    69     // begin connecting, the client will callback us with setupGame once it's connected
    32     net_client = new NetworkClient(*this, game_state, connect_addr);
    70     net_client_connect = new NetworkClientConnect(*this, connect_addr);
    33 }
    71 }
    34 
    72 
    35 void Engine::setupSinglePlayer (void) {
    73 void Engine::setupSinglePlayer (void) {
       
    74     // setup default game
       
    75     setupGame();
       
    76 
    36     // create player directly
    77     // create player directly
    37  	LocalPlayer* lp = new SinglePlayer(game_state);
    78  	LocalPlayer* lp = new SinglePlayer(*game_state);
    38 
    79 
    39     // add to gamestate
    80     // add to gamestate
    40 	game_state.setLocalPlayer(lp);
    81 	game_state->setLocalPlayer(lp);
    41 }
    82 }
    42 
    83 
    43 void Engine::stop (void) {
    84 void Engine::stop (void) {
    44     is_running = false;
    85     is_running = false;
    45 }
    86 }