1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | This program is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with this program; if not, write to the Free Software Foundation, |
---|
18 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | |
---|
20 | |
---|
21 | ### File Specific: |
---|
22 | main-programmer: Patrick Boenzli |
---|
23 | co-programmer: Christian Meyer |
---|
24 | */ |
---|
25 | |
---|
26 | #include "orxonox.h" |
---|
27 | #include "world.h" |
---|
28 | #include "camera.h" |
---|
29 | #include "data_tank.h" |
---|
30 | #include "command_node.h" |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | Orxonox::Orxonox () |
---|
35 | { |
---|
36 | pause = false; |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | Orxonox::~Orxonox () |
---|
42 | { |
---|
43 | Orxonox::singleton_ref = NULL; |
---|
44 | if( world != NULL) delete world; |
---|
45 | if( localinput != NULL) delete world; |
---|
46 | if( localcamera != NULL) delete localcamera; |
---|
47 | if( resources != NULL) delete resources; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | /* this is a singleton class to prevent duplicates */ |
---|
52 | Orxonox* Orxonox::singleton_ref = 0; |
---|
53 | World* Orxonox::world = 0; |
---|
54 | bool Orxonox::pause = false; |
---|
55 | |
---|
56 | |
---|
57 | Orxonox* Orxonox::getInstance (void) |
---|
58 | { |
---|
59 | if (singleton_ref == NULL) |
---|
60 | singleton_ref = new Orxonox(); |
---|
61 | return singleton_ref; |
---|
62 | } |
---|
63 | |
---|
64 | void Orxonox::get_config_file (int argc, char** argv) |
---|
65 | { |
---|
66 | /* char* path; |
---|
67 | #ifdef __WIN32__ |
---|
68 | path = getenv(""); |
---|
69 | #else |
---|
70 | path = getenv("HOME"); |
---|
71 | #endif |
---|
72 | |
---|
73 | if( path != NULL) strcpy (configfilename, path); |
---|
74 | else strcpy (configfilename, "./"); |
---|
75 | strcat (configfilename, "/.orxonox.conf");*/ |
---|
76 | |
---|
77 | strcpy (configfilename, "./orxonox.conf"); |
---|
78 | } |
---|
79 | |
---|
80 | int Orxonox::init (int argc, char** argv) |
---|
81 | { |
---|
82 | // parse command line |
---|
83 | // config file |
---|
84 | |
---|
85 | get_config_file (argc, argv); |
---|
86 | |
---|
87 | // initialize SDL |
---|
88 | printf("> Initializing SDL\n"); |
---|
89 | if( SDL_Init (SDL_INIT_EVERYTHING) == -1) |
---|
90 | { |
---|
91 | printf ("Could not SDL_Init(): %s\n", SDL_GetError()); |
---|
92 | return -1; |
---|
93 | } |
---|
94 | |
---|
95 | // initialize everything |
---|
96 | printf("> Initializing video\n"); |
---|
97 | if( init_video () == -1) return -1; |
---|
98 | printf("> Initializing sound\n"); |
---|
99 | if( init_sound () == -1) return -1; |
---|
100 | printf("> Initializing input\n"); |
---|
101 | if( init_input () == -1) return -1; |
---|
102 | printf("> Initializing networking\n"); |
---|
103 | if( init_networking () == -1) return -1; |
---|
104 | printf("> Initializing resources\n"); |
---|
105 | if( init_resources () == -1) return -1; |
---|
106 | printf("> Initializing world\n"); |
---|
107 | if( init_world () == -1) return -1; |
---|
108 | |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | int Orxonox::init_video () |
---|
113 | { |
---|
114 | // Set video mode |
---|
115 | // TO DO: parse arguments for settings |
---|
116 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5); |
---|
117 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5); |
---|
118 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5); |
---|
119 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
---|
120 | |
---|
121 | int bpp = 16; |
---|
122 | int width = 640; |
---|
123 | int height = 480; |
---|
124 | Uint32 flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER; |
---|
125 | |
---|
126 | if( (screen = SDL_SetVideoMode (width, height, bpp, flags)) == NULL) |
---|
127 | { |
---|
128 | printf ("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", width, height, bpp, flags, SDL_GetError()); |
---|
129 | SDL_Quit(); |
---|
130 | return -1; |
---|
131 | } |
---|
132 | |
---|
133 | // Set window labeling |
---|
134 | // TO DO: Add version information to caption |
---|
135 | SDL_WM_SetCaption( "Orxonox", "Orxonox"); |
---|
136 | |
---|
137 | // TO DO: Create a cool icon and use it here |
---|
138 | // SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask); |
---|
139 | |
---|
140 | // OpenGL stuff |
---|
141 | // (Is this all we initialize globally???) |
---|
142 | glClearColor(0.0, 0.0, 0.0, 0.0); |
---|
143 | glEnable(GL_DEPTH_TEST); |
---|
144 | glShadeModel(GL_FLAT); |
---|
145 | |
---|
146 | // create camera |
---|
147 | localcamera = new Camera(); |
---|
148 | |
---|
149 | return 0; |
---|
150 | } |
---|
151 | |
---|
152 | int Orxonox::init_sound () |
---|
153 | { |
---|
154 | printf("Not yet implemented\n"); |
---|
155 | return 0; |
---|
156 | } |
---|
157 | |
---|
158 | int Orxonox::init_input () |
---|
159 | { |
---|
160 | // create localinput |
---|
161 | localinput = new CommandNode( configfilename); |
---|
162 | |
---|
163 | return 0; |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | int Orxonox::init_networking () |
---|
168 | { |
---|
169 | printf("Not yet implemented\n"); |
---|
170 | return 0; |
---|
171 | } |
---|
172 | |
---|
173 | int Orxonox::init_resources () |
---|
174 | { |
---|
175 | printf("Not yet implemented\n"); |
---|
176 | return 0; |
---|
177 | } |
---|
178 | |
---|
179 | int Orxonox::init_world () |
---|
180 | { |
---|
181 | world = new World(); |
---|
182 | |
---|
183 | // TO DO: replace this with a menu/intro |
---|
184 | world->load_debug_level(); |
---|
185 | |
---|
186 | return 0; |
---|
187 | } |
---|
188 | |
---|
189 | void Orxonox::quitGame() |
---|
190 | { |
---|
191 | bQuitOrxonox = true; |
---|
192 | //cout << "finished garbage colletion, quitting..." << endl; |
---|
193 | } |
---|
194 | void Orxonox::mainLoop() |
---|
195 | { |
---|
196 | lastframe = SDL_GetTicks(); |
---|
197 | |
---|
198 | // This is where everything is run |
---|
199 | while( !bQuitOrxonox) |
---|
200 | { |
---|
201 | // Network |
---|
202 | synchronize(); |
---|
203 | // Process input |
---|
204 | handle_input(); |
---|
205 | // Process time |
---|
206 | time_slice(); |
---|
207 | // Process collision |
---|
208 | collision(); |
---|
209 | // Draw |
---|
210 | display(); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | void Orxonox::event_handler (SDL_Event* event) |
---|
215 | { |
---|
216 | // Handle special events such as reshape, quit, focus changes |
---|
217 | } |
---|
218 | |
---|
219 | void Orxonox::synchronize () |
---|
220 | { |
---|
221 | // Get remote input |
---|
222 | // Update synchronizables |
---|
223 | } |
---|
224 | |
---|
225 | void Orxonox::handle_input () |
---|
226 | { |
---|
227 | // localinput |
---|
228 | localinput->process(); |
---|
229 | // remoteinput |
---|
230 | } |
---|
231 | |
---|
232 | void Orxonox::time_slice () |
---|
233 | { |
---|
234 | Uint32 curframe = SDL_GetTicks(); |
---|
235 | if( !pause) |
---|
236 | { |
---|
237 | world->time_slice (curframe - lastframe); |
---|
238 | world->update (); |
---|
239 | localcamera->time_slice (curframe - lastframe); |
---|
240 | } |
---|
241 | lastframe = curframe; |
---|
242 | } |
---|
243 | |
---|
244 | void Orxonox::collision () |
---|
245 | { |
---|
246 | world->collide (); |
---|
247 | } |
---|
248 | |
---|
249 | void Orxonox::display () |
---|
250 | { |
---|
251 | // clear buffer |
---|
252 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
253 | // set camera |
---|
254 | localcamera->apply (); |
---|
255 | // draw world |
---|
256 | world->draw (); |
---|
257 | // draw HUD |
---|
258 | // flip buffers |
---|
259 | SDL_Flip( screen); |
---|
260 | } |
---|
261 | |
---|
262 | Camera* Orxonox::get_camera () |
---|
263 | { |
---|
264 | return localcamera; |
---|
265 | } |
---|
266 | |
---|
267 | CommandNode* Orxonox::get_localinput () |
---|
268 | { |
---|
269 | return localinput; |
---|
270 | } |
---|
271 | |
---|
272 | World* Orxonox::get_world () |
---|
273 | { |
---|
274 | return world; |
---|
275 | } |
---|
276 | |
---|
277 | int main (int argc, char** argv) |
---|
278 | { |
---|
279 | Orxonox *orx = Orxonox::getInstance(); |
---|
280 | |
---|
281 | if( (*orx).init(argc, argv) == -1) |
---|
282 | { |
---|
283 | printf("! Orxonox initialization failed\n"); |
---|
284 | return -1; |
---|
285 | } |
---|
286 | |
---|
287 | (*orx).mainLoop(); |
---|
288 | |
---|
289 | return 0; |
---|
290 | } |
---|