-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicWindow.cpp
More file actions
230 lines (186 loc) · 4.49 KB
/
basicWindow.cpp
File metadata and controls
230 lines (186 loc) · 4.49 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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "SDL.h"
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;
/*
* Exit the program, cleaning up all memory uses and returning to
* the default desktop properly.
*/
void Quit(int returnCode){
SDL_Quit();
exit(returnCode);
}
/*
* 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);
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
*/
/*
* 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:
handleKeyPress(&event.key.keysym);
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;
}