/* * main.c * * Where it all begins * * (c) 2008 Thomas White * * thrust3d - a silly game * */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "types.h" #include "model.h" #include "game.h" #include "render.h" #include "physics.h" int main(int argc, char *argv[]) { SDL_Event event; int finished; SDL_Surface *screen; int width, height; /* SDL initial setup */ if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0 ) { fprintf(stderr, "Couldn't initialise SDL: %s\n", SDL_GetError()); return 1; } atexit(SDL_Quit); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); width = 1680; height = 1050; screen = SDL_SetVideoMode(width, height, 16, SDL_OPENGL | SDL_FULLSCREEN); if ( screen == NULL ) { width = 1280; height = 1024; screen = SDL_SetVideoMode(width, height, 16, SDL_OPENGL | SDL_FULLSCREEN); if (screen == NULL) { fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError()); SDL_Quit(); return 1; } } SDL_WM_SetCaption("Thrust3D", "Thrust3D"); SDL_ShowCursor(SDL_DISABLE); /* World setup */ Game *game; game = game_new(width, height); /* Main loop */ finished = 0; while ( !finished ) { SDL_PollEvent(&event); switch ( event.type ) { case SDL_KEYDOWN : if ( !game->paused ) { if ( event.key.keysym.sym == SDLK_SPACE ) game->thrusting = 1; if ( event.key.keysym.sym == SDLK_LEFT ) game->turn_left = 1; if ( event.key.keysym.sym == SDLK_RIGHT ) game->turn_right = 1; if ( event.key.keysym.sym == SDLK_UP ) game->forward = 1; if ( event.key.keysym.sym == SDLK_DOWN ) game->reverse = 1; } if ( event.key.keysym.sym == SDLK_p ) game_pause(game); if ( event.key.keysym.sym == SDLK_q ) finished = 1; break; case SDL_KEYUP : if ( !game->paused ) { if ( event.key.keysym.sym == SDLK_SPACE ) game->thrusting = 0; if ( event.key.keysym.sym == SDLK_LEFT ) game->turn_left = 0; if ( event.key.keysym.sym == SDLK_RIGHT ) game->turn_right = 0; if ( event.key.keysym.sym == SDLK_UP ) game->forward = 0; if ( event.key.keysym.sym == SDLK_DOWN ) game->reverse = 0; } if ( event.key.keysym.sym == SDLK_p ) game->pause_rel = 1; break; case SDL_VIDEOEXPOSE : /* Don't bother redrawing if not paused - not long to wait! */ if ( game->paused ) render_draw(game); break; case SDL_QUIT : finished = 1; break; } if ( !game->paused ) { physics_step(game); render_draw(game); } /* Sleep for around 8ms to avoid hogging the CPU. This value gives about 100fps on my (rather modest) machine. */ usleep(8000); } render_shutdown(game->render); SDL_Quit(); printf("\n"); return 0; }