src/Engine.cc
branchnew_graphics
changeset 411 106aaf6eadfe
parent 409 1a03ff151abc
child 413 7dddc163489a
equal deleted inserted replaced
410:41fd46cffc52 411:106aaf6eadfe
     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     terrain(NULL), game_state(NULL), graphics(NULL), net_server(NULL), net_client_connect(NULL),
    11     is_running(true), resources(resource_xml_path)
    11     is_running(true), resources(resource_xml_path)
    12 {
    12 {
    13     
    13     
    14 }
    14 }
    15 
    15 
    20     // remember the terrain
    20     // remember the terrain
    21     this->terrain = terrain;
    21     this->terrain = terrain;
    22 
    22 
    23     // create the GameState
    23     // create the GameState
    24     game_state = new GameState(*terrain);
    24     game_state = new GameState(*terrain);
    25     
    25 
    26     // start graphics?
    26     // put graphics into GameView mode
    27     if (graphics_config)
    27     // XXX: this state is a mess, this is done in three places in weird ways
    28         startGraphics();
    28     // this depends on graphics not yet being set in client mode, but not in singleplayer mode
    29     
    29     if (graphics)
       
    30         graphics->displayGameView(*game_state, game_state->getLocalPlayer());
       
    31    
    30     return *game_state;
    32     return *game_state;
    31 }
    33 }
    32         
    34         
    33 void Engine::setupGame (const TerrainConfig &config) {
    35 void Engine::setupGame (const TerrainConfig &config) {
    34     // proxy off to setupGame(Terrain *)
    36     // proxy off to setupGame(Terrain *)
    35     setupGame(new Terrain(config));
    37     setupGame(new Terrain(config));
    36 }
    38 }
    37 
    39 
    38 void Engine::setupGraphics (const GraphicsConfig &config) {
    40 void Engine::setupGraphics (const graphics::DisplayConfig &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 
       
    51     // create the graphics
    41     // create the graphics
    52     graphics = new Graphics(*this, *game_state, *graphics_config);
    42     graphics = new graphics::Graphics(*this, resources, config);
    53 }
    43 }
    54 
    44 
    55 void Engine::setupNetworkServer (const std::string &listen_port) {
    45 void Engine::setupNetworkServer (const std::string &listen_port) {
    56     NetworkEndpoint listen_addr(listen_port);
    46     NetworkEndpoint listen_addr(listen_port);
    57     
    47     
    58     assert(terrain && game_state);
    48     assert(terrain && game_state);
    59     
    49     
    60     // create the server
    50     // create the server
    61     net_server = new NetworkServer(*game_state, listen_addr);
    51     net_server = new NetworkServer(*game_state, listen_addr);
       
    52 
       
    53     // put graphics into GameView mode
       
    54     if (graphics)
       
    55         graphics->displayGameView(*game_state, NULL);
       
    56  
    62 }
    57 }
    63 
    58 
    64 void Engine::setupNetworkClient (const std::string &connect_host, const std::string &connect_port) {
    59 void Engine::setupNetworkClient (const std::string &connect_host, const std::string &connect_port) {
    65     // connect_to
    60     // connect_to
    66     NetworkEndpoint connect_addr(connect_host, connect_port);
    61     NetworkEndpoint connect_addr(connect_host, connect_port);
    75     // create player directly
    70     // create player directly
    76  	LocalPlayer* lp = new SinglePlayer(*game_state);
    71  	LocalPlayer* lp = new SinglePlayer(*game_state);
    77 
    72 
    78     // add to gamestate
    73     // add to gamestate
    79 	game_state->setLocalPlayer(lp);
    74 	game_state->setLocalPlayer(lp);
       
    75 
       
    76     // put graphics into GameView mode
       
    77     if (graphics)
       
    78         graphics->displayGameView(*game_state, game_state->getLocalPlayer());
       
    79  
    80 }
    80 }
    81 
    81 
    82 void Engine::stop (void) {
    82 void Engine::stop (void) {
    83     is_running = false;
    83     is_running = false;
    84 }
    84 }