Explorar el Código

Stopped using GLKit for render, still need to implement CADisplayLink

Ivan Safrin hace 10 años
padre
commit
50fc83b0e3

+ 5 - 1
build/ios/TemplateApp/TemplateApp/AppDelegate.m

@@ -7,6 +7,7 @@
 //
 
 #import "AppDelegate.h"
+#import "PolycodeView.h"
 
 @interface AppDelegate ()
 
@@ -16,7 +17,10 @@
 
 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-    // Override point for customization after application launch.
+
+    [self.window makeKeyAndVisible];    
+
+    
     return YES;
 }
 

+ 8 - 8
build/ios/TemplateApp/TemplateApp/Base.lproj/Main.storyboard

@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9532" systemVersion="15C50" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
     <dependencies>
         <deployment identifier="iOS"/>
-        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9530"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
     </dependencies>
     <scenes>
         <!--View Controller-->
@@ -10,16 +10,16 @@
             <objects>
                 <viewController id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
                     <layoutGuides>
-                        <viewControllerLayoutGuide type="top" id="CcZ-Kq-cqd"/>
-                        <viewControllerLayoutGuide type="bottom" id="cfD-uc-FeA"/>
+                        <viewControllerLayoutGuide type="top" id="ZpO-4R-yaC"/>
+                        <viewControllerLayoutGuide type="bottom" id="G2J-Lv-dn1"/>
                     </layoutGuides>
-                    <glkView key="view" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" enableSetNeedsDisplay="NO" id="2t5-1N-wWB" customClass="PolycodeView">
+                    <view key="view" contentMode="scaleToFill" id="9AP-Q0-y7C" customClass="PolycodeView">
                         <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                         <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
-                        <color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
-                    </glkView>
+                        <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
+                    </view>
                     <connections>
-                        <outlet property="mainView" destination="2t5-1N-wWB" id="N2W-rf-Rkf"/>
+                        <outlet property="mainView" destination="9AP-Q0-y7C" id="hEZ-tj-YhJ"/>
                     </connections>
                 </viewController>
                 <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>

+ 1 - 0
build/ios/TemplateApp/TemplateApp/PolycodeTemplateApp.h

@@ -17,5 +17,6 @@ public:
     
 private:
 
+    ScenePrimitive *test;
     Core *core;
 };

+ 8 - 4
build/ios/TemplateApp/TemplateApp/PolycodeTemplateApp.mm

@@ -6,7 +6,7 @@
 
 
 PolycodeTemplateApp::PolycodeTemplateApp(PolycodeView *view) {
-    core = new POLYCODE_CORE(view, 64,64,false,false, 0,0,60, 0, true);
+    core = new POLYCODE_CORE(view, 1920, 1080,false,false, 0,0,60, 0, true);
     
     core->addFileSource("archive", core->getResourcePathForFile("default.pak"));
     ResourcePool *globalPool = Services()->getResourceManager()->getGlobalPool();
@@ -18,11 +18,11 @@ PolycodeTemplateApp::PolycodeTemplateApp(PolycodeView *view) {
     scene->useClearColor = true;
     scene->clearColor.setColor(0.2, 0.0, 0.0, 1.0);
     
-    ScenePrimitive *test = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 10.0, 10.0);
+    test = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 0.5, 0.2);
     test->setMaterialByName("UnlitUntextured");
     test->color = Color(1.0, 0.0, 0.0, 1.0);
-    test->enableScissor = true;
-    test->scissorBox.setRect(100, 50, 100, 30);
+//    test->enableScissor = true;
+//    test->scissorBox.setRect(100, 50, 100, 30);
     scene->addChild(test);
     
 //    Sound *music = new Sound("BUGSHUFFLE.ogg");
@@ -40,5 +40,9 @@ PolycodeTemplateApp::~PolycodeTemplateApp() {
 }
 
 bool PolycodeTemplateApp::Update() {
+    Number elapsed = core->getElapsed();
+    
+    test->Roll(elapsed * 45.0);
+    
     return core->updateAndRender();
 }

+ 1 - 0
build/ios/TemplateApp/TemplateApp/ViewController.h

@@ -9,6 +9,7 @@
 #import <UIKit/UIKit.h>
 #import "polycode/view/ios/PolycodeView.h"
 #include "PolycodeTemplateApp.h"
+#import <GLKit/GLKit.h>
 
 @interface ViewController : UIViewController {
 @private

+ 1 - 0
build/ios/TemplateApp/TemplateApp/ViewController.m

@@ -25,6 +25,7 @@
                                   userInfo:nil
                                    repeats:YES];
     [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
+    [_mainView setNeedsDisplay];
 }
 
 - (void)animationTimer:(NSTimer *)timer

+ 2 - 1
include/polycode/core/PolyCore.h

@@ -300,7 +300,8 @@ namespace Polycode {
 
 		virtual void handleVideoModeChange(VideoModeChangeInfo *modeInfo) = 0;
 		virtual void flushRenderContext() = 0;
-		
+        virtual void prepareRenderContext() {}
+        
 		CoreFile *openFile(const Polycode::String& fileName, const Polycode::String& opts);
 		void closeFile(CoreFile *file);
 		

+ 12 - 1
include/polycode/core/PolyIOSCore.h

@@ -65,7 +65,10 @@ namespace Polycode {
 		std::vector<String> openFilePicker(std::vector<CoreFileExtension> extensions, bool allowMultiple);
 		String saveFilePicker(std::vector<CoreFileExtension> extensions);
 		void handleVideoModeChange(VideoModeChangeInfo *modeInfo);
-		void flushRenderContext();
+		
+        void flushRenderContext();
+        void prepareRenderContext();
+        
 		void openURL(String url);
 		unsigned int getTicks();
 		String executeExternalCommand(String command, String args, String inDirectory);
@@ -75,12 +78,20 @@ namespace Polycode {
 
 		void checkEvents();
 		void setDeviceSize(Number x, Number y);
+        
+        void renderTest();
 
 		Number getBackingXRes();
 		Number getBackingYRes();
 
 	private:
         
+        GLuint colorRenderbuffer;
+        GLuint depthRenderbuffer;
+        GLuint defaultFBOName;
+        
+        EAGLContext *context;
+        uint64_t initTime;
         PolycodeView *glView;
 
 	};

+ 3 - 5
include/polycode/view/ios/PolycodeView.h

@@ -1,9 +1,7 @@
 
 #import <UIKit/UIKit.h>
-#import <GLKit/GLKView.h>
 
-@interface PolycodeView : GLKView {
-    
-}
+@interface PolycodeView : UIView
 
-@end
+
+@end

+ 55 - 13
src/core/PolyIOSCore.mm

@@ -25,6 +25,8 @@ THE SOFTWARE.
 
 #include "polycode/core/PolyBasicFileProvider.h"
 #include "polycode/core/PolyPhysFSFileProvider.h"
+#include <mach/mach.h>
+#include <mach/mach_time.h>
 
 using namespace Polycode;
 
@@ -39,6 +41,7 @@ void PosixMutex::unlock() {
 IOSCore::IOSCore(PolycodeView *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate, int monitorIndex, bool retinaSupport)
 	: Core(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate, monitorIndex) {
         
+        initTime = mach_absolute_time();
         
         fileProviders.push_back(new BasicFileProvider());
         fileProviders.push_back(new PhysFSFileProvider());
@@ -56,17 +59,16 @@ IOSCore::IOSCore(PolycodeView *view, int xRes, int yRes, bool fullScreen, bool v
 }
 
 IOSCore::~IOSCore() {
-
+    glDeleteFramebuffers(1, &defaultFBOName);
+    glDeleteRenderbuffers(1, &colorRenderbuffer);
 }
 
 void IOSCore::Render() {
     renderer->beginFrame();
     services->Render(Polycode::Rectangle(0, 0, getBackingXRes(), getBackingYRes()));
     renderer->endFrame();
-    [glView display];
 }
 
-
 void IOSCore::checkEvents() {
 
 
@@ -150,23 +152,52 @@ String IOSCore::saveFilePicker(std::vector<CoreFileExtension> extensions) {
 
 void IOSCore::handleVideoModeChange(VideoModeChangeInfo *modeInfo) {
     
+    // IOS_TODO: Implement CADisplayLink
+    
     xRes = modeInfo->xRes;
     yRes = modeInfo->yRes;
     
-    EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
+    CAEAGLLayer *eaglLayer = (CAEAGLLayer *)glView.layer;
+    eaglLayer.opaque = TRUE;
+    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
+                                    [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
+    
+    
+    context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
     
-    if (!context) {
-        printf("Failed to create ES context...\n");
+    if (!context || ![EAGLContext setCurrentContext:context]) {
+        printf("ERROR CREATING GL CONTEXT\n");
+        return;
     }
+
+    glGenFramebuffers(1, &defaultFBOName);
+    
+    glGenRenderbuffers(1, &colorRenderbuffer);
+    glBindFramebuffer(GL_FRAMEBUFFER, defaultFBOName);
+    glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
+    
+    [context renderbufferStorage:GL_RENDERBUFFER fromDrawable: (id<EAGLDrawable>) glView.layer];
+
+    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
     
-    glView.context = context;
-    glView.drawableDepthFormat = GLKViewDrawableDepthFormat24;
+    GLint backingWidth;
+    GLint backingHeight;
+    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
+    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
     
-    [EAGLContext setCurrentContext:context];
+    glGenRenderbuffers(1, &depthRenderbuffer);
+    glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
+    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, modeInfo->xRes, modeInfo->yRes);
+    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
     
+    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
+    {
+        printf("ERROR CREATING RENDERBUFFER\n");
+    }
+
 }
 
-String IOSCore::getResourcePathForFile(const String &fileName) {    
+String IOSCore::getResourcePathForFile(const String &fileName) {
     NSString* fullFileName = [NSString stringWithUTF8String:fileName.c_str()];
     NSString* fileNameNoExt = [[fullFileName lastPathComponent] stringByDeletingPathExtension];
     NSString* extension = [fullFileName pathExtension];
@@ -174,8 +205,13 @@ String IOSCore::getResourcePathForFile(const String &fileName) {
     return String([str UTF8String]);
 }
 
-void IOSCore::flushRenderContext() {
+void IOSCore::prepareRenderContext() {
+    glBindFramebuffer(GL_FRAMEBUFFER, defaultFBOName);
+}
 
+void IOSCore::flushRenderContext() {
+    glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
+    [context presentRenderbuffer:GL_RENDERBUFFER];
 }
 
 void IOSCore::openURL(String url) {
@@ -183,8 +219,14 @@ void IOSCore::openURL(String url) {
 }
 
 unsigned int IOSCore::getTicks() {
-
-	return 0;
+    uint64_t time = mach_absolute_time();
+    double conversion = 0.0;
+    
+    mach_timebase_info_data_t info;
+    mach_timebase_info( &info );
+    conversion = 1e-9 * (double) info.numer / (double) info.denom;
+    
+    return (((double)(time - initTime)) * conversion) * 1000.0f;
 }
 
 String IOSCore::executeExternalCommand(String command, String args, String inDirectory) {

+ 7 - 4
src/core/PolyRenderer.cpp

@@ -348,6 +348,7 @@ void RenderThread::processJob(const RendererThreadJob &job) {
             currentDebugFrameInfo.drawCallsProcessed = 0;
             currentDebugFrameInfo.timeTaken = 0;
             frameStart = Services()->getCore()->getTicks();
+            core->prepareRenderContext();
         }
         break;
         case JOB_END_FRAME:
@@ -417,12 +418,14 @@ void RenderThread::setGraphicsInterface(Core *core, GraphicsInterface *graphicsI
     this->core = core;
 }
 
-Renderer::Renderer() : backingResolutionScaleX(1.0), backingResolutionScaleY(1.0) {
+Renderer::Renderer() :
+    backingResolutionScaleX(1.0),
+    backingResolutionScaleY(1.0),
+    cpuBufferIndex(0),
+    gpuBufferIndex(1) {
+        
     renderThread = new RenderThread();
     Services()->getCore()->createThread(renderThread);
-    
-    cpuBufferIndex = 0;
-    gpuBufferIndex = 1;
 }
 
 Renderer::~Renderer() {

+ 12 - 2
src/view/ios/PolycodeView.m

@@ -1,8 +1,18 @@
 
+#import "PolycodeView.h"
 
-#import "polycode/view/ios/PolycodeView.h"
+@interface PolycodeView ()
+{
+
+}
+@end
 
 @implementation PolycodeView
 
+// Must return the CAEAGLLayer class so that CA allocates an EAGLLayer backing for this view
++ (Class) layerClass
+{
+    return [CAEAGLLayer class];
+}
 
-@end
+@end