/* * 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" #include "utils.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; width=640;height=480; 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); } printf("%+7.4f %+7.4f %+7.4f %+5.1f deg %+7.5f %+7.5f %+7.5f %2i %2i %2i %3i %3i fps\r", game->lander->x, game->lander->y, game->lander->z, rad2deg(game->lander->yaw), game->lander->vx, game->lander->vy, game->lander->vz, game->cur_room_x, game->cur_room_y, game->cur_room_z, game->num_rooms, game->render->fps); fflush(stdout); /* Attempt to hold output FPS close to 100 */ if ( (!game->frame_delay_fiddled) && (game->render->fps < 90) ) { game->frame_delay -= game->frame_delay/20; game->frame_delay_fiddled = 1; } if ( (!game->frame_delay_fiddled) && (game->render->fps > 110) ) { game->frame_delay += game->frame_delay/20; game->frame_delay_fiddled = 1; } /* Sleep for a bit to avoid hogging the CPU. */ usleep(game->frame_delay); } render_shutdown(game->render); SDL_Quit(); printf("\n"); return 0; }