aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 055051a2de8afadf33cb16189a103358416ca4cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * main.c
 *
 * Where it all begins
 *
 * (c) 2008 Thomas White <taw27@cam.ac.uk>
 *
 *  thrust3d - a silly game
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <SDL.h>
#include <assert.h>
#include <getopt.h>

#include "types.h"
#include "model.h"
#include "game.h"
#include "render.h"
#include "physics.h"
#include "utils.h"

typedef enum {
	RES_640,
	RES_800,
	RES_1024,
	RES_1280,
	RES_1330,
	RES_1440,
	RES_1680
} ScreenResolution;

static void main_version() {
	printf("Thrust3D version "PACKAGE_VERSION", (c) 2008 Thomas White <taw27@srcf.ucam.org>\n");
}

int main(int argc, char *argv[]) {

	SDL_Event event;
	int finished;
	SDL_Surface *screen;
	int width, height;
	int c;
	Uint32 video_flags;
	ScreenResolution res;
	Uint32 t;
	
	int disable_vbos = 0;
	int disable_fbos = 0;
	int disable_shaders = 0;
	const struct option longopts[] = {	{"fullscreen", 0, NULL, 'f'},
						{"resolution", 1, NULL, 'r'},
						{"help", 0, NULL, 'h'},
						{"version", 0, NULL, 'v'},
						{"disable-vbos", 0, &disable_vbos, 1},
						{"disable-fbos", 0, &disable_fbos, 1},
						{"disable-shaders", 0, &disable_shaders, 1},
						{0, 0, NULL, 0}				};

	
	res = RES_800;
	width = 800;  height = 600;
	video_flags = SDL_OPENGL;
	
	while ((c = getopt_long(argc, argv, "hvr:f", longopts, NULL)) != -1) {
	
		switch ( c ) {
		
			case 'r' : {
				     if ( strcasecmp(optarg,   "640") == 0 ) res = RES_640;
				else if ( strcasecmp(optarg,   "800") == 0 ) res = RES_800;
				else if ( strcasecmp(optarg,  "1024") == 0 ) res = RES_1024;
				else if ( strcasecmp(optarg,  "1280") == 0 ) res = RES_1280;
				else if ( strcasecmp(optarg,  "1330") == 0 ) res = RES_1330;
				else if ( strcasecmp(optarg,  "1440") == 0 ) res = RES_1440;
				else if ( strcasecmp(optarg,  "1680") == 0 ) res = RES_1680;
				else {
					fprintf(stderr, "Unrecognised resolution '%s'\n", optarg);
					exit(1);
				}
				break;
			}
			
			case 'f' : {
				video_flags = video_flags | SDL_FULLSCREEN;
				break;
			}
			
			case 'v' : {
				main_version();
				return 0;
			}
						
			case 'h' : {
				printf("Syntax: %s [options]\n\n", argv[0]);
				printf("Post-apocalyptic Jet Set Willy, set in a 3D nuclear power station.\n\n");
				printf("  -h,  --help               Display this help message and exit.\n");
				printf("  -v,  --version            Display version number and exit.\n");
				printf("  -r,  --resolution <res>   Set display resolution.  See below for possible values for <res>.\n");
				printf("       --disable-vbos       Disable the use of vertex buffer objects (advanced).\n");
				printf("       --disable-fbos       Disable the use of framebuffer objects (advanced).\n");
				printf("       --disable-shaders    Disable the use of shaders (advanced).\n");
				printf("  -f,  --fullscreen         Use the full screen.\n\n");
				printf("Allowable values for <res> are as follows:\n\n");
				printf("<res>    Width   Height\n");
				printf("  640  -   640 x  480\n");
				printf("  800  -   800 x  600 (this is the default)\n");
				printf(" 1024  -  1024 x  768\n");
				printf(" 1280  -  1280 x 1024\n");
				printf(" 1330  -  1330 x  900 (widescreen)\n");
				printf(" 1440  -  1440 x  900 (widescreen)\n");
				printf(" 1680  -  1680 x 1050 (widescreen)\n");
				printf("\n");
				main_version();
				return 0;
			}
			
			case 0 : {
				break;
			}
			
			default : {
				return 1;
			}
		
		}
	
	}
	
	/* SDL initial setup */
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr, "Couldn't initialise SDL: %s\n", SDL_GetError());
		return 1;
	}
	atexit(SDL_Quit);
	
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	switch ( res ) {
		case RES_640   : { width =  640;  height =  480;  break; }
		case RES_800   : { width =  800;  height =  600;  break; }
		case RES_1024  : { width = 1024;  height =  768;  break; }
		case RES_1280  : { width = 1280;  height = 1024;  break; }
		case RES_1330  : { width = 1330;  height =  900;  break; }
		case RES_1440  : { width = 1440;  height =  900;  break; }
		case RES_1680  : { width = 1680;  height = 1050;  break; }
		default : { assert(this_point_not_reached); break; }
	}
	screen = SDL_SetVideoMode(width, height, 16, video_flags);
	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, disable_vbos, disable_fbos, disable_shaders);
	
	/* Main loop */
	finished = 0;
	t = SDL_GetTicks();
	while ( !finished ) {
	
		/* Timer advances only when game is not paused */
		t = SDL_GetTicks();
		if ( game->paused ) {
			game->tlast = t;
		}
		
		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_w ) render_set_wireframe(1);
					if ( event.key.keysym.sym == SDLK_e ) render_set_wireframe(0);
				}
				if ( event.key.keysym.sym == SDLK_p ) game_pause(game);
				if ( event.key.keysym.sym == SDLK_q ) finished = 1;
				break;
			case SDL_KEYUP :
				/* Process key releases even when 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, t);
				break;
			case SDL_QUIT :
				finished = 1;
				break;
		}
		if ( !game->paused ) {
			physics_step(game, t);
			render_draw(game, t);
		}
		
		//printf("%+7.4f %+7.4f %+7.4f    %+5.1f deg   %+7.5f %+7.5f %+7.5f     %2i %2i %2i   %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->fps);
		//fflush(stdout);
		
		/* Calculate FPS every half a second */
		game->frames++;
		if ( t - game->t_fps > 500 ) {
			game->fps = (500*game->frames) / (t - game->t_fps);
			game->t_fps = t;
			game->frames = 0;
		}
		game->radiation = game->fps / 40.0;
		
		/* Sleep for a bit to avoid hogging the CPU.
		 * This is a fudge - ideally this delay would adapt so that the CPU is not hogged when the GPU is the
		 * limiting factor, and be zero when the CPU is limiting.  I don't know of a sensible way to tell which
		 * is the case.	 */
		usleep(25000);
		
	}
	
	render_shutdown(game->render);
	SDL_CloseAudio();
	SDL_Quit();
	
	//printf("\n");
	
	return 0;

}