Skip to content

Commit

Permalink
improved view and view controller logic
Browse files Browse the repository at this point in the history
  • Loading branch information
racarone committed Jan 26, 2016
1 parent f5352c3 commit 2d448c3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
30 changes: 30 additions & 0 deletions sparrow/src/Classes/SPView.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,36 @@ + (Class)layerClass
return [CAEAGLLayer class];
}

- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
[self initCommon];

return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
[self initCommon];

return self;
}

- (void)initCommon
{
self.opaque = YES;
self.clearsContextBeforeDrawing = NO;
#if !TARGET_OS_TV
self.multipleTouchEnabled = YES;
#endif
}

- (void)displayLayer:(CALayer *)layer
{
[_viewController render];
}

- (void)layoutSubviews
{
[super layoutSubviews];
Expand Down
31 changes: 18 additions & 13 deletions sparrow/src/Classes/SPViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ @implementation SPViewController
BOOL _supportHighResolutions;
BOOL _doubleOnPad;
BOOL _showStats;
BOOL _started;
BOOL _paused;
BOOL _rendering;
}
Expand Down Expand Up @@ -127,6 +128,7 @@ - (void)dealloc
[_programs release];
[_viewPort release];
[_previousViewPort release];
[_overlayView release];

[SPContext setCurrentContext:nil];
[Sparrow setCurrentController:nil];
Expand All @@ -138,6 +140,7 @@ - (void)dealloc
- (void)setup
{
_contentScaleFactor = 1.0;
_paused = YES;
_isPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
_stage = [[SPStage alloc] init];
_juggler = [[SPJuggler alloc] init];
Expand Down Expand Up @@ -178,7 +181,7 @@ - (void)setupContext
}
}

- (void)setupRenderCallback
- (void)setupDisplayLink
{
if (_displayLink)
{
Expand All @@ -188,7 +191,11 @@ - (void)setupRenderCallback

_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(renderingCallback)];
[_displayLink setFrameInterval:_frameInterval];
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_8_4)
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
else
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)renderingCallback
Expand Down Expand Up @@ -216,7 +223,7 @@ - (void)updateViewPort:(BOOL)forceUpdate
float newWidth = _viewPort.width * _viewScaleFactor / _contentScaleFactor;
float newHeight = _viewPort.height * _viewScaleFactor / _contentScaleFactor;

if (_stage.width != newWidth || _stage.height != newHeight)
if (_stage.width != newWidth || _stage.height != newHeight)
{
SPEvent *resizeEvent = [[SPResizeEvent alloc] initWithType:SPEventTypeResize
width:newWidth height:newHeight];
Expand All @@ -243,7 +250,8 @@ - (void)readjustStageSize

- (void)onActive:(NSNotification *)notification
{
self.rendering = YES;
if (_started)
self.rendering = YES;
}

- (void)onResign:(NSNotification *)notification
Expand Down Expand Up @@ -277,6 +285,7 @@ - (void)startWithRoot:(Class)rootClass supportHighResolutions:(BOOL)hd doubleOnP
_rootClass = rootClass;
_supportHighResolutions = hd;
_doubleOnPad = doubleOnPad;
_started = YES;

self.view.contentScaleFactor = _supportHighResolutions ? [[UIScreen mainScreen] scale] : 1.0f;
self.paused = NO;
Expand Down Expand Up @@ -314,8 +323,7 @@ - (void)advanceTime:(double)passedTime
- (void)render
{
if (!_rendering) return;
if (!_context) [self setupContext];
if (!_context) return;
if (!_context) return [self setupContext];

@autoreleasepool
{
Expand Down Expand Up @@ -475,6 +483,8 @@ - (void)setView:(SPView *)view
if (view != _internalView)
{
SP_RELEASE_AND_RETAIN(_internalView, view);
[_previousViewPort setEmpty];

super.view = view;
}
}
Expand All @@ -498,11 +508,6 @@ - (void)loadView
}

_internalView.viewController = self;
_internalView.opaque = YES;
_internalView.clearsContextBeforeDrawing = NO;
#if !TARGET_OS_TV
_internalView.multipleTouchEnabled = YES;
#endif

SP_RELEASE_AND_NIL(_context);
_hasRenderedOnce = NO;
Expand All @@ -513,7 +518,7 @@ - (void)viewDidLoad
{
if (!_overlayView)
{
_overlayView = [[[SPOverlayView alloc] initWithFrame:_internalView.frame] autorelease];
_overlayView = [[SPOverlayView alloc] initWithFrame:_internalView.frame];
_overlayView.opaque = NO;
_overlayView.contentScaleFactor = _internalView.contentScaleFactor;
_overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Expand Down Expand Up @@ -718,7 +723,7 @@ - (void)setRendering:(BOOL)rendering
}
else
{
[self setupRenderCallback];
[self setupDisplayLink];
}
}
}
Expand Down

0 comments on commit 2d448c3

Please sign in to comment.