+++ /dev/null
-// Copyright (C) 2015 Patryk Nadrowski\r
-// This file is part of the "Irrlicht Engine".\r
-// For conditions of distribution and use, see copyright notice in Irrlicht.h\r
-\r
-#include "CEAGLManager.h"\r
-\r
-#ifdef _IRR_COMPILE_WITH_EAGL_MANAGER_\r
-\r
-#include "irrString.h"\r
-#include "os.h"\r
-\r
-#import <UIKit/UIKit.h>\r
-#import <OpenGLES/EAGL.h>\r
-\r
-#if defined(_IRR_COMPILE_WITH_OGLES1_)\r
-#include <OpenGLES/ES1/gl.h>\r
-#include <OpenGLES/ES1/glext.h>\r
-#elif defined(_IRR_COMPILE_WITH_OGLES2_)\r
-#include <OpenGLES/ES2/gl.h>\r
-#include <OpenGLES/ES2/glext.h>\r
-#endif\r
-\r
-namespace irr\r
-{\r
-namespace video\r
-{\r
-\r
-struct SEAGLManagerDataStorage\r
-{\r
- SEAGLManagerDataStorage() : Layer(0), Context(0)\r
- {\r
- }\r
-\r
- CAEAGLLayer* Layer;\r
- EAGLContext* Context;\r
-};\r
-\r
-CEAGLManager::CEAGLManager() : IContextManager(), Configured(false), DataStorage(0)\r
-{\r
-#ifdef _DEBUG\r
- setDebugName("CEAGLManager");\r
-#endif\r
-\r
- DataStorage = new SEAGLManagerDataStorage();\r
-}\r
-\r
-CEAGLManager::~CEAGLManager()\r
-{\r
- destroyContext();\r
- destroySurface();\r
- terminate();\r
-\r
- delete static_cast<SEAGLManagerDataStorage*>(DataStorage);\r
-}\r
-\r
-bool CEAGLManager::initialize(const SIrrlichtCreationParameters& params, const SExposedVideoData& data)\r
-{\r
- SEAGLManagerDataStorage* dataStorage = static_cast<SEAGLManagerDataStorage*>(DataStorage);\r
-\r
- if (dataStorage->Layer != nil)\r
- return true;\r
-\r
- Params = params;\r
- Data = data;\r
-\r
- UIView* view = (__bridge UIView*)data.OpenGLiOS.View;\r
-\r
- if (view == nil || ![[view layer] isKindOfClass:[CAEAGLLayer class]])\r
- {\r
- os::Printer::log("Could not get EAGL display.");\r
- return false;\r
- }\r
-\r
- dataStorage->Layer = (CAEAGLLayer*)[view layer];\r
-\r
- return true;\r
-}\r
-\r
-void CEAGLManager::terminate()\r
-{\r
- SEAGLManagerDataStorage* dataStorage = static_cast<SEAGLManagerDataStorage*>(DataStorage);\r
-\r
- [EAGLContext setCurrentContext:0];\r
-\r
- destroySurface();\r
-\r
- if (dataStorage->Layer != nil)\r
- dataStorage->Layer = 0;\r
-}\r
-\r
-bool CEAGLManager::generateSurface()\r
-{\r
- SEAGLManagerDataStorage* dataStorage = static_cast<SEAGLManagerDataStorage*>(DataStorage);\r
- CAEAGLLayer* layer = dataStorage->Layer;\r
-\r
- if (layer == nil)\r
- return false;\r
-\r
- if (Configured)\r
- return true;\r
-\r
- NSDictionary* attribs = [NSDictionary dictionaryWithObjectsAndKeys:\r
- [NSNumber numberWithBool:NO],\r
- kEAGLDrawablePropertyRetainedBacking,\r
- (Params.Bits > 16) ? kEAGLColorFormatRGBA8 : kEAGLColorFormatRGB565,\r
- kEAGLDrawablePropertyColorFormat,\r
- nil];\r
-\r
- [layer setOpaque:(Params.WithAlphaChannel) ? YES : NO];\r
- [layer setDrawableProperties:attribs];\r
-\r
- Configured = true;\r
-\r
- return true;\r
-}\r
-\r
-void CEAGLManager::destroySurface()\r
-{\r
- SEAGLManagerDataStorage* dataStorage = static_cast<SEAGLManagerDataStorage*>(DataStorage);\r
- CAEAGLLayer* layer = dataStorage->Layer;\r
-\r
- if (layer == nil)\r
- return;\r
-\r
- [layer setOpaque:NO];\r
- [layer setDrawableProperties:nil];\r
-\r
- Configured = false;\r
-}\r
-\r
-bool CEAGLManager::generateContext()\r
-{\r
- SEAGLManagerDataStorage* dataStorage = static_cast<SEAGLManagerDataStorage*>(DataStorage);\r
-\r
- if (dataStorage->Context != nil || !Configured)\r
- return false;\r
-\r
- EAGLRenderingAPI OpenGLESVersion = kEAGLRenderingAPIOpenGLES2;\r
-\r
- switch (Params.DriverType)\r
- {\r
- case EDT_OGLES1:\r
- OpenGLESVersion = kEAGLRenderingAPIOpenGLES1;\r
- break;\r
- case EDT_OGLES2:\r
- OpenGLESVersion = kEAGLRenderingAPIOpenGLES2;\r
- break;\r
- default:\r
- break;\r
- }\r
-\r
- dataStorage->Context = [[EAGLContext alloc] initWithAPI:OpenGLESVersion];\r
-\r
- if (dataStorage->Context == nil)\r
- {\r
- os::Printer::log("Could not create EAGL context.", ELL_ERROR);\r
- return false;\r
- }\r
-\r
- Data.OpenGLiOS.Context = (__bridge void*)dataStorage->Context;\r
-\r
- os::Printer::log("EAGL context created with OpenGLESVersion: ", core::stringc(static_cast<int>(OpenGLESVersion)), ELL_DEBUG);\r
-\r
- return true;\r
-}\r
-\r
-void CEAGLManager::destroyContext()\r
-{\r
- SEAGLManagerDataStorage* dataStorage = static_cast<SEAGLManagerDataStorage*>(DataStorage);\r
-\r
- [dataStorage->Context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:nil];\r
-\r
- if (FrameBuffer.BufferID != 0)\r
- {\r
- glDeleteFramebuffersOES(1, &FrameBuffer.BufferID);\r
- FrameBuffer.BufferID = 0;\r
- }\r
-\r
- if (FrameBuffer.ColorBuffer != 0)\r
- {\r
- glDeleteRenderbuffersOES(1, &FrameBuffer.ColorBuffer);\r
- FrameBuffer.ColorBuffer = 0;\r
- }\r
-\r
- if (FrameBuffer.DepthBuffer != 0)\r
- {\r
- glDeleteRenderbuffersOES(1, &FrameBuffer.DepthBuffer);\r
- FrameBuffer.DepthBuffer = 0;\r
- }\r
-\r
- [EAGLContext setCurrentContext:0];\r
-\r
- if (dataStorage->Context != nil)\r
- dataStorage->Context = 0;\r
-\r
- Data.OpenGLiOS.Context = 0;\r
-}\r
-\r
-bool CEAGLManager::activateContext(const SExposedVideoData& videoData, bool restorePrimaryOnZero)\r
-{\r
- SEAGLManagerDataStorage* dataStorage = static_cast<SEAGLManagerDataStorage*>(DataStorage);\r
- EAGLContext* context = dataStorage->Context;\r
-\r
- bool status = false;\r
-\r
- if (context != nil)\r
- {\r
- status = ([EAGLContext currentContext] == context || [EAGLContext setCurrentContext:context]);\r
- }\r
-\r
- if (status)\r
- {\r
- if (FrameBuffer.ColorBuffer == 0)\r
- {\r
- glGenRenderbuffersOES(1, &FrameBuffer.ColorBuffer);\r
- glBindRenderbufferOES(GL_RENDERBUFFER_OES, FrameBuffer.ColorBuffer);\r
- [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:dataStorage->Layer];\r
- }\r
-\r
- if (FrameBuffer.DepthBuffer == 0)\r
- {\r
- GLenum depth = (Params.ZBufferBits >= 24) ? GL_DEPTH_COMPONENT24_OES : GL_DEPTH_COMPONENT16_OES;\r
-\r
- glGenRenderbuffersOES(1, &FrameBuffer.DepthBuffer);\r
- glBindRenderbufferOES(GL_RENDERBUFFER_OES, FrameBuffer.DepthBuffer);\r
- glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depth, Params.WindowSize.Width, Params.WindowSize.Height);\r
- }\r
-\r
- if (FrameBuffer.BufferID == 0)\r
- {\r
- glGenFramebuffersOES(1, &FrameBuffer.BufferID);\r
- glBindFramebufferOES(GL_FRAMEBUFFER_OES, FrameBuffer.BufferID);\r
- glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, FrameBuffer.ColorBuffer);\r
- glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, FrameBuffer.DepthBuffer);\r
- }\r
-\r
- glBindFramebufferOES(GL_FRAMEBUFFER_OES, FrameBuffer.BufferID);\r
- }\r
- else\r
- {\r
- os::Printer::log("Could not make EGL context current.");\r
- }\r
-\r
- return status;\r
-}\r
-\r
-const SExposedVideoData& CEAGLManager::getContext() const\r
-{\r
- return Data;\r
-}\r
-\r
-bool CEAGLManager::swapBuffers()\r
-{\r
- SEAGLManagerDataStorage* dataStorage = static_cast<SEAGLManagerDataStorage*>(DataStorage);\r
- EAGLContext* context = dataStorage->Context;\r
-\r
- bool status = false;\r
-\r
- if (context != nil && context == [EAGLContext currentContext])\r
- {\r
- glBindRenderbufferOES(GL_RENDERBUFFER_OES, FrameBuffer.ColorBuffer);\r
- [context presentRenderbuffer:GL_RENDERBUFFER_OES];\r
-\r
- status = true;\r
- }\r
-\r
- return status;\r
-}\r
-\r
-}\r
-}\r
-\r
-#endif\r
+++ /dev/null
-// Copyright (C) 2002-2008 Nikolaus Gebhardt\r
-// Copyright (C) 2008 Redshift Software, Inc.\r
-// Copyright (C) 2012 Patryk Nadrowski\r
-// This file is part of the "Irrlicht Engine".\r
-// For conditions of distribution and use, see copyright notice in irrlicht.h\r
-\r
-#import "CIrrDeviceiOS.h"\r
-\r
-#ifdef _IRR_COMPILE_WITH_IOS_DEVICE_\r
-\r
-#include "IFileSystem.h"\r
-#include "CTimer.h"\r
-#include "CEAGLManager.h"\r
-\r
-#import <UIKit/UIKit.h>\r
-#import <CoreMotion/CoreMotion.h>\r
-\r
-/* Important information */\r
-\r
-// The application state events and following methods: IrrlichtDevice::isWindowActive, IrrlichtDevice::isWindowFocused\r
-// and IrrlichtDevice::isWindowMinimized works out of box only if you'll use built-in CIrrDelegateiOS,\r
-// so _IRR_COMPILE_WITH_IOS_BUILTIN_MAIN_ must be enabled in this case. If you need a custom UIApplicationDelegate you must\r
-// handle all application events yourself.\r
-\r
-#ifdef _IRR_COMPILE_WITH_IOS_BUILTIN_MAIN_\r
-\r
-namespace irr\r
-{\r
- class CIrrDeviceiOS;\r
-}\r
-\r
-/* CIrrDelegateiOS */\r
-\r
-@interface CIrrDelegateiOS : NSObject<UIApplicationDelegate>\r
-\r
-- (void)setDevice:(irr::CIrrDeviceiOS*)device;\r
-- (bool)isActive;\r
-- (bool)hasFocus;\r
-\r
-@property (strong, nonatomic) UIWindow* window;\r
-\r
-@end\r
-\r
-@implementation CIrrDelegateiOS\r
-{\r
- irr::CIrrDeviceiOS* Device;\r
- bool Active;\r
- bool Focus;\r
-}\r
-\r
-- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)options\r
-{\r
- Device = nil;\r
- Active = true;\r
- Focus = false;\r
- \r
- [self performSelectorOnMainThread:@selector(runIrrlicht) withObject:nil waitUntilDone:NO];\r
- \r
- return YES;\r
-}\r
-\r
-- (void)applicationWillTerminate:(UIApplication*)application\r
-{\r
- if (Device != nil)\r
- {\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_APPLICATION_EVENT;\r
- ev.ApplicationEvent.EventType = irr::EAET_WILL_TERMINATE;\r
-\r
- Device->postEventFromUser(ev);\r
- \r
- Device->closeDevice();\r
- }\r
-}\r
-\r
-- (void)applicationDidReceiveMemoryWarning:(UIApplication*)application\r
-{\r
- if (Device != nil)\r
- {\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_APPLICATION_EVENT;\r
- ev.ApplicationEvent.EventType = irr::EAET_MEMORY_WARNING;\r
- \r
- Device->postEventFromUser(ev);\r
- }\r
-}\r
-\r
-- (void)applicationWillResignActive:(UIApplication*)application\r
-{\r
- if (Device != nil)\r
- {\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_APPLICATION_EVENT;\r
- ev.ApplicationEvent.EventType = irr::EAET_WILL_PAUSE;\r
- \r
- Device->postEventFromUser(ev);\r
- }\r
- \r
- Focus = false;\r
-}\r
-\r
-- (void)applicationDidEnterBackground:(UIApplication*)application\r
-{\r
- if (Device != nil)\r
- {\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_APPLICATION_EVENT;\r
- ev.ApplicationEvent.EventType = irr::EAET_DID_PAUSE;\r
- \r
- Device->postEventFromUser(ev);\r
- }\r
- \r
- Active = false;\r
-}\r
-\r
-- (void)applicationWillEnterForeground:(UIApplication*)application\r
-{\r
- if (Device != nil)\r
- {\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_APPLICATION_EVENT;\r
- ev.ApplicationEvent.EventType = irr::EAET_WILL_RESUME;\r
- \r
- Device->postEventFromUser(ev);\r
- }\r
- \r
- Active = true;\r
-}\r
-\r
-- (void)applicationDidBecomeActive:(UIApplication*)application\r
-{\r
- if (Device != nil)\r
- {\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_APPLICATION_EVENT;\r
- ev.ApplicationEvent.EventType = irr::EAET_DID_RESUME;\r
- \r
- Device->postEventFromUser(ev);\r
- }\r
- \r
- Focus = true;\r
-}\r
-\r
-- (void)runIrrlicht\r
-{\r
- irrlicht_main();\r
-}\r
-\r
-- (void)setDevice:(irr::CIrrDeviceiOS*)device\r
-{\r
- Device = device;\r
-}\r
-\r
-- (bool)isActive\r
-{\r
- return Active;\r
-}\r
-\r
-- (bool)hasFocus\r
-{\r
- return Focus;\r
-}\r
-\r
-@end\r
-\r
-#endif\r
-\r
-/* CIrrViewiOS */\r
-\r
-@interface CIrrViewiOS : UIView\r
-\r
-- (id)initWithFrame:(CGRect)frame forDevice:(irr::CIrrDeviceiOS*)device;\r
-\r
-@end\r
-\r
-@implementation CIrrViewiOS\r
-{\r
- irr::CIrrDeviceiOS* Device;\r
- float Scale;\r
-}\r
-\r
-- (id)initWithFrame:(CGRect)frame forDevice:(irr::CIrrDeviceiOS*)device;\r
-{\r
- self = [super initWithFrame:frame];\r
- \r
- if (self)\r
- {\r
- Device = device;\r
- Scale = ([self respondsToSelector:@selector(setContentScaleFactor:)]) ? [[UIScreen mainScreen] scale] : 1.f;\r
- }\r
- \r
- return self;\r
-}\r
-\r
-- (BOOL)isMultipleTouchEnabled\r
-{\r
- return YES;\r
-}\r
-\r
-- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event\r
-{\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_TOUCH_INPUT_EVENT;\r
- ev.TouchInput.Event = irr::ETIE_PRESSED_DOWN;\r
- \r
- for (UITouch* touch in touches)\r
- {\r
- ev.TouchInput.ID = (size_t)touch;\r
-\r
- CGPoint touchPoint = [touch locationInView:self];\r
- \r
- ev.TouchInput.X = touchPoint.x*Scale;\r
- ev.TouchInput.Y = touchPoint.y*Scale;\r
-\r
- Device->postEventFromUser(ev);\r
- }\r
-}\r
-\r
-- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event\r
-{\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_TOUCH_INPUT_EVENT;\r
- ev.TouchInput.Event = irr::ETIE_MOVED;\r
- \r
- for (UITouch* touch in touches)\r
- {\r
- ev.TouchInput.ID = (size_t)touch;\r
-\r
- CGPoint touchPoint = [touch locationInView:self];\r
- \r
- ev.TouchInput.X = touchPoint.x*Scale;\r
- ev.TouchInput.Y = touchPoint.y*Scale;\r
- \r
- Device->postEventFromUser(ev);\r
- }\r
-}\r
-\r
-- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event\r
-{\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_TOUCH_INPUT_EVENT;\r
- ev.TouchInput.Event = irr::ETIE_LEFT_UP;\r
- \r
- for (UITouch* touch in touches)\r
- {\r
- ev.TouchInput.ID = (size_t)touch;\r
-\r
- CGPoint touchPoint = [touch locationInView:self];\r
- \r
- ev.TouchInput.X = touchPoint.x*Scale;\r
- ev.TouchInput.Y = touchPoint.y*Scale;\r
- \r
- Device->postEventFromUser(ev);\r
- }\r
-}\r
-\r
-- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event\r
-{\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_TOUCH_INPUT_EVENT;\r
- ev.TouchInput.Event = irr::ETIE_LEFT_UP;\r
- \r
- for (UITouch* touch in touches)\r
- {\r
- ev.TouchInput.ID = (size_t)touch;\r
-\r
- CGPoint touchPoint = [touch locationInView:self];\r
- \r
- ev.TouchInput.X = touchPoint.x*Scale;\r
- ev.TouchInput.Y = touchPoint.y*Scale;\r
- \r
- Device->postEventFromUser(ev);\r
- }\r
-}\r
-\r
-@end\r
-\r
-/* CIrrViewEAGLiOS */\r
-\r
-@interface CIrrViewEAGLiOS : CIrrViewiOS\r
-\r
-@end\r
-\r
-@implementation CIrrViewEAGLiOS\r
-\r
-+ (Class)layerClass\r
-{\r
- return [CAEAGLLayer class];\r
-}\r
-\r
-@end\r
-\r
-namespace irr\r
-{\r
- namespace video\r
- {\r
- IVideoDriver* createOGLES1Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);\r
- \r
- IVideoDriver* createOGLES2Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);\r
- }\r
- \r
- struct SIrrDeviceiOSDataStorage\r
- {\r
- SIrrDeviceiOSDataStorage() : Window(0), ViewController(0), View(0), MotionManager(0), ReferenceAttitude(0)\r
- {\r
- MotionManager = [[CMMotionManager alloc] init];\r
- }\r
- \r
- UIWindow* Window;\r
- UIViewController* ViewController;\r
- CIrrViewiOS* View;\r
- CMMotionManager* MotionManager;\r
- CMAttitude* ReferenceAttitude;\r
- };\r
- \r
- CIrrDeviceiOS::CIrrDeviceiOS(const SIrrlichtCreationParameters& params) : CIrrDeviceStub(params), DataStorage(0), Close(false)\r
- {\r
-#ifdef _DEBUG\r
- setDebugName("CIrrDeviceiOS");\r
-#endif\r
- \r
-#ifdef _IRR_COMPILE_WITH_IOS_BUILTIN_MAIN_\r
- CIrrDelegateiOS* delegate = [UIApplication sharedApplication].delegate;\r
- [delegate setDevice:this];\r
-#endif\r
- \r
- DataStorage = new SIrrDeviceiOSDataStorage();\r
-\r
- FileSystem->changeWorkingDirectoryTo([[[NSBundle mainBundle] resourcePath] UTF8String]);\r
-\r
- createWindow();\r
- createViewAndDriver();\r
- \r
- if (!VideoDriver)\r
- return;\r
- \r
- createGUIAndScene();\r
- }\r
-\r
- CIrrDeviceiOS::~CIrrDeviceiOS()\r
- {\r
- deactivateDeviceMotion();\r
- deactivateGyroscope();\r
- deactivateAccelerometer();\r
- \r
- delete static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
-\r
-#ifdef _IRR_COMPILE_WITH_IOS_BUILTIN_MAIN_\r
- CIrrDelegateiOS* delegate = [UIApplication sharedApplication].delegate;\r
- [delegate setDevice:nil];\r
-#endif\r
- }\r
-\r
- bool CIrrDeviceiOS::run()\r
- {\r
- if (!Close)\r
- {\r
- const CFTimeInterval timeInSeconds = 0.000002;\r
- \r
- s32 result = 0;\r
- \r
- do\r
- {\r
- result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeInSeconds, TRUE);\r
- }\r
- while (result == kCFRunLoopRunHandledSource);\r
- \r
- os::Timer::tick();\r
- \r
- //! Update events\r
- \r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- CMMotionManager* motionManager = dataStorage->MotionManager;\r
- \r
- //! Accelerometer\r
- if (motionManager.isAccelerometerActive)\r
- {\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_ACCELEROMETER_EVENT;\r
- ev.AccelerometerEvent.X = motionManager.accelerometerData.acceleration.x;\r
- ev.AccelerometerEvent.Y = motionManager.accelerometerData.acceleration.y;\r
- ev.AccelerometerEvent.Z = motionManager.accelerometerData.acceleration.z;\r
- \r
- postEventFromUser(ev);\r
- }\r
- \r
- //! Gyroscope\r
- if (motionManager.isGyroActive)\r
- {\r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_GYROSCOPE_EVENT;\r
- ev.GyroscopeEvent.X = motionManager.gyroData.rotationRate.x;\r
- ev.GyroscopeEvent.Y = motionManager.gyroData.rotationRate.y;\r
- ev.GyroscopeEvent.Z = motionManager.gyroData.rotationRate.z;\r
- \r
- postEventFromUser(ev);\r
- }\r
- \r
- //! Device Motion\r
- if (motionManager.isDeviceMotionActive)\r
- {\r
- CMAttitude* currentAttitude = motionManager.deviceMotion.attitude;\r
- CMAttitude* referenceAttitude = dataStorage->ReferenceAttitude;\r
- \r
- if (referenceAttitude != nil)\r
- [currentAttitude multiplyByInverseOfAttitude: referenceAttitude];\r
- else\r
- referenceAttitude = motionManager.deviceMotion.attitude;\r
- \r
- irr::SEvent ev;\r
- ev.EventType = irr::EET_DEVICE_MOTION_EVENT;\r
- ev.AccelerometerEvent.X = currentAttitude.roll;\r
- ev.AccelerometerEvent.Y = currentAttitude.pitch;\r
- ev.AccelerometerEvent.Z = currentAttitude.yaw;\r
- \r
- postEventFromUser(ev);\r
- }\r
- }\r
-\r
- return !Close;\r
- }\r
-\r
- void CIrrDeviceiOS::yield()\r
- {\r
- struct timespec ts = {0,0};\r
- nanosleep(&ts, NULL);\r
- }\r
-\r
- void CIrrDeviceiOS::sleep(u32 timeMs, bool pauseTimer=false)\r
- {\r
- bool wasStopped = Timer ? Timer->isStopped() : true;\r
- \r
- struct timespec ts;\r
- ts.tv_sec = (time_t) (timeMs / 1000);\r
- ts.tv_nsec = (long) (timeMs % 1000) * 1000000;\r
- \r
- if (pauseTimer && !wasStopped)\r
- Timer->stop();\r
- \r
- nanosleep(&ts, NULL);\r
- \r
- if (pauseTimer && !wasStopped)\r
- Timer->start();\r
- }\r
-\r
- void CIrrDeviceiOS::setWindowCaption(const wchar_t* text)\r
- {\r
- }\r
- \r
- bool CIrrDeviceiOS::isWindowActive() const\r
- {\r
-#ifdef _IRR_COMPILE_WITH_IOS_BUILTIN_MAIN_\r
- CIrrDelegateiOS* delegate = [UIApplication sharedApplication].delegate;\r
- \r
- return [delegate isActive];\r
-#else\r
- return false;\r
-#endif\r
- }\r
- \r
- bool CIrrDeviceiOS::isWindowFocused() const\r
- {\r
-#ifdef _IRR_COMPILE_WITH_IOS_BUILTIN_MAIN_\r
- CIrrDelegateiOS* delegate = [UIApplication sharedApplication].delegate;\r
- \r
- return [delegate hasFocus];\r
-#else\r
- return false;\r
-#endif\r
- }\r
- \r
- bool CIrrDeviceiOS::isWindowMinimized() const\r
- {\r
-#ifdef _IRR_COMPILE_WITH_IOS_BUILTIN_MAIN_\r
- CIrrDelegateiOS* delegate = [UIApplication sharedApplication].delegate;\r
- \r
- return ![delegate isActive];\r
-#else\r
- return false;\r
-#endif\r
- }\r
-\r
- void CIrrDeviceiOS::closeDevice()\r
- {\r
- CFRunLoopStop(CFRunLoopGetMain());\r
- \r
- Close = true;\r
- }\r
-\r
- void CIrrDeviceiOS::setResizable(bool resize)\r
- {\r
- }\r
-\r
- void CIrrDeviceiOS::minimizeWindow()\r
- {\r
- }\r
-\r
- void CIrrDeviceiOS::maximizeWindow()\r
- {\r
- }\r
-\r
- void CIrrDeviceiOS::restoreWindow()\r
- {\r
- }\r
- \r
- core::position2di CIrrDeviceiOS::getWindowPosition()\r
- {\r
- return core::position2di(0, 0);\r
- }\r
-\r
- bool CIrrDeviceiOS::activateAccelerometer(float updateInterval)\r
- {\r
- bool status = false;\r
- \r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- CMMotionManager* motionManager = dataStorage->MotionManager;\r
- \r
- if (motionManager.isAccelerometerAvailable)\r
- {\r
- if (!motionManager.isAccelerometerActive)\r
- {\r
- motionManager.accelerometerUpdateInterval = updateInterval;\r
- [motionManager startAccelerometerUpdates];\r
- }\r
- \r
- status = true;\r
- }\r
- \r
- return status;\r
- }\r
-\r
- bool CIrrDeviceiOS::deactivateAccelerometer()\r
- {\r
- bool status = false;\r
- \r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- CMMotionManager* motionManager = dataStorage->MotionManager;\r
- \r
- if (motionManager.isAccelerometerAvailable)\r
- {\r
- if (motionManager.isAccelerometerActive)\r
- [motionManager stopAccelerometerUpdates];\r
- \r
- status = true;\r
- }\r
- \r
- return status;\r
- }\r
- \r
- bool CIrrDeviceiOS::isAccelerometerActive()\r
- {\r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
-\r
- return (dataStorage->MotionManager.isAccelerometerActive);\r
- }\r
-\r
- bool CIrrDeviceiOS::isAccelerometerAvailable()\r
- {\r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- \r
- return (dataStorage->MotionManager.isAccelerometerAvailable);\r
- }\r
- \r
- bool CIrrDeviceiOS::activateGyroscope(float updateInterval)\r
- {\r
- bool status = false;\r
- \r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- CMMotionManager* motionManager = dataStorage->MotionManager;\r
- \r
- if (motionManager.isGyroAvailable)\r
- {\r
- if (!motionManager.isGyroActive)\r
- {\r
- motionManager.gyroUpdateInterval = updateInterval;\r
- [motionManager startGyroUpdates];\r
- }\r
- \r
- status = true;\r
- }\r
- \r
- return status;\r
- }\r
-\r
- bool CIrrDeviceiOS::deactivateGyroscope()\r
- {\r
- bool status = false;\r
- \r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- CMMotionManager* motionManager = dataStorage->MotionManager;\r
- \r
- if (motionManager.isGyroAvailable)\r
- {\r
- if (motionManager.isGyroActive)\r
- [motionManager stopGyroUpdates];\r
- \r
- status = true;\r
- }\r
- \r
- return status;\r
- }\r
- \r
- bool CIrrDeviceiOS::isGyroscopeActive()\r
- {\r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- \r
- return (dataStorage->MotionManager.isGyroActive);\r
- }\r
-\r
- bool CIrrDeviceiOS::isGyroscopeAvailable()\r
- {\r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- \r
- return (dataStorage->MotionManager.isGyroAvailable);\r
- }\r
- \r
- bool CIrrDeviceiOS::activateDeviceMotion(float updateInterval)\r
- {\r
- bool status = false;\r
- \r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- CMMotionManager* motionManager = dataStorage->MotionManager;\r
- \r
- if (motionManager.isDeviceMotionAvailable)\r
- {\r
- if (!motionManager.isDeviceMotionActive)\r
- {\r
- dataStorage->ReferenceAttitude = nil;\r
- \r
- motionManager.deviceMotionUpdateInterval = updateInterval;\r
- [motionManager startDeviceMotionUpdates];\r
- }\r
- \r
- status = true;\r
- }\r
- \r
- return status;\r
- }\r
- \r
- bool CIrrDeviceiOS::deactivateDeviceMotion()\r
- {\r
- bool status = false;\r
- \r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- CMMotionManager* motionManager = dataStorage->MotionManager;\r
- \r
- if (motionManager.isDeviceMotionAvailable)\r
- {\r
- if (motionManager.isDeviceMotionActive)\r
- {\r
- [motionManager stopDeviceMotionUpdates];\r
- \r
- dataStorage->ReferenceAttitude = nil;\r
- }\r
- \r
- status = true;\r
- }\r
- \r
- return status;\r
- }\r
- \r
- bool CIrrDeviceiOS::isDeviceMotionActive()\r
- {\r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- \r
- return (dataStorage->MotionManager.isDeviceMotionActive);\r
- }\r
- \r
- bool CIrrDeviceiOS::isDeviceMotionAvailable()\r
- {\r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- \r
- return (dataStorage->MotionManager.isDeviceMotionAvailable);\r
- }\r
- \r
- E_DEVICE_TYPE CIrrDeviceiOS::getType() const\r
- {\r
- return EIDT_IOS;\r
- }\r
-\r
- void CIrrDeviceiOS::createWindow()\r
- {\r
- if (CreationParams.DriverType != video::EDT_NULL)\r
- {\r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- \r
- UIView* externalView = (__bridge UIView*)CreationParams.WindowId;\r
- \r
- if (externalView == nil)\r
- {\r
- dataStorage->Window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];\r
- dataStorage->ViewController = [[UIViewController alloc] init];\r
- dataStorage->Window.rootViewController = dataStorage->ViewController;\r
- \r
- [dataStorage->Window makeKeyAndVisible];\r
- }\r
- else\r
- {\r
- dataStorage->Window = externalView.window;\r
- \r
- UIResponder* currentResponder = externalView.nextResponder;\r
- \r
- do\r
- {\r
- if ([currentResponder isKindOfClass:[UIViewController class]])\r
- {\r
- dataStorage->ViewController = (UIViewController*)currentResponder;\r
- \r
- currentResponder = nil;\r
- }\r
- else if ([currentResponder isKindOfClass:[UIView class]])\r
- {\r
- currentResponder = currentResponder.nextResponder;\r
- }\r
- else\r
- {\r
- currentResponder = nil;\r
- \r
- // Could not find view controller.\r
- _IRR_DEBUG_BREAK_IF(true);\r
- }\r
- }\r
- while (currentResponder != nil);\r
- }\r
- }\r
- }\r
- \r
- void CIrrDeviceiOS::createViewAndDriver()\r
- {\r
- SIrrDeviceiOSDataStorage* dataStorage = static_cast<SIrrDeviceiOSDataStorage*>(DataStorage);\r
- \r
- video::SExposedVideoData data;\r
- data.OpenGLiOS.Window = (__bridge void*)dataStorage->Window;\r
- data.OpenGLiOS.ViewController = (__bridge void*)dataStorage->ViewController;\r
- \r
- UIView* externalView = (__bridge UIView*)CreationParams.WindowId;\r
- \r
- CGRect resolution = (externalView == nil) ? [[UIScreen mainScreen] bounds] : externalView.bounds;\r
-\r
- switch (CreationParams.DriverType)\r
- {\r
- case video::EDT_OGLES1:\r
-#ifdef _IRR_COMPILE_WITH_OGLES1_\r
- {\r
- CIrrViewEAGLiOS* view = [[CIrrViewEAGLiOS alloc] initWithFrame:resolution forDevice:this];\r
- CreationParams.WindowSize = core::dimension2d<u32>(view.frame.size.width, view.frame.size.height);\r
- \r
- dataStorage->View = view;\r
- data.OpenGLiOS.View = (__bridge void*)view;\r
-\r
- ContextManager = new video::CEAGLManager();\r
- ContextManager->initialize(CreationParams, data);\r
- \r
- VideoDriver = video::createOGLES1Driver(CreationParams, FileSystem, ContextManager);\r
- \r
- if (!VideoDriver)\r
- os::Printer::log("Could not create OpenGL ES 1.x driver.", ELL_ERROR);\r
- }\r
-#else\r
- os::Printer::log("No OpenGL ES 1.x support compiled in.", ELL_ERROR);\r
-#endif\r
- break;\r
- \r
- case video::EDT_OGLES2:\r
-#ifdef _IRR_COMPILE_WITH_OGLES2_\r
- {\r
- CIrrViewEAGLiOS* view = [[CIrrViewEAGLiOS alloc] initWithFrame:resolution forDevice:this];\r
- CreationParams.WindowSize = core::dimension2d<u32>(view.frame.size.width, view.frame.size.height);\r
- \r
- dataStorage->View = view;\r
- data.OpenGLiOS.View = (__bridge void*)view;\r
- \r
- ContextManager = new video::CEAGLManager();\r
- ContextManager->initialize(CreationParams, data);\r
- \r
- VideoDriver = video::createOGLES2Driver(CreationParams, FileSystem, ContextManager);\r
- \r
- if (!VideoDriver)\r
- os::Printer::log("Could not create OpenGL ES 2.x driver.", ELL_ERROR);\r
- }\r
-#else\r
- os::Printer::log("No OpenGL ES 2.x support compiled in.", ELL_ERROR);\r
-#endif\r
- break;\r
- \r
- case video::EDT_SOFTWARE:\r
- case video::EDT_BURNINGSVIDEO:\r
- case video::DEPRECATED_EDT_DIRECT3D8_NO_LONGER_EXISTS:\r
- case video::EDT_DIRECT3D9:\r
- case video::EDT_OPENGL:\r
- os::Printer::log("This driver is not available in iOS. Try OpenGL ES.", ELL_ERROR);\r
- break;\r
- \r
- case video::EDT_NULL:\r
- VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);\r
- break;\r
- \r
- default:\r
- os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);\r
- break;\r
- }\r
- \r
- if (externalView == nil)\r
- dataStorage->ViewController.view = dataStorage->View;\r
- else\r
- [externalView addSubview:dataStorage->View];\r
- }\r
-}\r
-\r
-#ifdef _IRR_COMPILE_WITH_IOS_BUILTIN_MAIN_\r
-int main(int argc, char** argv)\r
-{\r
- int result = UIApplicationMain(argc, argv, 0, NSStringFromClass([CIrrDelegateiOS class]));\r
- \r
- return result;\r
-}\r
-#endif\r
-\r
-#endif\r