- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDLstuff.cpp
More file actions
Latest commit
242 lines (189 loc) · 6.23 KB
/
Copy pathSDLstuff.cpp
File metadata and controls
242 lines (189 loc) · 6.23 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
#include"SDLstuff.h"
#include<SDL_syswm.h>
SDLstuff::SDLstuff(){
mainWindow = NULL;
sdl_glContext = NULL;
init();
}
SDLstuff::~SDLstuff(){
//SDL_GL_DeleteContext(sdl_glContext);
SDL_DestroyWindow(mainWindow);
}
voidSDLstuff::init(){
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 ) {
cerr << "Failed initializing SDL Video: " << SDL_GetError() << endl;
exit(1);
}
cout << "Display mode: " << Engine::screenWidthGL << " x " << Engine::screenHeightGL << endl;
cout << "Render mode: " << Engine::screenWidthRT << " x " << Engine::screenHeightRT << endl;
int videoFlags = SDL_WINDOW_OPENGL;
if(Engine::fullscreen)
videoFlags |= SDL_WINDOW_FULLSCREEN;
#ifdef ANDROID
// force fullscreen on android
videoFlags |= SDL_WINDOW_FULLSCREEN;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif
strcpy(title, "MinClient");
sprintf(title, "MinClient %s", MINCLIENT_VERSION);
mainWindow = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Engine::screenWidthGL, Engine::screenHeightGL, videoFlags);
if(!mainWindow){
cout << "Failed to open window: " << SDL_GetError() << endl;
exit(0);
}
sdl_glContext = SDL_GL_CreateContext(mainWindow);
if(!sdl_glContext){
cout << "Failed to create OpenGL Context: " << SDL_GetError() << endl;
exit(0);
}
}
voidSDLstuff::tweakFontSize(){
if(Engine::screenWidthGL < 512 || Engine::screenHeightGL < 512){
Engine::fontSize = 16;
}
if (Engine::screenWidthGL < 256 || Engine::screenHeightGL < 256){
Engine::fontSize = 10;
}
}
voidSDLstuff::printResolution(){
char output[128];
sprintf(output, "%ix%i display mode", Engine::screenWidthGL, Engine::screenHeightGL);
minclient::Font::glPrint(10, minclient::Font::AUTO, output, true);
sprintf(output, "%ix%i render mode (2^n)", Engine::screenWidthRT, Engine::screenHeightRT);
minclient::Font::glPrint(10, minclient::Font::AUTO, output, true);
sprintf(output, "%ix%i display mode", Engine::screenWidthGL, Engine::screenHeightGL);
minclient::Font::glPrint(10, minclient::Font::AUTO, output, true);
}
voidSDLstuff::checkEvents(){
while( SDL_PollEvent(& event) ) {
switch(event.type){
caseSDL_KEYDOWN:
handleKeyPressEvent(&event.key.keysym);
break;
caseSDL_KEYUP:
handleKeyReleaseEvent(&event.key.keysym);
break;
caseSDL_MOUSEBUTTONDOWN:
handleMousePressEvent(&event.button);
break;
caseSDL_MOUSEBUTTONUP:
handleMouseReleaseEvent(&event.button);
break;
caseSDL_MOUSEMOTION:
handleMouseMotionEvent(&event.motion);
break;
default:
break;
}
}
}
voidSDLstuff::handleMouseMotionEvent(SDL_MouseMotionEvent* mouseMotion){
Engine::relativeMouseMotion.x = mouseMotion->xrel;
Engine::relativeMouseMotion.y = mouseMotion->yrel;
if(Engine::buttonPressed != CVector2(-666.6f, -666.6f)){
Engine::buttonPressed = Engine::buttonPressed + Engine::relativeMouseMotion;
}
}
voidSDLstuff::handleMousePressEvent(SDL_MouseButtonEvent* mouse){
/*
if((mouse->button & SDL_BUTTON_LEFT) == SDL_BUTTON_LEFT)
Engine::leftButton = true;
else if((mouse->button & SDL_BUTTON_RIGHT) == SDL_BUTTON_RIGHT)
Engine::rightButton = true;
*/
Engine::buttonPressed = CVector2(mouse->x, mouse->y);
Engine::clicksPerSecond++;
// exit if frequently clicked
if(Engine::clicksPerSecond > 4)
Engine::done = true;
}
voidSDLstuff::handleMouseReleaseEvent(SDL_MouseButtonEvent* mouse){
/*
if((mouse->button & SDL_BUTTON_LEFT) == SDL_BUTTON_LEFT)
Engine::leftButton = false;
else if((mouse->button & SDL_BUTTON_RIGHT) == SDL_BUTTON_RIGHT)
Engine::rightButton = false;
*/
Engine::buttonPressed = CVector2(-666.6f, -666.6f);
Engine::upKey = false;
Engine::downKey = false;
Engine::leftKey = false;
Engine::rightKey = false;
Engine::jumpKey = false;
Engine::crouchKey = false;
}
voidSDLstuff::handleKeyPressEvent(SDL_Keysym * keysym){
SDL_Keycode key = keysym->sym;
switch(key){
caseSDLK_ESCAPE:
Engine::done = true;
break;
caseSDLK_UP:
case SDLK_w:
Engine::upKey = true;
break;
caseSDLK_DOWN:
case SDLK_s:
Engine::downKey = true;
break;
caseSDLK_RIGHT:
case SDLK_d:
Engine::leftKey = true;
break;
caseSDLK_LEFT:
case SDLK_a:
Engine::rightKey = true;
break;
caseSDLK_SPACE:
Engine::jumpKey = true;
break;
case SDLK_c:
Engine::crouchKey = true;
break;
default:
break;
}
}
voidSDLstuff::handleKeyReleaseEvent(SDL_Keysym * keysym){
SDL_Keycode key = keysym->sym;
switch(key){
caseSDLK_UP:
case SDLK_w:
Engine::upKey = false;
break;
caseSDLK_DOWN:
case SDLK_s:
Engine::downKey = false;
break;
caseSDLK_RIGHT:
case SDLK_d:
Engine::leftKey = false;
break;
caseSDLK_LEFT:
case SDLK_a:
Engine::rightKey = false;
break;
caseSDLK_SPACE:
Engine::jumpKey = false;
break;
case SDLK_c:
Engine::crouchKey = false;
break;
default:
break;
}
}
voidSDLstuff::grabKeyAndMouse(){
SDL_SetRelativeMouseMode(SDL_TRUE);
}
voidSDLstuff::ungrabKeyAndMouse(){
SDL_SetRelativeMouseMode(SDL_FALSE);
}
voidSDLstuff::closeSDL_Net(){
for(int i = 0; i < Engine::numServers; i++){
SDLNet_TCP_Close(Engine::socketDescriptor[i]);
}
SDLNet_Quit();
}