Scaffold EVE fullscreen OpenGL app
This commit is contained in:
commit
c6c73d60c0
15 changed files with 494 additions and 0 deletions
7
Sources/EVEAppDelegate.h
Normal file
7
Sources/EVEAppDelegate.h
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface EVEAppDelegate : UIResponder <UIApplicationDelegate>
|
||||
|
||||
@property(nonatomic, strong) UIWindow *window;
|
||||
|
||||
@end
|
||||
18
Sources/EVEAppDelegate.m
Normal file
18
Sources/EVEAppDelegate.m
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#import "EVEAppDelegate.h"
|
||||
|
||||
#import "EVEViewController.h"
|
||||
|
||||
@implementation EVEAppDelegate
|
||||
|
||||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
||||
(void)application;
|
||||
(void)launchOptions;
|
||||
|
||||
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
|
||||
self.window.rootViewController = [[EVEViewController alloc] init];
|
||||
self.window.backgroundColor = UIColor.blackColor;
|
||||
[self.window makeKeyAndVisible];
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
||||
8
Sources/EVEGLView.h
Normal file
8
Sources/EVEGLView.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface EVEGLView : UIView
|
||||
|
||||
- (void)resizeDrawableIfNeeded;
|
||||
- (void)renderAtTime:(NSTimeInterval)time;
|
||||
|
||||
@end
|
||||
95
Sources/EVEGLView.mm
Normal file
95
Sources/EVEGLView.mm
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
#import "EVEGLView.h"
|
||||
|
||||
#import <math.h>
|
||||
#import <OpenGLES/ES3/gl.h>
|
||||
#import <QuartzCore/CAEAGLLayer.h>
|
||||
|
||||
@interface EVEGLView ()
|
||||
|
||||
@property(nonatomic, strong) EAGLContext *context;
|
||||
@property(nonatomic, assign) GLuint framebuffer;
|
||||
@property(nonatomic, assign) GLuint colorRenderbuffer;
|
||||
@property(nonatomic, assign) GLint drawableWidth;
|
||||
@property(nonatomic, assign) GLint drawableHeight;
|
||||
|
||||
@end
|
||||
|
||||
@implementation EVEGLView
|
||||
|
||||
+ (Class)layerClass {
|
||||
return CAEAGLLayer.class;
|
||||
}
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame {
|
||||
self = [super initWithFrame:frame];
|
||||
if (!self) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
self.contentScaleFactor = UIScreen.mainScreen.scale;
|
||||
self.opaque = YES;
|
||||
self.multipleTouchEnabled = YES;
|
||||
|
||||
CAEAGLLayer *layer = (CAEAGLLayer *)self.layer;
|
||||
layer.opaque = YES;
|
||||
layer.drawableProperties = @{
|
||||
kEAGLDrawablePropertyRetainedBacking: @NO,
|
||||
kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8,
|
||||
};
|
||||
|
||||
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
|
||||
NSAssert(self.context != nil, @"EVE requires OpenGL ES 3.0.");
|
||||
[EAGLContext setCurrentContext:self.context];
|
||||
|
||||
glGenFramebuffers(1, &_framebuffer);
|
||||
glGenRenderbuffers(1, &_colorRenderbuffer);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, self.framebuffer);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, self.colorRenderbuffer);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, self.colorRenderbuffer);
|
||||
|
||||
[self resizeDrawableIfNeeded];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[EAGLContext setCurrentContext:self.context];
|
||||
if (_colorRenderbuffer) {
|
||||
glDeleteRenderbuffers(1, &_colorRenderbuffer);
|
||||
}
|
||||
if (_framebuffer) {
|
||||
glDeleteFramebuffers(1, &_framebuffer);
|
||||
}
|
||||
[EAGLContext setCurrentContext:nil];
|
||||
}
|
||||
|
||||
- (void)layoutSubviews {
|
||||
[super layoutSubviews];
|
||||
[self resizeDrawableIfNeeded];
|
||||
}
|
||||
|
||||
- (void)resizeDrawableIfNeeded {
|
||||
[EAGLContext setCurrentContext:self.context];
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, self.colorRenderbuffer);
|
||||
[self.context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
|
||||
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &_drawableWidth);
|
||||
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &_drawableHeight);
|
||||
glViewport(0, 0, self.drawableWidth, self.drawableHeight);
|
||||
}
|
||||
|
||||
- (void)renderAtTime:(NSTimeInterval)time {
|
||||
[EAGLContext setCurrentContext:self.context];
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, self.framebuffer);
|
||||
glViewport(0, 0, self.drawableWidth, self.drawableHeight);
|
||||
|
||||
float pulse = (float)((sin(time * 0.8) + 1.0) * 0.5);
|
||||
float green = 0.08f + pulse * 0.12f;
|
||||
float blue = 0.13f + pulse * 0.18f;
|
||||
|
||||
glClearColor(0.02f, green, blue, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, self.colorRenderbuffer);
|
||||
[self.context presentRenderbuffer:GL_RENDERBUFFER];
|
||||
}
|
||||
|
||||
@end
|
||||
5
Sources/EVEViewController.h
Normal file
5
Sources/EVEViewController.h
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface EVEViewController : UIViewController
|
||||
|
||||
@end
|
||||
148
Sources/EVEViewController.mm
Normal file
148
Sources/EVEViewController.mm
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
#import "EVEViewController.h"
|
||||
|
||||
#import <CoreMotion/CoreMotion.h>
|
||||
#import <QuartzCore/QuartzCore.h>
|
||||
|
||||
#import "EVEGLView.h"
|
||||
|
||||
@interface EVEViewController ()
|
||||
|
||||
@property(nonatomic, strong) EVEGLView *glView;
|
||||
@property(nonatomic, strong) UILabel *overlayLabel;
|
||||
@property(nonatomic, strong) CADisplayLink *displayLink;
|
||||
@property(nonatomic, strong) CMMotionManager *motionManager;
|
||||
@property(nonatomic, assign) NSTimeInterval previousTimestamp;
|
||||
@property(nonatomic, assign) double filteredFPS;
|
||||
@property(nonatomic, assign) NSUInteger touchCount;
|
||||
|
||||
@end
|
||||
|
||||
@implementation EVEViewController
|
||||
|
||||
- (void)loadView {
|
||||
UIView *root = [[UIView alloc] initWithFrame:UIScreen.mainScreen.bounds];
|
||||
root.backgroundColor = UIColor.blackColor;
|
||||
root.multipleTouchEnabled = YES;
|
||||
|
||||
self.glView = [[EVEGLView alloc] initWithFrame:root.bounds];
|
||||
self.glView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
||||
[root addSubview:self.glView];
|
||||
|
||||
self.overlayLabel = [[UILabel alloc] initWithFrame:CGRectZero];
|
||||
self.overlayLabel.translatesAutoresizingMaskIntoConstraints = NO;
|
||||
self.overlayLabel.numberOfLines = 0;
|
||||
self.overlayLabel.textColor = UIColor.whiteColor;
|
||||
self.overlayLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.45];
|
||||
self.overlayLabel.font = [UIFont monospacedSystemFontOfSize:13.0 weight:UIFontWeightRegular];
|
||||
self.overlayLabel.layer.cornerRadius = 6.0;
|
||||
self.overlayLabel.layer.masksToBounds = YES;
|
||||
[root addSubview:self.overlayLabel];
|
||||
|
||||
UILayoutGuide *safe = root.safeAreaLayoutGuide;
|
||||
[NSLayoutConstraint activateConstraints:@[
|
||||
[self.overlayLabel.leadingAnchor constraintEqualToAnchor:safe.leadingAnchor constant:12.0],
|
||||
[self.overlayLabel.topAnchor constraintEqualToAnchor:safe.topAnchor constant:12.0],
|
||||
[self.overlayLabel.widthAnchor constraintLessThanOrEqualToAnchor:safe.widthAnchor multiplier:0.92],
|
||||
]];
|
||||
|
||||
self.view = root;
|
||||
}
|
||||
|
||||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
|
||||
self.motionManager = [[CMMotionManager alloc] init];
|
||||
self.motionManager.accelerometerUpdateInterval = 1.0 / 30.0;
|
||||
self.motionManager.gyroUpdateInterval = 1.0 / 30.0;
|
||||
if (self.motionManager.accelerometerAvailable) {
|
||||
[self.motionManager startAccelerometerUpdates];
|
||||
}
|
||||
if (self.motionManager.gyroAvailable) {
|
||||
[self.motionManager startGyroUpdates];
|
||||
}
|
||||
|
||||
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(frameTick:)];
|
||||
[self.displayLink addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[self.displayLink invalidate];
|
||||
[self.motionManager stopAccelerometerUpdates];
|
||||
[self.motionManager stopGyroUpdates];
|
||||
}
|
||||
|
||||
- (BOOL)prefersStatusBarHidden {
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures {
|
||||
return UIRectEdgeAll;
|
||||
}
|
||||
|
||||
- (BOOL)prefersHomeIndicatorAutoHidden {
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)viewDidLayoutSubviews {
|
||||
[super viewDidLayoutSubviews];
|
||||
[self.glView resizeDrawableIfNeeded];
|
||||
}
|
||||
|
||||
- (void)frameTick:(CADisplayLink *)link {
|
||||
if (self.previousTimestamp > 0) {
|
||||
NSTimeInterval delta = link.timestamp - self.previousTimestamp;
|
||||
if (delta > 0) {
|
||||
double instantFPS = 1.0 / delta;
|
||||
self.filteredFPS = self.filteredFPS == 0 ? instantFPS : (self.filteredFPS * 0.9 + instantFPS * 0.1);
|
||||
}
|
||||
}
|
||||
self.previousTimestamp = link.timestamp;
|
||||
|
||||
[self.glView renderAtTime:link.timestamp];
|
||||
[self updateOverlay];
|
||||
}
|
||||
|
||||
- (void)updateOverlay {
|
||||
UIScreen *screen = UIScreen.mainScreen;
|
||||
CGSize points = screen.bounds.size;
|
||||
CGFloat scale = screen.scale;
|
||||
CGSize pixels = CGSizeMake(points.width * scale, points.height * scale);
|
||||
|
||||
CMAcceleration acceleration = self.motionManager.accelerometerData.acceleration;
|
||||
CMRotationRate rotation = self.motionManager.gyroData.rotationRate;
|
||||
|
||||
self.overlayLabel.text = [NSString stringWithFormat:
|
||||
@"EVE Canvas\n"
|
||||
"OpenGL ES 3.0 + UIKit text\n"
|
||||
"points %.0fx%.0f pixels %.0fx%.0f @ %.1fx\n"
|
||||
"fps %.1f touches %lu\n"
|
||||
"accel %+0.2f %+0.2f %+0.2f\n"
|
||||
"gyro %+0.2f %+0.2f %+0.2f",
|
||||
points.width, points.height, pixels.width, pixels.height, scale,
|
||||
self.filteredFPS, (unsigned long)self.touchCount,
|
||||
acceleration.x, acceleration.y, acceleration.z,
|
||||
rotation.x, rotation.y, rotation.z];
|
||||
[self.overlayLabel sizeToFit];
|
||||
}
|
||||
|
||||
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
|
||||
(void)touches;
|
||||
self.touchCount = event.allTouches.count;
|
||||
}
|
||||
|
||||
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
|
||||
(void)touches;
|
||||
self.touchCount = event.allTouches.count;
|
||||
}
|
||||
|
||||
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
|
||||
(void)touches;
|
||||
self.touchCount = event.allTouches.count;
|
||||
}
|
||||
|
||||
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
|
||||
(void)touches;
|
||||
self.touchCount = event.allTouches.count;
|
||||
}
|
||||
|
||||
@end
|
||||
9
Sources/main.m
Normal file
9
Sources/main.m
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#import <UIKit/UIKit.h>
|
||||
|
||||
#import "EVEAppDelegate.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
@autoreleasepool {
|
||||
return UIApplicationMain(argc, argv, nil, NSStringFromClass([EVEAppDelegate class]));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue