From 87fa8256ebc18aa4359a22762fc8f68240fbfafb Mon Sep 17 00:00:00 2001 From: Thomas Friedel Date: Tue, 15 May 2018 18:49:30 +0200 Subject: [PATCH] added an iOS GL implementation and example --- .gitignore | 1 + canvas.go | 1 + examples/ios/CanvasIOSExample/.gitignore | 4 + .../project.pbxproj | 325 ++++++++++++++++++ .../CanvasIOSExample/AppDelegate.swift | 38 ++ .../AppIcon.appiconset/Contents.json | 93 +++++ .../Base.lproj/LaunchScreen.storyboard | 25 ++ .../CanvasIOSExample/Info.plist | 43 +++ .../CanvasIOSExample/MainViewController.swift | 54 +++ examples/ios/CanvasIOSExample/README.md | 5 + examples/ios/ios.go | 41 +++ gliosimpl/gliosimpl.go | 224 ++++++++++++ 12 files changed, 854 insertions(+) create mode 100644 examples/ios/CanvasIOSExample/.gitignore create mode 100644 examples/ios/CanvasIOSExample/CanvasIOSExample.xcodeproj/project.pbxproj create mode 100644 examples/ios/CanvasIOSExample/CanvasIOSExample/AppDelegate.swift create mode 100644 examples/ios/CanvasIOSExample/CanvasIOSExample/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 examples/ios/CanvasIOSExample/CanvasIOSExample/Base.lproj/LaunchScreen.storyboard create mode 100644 examples/ios/CanvasIOSExample/CanvasIOSExample/Info.plist create mode 100644 examples/ios/CanvasIOSExample/CanvasIOSExample/MainViewController.swift create mode 100644 examples/ios/CanvasIOSExample/README.md create mode 100644 examples/ios/ios.go create mode 100644 gliosimpl/gliosimpl.go diff --git a/.gitignore b/.gitignore index beba11f..7073664 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ plan.txt glowgen testapp videocap +.DS_Store diff --git a/canvas.go b/canvas.go index ba7d7db..75aa9a4 100644 --- a/canvas.go +++ b/canvas.go @@ -135,6 +135,7 @@ func (cv *Canvas) tf(v vec) vec { // to be called if any other GL code changes the viewport func (cv *Canvas) Activate() { gli.Viewport(int32(cv.x), int32(cv.y), int32(cv.w), int32(cv.h)) + cv.applyScissor() } var activeCanvas *Canvas diff --git a/examples/ios/CanvasIOSExample/.gitignore b/examples/ios/CanvasIOSExample/.gitignore new file mode 100644 index 0000000..d9349d6 --- /dev/null +++ b/examples/ios/CanvasIOSExample/.gitignore @@ -0,0 +1,4 @@ +xcuserdata +project.xcworkspace +Example.framework + diff --git a/examples/ios/CanvasIOSExample/CanvasIOSExample.xcodeproj/project.pbxproj b/examples/ios/CanvasIOSExample/CanvasIOSExample.xcodeproj/project.pbxproj new file mode 100644 index 0000000..066fe37 --- /dev/null +++ b/examples/ios/CanvasIOSExample/CanvasIOSExample.xcodeproj/project.pbxproj @@ -0,0 +1,325 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 48; + objects = { + +/* Begin PBXBuildFile section */ + A02D492720AB3AA900E68C35 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A02D492620AB3AA900E68C35 /* MainViewController.swift */; }; + A094B52520AB3D0C000BCEA6 /* Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A094B52420AB3D0C000BCEA6 /* Example.framework */; }; + A0F2843E20AAD80F0049BD39 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0F2843D20AAD80F0049BD39 /* AppDelegate.swift */; }; + A0F2844520AAD8100049BD39 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A0F2844420AAD8100049BD39 /* Assets.xcassets */; }; + A0F2844820AAD8100049BD39 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A0F2844620AAD8100049BD39 /* LaunchScreen.storyboard */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + A02D492620AB3AA900E68C35 /* MainViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = ""; }; + A094B52420AB3D0C000BCEA6 /* Example.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Example.framework; sourceTree = ""; }; + A0F2843A20AAD80F0049BD39 /* CanvasIOSExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CanvasIOSExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; + A0F2843D20AAD80F0049BD39 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + A0F2844420AAD8100049BD39 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + A0F2844720AAD8100049BD39 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + A0F2844920AAD8100049BD39 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + A0F2843720AAD80F0049BD39 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + A094B52520AB3D0C000BCEA6 /* Example.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + A0F2843120AAD80F0049BD39 = { + isa = PBXGroup; + children = ( + A0F2843C20AAD80F0049BD39 /* CanvasIOSExample */, + A0F2843B20AAD80F0049BD39 /* Products */, + ); + sourceTree = ""; + }; + A0F2843B20AAD80F0049BD39 /* Products */ = { + isa = PBXGroup; + children = ( + A0F2843A20AAD80F0049BD39 /* CanvasIOSExample.app */, + ); + name = Products; + sourceTree = ""; + }; + A0F2843C20AAD80F0049BD39 /* CanvasIOSExample */ = { + isa = PBXGroup; + children = ( + A0F2843D20AAD80F0049BD39 /* AppDelegate.swift */, + A02D492620AB3AA900E68C35 /* MainViewController.swift */, + A0F2844420AAD8100049BD39 /* Assets.xcassets */, + A0F2844620AAD8100049BD39 /* LaunchScreen.storyboard */, + A0F2844920AAD8100049BD39 /* Info.plist */, + A094B52420AB3D0C000BCEA6 /* Example.framework */, + ); + path = CanvasIOSExample; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + A0F2843920AAD80F0049BD39 /* CanvasIOSExample */ = { + isa = PBXNativeTarget; + buildConfigurationList = A0F2844C20AAD8100049BD39 /* Build configuration list for PBXNativeTarget "CanvasIOSExample" */; + buildPhases = ( + A0F2843620AAD80F0049BD39 /* Sources */, + A0F2843720AAD80F0049BD39 /* Frameworks */, + A0F2843820AAD80F0049BD39 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = CanvasIOSExample; + productName = CanvasIOSExample; + productReference = A0F2843A20AAD80F0049BD39 /* CanvasIOSExample.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + A0F2843220AAD80F0049BD39 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0910; + LastUpgradeCheck = 0910; + ORGANIZATIONNAME = example; + TargetAttributes = { + A0F2843920AAD80F0049BD39 = { + CreatedOnToolsVersion = 9.1; + ProvisioningStyle = Automatic; + }; + }; + }; + buildConfigurationList = A0F2843520AAD80F0049BD39 /* Build configuration list for PBXProject "CanvasIOSExample" */; + compatibilityVersion = "Xcode 8.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = A0F2843120AAD80F0049BD39; + productRefGroup = A0F2843B20AAD80F0049BD39 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + A0F2843920AAD80F0049BD39 /* CanvasIOSExample */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + A0F2843820AAD80F0049BD39 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + A0F2844820AAD8100049BD39 /* LaunchScreen.storyboard in Resources */, + A0F2844520AAD8100049BD39 /* Assets.xcassets in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + A0F2843620AAD80F0049BD39 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + A0F2843E20AAD80F0049BD39 /* AppDelegate.swift in Sources */, + A02D492720AB3AA900E68C35 /* MainViewController.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + A0F2844620AAD8100049BD39 /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + A0F2844720AAD8100049BD39 /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + A0F2844A20AAD8100049BD39 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 11.1; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + A0F2844B20AAD8100049BD39 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 11.1; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + A0F2844D20AAD8100049BD39 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_STYLE = Automatic; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(PROJECT_DIR)/CanvasIOSExample", + ); + INFOPLIST_FILE = CanvasIOSExample/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.example.CanvasIOSExample; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + A0F2844E20AAD8100049BD39 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_STYLE = Automatic; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(PROJECT_DIR)/CanvasIOSExample", + ); + INFOPLIST_FILE = CanvasIOSExample/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.example.CanvasIOSExample; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + A0F2843520AAD80F0049BD39 /* Build configuration list for PBXProject "CanvasIOSExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + A0F2844A20AAD8100049BD39 /* Debug */, + A0F2844B20AAD8100049BD39 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + A0F2844C20AAD8100049BD39 /* Build configuration list for PBXNativeTarget "CanvasIOSExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + A0F2844D20AAD8100049BD39 /* Debug */, + A0F2844E20AAD8100049BD39 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = A0F2843220AAD80F0049BD39 /* Project object */; +} diff --git a/examples/ios/CanvasIOSExample/CanvasIOSExample/AppDelegate.swift b/examples/ios/CanvasIOSExample/CanvasIOSExample/AppDelegate.swift new file mode 100644 index 0000000..cfaf6cc --- /dev/null +++ b/examples/ios/CanvasIOSExample/CanvasIOSExample/AppDelegate.swift @@ -0,0 +1,38 @@ +import UIKit + +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + + var window: UIWindow? + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + + window = UIWindow(frame: UIScreen.main.bounds) + window?.rootViewController = MainViewController() + self.window?.makeKeyAndVisible() + + return true + } + + func applicationWillResignActive(_ application: UIApplication) { + // 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 invalidate graphics rendering callbacks. Games should use this method to pause the game. + } + + func applicationDidEnterBackground(_ application: UIApplication) { + // 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. + } + + func applicationWillEnterForeground(_ application: UIApplication) { + // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. + } + + func applicationDidBecomeActive(_ application: UIApplication) { + // 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. + } + + func applicationWillTerminate(_ application: UIApplication) { + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. + } +} diff --git a/examples/ios/CanvasIOSExample/CanvasIOSExample/Assets.xcassets/AppIcon.appiconset/Contents.json b/examples/ios/CanvasIOSExample/CanvasIOSExample/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..1d060ed --- /dev/null +++ b/examples/ios/CanvasIOSExample/CanvasIOSExample/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,93 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "83.5x83.5", + "scale" : "2x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/examples/ios/CanvasIOSExample/CanvasIOSExample/Base.lproj/LaunchScreen.storyboard b/examples/ios/CanvasIOSExample/CanvasIOSExample/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..f83f6fd --- /dev/null +++ b/examples/ios/CanvasIOSExample/CanvasIOSExample/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/ios/CanvasIOSExample/CanvasIOSExample/Info.plist b/examples/ios/CanvasIOSExample/CanvasIOSExample/Info.plist new file mode 100644 index 0000000..4222ac2 --- /dev/null +++ b/examples/ios/CanvasIOSExample/CanvasIOSExample/Info.plist @@ -0,0 +1,43 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/examples/ios/CanvasIOSExample/CanvasIOSExample/MainViewController.swift b/examples/ios/CanvasIOSExample/CanvasIOSExample/MainViewController.swift new file mode 100644 index 0000000..09f8ab4 --- /dev/null +++ b/examples/ios/CanvasIOSExample/CanvasIOSExample/MainViewController.swift @@ -0,0 +1,54 @@ +import UIKit +import GLKit +import Example + +class MainViewController : UIViewController, GLKViewDelegate { + var loaded:Bool? + + override func viewDidLoad() { + loaded = false + let view = GLKView(frame: UIScreen.main.bounds) + let context : EAGLContext? = EAGLContext(api: .openGLES2) + view.context = context! + view.drawableColorFormat = .RGBA8888 + view.drawableDepthFormat = .formatNone + view.drawableStencilFormat = .format8 + view.drawableMultisample = .multisample4X + view.delegate = self + self.view = view + } + + func glkView(_ view: GLKView, drawIn rect: CGRect) { + if loaded == nil || !loaded! { + loaded = true + let scale = UIScreen.main.nativeScale + ExampleLoadGL(Int(rect.width * scale), Int(rect.height * scale)) + } + + ExampleDrawFrame() + DispatchQueue.main.async { + view.setNeedsDisplay() + } + } + + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + let scale = UIScreen.main.nativeScale + let touch = touches.first! + let loc = touch.location(in: self.view) + ExampleTouchEvent("down", Int(loc.x*scale), Int(loc.y*scale)) + } + + override func touchesMoved(_ touches: Set, with event: UIEvent?) { + let scale = UIScreen.main.nativeScale + let touch = touches.first! + let loc = touch.location(in: self.view) + ExampleTouchEvent("move", Int(loc.x*scale), Int(loc.y*scale)) + } + + override func touchesEnded(_ touches: Set, with event: UIEvent?) { + let scale = UIScreen.main.nativeScale + let touch = touches.first! + let loc = touch.location(in: self.view) + ExampleTouchEvent("up", Int(loc.x*scale), Int(loc.y*scale)) + } +} diff --git a/examples/ios/CanvasIOSExample/README.md b/examples/ios/CanvasIOSExample/README.md new file mode 100644 index 0000000..2fd0e28 --- /dev/null +++ b/examples/ios/CanvasIOSExample/README.md @@ -0,0 +1,5 @@ +Run this command: + +gomobile bind -target ios + +Then add the resulting Example.framework into the Xcode project, and it should compile and run from there diff --git a/examples/ios/ios.go b/examples/ios/ios.go new file mode 100644 index 0000000..8fc154c --- /dev/null +++ b/examples/ios/ios.go @@ -0,0 +1,41 @@ +package example + +import ( + "math" + "time" + + "github.com/tfriedel6/canvas" + "github.com/tfriedel6/canvas/gliosimpl" +) + +var cv *canvas.Canvas +var mx, my float64 + +func TouchEvent(typ string, x, y int) { + mx, my = float64(x), float64(y) +} + +func LoadGL(w, h int) { + err := canvas.LoadGL(gliosimpl.GLImpl{}) + if err != nil { + time.Sleep(100 * time.Millisecond) + panic(err) + } + cv = canvas.New(0, 0, w, h) +} + +func DrawFrame() { + if cv == nil { + return + } + w, h := float64(cv.Width()), float64(cv.Height()) + + cv.SetFillStyle("#000") + cv.FillRect(0, 0, w, h) + cv.SetFillStyle("#0F0") + cv.FillRect(w*0.25, h*0.25, w*0.5, h*0.5) + cv.SetLineWidth(6) + sqrSize := math.Min(w, h) * 0.1 + cv.SetStrokeStyle("#F00") + cv.StrokeRect(mx-sqrSize/2, my-sqrSize/2, sqrSize, sqrSize) +} diff --git a/gliosimpl/gliosimpl.go b/gliosimpl/gliosimpl.go new file mode 100644 index 0000000..0896dd4 --- /dev/null +++ b/gliosimpl/gliosimpl.go @@ -0,0 +1,224 @@ +package gliosimpl + +// #include +// #include +// #cgo ios LDFLAGS: -framework OpenGLES +import "C" +import ( + "fmt" + "reflect" + "unsafe" + + "github.com/tfriedel6/canvas" +) + +type GLImpl struct{} + +var _ canvas.GL = GLImpl{} + +func (_ GLImpl) Ptr(data interface{}) unsafe.Pointer { + if data == nil { + return unsafe.Pointer(nil) + } + var addr unsafe.Pointer + v := reflect.ValueOf(data) + switch v.Type().Kind() { + case reflect.Ptr: + e := v.Elem() + switch e.Kind() { + case + reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, + reflect.Float32, reflect.Float64: + addr = unsafe.Pointer(e.UnsafeAddr()) + default: + panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind())) + } + case reflect.Uintptr: + addr = unsafe.Pointer(v.Pointer()) + case reflect.Slice: + addr = unsafe.Pointer(v.Index(0).UnsafeAddr()) + default: + panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type())) + } + return addr +} +func (_ GLImpl) ActiveTexture(texture uint32) { + C.glActiveTexture(C.GLenum(texture)) +} +func (_ GLImpl) AttachShader(program uint32, shader uint32) { + C.glAttachShader(C.GLuint(program), C.GLuint(shader)) +} +func (_ GLImpl) BindBuffer(target uint32, buffer uint32) { + C.glBindBuffer(C.GLenum(target), C.GLuint(buffer)) +} +func (_ GLImpl) BindTexture(target uint32, texture uint32) { + C.glBindTexture(C.GLenum(target), C.GLuint(texture)) +} +func (_ GLImpl) BlendFunc(sfactor uint32, dfactor uint32) { + C.glBlendFunc(C.GLenum(sfactor), C.GLenum(dfactor)) +} +func (_ GLImpl) BufferData(target uint32, size int, data unsafe.Pointer, usage uint32) { + C.glBufferData(C.GLenum(target), C.GLsizeiptr(size), data, C.GLenum(usage)) +} +func (_ GLImpl) Clear(mask uint32) { + C.glClear(C.GLbitfield(mask)) +} +func (_ GLImpl) ColorMask(red bool, green bool, blue bool, alpha bool) { + var r, g, b, a C.GLboolean + if red { + r = 1 + } + if green { + g = 1 + } + if blue { + b = 1 + } + if alpha { + a = 1 + } + C.glColorMask(r, g, b, a) +} +func (_ GLImpl) CompileShader(shader uint32) { + C.glCompileShader(C.GLuint(shader)) +} +func (_ GLImpl) CreateProgram() uint32 { + return uint32(C.glCreateProgram()) +} +func (_ GLImpl) CreateShader(xtype uint32) uint32 { + return uint32(C.glCreateShader(C.GLenum(xtype))) +} +func (_ GLImpl) DeleteShader(shader uint32) { + C.glDeleteShader(C.GLuint(shader)) +} +func (_ GLImpl) DeleteTextures(n int32, textures *uint32) { + C.glDeleteTextures(C.GLsizei(n), (*C.GLuint)(textures)) +} +func (_ GLImpl) DisableVertexAttribArray(index uint32) { + C.glDisableVertexAttribArray(C.GLuint(index)) +} +func (_ GLImpl) DrawArrays(mode uint32, first int32, count int32) { + C.glDrawArrays(C.GLenum(mode), C.GLint(first), C.GLsizei(count)) +} +func (_ GLImpl) Enable(cap uint32) { + C.glEnable(C.GLenum(cap)) +} +func (_ GLImpl) EnableVertexAttribArray(index uint32) { + C.glEnableVertexAttribArray(C.GLuint(index)) +} +func (_ GLImpl) GenBuffers(n int32, buffers *uint32) { + C.glGenBuffers(C.GLsizei(n), (*C.GLuint)(buffers)) +} +func (_ GLImpl) GenTextures(n int32, textures *uint32) { + C.glGenTextures(C.GLsizei(n), (*C.GLuint)(textures)) +} +func (_ GLImpl) GenerateMipmap(target uint32) { + C.glGenerateMipmap(C.GLenum(target)) +} +func (_ GLImpl) GetAttribLocation(program uint32, name string) int32 { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + return int32(C.glGetAttribLocation(C.GLuint(program), (*C.GLchar)(cname))) +} +func (_ GLImpl) GetError() uint32 { + return uint32(C.glGetError()) +} +func (_ GLImpl) GetProgramInfoLog(program uint32) string { + var length C.GLint + C.glGetProgramiv(C.GLuint(program), C.GL_INFO_LOG_LENGTH, &length) + if length == 0 { + return "" + } + clog := C.CBytes(make([]byte, int(length)+1)) + defer C.free(clog) + C.glGetProgramInfoLog(C.GLuint(program), C.GLsizei(length), nil, (*C.GLchar)(clog)) + return string(C.GoBytes(clog, C.int(length))) +} +func (_ GLImpl) GetProgramiv(program uint32, pname uint32, params *int32) { + C.glGetProgramiv(C.GLuint(program), C.GLenum(pname), (*C.GLint)(params)) +} +func (_ GLImpl) GetShaderInfoLog(program uint32) string { + var length C.GLint + C.glGetShaderiv(C.GLuint(program), C.GL_INFO_LOG_LENGTH, &length) + if length == 0 { + return "" + } + clog := C.CBytes(make([]byte, int(length)+1)) + defer C.free(clog) + C.glGetShaderInfoLog(C.GLuint(program), C.GLsizei(length), nil, (*C.GLchar)(clog)) + return string(C.GoBytes(clog, C.int(length))) +} +func (_ GLImpl) GetShaderiv(shader uint32, pname uint32, params *int32) { + C.glGetShaderiv(C.GLuint(shader), C.GLenum(pname), (*C.GLint)(params)) + +} +func (_ GLImpl) GetUniformLocation(program uint32, name string) int32 { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + return int32(C.glGetUniformLocation(C.GLuint(program), (*C.GLchar)(cname))) +} +func (_ GLImpl) LinkProgram(program uint32) { + C.glLinkProgram(C.GLuint(program)) +} +func (_ GLImpl) ReadPixels(x int32, y int32, width int32, height int32, format uint32, xtype uint32, pixels unsafe.Pointer) { + C.glReadPixels(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height), C.GLenum(format), C.GLenum(xtype), pixels) +} +func (_ GLImpl) Scissor(x int32, y int32, width int32, height int32) { + C.glScissor(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height)) +} +func (_ GLImpl) ShaderSource(shader uint32, source string) { + csource := (*C.GLchar)(C.CString(source)) + defer C.free(unsafe.Pointer(csource)) + C.glShaderSource(C.GLuint(shader), 1, &csource, nil) +} +func (_ GLImpl) StencilFunc(xfunc uint32, ref int32, mask uint32) { + C.glStencilFunc(C.GLenum(xfunc), C.GLint(ref), C.GLuint(mask)) +} +func (_ GLImpl) StencilMask(mask uint32) { + C.glStencilMask(C.GLuint(mask)) +} +func (_ GLImpl) StencilOp(fail uint32, zfail uint32, zpass uint32) { + C.glStencilOp(C.GLenum(fail), C.GLenum(zfail), C.GLenum(zpass)) +} +func (_ GLImpl) TexImage2D(target uint32, level int32, internalformat int32, width int32, height int32, border int32, format uint32, xtype uint32, pixels unsafe.Pointer) { + C.glTexImage2D(C.GLenum(target), C.GLint(level), C.GLint(internalformat), C.GLsizei(width), C.GLsizei(height), C.GLint(border), C.GLenum(format), C.GLenum(xtype), pixels) +} +func (_ GLImpl) TexParameteri(target uint32, pname uint32, param int32) { + C.glTexParameteri(C.GLenum(target), C.GLenum(pname), C.GLint(param)) +} +func (_ GLImpl) TexSubImage2D(target uint32, level int32, xoffset int32, yoffset int32, width int32, height int32, format uint32, xtype uint32, pixels unsafe.Pointer) { + C.glTexSubImage2D(C.GLenum(target), C.GLint(level), C.GLint(xoffset), C.GLint(yoffset), C.GLsizei(width), C.GLsizei(height), C.GLenum(format), C.GLenum(xtype), pixels) +} +func (_ GLImpl) Uniform1f(location int32, v0 float32) { + C.glUniform1f(C.GLint(location), C.GLfloat(v0)) +} +func (_ GLImpl) Uniform1i(location int32, v0 int32) { + C.glUniform1i(C.GLint(location), C.GLint(v0)) +} +func (_ GLImpl) Uniform2f(location int32, v0 float32, v1 float32) { + C.glUniform2f(C.GLint(location), C.GLfloat(v0), C.GLfloat(v1)) +} +func (_ GLImpl) Uniform4f(location int32, v0 float32, v1 float32, v2 float32, v3 float32) { + C.glUniform4f(C.GLint(location), C.GLfloat(v0), C.GLfloat(v1), C.GLfloat(v2), C.GLfloat(v3)) +} +func (_ GLImpl) UniformMatrix3fv(location int32, count int32, transpose bool, value *float32) { + var t C.GLboolean + if transpose { + t = 1 + } + C.glUniformMatrix3fv(C.GLint(location), C.GLsizei(count), t, (*C.GLfloat)(value)) +} +func (_ GLImpl) UseProgram(program uint32) { + C.glUseProgram(C.GLuint(program)) +} +func (_ GLImpl) VertexAttribPointer(index uint32, size int32, xtype uint32, normalized bool, stride int32, offset uint32) { + var n C.GLboolean + if normalized { + n = 1 + } + C.glVertexAttribPointer(C.GLuint(index), C.GLint(size), C.GLenum(xtype), n, C.GLsizei(stride), unsafe.Pointer(uintptr(offset))) +} +func (_ GLImpl) Viewport(x int32, y int32, width int32, height int32) { + C.glViewport(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height)) +}