forked from nbcraft-org/nbcraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppPlatform_sdl_base.cpp
More file actions
427 lines (371 loc) · 9.15 KB
/
AppPlatform_sdl_base.cpp
File metadata and controls
427 lines (371 loc) · 9.15 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <sstream>
#include <fstream>
#include <sys/stat.h>
#include <cstdlib>
#include "AppPlatform_sdl_base.hpp"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#else
#include "thirdparty/GL/GL.hpp"
#endif
#include "common/Utils.hpp"
#include "CustomSoundSystem.hpp"
// Macros are cursed
#define _STR(x) #x
#define STR(x) _STR(x)
#include "client/player/input/Controller.hpp"
void AppPlatform_sdl_base::_init(std::string storageDir, SDL_Window *window)
{
_storageDir = storageDir;
_window = window;
_iconTexture = nullptr;
_icon = nullptr;
m_bShiftPressed[0] = false;
m_bShiftPressed[1] = false;
ensureDirectoryExists(_storageDir.c_str());
m_pSoundSystem = nullptr;
// Default Touchscreen Mode
#ifdef ANDROID
m_bIsTouchscreen = true;
#else
m_bIsTouchscreen = false;
#endif
// Custom Touchscreen Mode
const char *mode = getenv("MCPE_INPUT_MODE");
if (mode)
{
if (strcmp(mode, "touch") == 0)
{
m_bIsTouchscreen = true;
}
else if (strcmp(mode, "mouse") == 0)
{
m_bIsTouchscreen = false;
}
}
// Look for a pre-existing controller
_controller = findGameController();
clearDiff();
}
void AppPlatform_sdl_base::initSoundSystem()
{
if (!m_pSoundSystem)
{
LOG_I("Initializing " STR(SOUND_SYSTEM) "...");
m_pSoundSystem = new SOUND_SYSTEM();
}
else
{
LOG_E("Trying to initialize SoundSystem more than once!");
}
}
std::string AppPlatform_sdl_base::getDateString(int time)
{
time_t tt = time;
struct tm t;
#ifdef _WIN32
gmtime_s(&t, &tt);
#else
gmtime_r(&tt, &t);
#endif
// Format String
char buf[2048];
strftime(buf, sizeof buf, "%b %d %Y %H:%M:%S", &t);
// Return
return std::string(buf);
}
void AppPlatform_sdl_base::setIcon(const Texture& icon)
{
if (!icon.m_pixels)
return;
SAFE_DELETE(_iconTexture);
if (_icon) SDL_FreeSurface(_icon);
_iconTexture = new Texture(icon);
_icon = getSurfaceForTexture(_iconTexture);
if (_icon)
SDL_SetWindowIcon(_window, _icon);
}
AppPlatform_sdl_base::~AppPlatform_sdl_base()
{
if (_icon) SDL_FreeSurface(_icon);
SAFE_DELETE(_iconTexture);
SAFE_DELETE(m_pSoundSystem);
}
SDL_GameController* AppPlatform_sdl_base::findGameController()
{
for (int i = 0; i < SDL_NumJoysticks(); i++) {
if (SDL_IsGameController(i)) {
return SDL_GameControllerOpen(i);
}
}
return nullptr;
}
SDL_Surface* AppPlatform_sdl_base::getSurfaceForTexture(const Texture* const texture)
{
if (!texture) return nullptr;
void * const pixels = texture->m_pixels;
const int width = texture->m_width;
const int height = texture->m_height;
const int depth = 32; // Color depth (32-bit by default)
SDL_Surface* surface = SDL_CreateRGBSurfaceFrom(
pixels, width, height, depth,
width * 4, // Pitch
0x000000FF, 0x0000FF00, 0x00FF0000,
0xFF000000
);
if (!surface)
LOG_E("Failed loading SDL_Surface from Texture: %s", SDL_GetError());
return surface;
}
int AppPlatform_sdl_base::checkLicense()
{
// we own the game!!
return 1;
}
const char* const AppPlatform_sdl_base::getWindowTitle() const
{
return SDL_GetWindowTitle(_window);
}
int AppPlatform_sdl_base::getScreenWidth() const
{
int width;
SDL_GL_GetDrawableSize(_window, &width, nullptr);
return width;
}
int AppPlatform_sdl_base::getScreenHeight() const
{
int height;
SDL_GL_GetDrawableSize(_window, nullptr, &height);
return height;
}
void AppPlatform_sdl_base::setMouseGrabbed(bool b)
{
SDL_SetWindowGrab(_window, b ? SDL_TRUE : SDL_FALSE);
/**
* @NOTE: There is a bug with older versions of SDL2 (ex: 2.0.4) where disabling RelativeMouseMode will cause a mouse event to be fired
* that just moves the cursor to where it would've been if the mode had never been enabled in the first place, effectively uncentering it.
* https://github.com/libsdl-org/SDL/issues/6002 (I'm not sure if this is the right issue, I just updated SDL after seeing this and it fixed the above problem.)
**/
SDL_SetRelativeMouseMode(b ? SDL_TRUE : SDL_FALSE);
clearDiff();
}
void AppPlatform_sdl_base::setMouseDiff(int x, int y)
{
xrel += x;
yrel += y;
}
void AppPlatform_sdl_base::getMouseDiff(int& x, int& y)
{
x = xrel;
y = yrel;
}
void AppPlatform_sdl_base::clearDiff()
{
xrel = 0;
yrel = 0;
}
bool AppPlatform_sdl_base::shiftPressed()
{
return m_bShiftPressed[0] || m_bShiftPressed[1];
}
void AppPlatform_sdl_base::setShiftPressed(bool b, bool isLeft)
{
m_bShiftPressed[isLeft ? 0 : 1] = b;
}
int AppPlatform_sdl_base::getUserInputStatus()
{
return -1;
}
MouseButtonType AppPlatform_sdl_base::GetMouseButtonType(SDL_MouseButtonEvent event)
{
switch (event.button)
{
case SDL_BUTTON_LEFT:
return BUTTON_LEFT;
case SDL_BUTTON_RIGHT:
return BUTTON_RIGHT;
case SDL_BUTTON_MIDDLE:
return BUTTON_MIDDLE;
default:
return BUTTON_NONE;
}
}
bool AppPlatform_sdl_base::GetMouseButtonState(SDL_Event event)
{
bool result;
switch (event.type)
{
case SDL_MOUSEBUTTONDOWN:
result = true;
break;
case SDL_MOUSEBUTTONUP:
result = false;
break;
case SDL_MOUSEWHEEL:
{
short wheelDelta = event.wheel.y;
if (wheelDelta > 0)
{
// "A positive value indicates that the wheel was rotated forward, away from the user."
result = false;
}
else
{
// "A negative value indicates that the wheel was rotated backward, toward the user."
result = true;
}
break;
}
default:
result = false;
break;
}
return result;
}
Keyboard::KeyState AppPlatform_sdl_base::GetKeyState(uint8_t state)
{
switch (state)
{
case SDL_RELEASED:
return Keyboard::UP;
case SDL_PRESSED:
default:
return Keyboard::DOWN;
}
}
void AppPlatform_sdl_base::showKeyboard(int x, int y, int w, int h)
{
if (SDL_IsTextInputActive())
{
hideKeyboard();
}
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_SetTextInputRect(&rect);
SDL_StartTextInput();
}
void AppPlatform_sdl_base::hideKeyboard()
{
if (SDL_IsTextInputActive())
{
SDL_StopTextInput();
}
}
bool AppPlatform_sdl_base::isTouchscreen() const
{
return m_bIsTouchscreen;
}
bool AppPlatform_sdl_base::hasGamepad() const
{
return _controller != nullptr;
}
void AppPlatform_sdl_base::gameControllerAdded(int32_t index)
{
if (!getPrimaryGameController())
{
setPrimaryGameController(SDL_GameControllerOpen(index));
Controller::reset();
}
}
void AppPlatform_sdl_base::gameControllerRemoved(int32_t index)
{
SDL_GameController* controller = getPrimaryGameController();
SDL_JoystickID joystickId = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller));
// Check if current controller has been removed
if (controller && index == joystickId)
{
SDL_GameControllerClose(controller);
// Hunt for a new primary controller
setPrimaryGameController(findGameController());
}
}
void AppPlatform_sdl_base::handleKeyEvent(int key, uint8_t state)
{
// This really should be handled somewhere else.
// Unforunately, there is no global keyboard handler.
// Keyboard events are either handled in Screen::keyboardEvent
// when a Screen is visible, or in Minecraft::tickInput
// when LocalPlayer exists.
switch (key)
{
case SDLVK_F2:
if (state == SDL_PRESSED)
saveScreenshot("", -1, -1);
return;
case SDLVK_AC_BACK:
// Android Back Button (This is currently handled in handle_events() in platforms/sdl/main.cpp)
// @TODO: handleBack function in AppPlatform that calls back to App via function pointer
//g_pApp->handleBack(event.key.state == SDL_PRESSED);
return;
case SDLVK_BACKSPACE:
// Text Editing (This is currently handled in handle_events() in platforms/sdl/main.cpp)
/*if (state == SDL_PRESSED)
g_pApp->handleCharInput('\b');*/
break;
case SDLVK_LSHIFT:
case SDLVK_RSHIFT:
setShiftPressed(state == SDL_PRESSED, key == SDLVK_LSHIFT);
break;
}
// Normal Key Press
Keyboard::feed(AppPlatform_sdl_base::GetKeyState(state), key);
}
void AppPlatform_sdl_base::handleButtonEvent(SDL_JoystickID controllerIndex, uint8_t button, uint8_t state)
{
// Normal Key Press
Keyboard::feed(AppPlatform_sdl_base::GetKeyState(state), button);
}
void AppPlatform_sdl_base::handleControllerAxisEvent(SDL_JoystickID controllerIndex, uint8_t axis, int16_t value)
{
float val = value / 32767.0f; // -32768 to 32767
switch (axis)
{
case SDL_CONTROLLER_AXIS_LEFTX:
Controller::feedStickX(1, true, val);
break;
case SDL_CONTROLLER_AXIS_LEFTY:
Controller::feedStickY(1, true, val);
break;
case SDL_CONTROLLER_AXIS_RIGHTX:
Controller::feedStickX(2, true, val);
break;
case SDL_CONTROLLER_AXIS_RIGHTY:
Controller::feedStickY(2, true, val);
break;
case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
Controller::feedTrigger(1, val);
break;
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
Controller::feedTrigger(2, val);
break;
}
}
AssetFile AppPlatform_sdl_base::readAssetFile(const std::string& str, bool quiet) const
{
std::string path = getAssetPath(str);
SDL_RWops *io = SDL_RWFromFile(path.c_str(), "rb");
// Open File
if (!io)
{
if (!quiet) LOG_W("Couldn't find asset file: %s", path.c_str());
return AssetFile();
}
// Get File Size
int64_t size = SDL_RWsize(io);
if (size < 0)
{
if (!quiet) LOG_E("Error determining the size of the asset file!");
SDL_RWclose(io);
return AssetFile();
}
// Read Data
unsigned char *buf = new unsigned char[size];
SDL_RWread(io, buf, size, 1);
// Close File
SDL_RWclose(io);
// Return
return AssetFile(size, buf);
}