author | Tero Marttila <terom@fixme.fi> |
Wed, 21 Jan 2009 23:25:29 +0200 | |
branch | new_graphics |
changeset 413 | 7dddc163489a |
parent 411 | 106aaf6eadfe |
child 417 | c503e0c6a740 |
permissions | -rw-r--r-- |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
1 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
2 |
#include "Engine.hh" |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
3 |
#include "SinglePlayer.hh" |
380 | 4 |
#include "Network/Reactor.hh" |
5 |
#include "Config.hh" |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
6 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
7 |
#include <iostream> |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
8 |
|
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
218
diff
changeset
|
9 |
Engine::Engine (const std::string resource_xml_path) : |
411
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
10 |
terrain(NULL), game_state(NULL), graphics(NULL), net_server(NULL), net_client_connect(NULL), |
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
218
diff
changeset
|
11 |
is_running(true), resources(resource_xml_path) |
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
218
diff
changeset
|
12 |
{ |
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
218
diff
changeset
|
13 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
14 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
15 |
|
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
16 |
GameState& Engine::setupGame (Terrain *terrain) { |
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
17 |
// ensure this isn't called in inappropriate ways |
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
18 |
assert(!net_server); |
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
19 |
|
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
20 |
// remember the terrain |
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
21 |
this->terrain = terrain; |
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
22 |
|
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
23 |
// create the GameState |
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
24 |
game_state = new GameState(*terrain); |
411
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
25 |
|
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
26 |
// put graphics into GameView mode |
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
27 |
// XXX: this state is a mess, this is done in three places in weird ways |
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
28 |
// this depends on graphics not yet being set in client mode, but not in singleplayer mode |
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
29 |
if (graphics) |
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
30 |
graphics->displayGameView(*game_state, game_state->getLocalPlayer()); |
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
31 |
|
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
32 |
return *game_state; |
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
33 |
} |
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
34 |
|
409
1a03ff151abc
add --terrain-seed and --terrain-size arguments, plus bugfixes
Tero Marttila <terom@fixme.fi>
parents:
408
diff
changeset
|
35 |
void Engine::setupGame (const TerrainConfig &config) { |
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
36 |
// proxy off to setupGame(Terrain *) |
409
1a03ff151abc
add --terrain-seed and --terrain-size arguments, plus bugfixes
Tero Marttila <terom@fixme.fi>
parents:
408
diff
changeset
|
37 |
setupGame(new Terrain(config)); |
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
38 |
} |
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
39 |
|
411
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
40 |
void Engine::setupGraphics (const graphics::DisplayConfig &config) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
41 |
// create the graphics |
411
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
42 |
graphics = new graphics::Graphics(*this, resources, config); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
43 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
44 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
45 |
void Engine::setupNetworkServer (const std::string &listen_port) { |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
46 |
NetworkEndpoint listen_addr(listen_port); |
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
47 |
|
409
1a03ff151abc
add --terrain-seed and --terrain-size arguments, plus bugfixes
Tero Marttila <terom@fixme.fi>
parents:
408
diff
changeset
|
48 |
assert(terrain && game_state); |
1a03ff151abc
add --terrain-seed and --terrain-size arguments, plus bugfixes
Tero Marttila <terom@fixme.fi>
parents:
408
diff
changeset
|
49 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
50 |
// create the server |
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
51 |
net_server = new NetworkServer(*game_state, listen_addr); |
411
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
52 |
|
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
53 |
// put graphics into GameView mode |
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
54 |
if (graphics) |
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
55 |
graphics->displayGameView(*game_state, NULL); |
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
56 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
57 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
58 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
59 |
void Engine::setupNetworkClient (const std::string &connect_host, const std::string &connect_port) { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
60 |
// connect_to |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
61 |
NetworkEndpoint connect_addr(connect_host, connect_port); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
62 |
|
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
63 |
// begin connecting, the client will callback us with setupGame once it's connected |
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
64 |
net_client_connect = new NetworkClientConnect(*this, connect_addr); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
65 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
66 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
67 |
void Engine::setupSinglePlayer (void) { |
409
1a03ff151abc
add --terrain-seed and --terrain-size arguments, plus bugfixes
Tero Marttila <terom@fixme.fi>
parents:
408
diff
changeset
|
68 |
assert(terrain && game_state); |
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
69 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
70 |
// create player directly |
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
71 |
LocalPlayer* lp = new SinglePlayer(*game_state); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
72 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
73 |
// add to gamestate |
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
389
diff
changeset
|
74 |
game_state->setLocalPlayer(lp); |
411
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
75 |
|
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
76 |
// put graphics into GameView mode |
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
77 |
if (graphics) |
413 | 78 |
graphics->displayGameView(*game_state, lp); |
411
106aaf6eadfe
there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
79 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
80 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
81 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
82 |
void Engine::stop (void) { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
83 |
is_running = false; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
84 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
85 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
86 |
void Engine::run (void) { |
380 | 87 |
// our NetworkReactor |
88 |
NetworkReactor *reactor = NetworkReactor::current; |
|
89 |
||
90 |
// timeout info |
|
91 |
timeval timeout; |
|
92 |
||
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
93 |
while (is_running) { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
94 |
// this does.... magical things |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
95 |
CL_System::keep_alive(); |
380 | 96 |
|
97 |
// setup our timeout to ENGINE_TIMEOUT_MS |
|
98 |
timeout.tv_sec = 0; |
|
99 |
timeout.tv_usec = ENGINE_TIMEOUT_MS * 1000; |
|
100 |
||
101 |
/* |
|
102 |
* Thursday came and went, I re-wrote clan-event. |
|
103 |
* |
|
104 |
* We use the NetworkReactor for sleeping, as it handles it effeciently even if we're not using network. |
|
105 |
*/ |
|
106 |
reactor->poll(&timeout); |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
107 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
108 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
109 |
|
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
218
diff
changeset
|
110 |
CL_ResourceManager* Engine::getResourceManager (void) { |
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
218
diff
changeset
|
111 |
return &resources; |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
186
diff
changeset
|
112 |
} |
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
186
diff
changeset
|
113 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
114 |
Logger Engine::log (enum LogLevel level, const char *type) { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
115 |
return Logger(level <= WARN ? std::cerr : std::cout, level, type); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
116 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
117 |