[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 | |
---|
[2101] | 26 | #include "orxonox.h" |
---|
[2058] | 27 | #include "world.h" |
---|
[2101] | 28 | #include "camera.h" |
---|
[2058] | 29 | #include "data_tank.h" |
---|
[2101] | 30 | #include "command_node.h" |
---|
[2058] | 31 | |
---|
[1803] | 32 | using namespace std; |
---|
| 33 | |
---|
[1872] | 34 | Orxonox::Orxonox () |
---|
| 35 | { |
---|
| 36 | pause = false; |
---|
| 37 | } |
---|
[1803] | 38 | |
---|
| 39 | |
---|
[1850] | 40 | |
---|
[1875] | 41 | Orxonox::~Orxonox () |
---|
[2068] | 42 | { |
---|
| 43 | Orxonox::singleton_ref = NULL; |
---|
| 44 | if( world != NULL) delete world; |
---|
| 45 | if( localinput != NULL) delete world; |
---|
[2101] | 46 | if( localcamera != NULL) delete localcamera; |
---|
[2068] | 47 | if( resources != NULL) delete resources; |
---|
| 48 | } |
---|
[1850] | 49 | |
---|
| 50 | |
---|
[2068] | 51 | /* this is a singleton class to prevent duplicates */ |
---|
[1850] | 52 | Orxonox* Orxonox::singleton_ref = 0; |
---|
[1872] | 53 | World* Orxonox::world = 0; |
---|
| 54 | bool Orxonox::pause = false; |
---|
| 55 | |
---|
[2058] | 56 | |
---|
[1850] | 57 | Orxonox* Orxonox::getInstance (void) |
---|
[1803] | 58 | { |
---|
[1850] | 59 | if (singleton_ref == NULL) |
---|
| 60 | singleton_ref = new Orxonox(); |
---|
| 61 | return singleton_ref; |
---|
| 62 | } |
---|
| 63 | |
---|
[2068] | 64 | void Orxonox::get_config_file (int argc, char** argv) |
---|
| 65 | { |
---|
[2101] | 66 | /* char* path; |
---|
[2068] | 67 | #ifdef __WIN32__ |
---|
| 68 | path = getenv(""); |
---|
| 69 | #else |
---|
| 70 | path = getenv("HOME"); |
---|
| 71 | #endif |
---|
| 72 | |
---|
| 73 | if( path != NULL) strcpy (configfilename, path); |
---|
[2080] | 74 | else strcpy (configfilename, "./"); |
---|
[2101] | 75 | strcat (configfilename, "/.orxonox.conf");*/ |
---|
| 76 | |
---|
| 77 | strcpy (configfilename, "./orxonox.conf"); |
---|
[2068] | 78 | } |
---|
[1850] | 79 | |
---|
[2068] | 80 | int Orxonox::init (int argc, char** argv) |
---|
[1850] | 81 | { |
---|
[2068] | 82 | // parse command line |
---|
| 83 | // config file |
---|
| 84 | |
---|
| 85 | get_config_file (argc, argv); |
---|
| 86 | |
---|
| 87 | // initialize SDL |
---|
| 88 | printf("> Initializing SDL\n"); |
---|
[2058] | 89 | if( SDL_Init (SDL_INIT_EVERYTHING) == -1) |
---|
| 90 | { |
---|
| 91 | printf ("Could not SDL_Init(): %s\n", SDL_GetError()); |
---|
| 92 | return -1; |
---|
| 93 | } |
---|
| 94 | |
---|
[2068] | 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 | { |
---|
[2058] | 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???) |
---|
[2068] | 142 | glClearColor(0.0, 0.0, 0.0, 0.0); |
---|
[1883] | 143 | glEnable(GL_DEPTH_TEST); |
---|
[1872] | 144 | glShadeModel(GL_FLAT); |
---|
[2058] | 145 | |
---|
[2068] | 146 | // create camera |
---|
| 147 | localcamera = new Camera(); |
---|
| 148 | |
---|
| 149 | return 0; |
---|
[1803] | 150 | } |
---|
| 151 | |
---|
[2068] | 152 | int Orxonox::init_sound () |
---|
[1803] | 153 | { |
---|
[2068] | 154 | printf("Not yet implemented\n"); |
---|
| 155 | return 0; |
---|
[1850] | 156 | } |
---|
[1849] | 157 | |
---|
[2068] | 158 | int Orxonox::init_input () |
---|
[1850] | 159 | { |
---|
[2068] | 160 | // create localinput |
---|
| 161 | localinput = new CommandNode( configfilename); |
---|
| 162 | |
---|
| 163 | return 0; |
---|
[1803] | 164 | } |
---|
| 165 | |
---|
| 166 | |
---|
[2068] | 167 | int Orxonox::init_networking () |
---|
[1897] | 168 | { |
---|
[2068] | 169 | printf("Not yet implemented\n"); |
---|
| 170 | return 0; |
---|
[1897] | 171 | } |
---|
| 172 | |
---|
[2068] | 173 | int Orxonox::init_resources () |
---|
[1858] | 174 | { |
---|
[2068] | 175 | printf("Not yet implemented\n"); |
---|
| 176 | return 0; |
---|
[1858] | 177 | } |
---|
[1849] | 178 | |
---|
[2068] | 179 | int Orxonox::init_world () |
---|
[1896] | 180 | { |
---|
[2080] | 181 | world = new World(); |
---|
| 182 | |
---|
| 183 | // TO DO: replace this with a menu/intro |
---|
| 184 | world->load_debug_level(); |
---|
| 185 | |
---|
[2068] | 186 | return 0; |
---|
[1896] | 187 | } |
---|
| 188 | |
---|
[1875] | 189 | void Orxonox::quitGame() |
---|
| 190 | { |
---|
[2068] | 191 | bQuitOrxonox = true; |
---|
[1896] | 192 | //cout << "finished garbage colletion, quitting..." << endl; |
---|
[1875] | 193 | } |
---|
[2068] | 194 | void Orxonox::mainLoop() |
---|
| 195 | { |
---|
[2101] | 196 | lastframe = SDL_GetTicks(); |
---|
| 197 | |
---|
[2068] | 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(); |
---|
[1875] | 211 | } |
---|
| 212 | } |
---|
| 213 | |
---|
[2068] | 214 | void Orxonox::event_handler (SDL_Event* event) |
---|
| 215 | { |
---|
| 216 | // Handle special events such as reshape, quit, focus changes |
---|
| 217 | } |
---|
[1875] | 218 | |
---|
[2068] | 219 | void Orxonox::synchronize () |
---|
[1859] | 220 | { |
---|
[2068] | 221 | // Get remote input |
---|
| 222 | // Update synchronizables |
---|
[1859] | 223 | } |
---|
| 224 | |
---|
[2068] | 225 | void Orxonox::handle_input () |
---|
| 226 | { |
---|
| 227 | // localinput |
---|
[2101] | 228 | localinput->process(); |
---|
[2068] | 229 | // remoteinput |
---|
| 230 | } |
---|
[1859] | 231 | |
---|
[2068] | 232 | void Orxonox::time_slice () |
---|
[1803] | 233 | { |
---|
[2068] | 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; |
---|
[2058] | 242 | } |
---|
[1803] | 243 | |
---|
[2068] | 244 | void Orxonox::collision () |
---|
[1872] | 245 | { |
---|
[2068] | 246 | world->collide (); |
---|
[1872] | 247 | } |
---|
[1850] | 248 | |
---|
[2068] | 249 | void Orxonox::display () |
---|
[1850] | 250 | { |
---|
[2068] | 251 | // clear buffer |
---|
[2101] | 252 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
[2068] | 253 | // set camera |
---|
| 254 | localcamera->apply (); |
---|
| 255 | // draw world |
---|
| 256 | world->draw (); |
---|
| 257 | // draw HUD |
---|
| 258 | // flip buffers |
---|
| 259 | SDL_Flip( screen); |
---|
[1803] | 260 | } |
---|
| 261 | |
---|
[2068] | 262 | Camera* Orxonox::get_camera () |
---|
[1872] | 263 | { |
---|
[2068] | 264 | return localcamera; |
---|
[1872] | 265 | } |
---|
[1850] | 266 | |
---|
[2101] | 267 | CommandNode* Orxonox::get_localinput () |
---|
[2096] | 268 | { |
---|
| 269 | return localinput; |
---|
| 270 | } |
---|
| 271 | |
---|
[2101] | 272 | World* Orxonox::get_world () |
---|
[2058] | 273 | { |
---|
[2068] | 274 | return world; |
---|
[2058] | 275 | } |
---|
[1872] | 276 | |
---|
[1850] | 277 | int main (int argc, char** argv) |
---|
[1803] | 278 | { |
---|
[1850] | 279 | Orxonox *orx = Orxonox::getInstance(); |
---|
[2058] | 280 | |
---|
[2068] | 281 | if( (*orx).init(argc, argv) == -1) |
---|
[2058] | 282 | { |
---|
[2068] | 283 | printf("! Orxonox initialization failed\n"); |
---|
[2058] | 284 | return -1; |
---|
| 285 | } |
---|
[2068] | 286 | |
---|
[2058] | 287 | (*orx).mainLoop(); |
---|
| 288 | |
---|
[1803] | 289 | return 0; |
---|
[2101] | 290 | } |
---|