From e8b63f42089681f3aa1ce77244f857dcd5e7b959 Mon Sep 17 00:00:00 2001 From: Matej Jan Date: Thu, 22 Jul 2010 16:13:55 +0000 Subject: [PATCH] Added initial window handling. git-svn-id: http://xni.googlecode.com/svn/XNI@7 ac433895-eea3-a490-d80a-17149a75e588 --- .../Xni/Framework/DisplayOrientation.h | 6 ++ Classes/Retronator/Xni/Framework/GameHost.h | 26 ++++++ Classes/Retronator/Xni/Framework/GameHost.m | 65 +++++++++++++++ Classes/Retronator/Xni/Framework/GameView.h | 19 +++++ Classes/Retronator/Xni/Framework/GameView.m | 47 +++++++++++ .../Xni/Framework/GameViewController.h | 18 ++++ .../Xni/Framework/GameViewController.m | 73 +++++++++++++++++ Classes/Retronator/Xni/Framework/GameWindow.h | 38 +++++++++ Classes/Retronator/Xni/Framework/GameWindow.m | 82 +++++++++++++++++++ Classes/Retronator/Xni/Framework/Rectangle.h | 4 + Classes/Retronator/Xni/Framework/Rectangle.m | 29 +++++++ .../Retronator.Xni.Framework.classes.h | 6 +- .../Xni/Framework/Retronator.Xni.Framework.h | 7 +- XNI.xcodeproj/project.pbxproj | 18 +++- 14 files changed, 435 insertions(+), 3 deletions(-) create mode 100644 Classes/Retronator/Xni/Framework/DisplayOrientation.h create mode 100644 Classes/Retronator/Xni/Framework/GameHost.h create mode 100644 Classes/Retronator/Xni/Framework/GameHost.m create mode 100644 Classes/Retronator/Xni/Framework/GameView.h create mode 100644 Classes/Retronator/Xni/Framework/GameView.m create mode 100644 Classes/Retronator/Xni/Framework/GameViewController.h create mode 100644 Classes/Retronator/Xni/Framework/GameViewController.m create mode 100644 Classes/Retronator/Xni/Framework/GameWindow.h create mode 100644 Classes/Retronator/Xni/Framework/GameWindow.m diff --git a/Classes/Retronator/Xni/Framework/DisplayOrientation.h b/Classes/Retronator/Xni/Framework/DisplayOrientation.h new file mode 100644 index 0000000..fb9b9db --- /dev/null +++ b/Classes/Retronator/Xni/Framework/DisplayOrientation.h @@ -0,0 +1,6 @@ +typedef enum { + DisplayOrientationDefault = 0, + DisplayOrientationLandscapeLeft = 1, + DisplayOrientationLandscapeRight = 2, + DisplayOrientationPortrait +} DisplayOrientation; \ No newline at end of file diff --git a/Classes/Retronator/Xni/Framework/GameHost.h b/Classes/Retronator/Xni/Framework/GameHost.h new file mode 100644 index 0000000..2c3b741 --- /dev/null +++ b/Classes/Retronator/Xni/Framework/GameHost.h @@ -0,0 +1,26 @@ +// +// GameHost.h +// XNI +// +// Created by Matej Jan on 20.7.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import +#import + +#import "Retronator.Xni.Framework.classes.h" + +@interface GameHost : UIApplication { + BOOL isExiting; + //Game *game; + GameWindow *window; +} + +@property (nonatomic, readonly) GameWindow *window; + +- (void) initialize; +- (void) run; +- (void) exit; + +@end diff --git a/Classes/Retronator/Xni/Framework/GameHost.m b/Classes/Retronator/Xni/Framework/GameHost.m new file mode 100644 index 0000000..5e06963 --- /dev/null +++ b/Classes/Retronator/Xni/Framework/GameHost.m @@ -0,0 +1,65 @@ +// +// GameHost.m +// XNI +// +// Created by Matej Jan on 20.7.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import "GameHost.h" + +#import "Retronator.Xni.Framework.h" + +@implementation GameHost + +- (id) init { + if (self = [super init]) { + window = [[GameWindow alloc] init]; + } + return self; +} + +@synthesize window; + +- (void) initialize { + [window initialize]; +} + +- (void) run { + // Hijack the run loop. + NSLog(@"Starting the game loop."); + + //game = [self delegate]; + + SInt32 runResult; + + while (!isExiting) + { + // We run a very tight autorelease pool loop. + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + // Handle all the waiting event sources. + do { + runResult = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, YES); + } while (runResult == kCFRunLoopRunHandledSource); + + //[game tick]; + + // We release memory every frame. + [pool release]; + } +} + +- (void) exit { + NSLog(@"Exiting the game loop."); + isExiting = YES; +} + +- (void) dealloc +{ + [window release]; + [super dealloc]; +} + + +@end diff --git a/Classes/Retronator/Xni/Framework/GameView.h b/Classes/Retronator/Xni/Framework/GameView.h new file mode 100644 index 0000000..66bb7c6 --- /dev/null +++ b/Classes/Retronator/Xni/Framework/GameView.h @@ -0,0 +1,19 @@ +// +// GameView.h +// XNI +// +// Created by Matej Jan on 22.7.10. +// Copyright 2010 Retronator, Razum. All rights reserved. +// + +#import +#import +#import "System.h" + +@interface GameView : UIView { + Event *viewSizeChanged; +} + +@property (nonatomic, readonly) Event *viewSizeChanged; + +@end diff --git a/Classes/Retronator/Xni/Framework/GameView.m b/Classes/Retronator/Xni/Framework/GameView.m new file mode 100644 index 0000000..4c47ac9 --- /dev/null +++ b/Classes/Retronator/Xni/Framework/GameView.m @@ -0,0 +1,47 @@ +// +// GameView.m +// XNI +// +// Created by Matej Jan on 22.7.10. +// Copyright 2010 Retronator, Razum. All rights reserved. +// + +#import "GameView.h" +#import + +@implementation GameView + +- (id)initWithFrame:(CGRect)frame { + if (self = [super initWithFrame:frame]) { + + viewSizeChanged = [[Event alloc] init]; + + CAEAGLLayer *eaglLayer = (CAEAGLLayer*)self.layer; + + eaglLayer.opaque = TRUE; + eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: + [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, + kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; + } + return self; +} + +@synthesize viewSizeChanged; + ++ (Class) layerClass +{ + return [CAEAGLLayer class]; +} + +- (void) layoutSubviews +{ + [viewSizeChanged raiseWithSender:self]; +} + +- (void) dealloc +{ + [viewSizeChanged release]; + [super dealloc]; +} + +@end diff --git a/Classes/Retronator/Xni/Framework/GameViewController.h b/Classes/Retronator/Xni/Framework/GameViewController.h new file mode 100644 index 0000000..537863f --- /dev/null +++ b/Classes/Retronator/Xni/Framework/GameViewController.h @@ -0,0 +1,18 @@ +// +// GameViewController.h +// XNI +// +// Created by Matej Jan on 22.7.10. +// Copyright 2010 Retronator, Razum. All rights reserved. +// + +#import +#import "Retronator.Xni.Framework.classes.h" + +@interface GameViewController : UIViewController { + GameWindow *gameWindow; +} + +- initWithGameWindow: (GameWindow*)theGameWindow; + +@end diff --git a/Classes/Retronator/Xni/Framework/GameViewController.m b/Classes/Retronator/Xni/Framework/GameViewController.m new file mode 100644 index 0000000..d7ea057 --- /dev/null +++ b/Classes/Retronator/Xni/Framework/GameViewController.m @@ -0,0 +1,73 @@ +// +// GameViewController.m +// XNI +// +// Created by Matej Jan on 22.7.10. +// Copyright 2010 Retronator, Razum. All rights reserved. +// + +#import "GameViewController.h" + +#import "Retronator.Xni.Framework.h" + + +@implementation GameViewController + +- initWithGameWindow: (GameWindow*)theGameWindow { + if (self = [super init]) { + gameWindow = theGameWindow; + } + return self; +} + +- (void)loadView { + GameView *gameView = [[GameView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; + self.view = gameView; + + [gameView release]; +} + + +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { + //InterfaceOrientationFlags orientationFlag = 1 << interfaceOrientation; + return YES;// gameWindow.allowedOrientations & orientationFlag; +} + + +- (void)didReceiveMemoryWarning { + // Releases the view if it doesn't have a superview. + [super didReceiveMemoryWarning]; + + // Release any cached data, images, etc that aren't in use. +} + + +- (void)viewDidUnload { + // Release any retained subviews of the main view. + // e.g. self.myOutlet = nil; +} + + +// Touches +/* +- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + [[MultiTouch instance].touchesBegan fireWithSender:self eventArgs:[TouchEventArgs argsWithTouches:touches event:event]]; +} + +- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { + [[MultiTouch instance].touchesMoved fireWithSender:self eventArgs:[TouchEventArgs argsWithTouches:touches event:event]]; +} + +- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { + [[MultiTouch instance].touchesEnded fireWithSender:self eventArgs:[TouchEventArgs argsWithTouches:touches event:event]]; +} + +- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { + [[MultiTouch instance].touchesCancelled fireWithSender:self eventArgs:[TouchEventArgs argsWithTouches:touches event:event]]; +}*/ + +- (void)dealloc { + [super dealloc]; +} + +@end diff --git a/Classes/Retronator/Xni/Framework/GameWindow.h b/Classes/Retronator/Xni/Framework/GameWindow.h new file mode 100644 index 0000000..f69eb04 --- /dev/null +++ b/Classes/Retronator/Xni/Framework/GameWindow.h @@ -0,0 +1,38 @@ +// +// GameWindow.h +// XNI +// +// Created by Matej Jan on 20.7.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import +#import +#import "System.h" + +#import "Retronator.Xni.Framework.classes.h" + +@interface GameWindow : NSObject { + Rectangle *clientBounds; + DisplayOrientation currentOrientation; + Event *clientSizeChanged; + Event *orientationChanged; + + UIWindow *window; + GameViewController *gameViewController; +} + +@property (nonatomic, readonly) Rectangle *clientBounds; +@property (nonatomic, readonly) DisplayOrientation currentOrientation; +@property (nonatomic, readonly) id handle; + +@property (nonatomic, readonly) Event *clientSizeChanged; +@property (nonatomic, readonly) Event *orientationChanged; + + +- (void) initialize; +- (void) beginScreenDeviceChangeWithFullscreen:(BOOL)willBeFullscreen; +- (void) endScreenDeviceChange; + +@end + diff --git a/Classes/Retronator/Xni/Framework/GameWindow.m b/Classes/Retronator/Xni/Framework/GameWindow.m new file mode 100644 index 0000000..be17dc3 --- /dev/null +++ b/Classes/Retronator/Xni/Framework/GameWindow.m @@ -0,0 +1,82 @@ +// +// GameWindow.m +// XNI +// +// Created by Matej Jan on 20.7.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import "GameWindow.h" + +#import "Retronator.Xni.Framework.h" + +@implementation GameWindow + +- (id) init { + if (self = [super init]) { + clientBounds = [[Rectangle empty] retain]; + currentOrientation = DisplayOrientationDefault; + clientSizeChanged = [[Event alloc] init]; + orientationChanged = [[Event alloc] init]; + } + return self; +} + +// PROPERTIES + +@synthesize clientBounds; +@synthesize currentOrientation; + +@synthesize clientSizeChanged; +@synthesize orientationChanged; + +- (id) handle { + return nil;//(id)gameViewController.view.layer; +} + +// METHODS + +- (void) initialize { + window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; + + // Initialize game view controller. + gameViewController = [[GameViewController alloc] initWithGameWindow:self]; + [((GameView*)gameViewController.view).viewSizeChanged + subscribeDelegate:[Delegate delegateWithTarget:self Method:@selector(gameViewSizeChanged)]]; + + // Add the game view to the window. + [window addSubview: gameViewController.view]; + + // Present the window. + [window makeKeyAndVisible]; +} + +- (void) beginScreenDeviceChangeWithFullscreen:(BOOL)willBeFullscreen { + gameViewController.wantsFullScreenLayout = willBeFullscreen; + [[UIApplication sharedApplication] setStatusBarHidden:willBeFullscreen]; + gameViewController.view.frame = [UIScreen mainScreen].applicationFrame; + clientBounds = [Rectangle rectangleWithCGRect:gameViewController.view.bounds]; +} + +- (void) endScreenDeviceChange { + +} + +- (void) gameViewSizeChanged { + Rectangle *newClientBounds = [Rectangle rectangleWithCGRect:gameViewController.view.bounds]; + if (![newClientBounds equals:clientBounds]) { + [clientBounds release]; + clientBounds = [newClientBounds retain]; + [clientSizeChanged raiseWithSender:self]; + } +} + +- (void) dealloc +{ + [clientBounds release]; + [gameViewController release]; + [window release]; + [super dealloc]; +} + +@end diff --git a/Classes/Retronator/Xni/Framework/Rectangle.h b/Classes/Retronator/Xni/Framework/Rectangle.h index 7aa651d..b826045 100644 --- a/Classes/Retronator/Xni/Framework/Rectangle.h +++ b/Classes/Retronator/Xni/Framework/Rectangle.h @@ -7,6 +7,7 @@ // #import +#import #import "Retronator.Xni.Framework.classes.h" @@ -21,6 +22,7 @@ + (Rectangle*) rectangleWithX:(int)x Y:(int)y Width:(int)width Height:(int)height; + (Rectangle*) rectangleWithStruct:(RectangleStruct*) rectangleStruct; + (Rectangle*) rectangleWithRectangle:(Rectangle*) rectangle; ++ (Rectangle*) rectangleWithCGRect:(CGRect) cgRect; @property (nonatomic) int x; @property (nonatomic) int y; @@ -29,6 +31,8 @@ @property (nonatomic, readonly) RectangleStruct *data; +- (BOOL) equals:(Rectangle*)rectangle; + + (Rectangle*) empty; @end diff --git a/Classes/Retronator/Xni/Framework/Rectangle.m b/Classes/Retronator/Xni/Framework/Rectangle.m index 021335f..f4f425b 100644 --- a/Classes/Retronator/Xni/Framework/Rectangle.m +++ b/Classes/Retronator/Xni/Framework/Rectangle.m @@ -11,6 +11,8 @@ @implementation Rectangle +// CONSTRUCTORS + - (id) initWithX:(int)x Y:(int)y Width:(int)width Height:(int)height { if (self = [super init]) { data = RectangleMake(x, y, width, height); @@ -41,6 +43,13 @@ return [[[Rectangle alloc] initWithRectangle:rectangle] autorelease]; } ++ (Rectangle*) rectangleWithCGRect:(CGRect) cgRect { + return [Rectangle rectangleWithX:cgRect.origin.x Y:cgRect.origin.y + Width:cgRect.size.width Height:cgRect.size.height]; +} + +// PROPERTIES + - (int) x {return data.x;} - (void) setX:(int)value {data.x = value;} @@ -55,6 +64,26 @@ - (RectangleStruct*) data {return &data;} +// METHODS + +- (BOOL) equals:(Rectangle*)rectangle { + return rectangle.data->x == data.x && rectangle.data->y == data.y && + rectangle.data->width == data.width && rectangle.data->height == data.height; +} + +- (BOOL) isEqual:(id)object { + if ([object isKindOfClass:[Rectangle class]]) { + return [self equals:object]; + } + return NO; +} + +- (NSUInteger) hash { + return data.x ^ data.y ^ data.width ^ data.height; +} + +// CONSTANTS + + (Rectangle*) empty {return [Rectangle rectangleWithX:0 Y:0 Width:0 Height:0];} @end diff --git a/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.classes.h b/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.classes.h index 8d49e7e..cbd3a64 100644 --- a/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.classes.h +++ b/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.classes.h @@ -1,3 +1,7 @@ // Data structures #import "RectangleStruct.h" -@class Rectangle; \ No newline at end of file +@class Rectangle; + +// Host +#import "DisplayOrientation.h" +@class GameWindow, GameViewController, GameView; \ No newline at end of file diff --git a/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.h b/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.h index c426357..f311723 100644 --- a/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.h +++ b/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.h @@ -1,2 +1,7 @@ // Data structures -#import "Rectangle.h" \ No newline at end of file +#import "Rectangle.h" + +// Host +#import "GameWindow.h" +#import "GameViewController.h" +#import "GameView.h" \ No newline at end of file diff --git a/XNI.xcodeproj/project.pbxproj b/XNI.xcodeproj/project.pbxproj index 87b6be7..1f1f0bc 100644 --- a/XNI.xcodeproj/project.pbxproj +++ b/XNI.xcodeproj/project.pbxproj @@ -26,6 +26,10 @@ B5DE190511F88AF500BF3275 /* Rectangle.m in Sources */ = {isa = PBXBuildFile; fileRef = B5DE190311F88AF500BF3275 /* Rectangle.m */; }; B5DE190711F88B5D00BF3275 /* RectangleStruct.h in Headers */ = {isa = PBXBuildFile; fileRef = B5DE190611F88B5D00BF3275 /* RectangleStruct.h */; }; B5DE193111F898AE00BF3275 /* DisplayOrientation.h in Headers */ = {isa = PBXBuildFile; fileRef = B5DE193011F898AE00BF3275 /* DisplayOrientation.h */; }; + B5DE194A11F89C0900BF3275 /* GameViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = B5DE194811F89C0900BF3275 /* GameViewController.h */; }; + B5DE194B11F89C0900BF3275 /* GameViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B5DE194911F89C0900BF3275 /* GameViewController.m */; }; + B5DE194E11F89C1F00BF3275 /* GameView.h in Headers */ = {isa = PBXBuildFile; fileRef = B5DE194C11F89C1F00BF3275 /* GameView.h */; }; + B5DE194F11F89C1F00BF3275 /* GameView.m in Sources */ = {isa = PBXBuildFile; fileRef = B5DE194D11F89C1F00BF3275 /* GameView.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -49,6 +53,10 @@ B5DE190311F88AF500BF3275 /* Rectangle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Rectangle.m; sourceTree = ""; }; B5DE190611F88B5D00BF3275 /* RectangleStruct.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RectangleStruct.h; sourceTree = ""; }; B5DE193011F898AE00BF3275 /* DisplayOrientation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DisplayOrientation.h; sourceTree = ""; }; + B5DE194811F89C0900BF3275 /* GameViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameViewController.h; sourceTree = ""; }; + B5DE194911F89C0900BF3275 /* GameViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameViewController.m; sourceTree = ""; }; + B5DE194C11F89C1F00BF3275 /* GameView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameView.h; sourceTree = ""; }; + B5DE194D11F89C1F00BF3275 /* GameView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameView.m; sourceTree = ""; }; D2AAC07E0554694100DB518D /* libXNI.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libXNI.a; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -131,12 +139,16 @@ B5DE18A011F8888B00BF3275 /* Retronator.Xni.Framework.h */, B5DE18FA11F88AD900BF3275 /* GameHost.h */, B5DE18FB11F88AD900BF3275 /* GameHost.m */, + B5DE193011F898AE00BF3275 /* DisplayOrientation.h */, B5DE18FC11F88AD900BF3275 /* GameWindow.h */, B5DE18FD11F88AD900BF3275 /* GameWindow.m */, B5DE190611F88B5D00BF3275 /* RectangleStruct.h */, B5DE190211F88AF500BF3275 /* Rectangle.h */, B5DE190311F88AF500BF3275 /* Rectangle.m */, - B5DE193011F898AE00BF3275 /* DisplayOrientation.h */, + B5DE194811F89C0900BF3275 /* GameViewController.h */, + B5DE194911F89C0900BF3275 /* GameViewController.m */, + B5DE194C11F89C1F00BF3275 /* GameView.h */, + B5DE194D11F89C1F00BF3275 /* GameView.m */, ); path = Framework; sourceTree = ""; @@ -175,6 +187,8 @@ B5DE190411F88AF500BF3275 /* Rectangle.h in Headers */, B5DE190711F88B5D00BF3275 /* RectangleStruct.h in Headers */, B5DE193111F898AE00BF3275 /* DisplayOrientation.h in Headers */, + B5DE194A11F89C0900BF3275 /* GameViewController.h in Headers */, + B5DE194E11F89C1F00BF3275 /* GameView.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -227,6 +241,8 @@ B5DE18FF11F88AD900BF3275 /* GameHost.m in Sources */, B5DE190111F88AD900BF3275 /* GameWindow.m in Sources */, B5DE190511F88AF500BF3275 /* Rectangle.m in Sources */, + B5DE194B11F89C0900BF3275 /* GameViewController.m in Sources */, + B5DE194F11F89C1F00BF3275 /* GameView.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };