equal
deleted
inserted
replaced
17 #endif |
17 #endif |
18 |
18 |
19 #include <iostream> |
19 #include <iostream> |
20 #include <cassert> |
20 #include <cassert> |
21 |
21 |
22 Engine::Engine (const std::string resource_xml_path) : |
22 /* |
|
23 * Initialize static Engine global state |
|
24 */ |
|
25 LogLevel Engine::log_level = DEBUG; |
|
26 |
|
27 Engine::Engine (const EngineConfig &config) : |
23 terrain(NULL), game_state(NULL), graphics(NULL), net_server(NULL), net_client_connect(NULL), |
28 terrain(NULL), game_state(NULL), graphics(NULL), net_server(NULL), net_client_connect(NULL), |
24 is_running(true), resources(resource_xml_path) |
29 is_running(true), resources(config.resource_path) |
25 { |
30 { |
26 |
31 // update global log_level |
|
32 Engine::log_level = config.log_level; |
27 } |
33 } |
28 |
34 |
29 GameState& Engine::setupGame (Terrain *terrain) { |
35 GameState& Engine::setupGame (Terrain *terrain) { |
30 // ensure this isn't called in inappropriate ways |
36 // ensure this isn't called in inappropriate ways |
31 assert(!net_server); |
37 assert(!net_server); |
79 |
85 |
80 } |
86 } |
81 |
87 |
82 void Engine::setupNetworkServer (const std::string &listen_port) { |
88 void Engine::setupNetworkServer (const std::string &listen_port) { |
83 |
89 |
84 #if NETWORK_EANBLED |
90 #if NETWORK_ENABLED |
85 NetworkEndpoint listen_addr(listen_port); |
91 NetworkEndpoint listen_addr(listen_port); |
86 |
92 |
87 assert(terrain && game_state); |
93 assert(terrain && game_state); |
88 |
94 |
89 // create the server |
95 // create the server |
101 #endif |
107 #endif |
102 } |
108 } |
103 |
109 |
104 void Engine::setupNetworkClient (const std::string &connect_host, const std::string &connect_port) { |
110 void Engine::setupNetworkClient (const std::string &connect_host, const std::string &connect_port) { |
105 |
111 |
106 #if NETWORK_EANBLED |
112 #if NETWORK_ENABLED |
107 // connect_to |
113 // connect_to |
108 NetworkEndpoint connect_addr(connect_host, connect_port); |
114 NetworkEndpoint connect_addr(connect_host, connect_port); |
109 |
115 |
110 // begin connecting, the client will callback us with setupGame once it's connected |
116 // begin connecting, the client will callback us with setupGame once it's connected |
111 net_client_connect = new NetworkClientConnect(*this, connect_addr); |
117 net_client_connect = new NetworkClientConnect(*this, connect_addr); |
138 void Engine::stop (void) { |
144 void Engine::stop (void) { |
139 is_running = false; |
145 is_running = false; |
140 } |
146 } |
141 |
147 |
142 void Engine::run (void) { |
148 void Engine::run (void) { |
|
149 // timeout for NetworkReactor |
|
150 timeval timeout; |
|
151 |
143 while (is_running) { |
152 while (is_running) { |
144 /* |
153 /* |
145 * Run internal ClanLib stuff (also includes our timers) until our timeout has elapsed |
154 * Run internal ClanLib stuff (also includes our timers) until our timeout has elapsed |
146 */ |
155 */ |
147 CL_System::keep_alive(ENGINE_TIMEOUT_MS); |
156 CL_System::keep_alive(ENGINE_TIMEOUT_MS); |
148 |
157 |
149 #if NETWORK_ENABLED |
158 #if NETWORK_ENABLED |
|
159 // setup timeout to zero |
|
160 timeout.tv_sec = 0; |
|
161 timeout.tv_usec = 0; |
|
162 |
150 /* |
163 /* |
151 * Thursday came and went, I re-wrote clan-event. |
164 * Thursday came and went, I re-wrote clan-event. |
152 * |
165 * |
153 * (actually, we only use it for zero-timeout polling now... not sure if this is better than using the above |
166 * (actually, we only use it for zero-timeout polling now... not sure if this is better than using the above |
154 * CL_System::keep_alive) |
167 * CL_System::keep_alive) |
155 */ |
168 */ |
156 NetworkReactor::current->poll(NULL); |
169 NetworkReactor::current->poll(&timeout); |
157 #endif |
170 #endif |
158 } |
171 } |
159 } |
172 } |
160 |
173 |
161 CL_ResourceManager* Engine::getResourceManager (void) { |
|
162 return &resources; |
|
163 } |
|
164 |
|
165 Logger Engine::log (enum LogLevel level, const char *type) { |
|
166 return Logger(level <= WARN ? std::cerr : std::cout, level, type); |
|
167 } |
|
168 |
|