-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDLApplication.cpp
206 lines (183 loc) · 4.95 KB
/
SDLApplication.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
#include <cstring>
#include <vector>
#include "SDLApplication.h"
#include "StarField.h"
SDLApplication::SDLApplication(int width, int height, std::string fileName) :
ErrorStateBase(),
videoInfo(0),
surface(0),
width(width),
height(height),
fileName(fileName),
glHandler(this->camera),
keepRunning(true),
videoFlags()
{
}
SDLApplication::~SDLApplication()
{
SDL_Quit();
}
void SDLApplication::initSDL()
{
if (checkState())
{
/* initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "Video initialization failed: " << SDL_GetError() << std::endl;
errorState = VIDEO_INITIALIZATION_FAILED;
}
}
}
void SDLApplication::initVideoInfo()
{
if (checkState())
{
/* Fetch the video info */
videoInfo = SDL_GetVideoInfo();
if (!videoInfo)
{
std::cerr << "Video query failed: " << SDL_GetError() << std::endl;
errorState = VIDEO_QUERY_FAILED;
}
}
}
void SDLApplication::createGLWindow()
{
if (checkState())
{
/* 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;
/* Sets up OpenGL double buffering */
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
/* get a SDL surface */
surface = SDL_SetVideoMode(width, height, 0, videoFlags);
/* Verify there is a surface */
if (!surface)
{
std::cerr << "Video mode set failed: " << SDL_GetError() << std::endl;
errorState = VIDEO_MODE_SET_FAILED;
}
}
}
void SDLApplication::initGL()
{
if (checkState())
{
errorState = this->glHandler.initGL();
glHandler.resizeWindow(width, height);
}
}
void SDLApplication::initStarData()
{
glHandler.setStarField(StarFieldBuilder().fromFile(fileName));
}
ErrorStateBase::ErrorCode SDLApplication::init()
{
initSDL();
initVideoInfo();
createGLWindow();
initStarData();
initGL();
return errorState;
}
void SDLApplication::handleKeyPress(SDL_keysym *keysym)
{
switch ( keysym->sym )
{
case SDLK_ESCAPE:
/* ESC key was pressed */
keepRunning = false;
break;
case SDLK_F1:
/* F1 key was pressed
* this toggles fullscreen mode
*/
SDL_WM_ToggleFullScreen( surface );
break;
default:
break;
}
}
void SDLApplication::handleMouseMotion(SDL_MouseMotionEvent event)
{
int x, y;
int buttons_state = SDL_GetMouseState(&x, &y);
if (buttons_state & SDL_BUTTON(SDL_BUTTON_LEFT) && buttons_state & SDL_BUTTON(SDL_BUTTON_RIGHT))
{
camera.updateFOV(event.yrel * 0.1);
}
else if (buttons_state & SDL_BUTTON(SDL_BUTTON_LEFT))
{
camera.updateOrientationXY(event.yrel * 0.001, event.xrel * 0.001);
}
else if (buttons_state & SDL_BUTTON(SDL_BUTTON_RIGHT))
{
camera.updatePositionFromDelta(event.yrel * 0.1);
}
}
void SDLApplication::mainLoop()
{
/* wait for events */
while (keepRunning)
{
if (!checkState())
{
keepRunning = false;
std::cerr << "State error, breaking SDL event loop" << std::endl;
break;
}
/* handle the events in the queue */
SDL_Event event;
std::memset(&event, 0, sizeof(event));
while (SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_ACTIVEEVENT:
/* Something's happend with our focus */
break;
case SDL_VIDEORESIZE:
/* handle resize event */
surface = SDL_SetVideoMode(event.resize.w,
event.resize.h,
16, videoFlags);
if ( !surface )
{
fprintf( stderr, "Could not get a surface after resize: %s\n", SDL_GetError( ) );
keepRunning = false;
errorState = INVALID_SURFACE_AFTER_RESIZE;
}
glHandler.resizeWindow( event.resize.w, event.resize.h );
break;
case SDL_KEYDOWN:
/* handle key presses */
handleKeyPress(&event.key.keysym);
break;
case SDL_MOUSEMOTION:
handleMouseMotion(event.motion);
break;
case SDL_QUIT:
/* handle quit requests */
keepRunning = false;
break;
default:
break;
}
}
glHandler.drawGLScene();
SDL_GL_SwapBuffers( );
}
}