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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/*
* 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_1280W,
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");
}
static void main_help(const char *n) {
printf("Syntax: %s [options]\n\n", n);
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(" -f, --fullscreen Use the full screen.\n");
printf(" -n, --no-music Don't play any background music.\n");
printf("\nAdvanced options:\n\n");
printf(" --disable-vbos Disable the use of vertex buffer objects.\n");
printf(" --disable-fbos Disable the use of framebuffer objects.\n");
printf(" --disable-shaders Disable the use of shaders.\n");
printf(" --audio-debug Print audio debugging messages to stdout.\n");
printf(" --game-debug Print game control debugging messages to stdout.\n");
printf(" --no-framerate-limit Do not wait between frames.\n");
printf(" --status-line Show game status on the terminal.\n");
printf("\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(" 1280W - 1280 x 768 (widescreen)\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();
}
int main_event_filter(const SDL_Event *event) {
if ( event->type == SDL_MOUSEMOTION ) return 0;
return 1;
}
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 = 0;
GameOptions gameopts;
Uint16 cx, cy;
double vyaw_scale, vang_scale;
gameopts.disable_vbos = 0;
gameopts.disable_fbos = 0;
gameopts.disable_shaders = 0;
gameopts.audio_debug = 0;
gameopts.game_debug = 0;
gameopts.no_music = 0;
gameopts.no_framerate_limit = 0;
gameopts.status_line = 0;
const struct option longopts[] = { {"fullscreen", 0, NULL, 'f'},
{"resolution", 1, NULL, 'r'},
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{"disable-vbos", 0, &gameopts.disable_vbos, 1},
{"disable-fbos", 0, &gameopts.disable_fbos, 1},
{"disable-shaders", 0, &gameopts.disable_shaders, 1},
{"audio-debug", 0, &gameopts.audio_debug, 1},
{"game-debug", 0, &gameopts.game_debug, 1},
{"no-framerate-limit", 0, &gameopts.no_framerate_limit, 1},
{"status-line", 0, &gameopts.status_line, 1},
{"no-music", 0, NULL, 'n'},
{0, 0, NULL, 0} };
res = RES_800;
width = 800; height = 600;
video_flags = SDL_OPENGL;
while ((c = getopt_long(argc, argv, "hvr:fn", 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, "1280W") == 0 ) res = RES_1280W;
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 'n' : {
gameopts.no_music = 1;
break;
}
case 'h' : {
main_help(argv[0]);
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_1280W : { width = 1280; 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);
//SDL_WM_GrabInput(SDL_GRAB_ON);
SDL_SetEventFilter(main_event_filter);
cx = width/2; cy = height/2;
SDL_WarpMouse(cx, cy);
vyaw_scale = (2*M_PI/width);
vang_scale = (2*M_PI/height);
/* World setup */
Game *game;
game = game_new(width, height, gameopts);
/* Main loop */
finished = 0;
while ( !finished ) {
int mx, my;
/* Timer advances only when game is not paused */
if ( !game->paused ) {
game->tlast = t;
t = SDL_GetTicks();
}
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;
if ( event.key.keysym.sym == SDLK_ESCAPE ) finished = 1;
if ( event.key.keysym.sym == SDLK_r ) SDL_WarpMouse(cx, cy);
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;
}
SDL_GetMouseState(&mx, &my);
game->view_yaw = -(mx-cx)*vyaw_scale;
game->view_angle = deg2rad(-20.0) + (my-cy)*vang_scale;
if ( game->view_angle > deg2rad(89.0) ) game->view_angle = deg2rad(89.0);
if ( game->view_angle < deg2rad(-89.0) ) game->view_angle = deg2rad(-89.0);
if ( !game->paused ) {
physics_step(game, t);
render_draw(game, t);
}
if ( gameopts.status_line ) {
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;
}
/* 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. */
if ( !gameopts.no_framerate_limit ) usleep(25000);
}
game_shutdown(game);
SDL_Quit();
if ( gameopts.status_line ) printf("\n");
return 0;
}
|