aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 0b65c0922872b0a8623368c48376be8ff720545f (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
/*
 * 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 <gl.h>
#include <glu.h>
#include <assert.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_1280,
	RES_1330,
	RES_1440,
	RES_1600
} 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;
	
	res = RES_800;
	width = 800;  height = 600;
	video_flags = SDL_OPENGL;
	
	while ((c = getopt(argc, argv, "hvr:f")) != -1) {
	
		switch ( c ) {
		
			case 'r' : {
				if ( strcasecmp(optarg,   "640") == 0 ) res = RES_640;
				if ( strcasecmp(optarg,   "800") == 0 ) res = RES_800;
				if ( strcasecmp(optarg,  "1280") == 0 ) res = RES_1280;
				if ( strcasecmp(optarg,  "1330") == 0 ) res = RES_1330;
				if ( strcasecmp(optarg,  "1440") == 0 ) res = RES_1440;
				if ( strcasecmp(optarg,  "1600") == 0 ) res = RES_1600;
				break;
			}
			
			case 'f' : {
				video_flags = video_flags | SDL_FULLSCREEN;
				break;
			}
			
			case 'v' : {
				main_version();
				return 0;
			}
						
			case 'h' : {
				printf("Syntax: %s [-h] [-r <res>] [-f]\n\n", argv[0]);
				printf("Post-apocalyptic Jet Set Willy, set in a 3D nuclear power station.\n\n");
				printf("  -h              Display this help message and exit.\n");
				printf("  -v              Display version number and exit.\n");
				printf("  -r <res>        Set display resolution.  See below for possible values for <res>.\n");
				printf("  -f              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(" 1280  -  1280 x 1024\n");
				printf(" 1330  -  1330 x  900 (widescreen)\n");
				printf(" 1440  -  1440 x  900 (widescreen)\n");
				printf(" 1600  -  1600 x 1050 (widescreen)\n");
				printf("\n");
				main_version();
				return 0;
			}
			
			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_1280  : { width = 1280;  height = 1024;  break; }
		case RES_1330  : { width = 1330;  height =  900;  break; }
		case RES_1440  : { width = 1440;  height =  900;  break; }
		case RES_1600  : { 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);
	
	/* 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_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->frame_delay_fiddled = 0;
		}
		
		/* Attempt to hold output FPS close to some target value */
		if ( (!game->frame_delay_fiddled) && (game->fps < 30) ) {
			game->frame_delay -= game->frame_delay/20;
			game->frame_delay_fiddled = 1;
		}
		if ( (!game->frame_delay_fiddled) && (game->fps > 50) ) {
			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_CloseAudio();
	SDL_Quit();
	
	printf("\n");
	
	return 0;

}