forked from nbcraft-org/nbcraft
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminecraftpeViewController.mm
More file actions
525 lines (440 loc) · 13.5 KB
/
minecraftpeViewController.mm
File metadata and controls
525 lines (440 loc) · 13.5 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//
// minecraftpeViewController.m
// minecraftpe
//
// Created by Brent on 10/12/23.
// Copyright (c) 2023 ReMinecraftPE. All rights reserved.
//
#import "minecraftpeViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "AppPlatform_iOS.h"
#include <string>
#include <vector>
#include "client/app/App.hpp"
#include "client/app/NinecraftApp.hpp"
#include "AppContext.hpp"
#include "client/player/input/Multitouch.hpp"
#include "client/gui/screens/ProgressScreen.hpp"
#include "EAGLView.h"
#include "ShowKeyboardView.h"
extern bool g_bIsMenuBackgroundAvailable;
NSThread *G_drawFrameThread = nil;
@interface minecraftpeViewController () {
GLuint _program;
float _rotation;
GLuint _vertexArray;
GLuint _vertexBuffer;
Minecraft *_app;
AppContext *_context;
AppPlatform_iOS *_platform;
UITouch *_touchMap[12];
int _dialogResultStatus;
std::vector<std::string> _dialogResultStrings;
ShowKeyboardView *_keyboardView;
//ThreadSafeQueue<std::function<void ()> > mMainQueue;
//ImagePickingCallback *mPickingCallback
float viewScale;
//GameControllerHandler_iOS *_gameControllerHandler;
//UIPopoverController *_imagePickerPopoverController;
}
@property (nonatomic, retain) EAGLContext *context;
@property (nonatomic, retain) CADisplayLink *displayLink;
//@property (strong, nonatomic) IASKAppSettingsViewController *appSettingsViewController;
- (void)initView;
- (int)_startTrackingTouch:(UITouch *)touch;
- (int)_stopTrackingTouch:(UITouch *)touch;
- (int)_getIndexForTouch:(UITouch *)touch;
@end
@implementation minecraftpeViewController
@synthesize animating, context, displayLink;
@synthesize platform = _platform;
- (int)width
{
CGRect bounds;
UIScreen *screen = [UIScreen mainScreen];
if (screen)
{
bounds = screen.bounds;
}
else
{
bounds.origin.x = 0, bounds.origin.y = 0;
bounds.size.width = 0, bounds.size.height = 0;
}
if (bounds.size.width > bounds.size.height)
bounds.size.height = bounds.size.width;
return bounds.size.width * self->viewScale;
}
- (int)height
{
CGRect bounds;
UIScreen *screen = [UIScreen mainScreen];
if (screen)
{
bounds = screen.bounds;
}
else
{
bounds.origin.x = 0, bounds.origin.y = 0;
bounds.size.width = 0, bounds.size.height = 0;
}
if (bounds.size.width > bounds.size.height)
bounds.size.height = bounds.size.width;
return bounds.size.height * self->viewScale;
}
- (void)updateDrawSize
{
// NOTE: Swapping width & height because of device orientation
// I guess when the device is sideways, the view doesn't rotate to be upright?
Minecraft::width = self.height; // drawWidth
Minecraft::height = self.width; // drawHeight
Minecraft::setRenderScaleMultiplier(self->viewScale);
self->_app->sizeUpdate(self.height / self->viewScale, self.width / self->viewScale); // windowWidth, windowHeight
NSLog(@"Updated draw size to %d, %d\n", self.height, self.width);
}
- (void)awakeFromNib
{
[super awakeFromNib];
// Moved to viewDidLoad - wasn't working here & supposedly makes no difference
}
- (void)drawFrame
{
if (!suspended)
{
NSThread *currentThread = [NSThread currentThread];
NSThread *thread = G_drawFrameThread;
if (!G_drawFrameThread)
{
G_drawFrameThread = currentThread;
thread = currentThread;
}
if (currentThread != thread)
{
NSLog(@"Warning! draw-frame thread changed (%@ -> %@)\n", G_drawFrameThread, thread);
G_drawFrameThread = thread;
}
[(EAGLView *)self.view setFramebuffer];
// In 0.12.2 we loop through self->mMainQueue & call some functions, then run _app->update() after each. Not sure what it's really doing though...
_app->update();
BOOL success = [(EAGLView *)self.view presentFramebuffer];
if (!success)
{
NSLog(@"Failed to present renderbuffer object %x\n", glCheckFramebufferStatus(GL_RENDERBUFFER));
}
}
}
- (void)viewDidLoad
{
//_keyboardView = [[ShowKeyboardView alloc] init];
[super viewDidLoad];
//EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
//
//if (!aContext)
//{
// aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
//}
EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!aContext)
NSLog(@"Failed to create ES context");
else if (![EAGLContext setCurrentContext:aContext])
NSLog(@"Failed to set ES context current");
self.context = aContext;
[(EAGLView *)self.view setContext:context];
[(EAGLView *)self.view setFramebuffer];
//if ([context API] == kEAGLRenderingAPIOpenGLES2)
// [self loadShaders];
animating = FALSE;
animationFrameInterval = 1;
self.displayLink = nil;
AppPlatform_iOS *platform = new AppPlatform_iOS(self);
self->_platform = platform;
AppContext *ctx = new AppContext();
ctx->platform = platform;
self->_context = ctx;
self->viewScale = 1.0;
self->suspended = NO;
NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1u, YES) objectAtIndex:0];
NinecraftApp *app = new NinecraftApp();
app->m_externalStorageDir = [dir UTF8String];
self->_app = app;
[self initView];
}
- (void)dealloc
{
//if (program)
//{
// glDeleteProgram(program);
// program = 0;
//}
// Tear down context.
if ([EAGLContext currentContext] == context)
[EAGLContext setCurrentContext:nil];
#if !__has_feature(objc_arc)
if (_app) delete _app;
if (_platform) delete _platform;
// This is what was done, but should we really be doing this?
if (_touchMap) delete[] (UITouch **)_touchMap;
[self.context release];
[super dealloc];
#endif
}
- (void)viewWillAppear:(BOOL)animated
{
[self startAnimation];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self stopAnimation];
[super viewWillDisappear:animated];
}
- (void)viewDidUnload
{
[super viewDidUnload];
//if (program)
//{
// glDeleteProgram(program);
// program = 0;
//}
// Tear down context.
if ([EAGLContext currentContext] == context)
[EAGLContext setCurrentContext:nil];
self.context = nil;
}
- (void)initView
{
_keyboardView = [[ShowKeyboardView alloc] initWithMinecraft:self->_app];
if ([self.view respondsToSelector:@selector(contentScaleFactor)])
{
self->viewScale = [self.view contentScaleFactor];
}
App *app = self->_app;
app->m_pPlatform = self->_context->platform;
app->init();
/*var1 = app->field_10;
app->var3 = *self->_context;
if ( var1 )
{
((void (__fastcall *)(App *))var0[15])(app);
}
else
{
((void (__fastcall *)(App *))var0[14])(app);
app->field_10 = 1;
}*/
[self updateDrawSize];
// Update draw size when device orientation changes (this accounts for typical view resizes)
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDrawSize) name:UIDeviceOrientationDidChangeNotification object:nil];
/*Minecraft *mc = (Minecraft *)app;
mc->selectLevel("TestWorld", "Test", (int)"iOS");
mc->hostMultiplayer();
mc->setScreen(new ProgressScreen);*/
}
- (NSInteger)animationFrameInterval
{
return animationFrameInterval;
}
- (void)setAnimationFrameInterval:(NSInteger)frameInterval
{
/*
Frame interval defines how many display frames must pass between each time the display link fires.
The display link will only fire 30 times a second when the frame internal is two on a display that refreshes 60 times a second. The default frame interval setting of one will fire 60 times a second when the display refreshes at 60 times a second. A frame interval setting of less than one results in undefined behavior.
*/
if (frameInterval >= 1)
{
animationFrameInterval = frameInterval;
NSLog(@"Set animationFrameInterval to %d\n", animationFrameInterval);
if (animating)
{
[self stopAnimation];
[self startAnimation];
}
}
}
- (void)startAnimation
{
if (![self isViewLoaded])
NSLog(@"Warning! Tried to startAnimation before view was loaded!\n");
if (!animating)
{
CADisplayLink *aDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame)];
[aDisplayLink setFrameInterval:animationFrameInterval];
[aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
self.displayLink = aDisplayLink;
NSLog(@"start-animation: %@\n", [NSThread currentThread]);
animating = TRUE;
}
}
- (void)stopAnimation
{
if (![self isViewLoaded])
NSLog(@"Warning! Tried to stopAnimation before view was loaded!\n");
if (animating)
{
[self.displayLink invalidate];
self.displayLink = nil;
animating = FALSE;
NSLog(@"stop-animation: %@\n", [NSThread currentThread]);
// HACK: applicationWillTerminate simply does not serve its purpose
_app->saveOptions();
}
}
- (void)setAudioEnabled:(BOOL)audioEnabled
{
if (audioEnabled)
_platform->getSoundSystem()->startEngine();
else
_platform->getSoundSystem()->stopEngine();
}
- (void)showKeyboard
{
for (UIView *view in self.view.subviews)
{
if ([view isKindOfClass:[ShowKeyboardView class]])
{
[((ShowKeyboardView*)view) showKeyboard];
}
}
[self.view insertSubview:_keyboardView atIndex:0];
[_keyboardView showKeyboard];
}
- (void)hideKeyboard
{
for (UIView *view in self.view.subviews)
{
if ([view isKindOfClass:[ShowKeyboardView class]])
{
[((ShowKeyboardView*)view) hideKeyboard];
[((ShowKeyboardView*)view) removeFromSuperview];
}
}
}
- (void)handleCharInput:(char)c
{
_app->handleCharInput(c);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown &&
interfaceOrientation != UIInterfaceOrientationPortrait
);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
int touchIndex = [self _startTrackingTouch: touch];
if (touchIndex > -1)
{
CGPoint point;
float posX, posY;
if (touch)
{
point = [touch locationInView:self.view];
}
else
{
point.x = 0, point.y = 0;
//point2.x = 0, point2.y = 0;
}
posX = viewScale * point.x;
posY = viewScale * point.y;
Mouse::feed(BUTTON_LEFT, true, posX, posY);
Multitouch::feed(BUTTON_LEFT, true, posX, posY, touchIndex);
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
int touchIndex = [self _getIndexForTouch: touch];
if (touchIndex > -1)
{
CGPoint point;
float posX, posY;
if (touch)
{
point = [touch locationInView:self.view];
}
else
{
point.x = 0, point.y = 0;
//point2.x = 0, point2.y = 0;
}
posX = viewScale * point.x;
posY = viewScale * point.y;
Mouse::feed(BUTTON_NONE, false, posX, posY);
Multitouch::feed(BUTTON_NONE, false, posX, posY, touchIndex);
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
int touchIndex = [self _stopTrackingTouch: touch];
if (touchIndex > -1)
{
CGPoint point;
float posX, posY;
if (touch)
{
point = [touch locationInView:self.view];
}
else
{
point.x = 0, point.y = 0;
//point2.x = 0, point2.y = 0;
}
posX = viewScale * point.x;
posY = viewScale * point.y;
Mouse::feed(BUTTON_LEFT, false, posX, posY);
Multitouch::feed(BUTTON_LEFT, false, posX, posY, touchIndex);
}
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
- (int)_startTrackingTouch:(UITouch *)touch
{
for (int i = 0; i < 12; i++)
{
if (!_touchMap[i])
{
_touchMap[i] = touch;
return i;
}
}
return -1;
}
- (int)_stopTrackingTouch:(UITouch *)touch
{
for (int i = 0; i < 12; i++)
{
if (_touchMap[i] == touch)
{
_touchMap[i] = nil;
return i;
}
}
return -1;
}
- (int)_getIndexForTouch:(UITouch *)touch
{
for (int i = 0; i < 12; i++)
{
if (_touchMap[i] == touch)
return i;
}
return -1;
}
@end