[1850] | 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 | |
---|
[1855] | 20 | |
---|
| 21 | ### File Specific: |
---|
| 22 | main-programmer: Patrick Boenzli |
---|
[2068] | 23 | co-programmer: Christian Meyer |
---|
[1850] | 24 | */ |
---|
| 25 | |
---|
[2068] | 26 | #ifdef __WIN32__ |
---|
| 27 | #include <windows.h> |
---|
| 28 | #endif |
---|
| 29 | |
---|
[2058] | 30 | #include <iostream> |
---|
| 31 | #include <cstdio> |
---|
| 32 | #include <GL/glut.h> |
---|
| 33 | #include <SDL/SDL.h> |
---|
| 34 | |
---|
| 35 | #include "environment.h" |
---|
| 36 | #include "world.h" |
---|
| 37 | #include "input_output.h" |
---|
| 38 | #include "data_tank.h" |
---|
| 39 | #include "stdincl.h" |
---|
| 40 | #include "player.h" |
---|
| 41 | #include "npc.h" |
---|
| 42 | #include "shoot_laser.h" |
---|
| 43 | |
---|
[1803] | 44 | #include "orxonox.h" |
---|
| 45 | |
---|
| 46 | using namespace std; |
---|
| 47 | |
---|
| 48 | |
---|
[1872] | 49 | Orxonox::Orxonox () |
---|
| 50 | { |
---|
| 51 | pause = false; |
---|
| 52 | } |
---|
[1803] | 53 | |
---|
| 54 | |
---|
[1850] | 55 | |
---|
[1875] | 56 | Orxonox::~Orxonox () |
---|
[2068] | 57 | { |
---|
| 58 | Orxonox::singleton_ref = NULL; |
---|
| 59 | if( world != NULL) delete world; |
---|
| 60 | if( localinput != NULL) delete world; |
---|
| 61 | if( localcamera != NULL) delete camera; |
---|
| 62 | if( resources != NULL) delete resources; |
---|
| 63 | } |
---|
[1850] | 64 | |
---|
| 65 | |
---|
[2068] | 66 | /* this is a singleton class to prevent duplicates */ |
---|
[1850] | 67 | Orxonox* Orxonox::singleton_ref = 0; |
---|
[1872] | 68 | World* Orxonox::world = 0; |
---|
| 69 | bool Orxonox::pause = false; |
---|
| 70 | |
---|
[2058] | 71 | |
---|
[1850] | 72 | Orxonox* Orxonox::getInstance (void) |
---|
[1803] | 73 | { |
---|
[1850] | 74 | if (singleton_ref == NULL) |
---|
| 75 | singleton_ref = new Orxonox(); |
---|
| 76 | return singleton_ref; |
---|
| 77 | } |
---|
| 78 | |
---|
[2068] | 79 | void Orxonox::get_config_file (int argc, char** argv) |
---|
| 80 | { |
---|
| 81 | char* path; |
---|
| 82 | #ifdef __WIN32__ |
---|
| 83 | path = getenv(""); |
---|
| 84 | #else |
---|
| 85 | path = getenv("HOME"); |
---|
| 86 | #endif |
---|
| 87 | |
---|
| 88 | if( path != NULL) strcpy (configfilename, path); |
---|
[2080] | 89 | else strcpy (configfilename, "./"); |
---|
[2068] | 90 | strcat (configfilename, "/.orxonox.conf"); |
---|
| 91 | } |
---|
[1850] | 92 | |
---|
[2068] | 93 | int Orxonox::init (int argc, char** argv) |
---|
[1850] | 94 | { |
---|
[2068] | 95 | // parse command line |
---|
| 96 | // config file |
---|
| 97 | |
---|
| 98 | get_config_file (argc, argv); |
---|
| 99 | |
---|
| 100 | // initialize SDL |
---|
| 101 | printf("> Initializing SDL\n"); |
---|
[2058] | 102 | if( SDL_Init (SDL_INIT_EVERYTHING) == -1) |
---|
| 103 | { |
---|
| 104 | printf ("Could not SDL_Init(): %s\n", SDL_GetError()); |
---|
| 105 | return -1; |
---|
| 106 | } |
---|
| 107 | |
---|
[2068] | 108 | // initialize everything |
---|
| 109 | printf("> Initializing video\n"); |
---|
| 110 | if( init_video () == -1) return -1; |
---|
| 111 | printf("> Initializing sound\n"); |
---|
| 112 | if( init_sound () == -1) return -1; |
---|
| 113 | printf("> Initializing input\n"); |
---|
| 114 | if( init_input () == -1) return -1; |
---|
| 115 | printf("> Initializing networking\n"); |
---|
| 116 | if( init_networking () == -1) return -1; |
---|
| 117 | printf("> Initializing resources\n"); |
---|
| 118 | if( init_resources () == -1) return -1; |
---|
| 119 | printf("> Initializing world\n"); |
---|
| 120 | if( init_world () == -1) return -1; |
---|
| 121 | |
---|
| 122 | return 0; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | int Orxonox::init_video () |
---|
| 126 | { |
---|
[2058] | 127 | // Set video mode |
---|
| 128 | // TO DO: parse arguments for settings |
---|
| 129 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5); |
---|
| 130 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5); |
---|
| 131 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5); |
---|
| 132 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
---|
| 133 | |
---|
| 134 | int bpp = 16; |
---|
| 135 | int width = 640; |
---|
| 136 | int height = 480; |
---|
| 137 | Uint32 flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER; |
---|
| 138 | |
---|
| 139 | if( (screen = SDL_SetVideoMode (width, height, bpp, flags)) == NULL) |
---|
| 140 | { |
---|
| 141 | printf ("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", width, height, bpp, flags, SDL_GetError()); |
---|
| 142 | SDL_Quit(); |
---|
| 143 | return -1; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | // Set window labeling |
---|
| 147 | // TO DO: Add version information to caption |
---|
| 148 | SDL_WM_SetCaption( "Orxonox", "Orxonox"); |
---|
| 149 | |
---|
| 150 | // TO DO: Create a cool icon and use it here |
---|
| 151 | // SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask); |
---|
| 152 | |
---|
| 153 | // OpenGL stuff |
---|
| 154 | // (Is this all we initialize globally???) |
---|
[2068] | 155 | glClearColor(0.0, 0.0, 0.0, 0.0); |
---|
[1883] | 156 | glEnable(GL_DEPTH_TEST); |
---|
[1872] | 157 | glShadeModel(GL_FLAT); |
---|
[2058] | 158 | |
---|
[2068] | 159 | // create camera |
---|
| 160 | localcamera = new Camera(); |
---|
| 161 | |
---|
| 162 | return 0; |
---|
[1803] | 163 | } |
---|
| 164 | |
---|
[2068] | 165 | int Orxonox::init_sound () |
---|
[1803] | 166 | { |
---|
[2068] | 167 | printf("Not yet implemented\n"); |
---|
| 168 | return 0; |
---|
[1850] | 169 | } |
---|
[1849] | 170 | |
---|
[2068] | 171 | int Orxonox::init_input () |
---|
[1850] | 172 | { |
---|
[2068] | 173 | // create localinput |
---|
| 174 | localinput = new CommandNode( configfilename); |
---|
| 175 | |
---|
| 176 | return 0; |
---|
[1803] | 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
[2068] | 180 | int Orxonox::init_networking () |
---|
[1897] | 181 | { |
---|
[2068] | 182 | printf("Not yet implemented\n"); |
---|
| 183 | return 0; |
---|
[1897] | 184 | } |
---|
| 185 | |
---|
[2068] | 186 | int Orxonox::init_resources () |
---|
[1858] | 187 | { |
---|
[2068] | 188 | printf("Not yet implemented\n"); |
---|
| 189 | return 0; |
---|
[1858] | 190 | } |
---|
[1849] | 191 | |
---|
[2068] | 192 | int Orxonox::init_world () |
---|
[1896] | 193 | { |
---|
[2080] | 194 | world = new World(); |
---|
| 195 | |
---|
| 196 | // TO DO: replace this with a menu/intro |
---|
| 197 | world->load_debug_level(); |
---|
| 198 | |
---|
[2068] | 199 | return 0; |
---|
[1896] | 200 | } |
---|
| 201 | |
---|
[1875] | 202 | void Orxonox::quitGame() |
---|
| 203 | { |
---|
[2068] | 204 | bQuitOrxonox = true; |
---|
[1896] | 205 | //cout << "finished garbage colletion, quitting..." << endl; |
---|
[1875] | 206 | } |
---|
[2068] | 207 | void Orxonox::mainLoop() |
---|
| 208 | { |
---|
| 209 | // This is where everything is run |
---|
| 210 | while( !bQuitOrxonox) |
---|
| 211 | { |
---|
| 212 | // Network |
---|
| 213 | synchronize(); |
---|
| 214 | // Process input |
---|
| 215 | handle_input(); |
---|
| 216 | // Process time |
---|
| 217 | time_slice(); |
---|
| 218 | // Process collision |
---|
| 219 | collision(); |
---|
| 220 | // Draw |
---|
| 221 | display(); |
---|
[1875] | 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
[2068] | 225 | void Orxonox::event_handler (SDL_Event* event) |
---|
| 226 | { |
---|
| 227 | // Handle special events such as reshape, quit, focus changes |
---|
| 228 | } |
---|
[1875] | 229 | |
---|
[2068] | 230 | void Orxonox::synchronize () |
---|
[1859] | 231 | { |
---|
[2068] | 232 | // Get remote input |
---|
| 233 | // Update synchronizables |
---|
[1859] | 234 | } |
---|
| 235 | |
---|
[2068] | 236 | void Orxonox::handle_input () |
---|
| 237 | { |
---|
| 238 | // localinput |
---|
| 239 | localinput.process(); |
---|
| 240 | // remoteinput |
---|
| 241 | } |
---|
[1859] | 242 | |
---|
[2068] | 243 | void Orxonox::time_slice () |
---|
[1803] | 244 | { |
---|
[2068] | 245 | Uint32 curframe = SDL_GetTicks(); |
---|
| 246 | if( !pause) |
---|
| 247 | { |
---|
| 248 | world->time_slice (curframe - lastframe); |
---|
| 249 | world->update (); |
---|
| 250 | localcamera->time_slice (curframe - lastframe); |
---|
| 251 | } |
---|
| 252 | lastframe = curframe; |
---|
[2058] | 253 | } |
---|
[1803] | 254 | |
---|
[2068] | 255 | void Orxonox::collision () |
---|
[1872] | 256 | { |
---|
[2068] | 257 | world->collide (); |
---|
[1872] | 258 | } |
---|
[1850] | 259 | |
---|
[2068] | 260 | void Orxonox::display () |
---|
[1850] | 261 | { |
---|
[2068] | 262 | // clear buffer |
---|
| 263 | glClear( GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT); |
---|
| 264 | // set camera |
---|
| 265 | localcamera->apply (); |
---|
| 266 | // draw world |
---|
| 267 | world->draw (); |
---|
| 268 | // draw HUD |
---|
| 269 | // flip buffers |
---|
| 270 | SDL_Flip( screen); |
---|
[1803] | 271 | } |
---|
| 272 | |
---|
[2068] | 273 | Camera* Orxonox::get_camera () |
---|
[1872] | 274 | { |
---|
[2068] | 275 | return localcamera; |
---|
[1872] | 276 | } |
---|
[1850] | 277 | |
---|
[2068] | 278 | Camera* Orxonox::get_world () |
---|
[2058] | 279 | { |
---|
[2068] | 280 | return world; |
---|
[2058] | 281 | } |
---|
[1872] | 282 | |
---|
[1850] | 283 | int main (int argc, char** argv) |
---|
[1803] | 284 | { |
---|
[1850] | 285 | Orxonox *orx = Orxonox::getInstance(); |
---|
[2058] | 286 | |
---|
[2068] | 287 | if( (*orx).init(argc, argv) == -1) |
---|
[2058] | 288 | { |
---|
[2068] | 289 | printf("! Orxonox initialization failed\n"); |
---|
[2058] | 290 | return -1; |
---|
| 291 | } |
---|
[1856] | 292 | |
---|
[2068] | 293 | lastframe = SDL_GetTicks(); |
---|
| 294 | |
---|
[2058] | 295 | (*orx).mainLoop(); |
---|
| 296 | |
---|
[1803] | 297 | return 0; |
---|
[2068] | 298 | } |
---|