-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDLstuff.cpp
More file actions
155 lines (122 loc) · 3.27 KB
/
SDLstuff.cpp
File metadata and controls
155 lines (122 loc) · 3.27 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
#include "SDLstuff.h"
SDLstuff::SDLstuff(RT_RayTracer* rayTracer_){
this->rayTracer = rayTracer_;
mainWindow = NULL;
sdl_glContext = NULL;
}
SDLstuff::~SDLstuff(){
if (sdl_glContext)
SDL_GL_DeleteContext(sdl_glContext);
}
void SDLstuff::init(){
if (!Engine::dedicated){
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
cerr << "Failed initializing SDL Video: " << SDL_GetError() << endl;
exit(1);
}
}
// move the window starting position a bit away from the upper left corner
const int totalOffsetX = 6;
const int totalOffsetY = 28;
if (!Engine::dedicated){
cout << "Creating SDL window with " << Engine::screenWidthGL << "x" << Engine::screenHeightGL << endl;
sprintf(title, "MinRT %s", MINRT_VERSION);
mainWindow = SDL_CreateWindow(title, totalOffsetX, totalOffsetY, Engine::screenWidthGL, Engine::screenHeightGL, SDL_WINDOW_OPENGL);
if (!mainWindow){
cout << "SDLstuff::createMyWindow. No mainWindow: " << SDL_GetError() << endl;
exit(1);
}
sdl_glContext = SDL_GL_CreateContext(mainWindow);
if (!sdl_glContext){
cout << "SDLstuff::createMyWindow. No mainWindow: " << SDL_GetError() << endl;
exit(1);
}
}
}
void SDLstuff::checkEvents(){
while (SDL_PollEvent(&event)){
switch (event.type){
case SDL_KEYDOWN:
handleKeyPressEvent(&event.key.keysym);
break;
case SDL_KEYUP:
handleKeyReleaseEvent(&event.key.keysym);
break;
case SDL_MOUSEMOTION:
handleMouseMotionEvent(&event.motion);
break;
default:
break;
}
}
}
void SDLstuff::handleMouseMotionEvent(SDL_MouseMotionEvent* mouseMotion){
Engine::relativeMouseMotion = CVector2i(mouseMotion->xrel, mouseMotion->yrel);
}
void SDLstuff::handleKeyPressEvent(SDL_Keysym* keysym){
SDL_Keycode key = keysym->sym;
switch (key){
case SDLK_ESCAPE:
Engine::done = true;
break;
case SDLK_UP:
case SDLK_w:
Engine::upKey = true;
break;
case SDLK_DOWN:
case SDLK_s:
Engine::downKey = true;
break;
case SDLK_RIGHT:
case SDLK_d:
Engine::leftKey = true;
break;
case SDLK_LEFT:
case SDLK_a:
Engine::rightKey = true;
break;
case SDLK_SPACE:
Engine::jumpKey = true;
break;
case SDLK_c:
Engine::crouchKey = true;
break;
default:
break;
}
}
void SDLstuff::handleKeyReleaseEvent(SDL_Keysym* keysym){
SDL_Keycode key = keysym->sym;
switch (key) {
case SDLK_UP:
case SDLK_w:
Engine::upKey = false;
break;
case SDLK_DOWN:
case SDLK_s:
Engine::downKey = false;
break;
case SDLK_RIGHT:
case SDLK_d:
Engine::leftKey = false;
break;
case SDLK_LEFT:
case SDLK_a:
Engine::rightKey = false;
break;
case SDLK_SPACE:
Engine::jumpKey = false;
break;
case SDLK_c:
Engine::crouchKey = false;
break;
default:
break;
}
}
void SDLstuff::grabKeyAndMouse(){
SDL_SetRelativeMouseMode(SDL_TRUE);
}
void SDLstuff::ungrabKeyAndMouse(){
SDL_SetRelativeMouseMode(SDL_FALSE);
}