This repository has been archived by the owner on Jul 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.cpp
251 lines (220 loc) · 6.49 KB
/
main.cpp
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
#include "main.h"
float you_angle = 0.0f;
float you_x = 0;
float you_z = 0;
float you_vel_angle = 0.0f;
float you_vel_x = 0.0f;
float you_vel_z = 0.0f;
float you_velocity = 10;
float you_turn = 0;
float you_dir = 0;
float piover180 = 0.01745329251994329577;
bool light = true;
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };
static GLint T0 = 0;
static GLint Frames = 0;
GLfloat fps = 500;
bool debug = false;
void quit_app( int code ) {
SDL_Quit( );
exit( code );
}
void handle_keydown( SDL_keysym* keysym ) {
switch (keysym->sym) {
case SDLK_UP:
you_dir = -1;
break;
case SDLK_DOWN:
you_dir = 1;
break;
case SDLK_LEFT:
you_turn = -1;
break;
case SDLK_RIGHT:
you_turn = 1;
break;
case SDLK_KP_PLUS:
you_velocity++;
break;
case SDLK_KP_MINUS:
you_velocity--;
break;
case SDLK_q:
case SDLK_ESCAPE:
quit_app( 0 );
break;
case SDLK_l:
light = !light;
if ( !light )
glDisable( GL_LIGHTING );
else
glEnable( GL_LIGHTING );
break;
case SDLK_d:
debug = !debug;
break;
default:
break;
}
}
void handle_keyup( SDL_keysym* keysym ) {
switch (keysym->sym) {
case SDLK_UP:
case SDLK_DOWN:
you_dir = 0;
break;
case SDLK_LEFT:
case SDLK_RIGHT:
you_turn = 0;
break;
}
}
void process_events( void ) {
SDL_Event event;
while( SDL_PollEvent( &event ) ) {
switch( event.type ) {
case SDL_KEYDOWN:
handle_keydown( &event.key.keysym );
break;
case SDL_KEYUP:
handle_keyup( &event.key.keysym );
break;
case SDL_QUIT:
quit_app( 0 );
break;
}
}
}
void you_compensate( void ) {
glRotatef( you_angle, 0.0f, 1.0f, 0.0f );
glTranslatef( -you_x, -1, -you_z);
}
void draw_screen( void ) {
// Step
you_angle += ( 10 * you_velocity * you_turn ) / fps;
float you_x_new = you_x - ( (float)sin(you_angle*piover180) * you_velocity * you_dir ) / fps;
float you_z_new = you_z + ( (float)cos(you_angle*piover180) * you_velocity * you_dir ) / fps;
int joel = boundCheck(you_x_new, you_x_new, you_z_new, you_z_new); // TODO change this to avoid repetition
if (joel == 1) {
you_x = you_x_new;
you_z = you_z_new;
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen and The Depth Buffer
/* We don't want to modify the projection matrix. */
glMatrixMode( GL_MODELVIEW );
glLoadIdentity(); you_compensate();
// Some nice material/colour settings
glColorMaterial ( GL_FRONT_AND_BACK, GL_EMISSION ) ;
glEnable ( GL_COLOR_MATERIAL ) ;
glLoadIdentity(); you_compensate();
glTranslatef( 0.0f, 3.0f, 0.0f );
draw_cube();
DrawWorld();
if (debug) {
glLoadIdentity(); glTranslatef(0, 0, -1);
glColor3f(1, 1, 1);
glRasterPos2f(-0.7f, -0.5f);
glPrint("X: %f Z: %f Angle: %f Velocity: %f", you_x, you_z, you_angle, you_velocity);
glRasterPos2f(-0.7f, 0.5f);
glPrint("FPS: %f", fps);
}
if (joel == 12) {
glLoadIdentity(); glTranslatef(0, 0, -1);
glColor3f(1, 1, 1);
glRasterPos2f(-0.7f, 0.5f);
glPrint("The Cake,");
glRasterPos2f(-0.7f, 0.4f);
glPrint("Is a Lie!!");
// glRasterPos2f(-0.7f, 0.3f);
// glPrint("Third Line");
}
Frames++;
GLint t = SDL_GetTicks();
if (t - T0 >= 100) {
GLfloat seconds = (t - T0) / 1000.0;
fps = Frames / seconds;
T0 = t;
Frames = 0;
}
SDL_GL_SwapBuffers( );
}
static void setup_opengl( int width, int height ) {
float ratio = (float) width / (float) height;
glShadeModel( GL_SMOOTH );
glCullFace( GL_BACK );
glFrontFace( GL_CCW );
glEnable( GL_CULL_FACE );
glEnable( GL_DEPTH_TEST );
glClearColor( 0, 0, 0, 0 );
glViewport( 0, 0, width, height );
glMatrixMode( GL_PROJECTION );
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glEnable(GL_LIGHT1);
glLoadIdentity( );
// TODO Replace this with a call to glFrustum.
gluPerspective( 60.0, ratio, 1.0, 1024.0 );
buildFont();
return;
}
int main( int argc, char* argv[] ) {
bool fullscreen = false;
char* world = "world";
for (int i=0; i<argc; i++) {
if (strcmp(argv[i], "-win") == 0) fullscreen = false;
if (strcmp(argv[i], "-f") == 0) fullscreen = true;
if (strcmp(argv[i], "-w") == 0)
world = argv[i+1];
if (strcmp(argv[i], "-d") == 0) debug = true;
}
SetupWorld(world);
you_x = getWorldX();
you_z = getWorldZ();
you_angle = getWorldAngle();
const SDL_VideoInfo* info = NULL;
int width = 0;
int height = 0;
int bpp = 0;
int flags = 0;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
fprintf( stderr, "Video initialization failed: %s\n",
SDL_GetError( ) );
quit_app( 1 );
}
info = SDL_GetVideoInfo( );
if( !info ) {
fprintf( stderr, "Video query failed: %s\n",
SDL_GetError( ) );
quit_app( 1 );
}
width = 640;
height = 480;
bpp = info->vfmt->BitsPerPixel;
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
flags = SDL_OPENGL;
if (fullscreen)
flags |= SDL_FULLSCREEN;
if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) {
fprintf( stderr, "Video mode set failed: %s\n",
SDL_GetError( ) );
quit_app( 1 );
}
setup_opengl( width, height );
bool init = true;
while( 1 ) {
process_events( );
draw_screen( );
if (init) { glEnable( GL_LIGHTING ); init=false; }
}
return 0;
}