-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
441 lines (361 loc) · 9.82 KB
/
Copy pathmain.cpp
File metadata and controls
441 lines (361 loc) · 9.82 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "SDL.h"
#include <string>
using namespace std;
/*
* Define the basic parameters for the SDL window
*/
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 16 //Bits per pixel
/*
* Derp boolean definitions. We shouldn't need this!!!!
*/
#define TRUE 1
#define FALSE 0
SDL_Surface *surface;
GLuint* tex1;
bool light; //Light on
bool lp; //L pressed
bool fp; //R pressed
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};
bool keys[255];
/*
* Exit the program, cleaning up all memory uses and returning to
* the default desktop properly.
*/
void Quit(int returnCode){
SDL_Quit();
exit(returnCode);
}
/*
* Load a bitmap into a openGL Texture.
*/
GLuint* LoadGLTexture(char* path){
//Create storage for the image
SDL_Surface* TextureImage[1];
GLuint* texture = (GLuint*) malloc(sizeof(texture));
//Load the bitmap, check for errors, and convert to a texture
if ((TextureImage[0] = SDL_LoadBMP(path))){
//Generate the texture
glGenTextures(1, texture);
//Set up bitmap mapping to texture
glBindTexture(GL_TEXTURE_2D, *texture);
//Generate the texture itself
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->w, TextureImage[0]->h,
0, GL_BGR, GL_UNSIGNED_BYTE, TextureImage[0]->pixels);
//Set up the images filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST); //Use mipmapping for near/large textures
} else {
fprintf(stderr, "Error, failed to load texture %s", path);
Quit(0);
return NULL;
}
//Clean up
if (TextureImage[0])
SDL_FreeSurface(TextureImage[0]);
if (texture){
return texture;
} else {
fprintf(stderr, "Error, failed to load texture %s", path);
Quit(0);
return NULL;
}
}
/*
* Resize the window, then recalculate the viewport.
*/
int resizeWindow(int width, int height){
//Ratio of width / height
GLfloat whratio;
if (height == 0) height = 1;
whratio = (GLfloat) width / (GLfloat) height;
//Change viewport
glViewport(0, 0, (GLint) width, (GLint) height);
//Set up perspective!
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 45.0f, whratio, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
return TRUE;
}
/*
* Handle any and all key events here
*/
void handleKeyPress(SDL_keysym* keysym){
switch (keysym->sym){
case SDLK_ESCAPE:
Quit(0);
break;
case SDLK_F11:
// Toggle full screen if F11 is pressed
SDL_WM_ToggleFullScreen(surface);
break;
default:
break;
}
}
/*
* Initialize openGL for use.
*/
int initGL(){
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//Set up lighting
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
//Set up blending
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
return TRUE;
}
/*
* All drawing to the screen, using the surface.
*/
int drawGLScene(){
//Calculate FPS
static GLint T0 = 0;
static GLint Frames = 0;
//Clean the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/*
* ALL DRAWING CODE GOES HERE
*/
glTranslatef(-1.5f, 0.0f, -6.0f);
glRotatef((float) Frames / 100.0f, 0.0f, 1.0f, 0.0f);
if (!tex1)
tex1 = LoadGLTexture("tex.bmp");
/* Triangle */
/*
glBegin(GL_TRIANGLES);
{
//Front
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
//Right
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f,-1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
//Back
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f,-1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
//Left
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
}
glEnd();
*/
glLoadIdentity();
glTranslatef(1.0f, 0.0f, -10.0f);
glRotatef((float) Frames / 47.0f, 1.0f, 30.0f, 45.0f);
glBindTexture(GL_TEXTURE_2D, *tex1);
glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
{
//Front
// glColor3f(0.0f, 0.0f, 1.0f);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
//Right
// glColor3f(0.0f, 1.0f, 0.0f);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
//Back
// glColor3f(0.0f, 0.0f, 1.0f);
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
//Left
// glColor3f(0.0f, 1.0f, 0.0f);
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
//Top
// glColor3f(1.0f, 0.0f, 0.0f);
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
//Bottom
// glColor3f(1.0f, 0.0f, 0.0f);
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
}
glEnd();
glDisable(GL_TEXTURE_2D);
/*
* END OF DRAWING CODE
*/
//Draw to the screen
SDL_GL_SwapBuffers();
//Calculate FPS
Frames ++;
{
GLint t = SDL_GetTicks();
if (Frames % 100 == 0){
GLfloat seconds = (t - T0) / 1000.0;
GLfloat fps = Frames / seconds;
printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
}
}
return TRUE;
}
int main(int argc, char **argv){
//Flags for SDL_SetVideoMode
int videoFlags;
//Main loop termination check
int done = FALSE;
//Event collection
SDL_Event event;
//Display information
const SDL_VideoInfo *videoInfo;
//Whether the window is active
int isActive = TRUE;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0){
fprintf( stderr, "SDL Video Initialization failed: %s\n", SDL_GetError());
Quit(1);
}
//Get the display information
videoInfo = SDL_GetVideoInfo();
if (!videoInfo){
fprintf( stderr, "SDL could not get video information: %s\n", SDL_GetError());
Quit(1);
}
/*
* Enable openGL, double buffering, hardware palette use, and window resizing in the videoFlags
*/
videoFlags = SDL_OPENGL;
videoFlags |= SDL_GL_DOUBLEBUFFER;
videoFlags |= SDL_HWPALETTE;
videoFlags |= SDL_RESIZABLE;
//Check if surfaces can be stored in memory
if (videoInfo -> hw_available){
videoFlags |= SDL_HWSURFACE;
} else {
videoFlags |= SDL_SWSURFACE;
}
//Try to enable hardware blits (I'm not sure what this is....)
if (videoInfo -> blit_hw)
videoFlags |= SDL_HWACCEL;
//Enable openGL double buffering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
//Get a surface
surface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags);
if (!surface){
fprintf( stderr, "SDL failed to initialize the surface: %s\n", SDL_GetError());
Quit(1);
}
if (initGL() == FALSE){
fprintf( stderr, "Failed to initialize openGL");
Quit(1);
}
//Size the initial window
resizeWindow(SCREEN_WIDTH, SCREEN_HEIGHT);
//Wait for events
while (!done){
//Iterate through all buffered events
while (SDL_PollEvent(&event)){
switch (event.type){
case SDL_ACTIVEEVENT: //Change in focus
if (event.active.gain == 0)
isActive = false;
else
isActive = true;
break;
case SDL_VIDEORESIZE:
surface = SDL_SetVideoMode(event.resize.w, event.resize.h, SCREEN_BPP, videoFlags);
break;
case SDL_KEYDOWN:
keys[event.key.keysym.sym] = true; //Store that the key is pressed
handleKeyPress(&event.key.keysym);
break;
case SDL_KEYUP:
keys[event.key.keysym.sym] = false; //Store that the key has been released
break;
case SDL_QUIT:
done = TRUE;
break;
default:
break;
}
}
//Redraw the screen after all events have been handled
if (isActive)
drawGLScene();
}
Quit(0);
return 0;
}