forked from nbcraft-org/nbcraft
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
423 lines (380 loc) · 10.2 KB
/
main.cpp
File metadata and controls
423 lines (380 loc) · 10.2 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
#include <stdarg.h>
#include "thirdparty/SDL2/SDL2.h"
#include "thirdparty/GL/GL.hpp"
#include "client/app/App.hpp"
#ifdef __EMSCRIPTEN__
#include "emscripten/AppPlatform_sdl.hpp"
#else
#include "desktop/AppPlatform_sdl.hpp"
#endif
typedef AppPlatform_sdl UsedAppPlatform;
#include "client/app/NinecraftApp.hpp"
#include "client/player/input/Multitouch.hpp"
static float g_fPointToPixelScale = 1.0f;
UsedAppPlatform *g_pAppPlatform;
NinecraftApp *g_pApp;
SDL_Window *window = NULL;
SDL_GLContext context = NULL;
static void teardown()
{
if (window != NULL)
{
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
window = NULL;
}
}
static int TranslateSDLKeyCodeToVirtual(int sdlCode)
{
switch (sdlCode) {
#define CODE(x) case SDLK_ ## x: return SDLVK_ ## x;
#include "compat/SDLKeyCodes.h"
#undef CODE
}
return SDLVK_UNKNOWN;
}
// Touch
#define TOUCH_IDS_SIZE (MAX_TOUCHES - 1) // ID 0 Is Reserved For The Mouse
struct touch_id_data {
bool active;
SDL_TouchID device;
SDL_FingerID finger;
touch_id_data()
{
active = false;
}
};
static touch_id_data touch_ids[TOUCH_IDS_SIZE];
static char get_touch_id(SDL_TouchID device, SDL_FingerID finger) {
for (int i = 0; i < TOUCH_IDS_SIZE; i++) {
touch_id_data &data = touch_ids[i];
if (data.active && data.device == device && data.finger == finger) {
return i + 1;
}
}
// Not Found
for (int i = 0; i < TOUCH_IDS_SIZE; i++) {
// Find First Inactive ID, And Activate It
touch_id_data &data = touch_ids[i];
if (!data.active) {
data.active = true;
data.device = device;
data.finger = finger;
return i + 1;
}
}
// Fail
return 0;
}
static void drop_touch_id(int id) {
touch_ids[id - 1].active = false;
}
static void handle_touch(int x, int y, int type, char id) {
if (id == 0) {
return;
}
switch (type) {
case SDL_FINGERDOWN:
case SDL_FINGERUP: {
bool data = type == SDL_FINGERUP ? 0 : 1;
Mouse::feed(BUTTON_LEFT, data, x, y);
Multitouch::feed(BUTTON_LEFT, data, x, y, id);
if (type == SDL_FINGERUP) {
drop_touch_id(id);
}
break;
}
case SDL_FINGERMOTION: {
Mouse::feed(BUTTON_NONE, 0, x, y);
Multitouch::feed(BUTTON_NONE, 0, x, y, id);
break;
}
}
}
// Resize From JS
#ifdef __EMSCRIPTEN__
extern "C" void resize_from_js(int new_width, int new_height)
{
SDL_SetWindowSize(window, new_width, new_height);
}
#endif
// Handle Events
static bool window_resized = false;
static void handle_events()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
case SDL_KEYUP:
{
// Android Back Button
if (event.key.keysym.sym == SDLK_AC_BACK)
{
g_pApp->handleBack(event.key.state == SDL_PRESSED);
break;
}
// Text Editing
if (event.key.keysym.sym == SDLK_BACKSPACE && event.key.state == SDL_PRESSED)
{
g_pApp->handleCharInput('\b');
}
g_pAppPlatform->handleKeyEvent(TranslateSDLKeyCodeToVirtual(event.key.keysym.sym), event.key.state);
break;
}
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
// Hate this hack
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_START && event.cbutton.state == SDL_PRESSED)
{
g_pApp->pauseGame() || g_pApp->resumeGame();
}
g_pAppPlatform->handleButtonEvent(event.cbutton.which, event.cbutton.button, event.cbutton.state);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
if (event.button.which != SDL_TOUCH_MOUSEID) {
const float scale = g_fPointToPixelScale;
MouseButtonType type = AppPlatform_sdl_base::GetMouseButtonType(event.button);
bool state = AppPlatform_sdl_base::GetMouseButtonState(event);
float x = event.button.x * scale;
float y = event.button.y * scale;
Mouse::feed(type, state, x, y);
Multitouch::feed(type, state, x, y, 0);
}
break;
}
case SDL_MOUSEMOTION:
{
if (event.button.which != SDL_TOUCH_MOUSEID) {
float scale = g_fPointToPixelScale;
float x = event.motion.x * scale;
float y = event.motion.y * scale;
Multitouch::feed(BUTTON_NONE, false, x, y, 0);
Mouse::feed(BUTTON_NONE, false, x, y);
g_pAppPlatform->setMouseDiff(event.motion.xrel * scale, event.motion.yrel * scale);
}
break;
}
case SDL_MOUSEWHEEL:
{
if (event.button.which != SDL_TOUCH_MOUSEID) {
Mouse::feed(BUTTON_SCROLLWHEEL, AppPlatform_sdl_base::GetMouseButtonState(event), Mouse::getX(), Mouse::getY());
}
break;
}
case SDL_CONTROLLERAXISMOTION:
g_pAppPlatform->handleControllerAxisEvent(event.caxis.which, event.caxis.axis, event.caxis.value);
break;
case SDL_FINGERDOWN:
case SDL_FINGERUP:
case SDL_FINGERMOTION: {
float x = event.tfinger.x * Minecraft::width;
float y = event.tfinger.y * Minecraft::height;
handle_touch(x, y, event.type, get_touch_id(event.tfinger.touchId, event.tfinger.fingerId));
break;
}
case SDL_TEXTINPUT:
{
if (g_pApp != nullptr)
{
size_t length = strlen(event.text.text);
for (size_t i = 0; i < length; i++)
{
char x = event.text.text[i];
if (x >= ' ' && x <= '~')
{
g_pApp->handleCharInput(x);
}
}
}
break;
}
case SDL_CONTROLLERDEVICEADDED:
g_pAppPlatform->gameControllerAdded(event.cdevice.which);
break;
case SDL_CONTROLLERDEVICEREMOVED:
g_pAppPlatform->gameControllerRemoved(event.cdevice.which);
break;
case SDL_WINDOWEVENT:
{
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
window_resized = true;
}
break;
}
case SDL_QUIT:
{
g_pApp->quit();
break;
}
}
}
}
// Resizing
static void resize()
{
int drawWidth, drawHeight;
SDL_GL_GetDrawableSize(window,
&drawWidth, &drawHeight);
int windowWidth, windowHeight;
SDL_GetWindowSize(window,
&windowWidth, &windowHeight);
Minecraft::width = drawWidth;
Minecraft::height = drawHeight;
// recalculate the point to pixel scale.
// This currently assumes that the aspect ratio is the same.
g_fPointToPixelScale = float(drawWidth) / float(windowWidth);
// Update the scale multiplier. We use the same value, because we pass to `sizeUpdate`, the window width/height.
// They will be multiplied by the GUI scale multiplier, becoming the drawwidth and drawheight, times the decided on GUI scale.
Minecraft::setRenderScaleMultiplier(g_fPointToPixelScale);
// give it an update.
// As said before, internally, this multiplies by the GUI scale multiplier
if (g_pApp)
g_pApp->sizeUpdate(windowWidth, windowHeight);
}
// Main Loop
static EM_BOOL main_loop(double time, void *user_data)
{
// Handle Events
handle_events();
// Screen Size
if (window_resized)
{
window_resized = false;
resize();
}
// Update MCPE
g_pApp->update();
// Swap Buffers
SDL_GL_SwapWindow(window);
if (g_pApp->wantToQuit())
{
g_pApp->saveOptions();
delete g_pApp;
delete g_pAppPlatform;
teardown();
// Stop Looping
return EM_FALSE;
}
else
{
// Keep Looping
return EM_TRUE;
}
}
// Main
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) < 0)
{
//LOG_E("Unable To Initialize SDL: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
// Configure OpenGL ES Context
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
#ifdef USE_GLES1_COMPATIBILITY_LAYER
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);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
#endif
// Double-Buffering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Window Size
#ifdef __EMSCRIPTEN__
Minecraft::width = std::stoi(argv[1]);
Minecraft::height = std::stoi(argv[2]);
#endif
// Lock To Landscape
SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight");
// Create Window
int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
window = SDL_CreateWindow("ReMinecraftPE", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Minecraft::width, Minecraft::height, flags);
if (!window)
{
//LOG_E("Unable to create SDL window");
exit(EXIT_FAILURE);
}
// Create OpenGL ES Context
context = SDL_GL_CreateContext(window);
if (!context)
{
const char* const GL_ERROR_MSG = "Unable to create OpenGL context";
//LOG_E(GL_ERROR_MSG);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "OpenGL Error", GL_ERROR_MSG, window);
exit(EXIT_FAILURE);
}
// Enable V-Sync
// Not setting this explicitly results in undefined behavior
if (SDL_GL_SetSwapInterval(-1) == -1) // Try adaptive
{
//LOG_W("Adaptive V-Sync is not supported on this platform. Falling back to standard V-Sync...");
// fallback to standard
if (SDL_GL_SetSwapInterval(1) == -1)
{
//LOG_W("Setting the swap interval for V-Sync is not supported on this platform!");
}
}
#ifdef _WIN32
xglInit();
if (!xglInitted())
{
const char* const GL_ERROR_MSG = "Error initializing GL extensions. OpenGL 2.0 or later is required. Update your graphics drivers!";
//LOG_E(GL_ERROR_MSG);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "OpenGL Error", GL_ERROR_MSG, window);
exit(EXIT_FAILURE);
}
#endif
// Setup Compatibility Layer If Needed
#ifdef USE_GLES1_COMPATIBILITY_LAYER
init_gles_compatibility_layer();
#endif
// Setup Teardown
#ifndef __EMSCRIPTEN__
atexit(teardown);
#endif
// Storage Directory
std::string storagePath;
#ifdef _WIN32
storagePath = getenv("APPDATA");
#elif defined(__EMSCRIPTEN__)
storagePath = "";
#elif defined(ANDROID)
storagePath = SDL_AndroidGetExternalStoragePath();
#else
storagePath = getenv("HOME");
#endif
storagePath += "/.reminecraftpe";
if (!storagePath.empty())
createFolderIfNotExists(storagePath.c_str());
// Start MCPE
g_pApp = new NinecraftApp;
g_pApp->m_externalStorageDir = storagePath;
g_pAppPlatform = new UsedAppPlatform(g_pApp->m_externalStorageDir, window);
g_pApp->m_pPlatform = g_pAppPlatform;
g_pApp->init();
// Set Size
resize();
// Loop
#ifndef __EMSCRIPTEN__
while (true)
{
EM_BOOL result = main_loop(0, nullptr);
if (result == EM_FALSE)
{
break;
}
}
#else
emscripten_request_animation_frame_loop(main_loop, nullptr);
#endif
return 0;
}