aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 76376553c8ede463b46d74b80ae35f161ec1910e (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
/*
 * 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 <unistd.h>
#include <SDL.h>
#include <gl.h>
#include <glu.h>

#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);
	
	width = 1680;//1024;
	height = 1050;//768;
	
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	screen = SDL_SetVideoMode(width, height, 16, SDL_OPENGL | SDL_FULLSCREEN);
	if (screen == NULL) {
		fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
		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;

}