diff --git a/Classes/Retronator/Xni/Framework/Content/BasicEffectReader.m b/Classes/Retronator/Xni/Framework/Content/BasicEffectReader.m index edbdccb..bd1b38e 100644 --- a/Classes/Retronator/Xni/Framework/Content/BasicEffectReader.m +++ b/Classes/Retronator/Xni/Framework/Content/BasicEffectReader.m @@ -25,6 +25,7 @@ effect.specularColor = content.specularColor; effect.emissiveColor = content.emissiveColor; effect.texture = [input readSharedResourceFrom:content.texture]; + NSLog(@"loaded: %i", [effect.texture retainCount]); effect.textureEnabled = effect.texture != nil; return effect; diff --git a/Classes/Retronator/Xni/Framework/Content/ContentManager.m b/Classes/Retronator/Xni/Framework/Content/ContentManager.m index fea3613..7a63be7 100644 --- a/Classes/Retronator/Xni/Framework/Content/ContentManager.m +++ b/Classes/Retronator/Xni/Framework/Content/ContentManager.m @@ -83,7 +83,7 @@ // Check if we have already loaded this file. id existing = [loadedFiles objectForKey:filePath]; if (existing) { - return existing; + return [existing retain]; // TODO: Expecting a retained object crap! Remove when proper. } // Find extension and absolute path. diff --git a/Classes/Retronator/Xni/Framework/Content/ModelMeshReader.m b/Classes/Retronator/Xni/Framework/Content/ModelMeshReader.m index 8007728..bf1ca0f 100644 --- a/Classes/Retronator/Xni/Framework/Content/ModelMeshReader.m +++ b/Classes/Retronator/Xni/Framework/Content/ModelMeshReader.m @@ -18,6 +18,8 @@ - (id) readFromInput:(ContentReader *)input into:(id)existingInstance { ModelMeshContent *content = input.content; + NSLog(@"Mesh: %@", content.name); + // Create all model mesh parts. NSMutableArray *meshParts = [NSMutableArray array]; for (ModelMeshPartContent *meshPartContent in content.meshParts) { diff --git a/Classes/Retronator/Xni/Framework/GameHost.m b/Classes/Retronator/Xni/Framework/GameHost.m index 932e4cf..9b481f8 100644 --- a/Classes/Retronator/Xni/Framework/GameHost.m +++ b/Classes/Retronator/Xni/Framework/GameHost.m @@ -10,11 +10,13 @@ #import #import "Retronator.Xni.Framework.h" +#import "Retronator.Xni.Framework.Media.h" @implementation GameHost - (id) init { if (self = [super init]) { + [MediaPlayer load]; window = [[GameWindow alloc] init]; } return self; diff --git a/Classes/Retronator/Xni/Framework/GameView.m b/Classes/Retronator/Xni/Framework/GameView.m index a857af8..492c5e3 100644 --- a/Classes/Retronator/Xni/Framework/GameView.m +++ b/Classes/Retronator/Xni/Framework/GameView.m @@ -11,15 +11,15 @@ @implementation GameView -- (id) initWithFrame:(CGRect)frame { +- (id) initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { viewSizeChanged = [[Event alloc] init]; CAEAGLLayer *eaglLayer = (CAEAGLLayer*)self.layer; - - eaglLayer.contentsScale = [UIScreen mainScreen].scale; - eaglLayer.opaque = TRUE; + + //eaglLayer.contentsScale = [UIScreen mainScreen].scale; + eaglLayer.opaque = YES; eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; diff --git a/Classes/Retronator/Xni/Framework/GameViewController.m b/Classes/Retronator/Xni/Framework/GameViewController.m index 730da1f..b5e82bf 100644 --- a/Classes/Retronator/Xni/Framework/GameViewController.m +++ b/Classes/Retronator/Xni/Framework/GameViewController.m @@ -102,7 +102,7 @@ [self dismissModalViewControllerAnimated:YES]; } -- (void)loadView { +- (void)loadView { GameView *gameView = [[GameView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; self.view = gameView; diff --git a/Classes/Retronator/Xni/Framework/GameWindow.h b/Classes/Retronator/Xni/Framework/GameWindow.h index 0fe93b0..efe850e 100644 --- a/Classes/Retronator/Xni/Framework/GameWindow.h +++ b/Classes/Retronator/Xni/Framework/GameWindow.h @@ -31,7 +31,7 @@ @property (nonatomic, readonly) Event *orientationChanged; - (void) beginScreenDeviceChangeWithFullscreen:(BOOL)willBeFullscreen; -- (void) endScreenDeviceChange; +- (void) endScreenDeviceChangeWithClientWidth:(int)clientWidth clientHeight:(int)clientHeight; - (void) setSupportedOrientations:(DisplayOrientation)orientations; diff --git a/Classes/Retronator/Xni/Framework/GameWindow.m b/Classes/Retronator/Xni/Framework/GameWindow.m index 67d7b8a..888838f 100644 --- a/Classes/Retronator/Xni/Framework/GameWindow.m +++ b/Classes/Retronator/Xni/Framework/GameWindow.m @@ -59,13 +59,50 @@ - (void) beginScreenDeviceChangeWithFullscreen:(BOOL)willBeFullscreen { gameViewController.wantsFullScreenLayout = willBeFullscreen; [[UIApplication sharedApplication] setStatusBarHidden:willBeFullscreen]; - gameViewController.view.frame = [UIScreen mainScreen].applicationFrame; - [clientBounds release]; - clientBounds = [[self calculateClientBounds] retain]; } -- (void) endScreenDeviceChange { - +- (void) endScreenDeviceChangeWithClientWidth:(int)clientWidth clientHeight:(int)clientHeight { + CGRect realFrame = [UIScreen mainScreen].applicationFrame; + float realAspectRatio = realFrame.size.width / realFrame.size.height; + + if (clientWidth == 0) { + clientWidth = realFrame.size.width; + } + + if (clientHeight == 0) { + clientHeight = realFrame.size.height; + } + + float targetAspectRatio = (float)clientWidth/(float)clientHeight; + CGRect targetFrame; + + if (targetAspectRatio >= realAspectRatio) { + // Add black borders on top and bottom. + targetFrame.size.width = realFrame.size.width; + targetFrame.size.height = targetFrame.size.width / targetAspectRatio; + } else { + // Add black borders on left and right. + targetFrame.size.height = realFrame.size.height; + targetFrame.size.width = targetFrame.size.height * targetAspectRatio; + } + + // Center the window. + targetFrame.origin.x = realFrame.origin.x + (realFrame.size.width - targetFrame.size.width) / 2; + targetFrame.origin.y = realFrame.origin.y + (realFrame.size.height - targetFrame.size.height) / 2; + + // Resize the target view. + gameViewController.view.frame = targetFrame; + + // Recalculate client bounds. + [clientBounds release]; + clientBounds = [[self calculateClientBounds] retain]; + + // Set scale factor. + if (targetAspectRatio >= realAspectRatio) { + gameViewController.view.contentScaleFactor = clientWidth / realFrame.size.width; + } else { + gameViewController.view.contentScaleFactor = clientHeight / realFrame.size.height; + } } - (void) initialize { @@ -74,6 +111,7 @@ // Create game view controller. gameViewController = [[GameViewController alloc] initWithGameWindow:self]; + [((GameView*)gameViewController.view).viewSizeChanged subscribeDelegate:[Delegate delegateWithTarget:self Method:@selector(gameViewSizeChanged)]]; @@ -102,7 +140,7 @@ - (Rectangle*) calculateClientBounds { Rectangle *bounds = [Rectangle rectangleWithCGRect:gameViewController.view.bounds]; - float scale = [UIScreen mainScreen].scale; + float scale = gameViewController.view.contentScaleFactor; bounds.width *= scale; bounds.height *= scale; return bounds; diff --git a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice+Internal.h b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice+Internal.h index df0bd95..7f2ef87 100644 --- a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice+Internal.h +++ b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice+Internal.h @@ -13,6 +13,8 @@ @interface GraphicsDevice (Internal) - (uint) createTexture; +- (void) releaseTexture:(uint)textureId; + //- (void) getData:(void*)data fromTexture2D:(Texture2D*)texture level:(int)level; - (void) setData:(void*)data toTexture2D:(Texture2D*)texture SourceRectangle:(Rectangle*)rect level:(int)level; diff --git a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m index 0814047..d0e4773 100644 --- a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m +++ b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m @@ -327,6 +327,10 @@ return textureId; } +- (void) releaseTexture:(uint)textureId { + glDeleteTextures(1, &textureId); +} + - (void) setData:(void*)data toTexture2D:(Texture2D*)texture SourceRectangle:(Rectangle*)rect level:(int)level { GLenum format, type; [GraphicsDevice getFormat:&format AndType:&type ForSurfaceFormat:texture.format]; diff --git a/Classes/Retronator/Xni/Framework/Graphics/Texture2D.m b/Classes/Retronator/Xni/Framework/Graphics/Texture2D.m index 7151e38..91a0766 100644 --- a/Classes/Retronator/Xni/Framework/Graphics/Texture2D.m +++ b/Classes/Retronator/Xni/Framework/Graphics/Texture2D.m @@ -92,5 +92,12 @@ [graphicsDevice setData:data toTexture2D:self SourceRectangle:rect level:level]; } +- (void) dealloc +{ + [graphicsDevice releaseTexture:textureId]; + [super dealloc]; +} + + @end diff --git a/Classes/Retronator/Xni/Framework/GraphicsDeviceManager.m b/Classes/Retronator/Xni/Framework/GraphicsDeviceManager.m index ddd5faa..a74f7c8 100644 --- a/Classes/Retronator/Xni/Framework/GraphicsDeviceManager.m +++ b/Classes/Retronator/Xni/Framework/GraphicsDeviceManager.m @@ -72,6 +72,7 @@ - (void) applyChanges { [game.window setSupportedOrientations:supportedOrientations]; [game.window beginScreenDeviceChangeWithFullscreen:isFullScreen]; + [game.window endScreenDeviceChangeWithClientWidth:self.preferedBackBufferWidth clientHeight:self.preferedBackBufferHeight]; if (graphicsDevice != nil && graphicsDevice.graphicsProfile != graphicsProfile) { // Different graphics profile requested. @@ -91,13 +92,11 @@ default: break; } - [game.window endScreenDeviceChange]; [deviceCreated raiseWithSender:self]; } else { // Reset the existing device. [deviceResetting raiseWithSender:self]; [graphicsDevice reset]; - [game.window endScreenDeviceChange]; [deviceReset raiseWithSender:self]; } } diff --git a/Classes/Retronator/Xni/Framework/Input/Touch/TouchPanel.m b/Classes/Retronator/Xni/Framework/Input/Touch/TouchPanel.m index 53376d4..d8a70d5 100644 --- a/Classes/Retronator/Xni/Framework/Input/Touch/TouchPanel.m +++ b/Classes/Retronator/Xni/Framework/Input/Touch/TouchPanel.m @@ -147,7 +147,7 @@ static TouchPanel *instance; } - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { - float scale = [UIScreen mainScreen].scale; + float scale = view.contentScaleFactor; for (UITouch *touch in touches) { XniTouchLocation *location = [touchLocations objectForKey:touch]; if (location) { @@ -194,7 +194,7 @@ static TouchPanel *instance; lateReleaseTouches = temp; // Add new touches - float scale = [UIScreen mainScreen].scale; + float scale = view.contentScaleFactor; for (UITouch *touch in addTouches) { CGPoint position = [touch locationInView:view]; XniTouchLocation *location = [[[XniTouchLocation alloc]