/
sdl_app.cpp
215 lines (176 loc) · 4.61 KB
/
sdl_app.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
/*
* Author: Josh Bryan <josh.bryan@gmail.com>
* This file is copyright 2010 by Josh Bryan.
*
* This file is part of Reinforcement Learning Demo.
*
* Foobar is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <stdexcept>
#include <GL/gl.h>
#include <GL/glu.h>
#include "sdl_app.h"
SdlApp::SdlApp() :
running(false)
{ }
SdlApp::~SdlApp()
{
tear_down();
}
/*
* This function sets up the SDL surface and gl options. Some of this has been
* borrowed from Ti Leggett's SDL port of Jeff Molofee's opengl examples at
* http://nehe.gamedev.net.
*/
void SdlApp::init()
{
/* Flags to pass to SDL_SetVideoMode */
int videoFlags;
const SDL_VideoInfo *videoInfo;
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
throw std::runtime_error(std::string("Video initialization failed: ")
+ SDL_GetError( ));
}
/* Fetch the video info */
videoInfo = SDL_GetVideoInfo( );
if ( !videoInfo )
{
throw std::runtime_error(std::string("Video query failed: ")
+ SDL_GetError( ));
}
/* the flags to pass to SDL_SetVideoMode */
videoFlags = SDL_OPENGL; /* Enable OpenGL in SDL */
videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */
videoFlags |= SDL_HWPALETTE; /* Store the palette in hardware */
videoFlags |= SDL_RESIZABLE; /* Enable window resizing */
/* This checks to see if surfaces can be stored in memory */
if ( videoInfo->hw_available )
videoFlags |= SDL_HWSURFACE;
else
videoFlags |= SDL_SWSURFACE;
/* This checks if hardware blits can be done */
if ( videoInfo->blit_hw )
videoFlags |= SDL_HWACCEL;
videoFlags |= SDL_FULLSCREEN;
/* Sets up OpenGL double buffering */
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
/* get a SDL surface */
surface = SDL_SetVideoMode( 0, 0, 32, videoFlags );
/* Verify there is a surface */
if ( !surface )
{
throw std::runtime_error(std::string("Video mode set failed: ")
+ SDL_GetError( ));
}
/* initialize OpenGL */
/* Enable smooth shading */
glShadeModel( GL_SMOOTH );
/* Set the background black */
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
/* Depth buffer setup */
glClearDepth( 1.0f );
/* Enables Depth Testing */
glEnable( GL_DEPTH_TEST );
/* The Type Of Depth Test To Do */
glDepthFunc( GL_LEQUAL );
/* Really Nice Perspective Calculations */
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
glEnable (GL_POINT_SMOOTH);
glEnable (GL_POLYGON_SMOOTH);
glEnable (GL_LINE_SMOOTH);
glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/* Height / width ration */
GLfloat ratio = (GLfloat) videoInfo->current_w
/ (GLfloat) videoInfo->current_h;
/* Setup our viewport. */
glViewport( 0, 0, ( GLsizei )videoInfo->current_w,
( GLsizei )videoInfo->current_h);
/* change to the projection matrix and set our viewing volume. */
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
/* Set our perspective */
gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
/* Make sure we're changing the model view and not the projection */
glMatrixMode( GL_MODELVIEW );
/* Reset The View */
glLoadIdentity( );
running = true;
}
/*
* call before quitting
*/
void SdlApp::tear_down()
{
running = false;
SDL_Quit();
}
/*
* Run through one frame drawing cycle
*/
void SdlApp::step()
{
if (!running) return;
draw_scene();
SDL_Event event;
while ( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_ACTIVEEVENT:
//TODO: handle focus change
break;
case SDL_KEYDOWN:
/* handle key presses */
handle_key_press( &event.key.keysym );
break;
case SDL_QUIT:
/* handle quit requests */
tear_down();
break;
default:
break;
}
}
}
/*
* iterate the frame loop until done
*/
void SdlApp::run_loop()
{
while(running)
{
step();
}
}
/*
* check for key presses
*/
void SdlApp::handle_key_press( SDL_keysym *keysym )
{
switch ( keysym->sym )
{
case SDLK_ESCAPE:
case SDLK_q:
tear_down( );
break;
default:
break;
}
}