added an iOS GL implementation and example

This commit is contained in:
Thomas Friedel 2018-05-15 18:49:30 +02:00
parent d56fc2e8ce
commit 87fa8256eb
12 changed files with 854 additions and 0 deletions

1
.gitignore vendored
View file

@ -3,4 +3,5 @@ plan.txt
glowgen
testapp
videocap
.DS_Store

View file

@ -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

View file

@ -0,0 +1,4 @@
xcuserdata
project.xcworkspace
Example.framework

View file

@ -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 = "<group>"; };
A094B52420AB3D0C000BCEA6 /* Example.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Example.framework; sourceTree = "<group>"; };
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 = "<group>"; };
A0F2844420AAD8100049BD39 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
A0F2844720AAD8100049BD39 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
A0F2844920AAD8100049BD39 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
/* 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 = "<group>";
};
A0F2843B20AAD80F0049BD39 /* Products */ = {
isa = PBXGroup;
children = (
A0F2843A20AAD80F0049BD39 /* CanvasIOSExample.app */,
);
name = Products;
sourceTree = "<group>";
};
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 = "<group>";
};
/* 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 = "<group>";
};
/* 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 */;
}

View file

@ -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:.
}
}

View file

@ -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"
}
}

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" systemVersion="17A277" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>

View file

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>

View file

@ -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<UITouch>, 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<UITouch>, 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<UITouch>, 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))
}
}

View file

@ -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

41
examples/ios/ios.go Normal file
View file

@ -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)
}

224
gliosimpl/gliosimpl.go Normal file
View file

@ -0,0 +1,224 @@
package gliosimpl
// #include <stdlib.h>
// #include <OpenGLES/ES2/gl.h>
// #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))
}