From 628d5933264eedda54783facda6364f654ab4ae3 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sun, 13 Apr 2014 13:47:50 +0200 Subject: [PATCH] [DEV] integration IOS first step --- external/date/date/date.cpp | 8 + sources/ewol/context/IOs/AppDelegate.h | 20 +++ sources/ewol/context/IOs/AppDelegate.mm | 75 ++++++++ sources/ewol/context/IOs/Context.cpp | 190 +++++++++++++++++++- sources/ewol/context/IOs/Context.h | 28 +++ sources/ewol/context/IOs/Interface.h | 23 +++ sources/ewol/context/IOs/Interface.m | 26 +++ sources/ewol/context/IOs/OpenglView.h | 48 +++++ sources/ewol/context/IOs/OpenglView.mm | 227 ++++++++++++++++++++++++ sources/ewol/ewol.cpp | 4 +- sources/ewol/openGL/openGL.h | 2 +- sources/ewol/widget/Windows.cpp | 2 +- 12 files changed, 648 insertions(+), 5 deletions(-) create mode 100644 sources/ewol/context/IOs/AppDelegate.h create mode 100644 sources/ewol/context/IOs/AppDelegate.mm create mode 100644 sources/ewol/context/IOs/Context.h create mode 100644 sources/ewol/context/IOs/Interface.h create mode 100644 sources/ewol/context/IOs/Interface.m create mode 100644 sources/ewol/context/IOs/OpenglView.h create mode 100644 sources/ewol/context/IOs/OpenglView.mm diff --git a/external/date/date/date.cpp b/external/date/date/date.cpp index d1a83ed4..de2a8e0d 100644 --- a/external/date/date/date.cpp +++ b/external/date/date/date.cpp @@ -7,6 +7,14 @@ */ #include +#ifdef __TARGET_OS__IOs +#define BUILD_YEAR 1070 +#define BUILD_MONTH 1 +#define BUILD_DAY 1 +#define BUILD_HOUR 0 +#define BUILD_MINUTE 0 +#define BUILD_SECOND 0 +#endif int32_t date::getYear(void) { return BUILD_YEAR; diff --git a/sources/ewol/context/IOs/AppDelegate.h b/sources/ewol/context/IOs/AppDelegate.h new file mode 100644 index 00000000..136dbc00 --- /dev/null +++ b/sources/ewol/context/IOs/AppDelegate.h @@ -0,0 +1,20 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#import +@class OpenglView; + +@interface AppDelegate : UIResponder { + UIWindow *window; + OpenglView *glView; +} + +@property (nonatomic, retain) IBOutlet UIWindow *window; +@property (nonatomic, retain) IBOutlet OpenglView *glView; + +@end diff --git a/sources/ewol/context/IOs/AppDelegate.mm b/sources/ewol/context/IOs/AppDelegate.mm new file mode 100644 index 00000000..db9c322f --- /dev/null +++ b/sources/ewol/context/IOs/AppDelegate.mm @@ -0,0 +1,75 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#import +#include "ewol/context/IOs/Interface.h" + +#import +#import + +@implementation AppDelegate + +@synthesize window; +@synthesize glView; + +- (void)applicationDidFinishLaunching:(UIApplication *)application { + + //No need for nib + window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; + glView = [[OpenglView alloc] initWithFrame:window.bounds]; + [window addSubview:glView]; + [window makeKeyAndVisible]; + + glView.animationInterval = 1.0 / 60.0; + [glView startAnimation]; +} +/* + - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions + { + // Override point for customization after application launch. + return YES; + } + */ + +- (void)applicationWillResignActive:(UIApplication *)application +{ + glView.animationInterval = 1.0 / 5.0; + // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. + // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. +} + +- (void)applicationDidEnterBackground:(UIApplication *)application +{ + // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. + // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. +} + +- (void)applicationWillEnterForeground:(UIApplication *)application +{ + // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. +} + +- (void)applicationDidBecomeActive:(UIApplication *)application +{ + glView.animationInterval = 1.0 / 60.0; + // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. +} + +- (void)applicationWillTerminate:(UIApplication *)application +{ + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. +} +/* + - (void)dealloc { + [window release]; + [glView release]; + [super dealloc]; + } + */ + +@end diff --git a/sources/ewol/context/IOs/Context.cpp b/sources/ewol/context/IOs/Context.cpp index 65799fed..0405dbe1 100644 --- a/sources/ewol/context/IOs/Context.cpp +++ b/sources/ewol/context/IOs/Context.cpp @@ -1,8 +1,194 @@ /** * @author Edouard DUPIN - * + * * @copyright 2011, Edouard DUPIN, all right reserved - * + * * @license BSD v3 (see license file) */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + + +#include +#include +#include + + +int64_t ewol::getTime(void) { + struct timespec now; + clock_serv_t cclock; + mach_timespec_t mts; + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + now.tv_sec = mts.tv_sec; + now.tv_nsec = mts.tv_nsec; + //EWOL_VERBOSE("current time : " << now.tv_sec << "s " << now.tv_usec << "us"); + return (int64_t)((int64_t)now.tv_sec*(int64_t)1000000 + (int64_t)now.tv_nsec/(int64_t)1000); +} + +#undef __class__ +#define __class__ "MacOSInterface" + + + +class MacOSInterface : public ewol::Context { +private: + ewol::key::Special m_guiKeyBoardMode; +public: + MacOSInterface(int32_t _argc, const char* _argv[]) : + ewol::Context(_argc, _argv) { + //mm_main(_argc, _argv); + } + + int32_t Run(void) { + return mm_run(); + return 0; + } +public: + //interface MacOS : + + bool MAC_Draw(bool _displayEveryTime) { + return OS_Draw(_displayEveryTime); + } + void MAC_Resize(float _x, float _y) { + OS_Resize(vec2(_x,_y)); + } + void MAC_SetMouseState(int32_t _id, bool _isDown, float _x, float _y) { + OS_SetMouseState(_id, _isDown, vec2(_x, _y)); + } + void MAC_SetMouseMotion(int32_t _id, float _x, float _y) { + OS_SetMouseMotion(_id, vec2(_x, _y)); + } + void MAC_SetKeyboard(ewol::key::Special _keyboardMode, int32_t _unichar, bool _isDown, bool _isAReapeateKey) { + if (_unichar == u32char::Delete) { + _unichar = u32char::Suppress; + } else if (_unichar == u32char::Suppress) { + _unichar = u32char::Delete; + } + if (_unichar == u32char::CarrierReturn) { + _unichar = u32char::Return; + } + //EWOL_DEBUG("key: " << _unichar << " up=" << !_isDown); + if (_unichar <= 4) { + enum ewol::key::keyboard move; + switch(_unichar) { + case 0: + move = ewol::key::keyboardUp; + break; + case 1: + move = ewol::key::keyboardDown; + break; + case 2: + move = ewol::key::keyboardLeft; + break; + case 3: + move = ewol::key::keyboardRight; + break; + } + OS_SetKeyboardMove(_keyboardMode, move, !_isDown, _isAReapeateKey); + } else { + OS_SetKeyboard(_keyboardMode, _unichar, !_isDown, _isAReapeateKey); + } + } + void MAC_SetKeyboardMove(ewol::key::Special& _special, + enum ewol::key::keyboard _move, + bool _isDown) { + OS_SetKeyboardMove(_special, _move, _isDown); + } +}; + + +MacOSInterface* interface = NULL; + + + +bool MacOs::draw(bool _displayEveryTime) { + if (interface == NULL) { + return false; + } + return interface->MAC_Draw(_displayEveryTime); +} + +void MacOs::resize(float _x, float _y) { + if (interface == NULL) { + return; + } + interface->MAC_Resize(_x, _y); +} + + +void MacOs::setMouseState(int32_t _id, bool _isDown, float _x, float _y) { + if (interface == NULL) { + return; + } + interface->MAC_SetMouseState(_id, _isDown, _x, _y); +} + +void MacOs::setMouseMotion(int32_t _id, float _x, float _y) { + if (interface == NULL) { + return; + } + interface->MAC_SetMouseMotion(_id, _x, _y); +} + +void MacOs::setKeyboard(ewol::key::Special _keyboardMode, int32_t _unichar, bool _isDown, bool _isAReapeateKey) { + if (interface == NULL) { + return; + } + interface->MAC_SetKeyboard(_keyboardMode, _unichar, _isDown, _isAReapeateKey); +} + +void MacOs::setKeyboardMove(ewol::key::Special& _keyboardMode, enum ewol::key::keyboard _move, bool _isDown) { + if (interface == NULL) { + return; + } + interface->MAC_SetKeyboardMove(_keyboardMode, _move, _isDown); +} + + +/** + * @brief Main of the program + * @param std IO + * @return std IO + */ +int ewol::run(int _argc, const char *_argv[]) { + etk::setArgZero(_argv[0]); + /* + interface = new MacOSInterface(_argc, _argv); + if (NULL == interface) { + EWOL_CRITICAL("Can not create the X11 interface ... MEMORY allocation error"); + return -2; + } + + int32_t retValue = interface->Run(); + delete(interface); + */ + int32_t retValue = mm_main(_argc, _argv); + interface = NULL; + return retValue; +} + + + + + + + diff --git a/sources/ewol/context/IOs/Context.h b/sources/ewol/context/IOs/Context.h new file mode 100644 index 00000000..9e7d8620 --- /dev/null +++ b/sources/ewol/context/IOs/Context.h @@ -0,0 +1,28 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#ifndef __MAC_OS_CONTEXT_H__ +#define __MAC_OS_CONTEXT_H__ + +#include + +namespace MacOs { + // return true if a flush is needed + bool draw(bool _displayEveryTime); + /** + * @brief The OS inform that the current windows has change his size. + * @param[in] _size new size of the windows. + */ + void resize(float _x, float _y); + void setMouseState(int32_t _id, bool _isDown, float _x, float _y); + void setMouseMotion(int32_t _id, float _x, float _y); + void setKeyboard(ewol::key::Special _keyboardMode, int32_t _unichar, bool _isDown, bool _isAReapeateKey); + void setKeyboardMove(ewol::key::Special& _keyboardMode, enum ewol::key::keyboard _move, bool _isDown); +}; + +#endif \ No newline at end of file diff --git a/sources/ewol/context/IOs/Interface.h b/sources/ewol/context/IOs/Interface.h new file mode 100644 index 00000000..ae3bcfa0 --- /dev/null +++ b/sources/ewol/context/IOs/Interface.h @@ -0,0 +1,23 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#ifndef __EWOL_MM_INTERFACE_H__ +#define __EWOL_MM_INTERFACE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +int mm_main(int argc, const char *argv[]); +int mm_run(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/sources/ewol/context/IOs/Interface.m b/sources/ewol/context/IOs/Interface.m new file mode 100644 index 00000000..352d3157 --- /dev/null +++ b/sources/ewol/context/IOs/Interface.m @@ -0,0 +1,26 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#import +#import +//#import "AppDelegate.h" + + + +int mm_main(int argc, const char *argv[]) { + @autoreleasepool { + return UIApplicationMain(argc, (char**)argv, nil, NSStringFromClass([AppDelegate class])); + } // return no error + return 0; +} + +int mm_run(void) { + //[NSApp run]; + // return no error + return 0; +} diff --git a/sources/ewol/context/IOs/OpenglView.h b/sources/ewol/context/IOs/OpenglView.h new file mode 100644 index 00000000..ead9b28c --- /dev/null +++ b/sources/ewol/context/IOs/OpenglView.h @@ -0,0 +1,48 @@ +// +// EAGLView.h +// OpenGLBasics +// +// Created by Charlie Key on 6/24/09. +// + + +#import +#import +#import +#import + +/* + This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass. + The view content is basically an EAGL surface you render your OpenGL scene into. + Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel. + */ +@interface OpenglView : UIView { + +@private + /* The pixel dimensions of the backbuffer */ + GLint backingWidth; + GLint backingHeight; + + EAGLContext *context; + + /* OpenGL names for the renderbuffer and framebuffers used to render to this view */ + GLuint viewRenderbuffer, viewFramebuffer; + + /* OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist) */ + GLuint depthRenderbuffer; + + //NSTimer *animationTimer; + NSTimeInterval animationInterval; + + CGPoint touchLocation; +} + +@property NSTimeInterval animationInterval; + +- (void)startAnimation; +- (void)stopAnimation; +- (void)drawView; + +- (void)setupView; + +@end diff --git a/sources/ewol/context/IOs/OpenglView.mm b/sources/ewol/context/IOs/OpenglView.mm new file mode 100644 index 00000000..30a1b002 --- /dev/null +++ b/sources/ewol/context/IOs/OpenglView.mm @@ -0,0 +1,227 @@ +// +// EAGLView.m +// OpenGLBasics +// +// Created by Charlie Key on 6/24/09. +// + +#import +#import + +#import "OpenglView.h" + +#define USE_DEPTH_BUFFER 1 +#define DEGREES_TO_RADIANS(__ANGLE) ((__ANGLE) / 180.0 * M_PI) + +// A class extension to declare private methods +@interface OpenglView () + +@property (nonatomic, retain) EAGLContext *context; +@property (nonatomic, assign) NSTimer *animationTimer; + +- (BOOL) createFramebuffer; +- (void) destroyFramebuffer; + +@end + + +@implementation OpenglView + +@synthesize context; +@synthesize animationTimer; +@synthesize animationInterval; + + +// You must implement this method ++ (Class)layerClass { + return [CAEAGLLayer class]; +} + +//Created GlView +- (id)initWithFrame:(CGRect)frame { + if ((self = [super initWithFrame:frame])) { + // Get the layer + CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; + + eaglLayer.opaque = YES; + eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: + [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; + + context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; + + if (!context || ![EAGLContext setCurrentContext:context]) { + //[self release]; + return nil; + } + + animationInterval = 1.0 / 60.0; + [self setupView]; + } + return self; +} + +- (void)setupView { + const GLfloat zNear = 0.1, zFar = 1000.0, fieldOfView = 60.0; + GLfloat size; + + glEnable(GL_DEPTH_TEST); + glMatrixMode(GL_PROJECTION); + size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0); + + //Grab the size of the screen + CGRect rect = self.bounds; + glFrustumf(-size, size, + -size / (rect.size.width / rect.size.height), + size / (rect.size.width / rect.size.height), + zNear, zFar); + glViewport(0, 0, rect.size.width, rect.size.height); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + + //Initialize touchLocation to 0, 0 + touchLocation = CGPointMake(160, 240); +} + +- (void)drawView { + const GLfloat triangleVertices[] = { + 0.0, 1.0, -6.0, // top center + -1.0, -1.0, -6.0, // bottom left + 1.0, -1.0, -6.0 // bottom right + }; + + const GLfloat triangleColors[] = { + 1.0, 0.0, 0.0, 1.0, // red + 0.0, 1.0, 0.0, 1.0, // green + 0.0, 0.0, 1.0, 1.0, // blue + }; + + //setting up the draw content + [EAGLContext setCurrentContext:context]; + glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); + + //Draw stuff + + //clear the back color back to our original color and depth buffer + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + //reset matrix to identity + glLoadIdentity(); + //rough approximation of screen to current 3D space + GLfloat x = (touchLocation.x - 160.0) / 38.0; + GLfloat y = (240.0 - touchLocation.y) / 38.0; + //translate the triangle + glTranslatef(x, y, -1.0); + //set the format and location for verticies + glVertexPointer(3, GL_FLOAT, 0, triangleVertices); + //set the opengl state + glEnableClientState(GL_VERTEX_ARRAY); + //set the format and location for colors + glColorPointer(4, GL_FLOAT, 0, triangleColors); + //set the opengl state + glEnableClientState(GL_COLOR_ARRAY); + //draw the triangles + glDrawArrays(GL_TRIANGLES, 0, 3); + + glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); + //show the render buffer + [context presentRenderbuffer:GL_RENDERBUFFER_OES]; +} + +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event +{ + UITouch *touch = [touches anyObject]; + touchLocation = [touch locationInView:self]; +} + +- (void)layoutSubviews { + [EAGLContext setCurrentContext:context]; + [self destroyFramebuffer]; + [self createFramebuffer]; + [self drawView]; +} + + +- (BOOL)createFramebuffer { + + glGenFramebuffersOES(1, &viewFramebuffer); + glGenRenderbuffersOES(1, &viewRenderbuffer); + + glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); + glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); + [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer]; + glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer); + + glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth); + glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight); + + if (USE_DEPTH_BUFFER) { + glGenRenderbuffersOES(1, &depthRenderbuffer); + glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer); + glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight); + glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer); + } + + if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) { + NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)); + return NO; + } + + return YES; +} + + +- (void)destroyFramebuffer { + + glDeleteFramebuffersOES(1, &viewFramebuffer); + viewFramebuffer = 0; + glDeleteRenderbuffersOES(1, &viewRenderbuffer); + viewRenderbuffer = 0; + + if(depthRenderbuffer) { + glDeleteRenderbuffersOES(1, &depthRenderbuffer); + depthRenderbuffer = 0; + } +} + + +- (void)startAnimation { + self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES]; +} + + +- (void)stopAnimation { + self.animationTimer = nil; +} + + +- (void)setAnimationTimer:(NSTimer *)newTimer { + [animationTimer invalidate]; + animationTimer = newTimer; +} + + +- (void)setAnimationInterval:(NSTimeInterval)interval { + + animationInterval = interval; + if (animationTimer) { + [self stopAnimation]; + [self startAnimation]; + } +} + + +- (void)dealloc { + + [self stopAnimation]; + + if ([EAGLContext currentContext] == context) { + [EAGLContext setCurrentContext:nil]; + } + + //[context release]; + //[super dealloc]; +} + +@end diff --git a/sources/ewol/ewol.cpp b/sources/ewol/ewol.cpp index b63ee1b7..3c3ce644 100644 --- a/sources/ewol/ewol.cpp +++ b/sources/ewol/ewol.cpp @@ -17,7 +17,9 @@ #undef __class__ #define __class__ "ewol" - +#ifndef EWOL_VERSION +#define EWOL_VERSION "0.0.0" +#endif std::string ewol::getCompilationMode(void) { #ifdef MODE_RELEASE diff --git a/sources/ewol/openGL/openGL.h b/sources/ewol/openGL/openGL.h index daaefa0e..388f6b53 100644 --- a/sources/ewol/openGL/openGL.h +++ b/sources/ewol/openGL/openGL.h @@ -44,7 +44,7 @@ extern "C" { #include #include #elif defined(__TARGET_OS__IOs) - + #include #else #error you need to specify a __TAGET_OS__ ... #endif diff --git a/sources/ewol/widget/Windows.cpp b/sources/ewol/widget/Windows.cpp index 5844844f..c524f1c9 100644 --- a/sources/ewol/widget/Windows.cpp +++ b/sources/ewol/widget/Windows.cpp @@ -96,7 +96,7 @@ void ewol::widget::Windows::sysDraw(void) { ewol::openGL::disable(ewol::openGL::FLAG_STENCIL_TEST); ewol::openGL::disable(ewol::openGL::FLAG_ALPHA_TEST); ewol::openGL::disable(ewol::openGL::FLAG_FOG); - #ifndef __TARGET_OS__Android + #if (!defined(__TARGET_OS__Android) && !defined(__TARGET_OS__IOs)) glPixelZoom(1.0,1.0); #endif ewol::openGL::disable(ewol::openGL::FLAG_TEXTURE_2D);