From 01f76821e6848879eddbcb639c2e60432555e5f6 Mon Sep 17 00:00:00 2001 From: Matej Jan Date: Tue, 27 Jul 2010 18:23:25 +0000 Subject: [PATCH] Added a graphics device manager. git-svn-id: http://xni.googlecode.com/svn/XNI@10 ac433895-eea3-a490-d80a-17149a75e588 --- Classes/Retronator/Xni/Framework/GameWindow.m | 2 +- .../Retronator/Xni/Framework/Graphics/Enums.h | 75 ++++++++++---- .../Xni/Framework/Graphics/GraphicsDevice.h | 2 + .../Xni/Framework/Graphics/GraphicsDevice.m | 17 +++- .../Xni/Framework/GraphicsDeviceManager.h | 49 ++++++++++ .../Xni/Framework/GraphicsDeviceManager.m | 98 +++++++++++++++++++ .../Xni/Framework/Retronator.Xni.Framework.h | 2 +- XNI.xcodeproj/project.pbxproj | 8 ++ 8 files changed, 229 insertions(+), 24 deletions(-) create mode 100644 Classes/Retronator/Xni/Framework/GraphicsDeviceManager.h create mode 100644 Classes/Retronator/Xni/Framework/GraphicsDeviceManager.m diff --git a/Classes/Retronator/Xni/Framework/GameWindow.m b/Classes/Retronator/Xni/Framework/GameWindow.m index be17dc3..9004d25 100644 --- a/Classes/Retronator/Xni/Framework/GameWindow.m +++ b/Classes/Retronator/Xni/Framework/GameWindow.m @@ -31,7 +31,7 @@ @synthesize orientationChanged; - (id) handle { - return nil;//(id)gameViewController.view.layer; + return (id)gameViewController.view.layer; } // METHODS diff --git a/Classes/Retronator/Xni/Framework/Graphics/Enums.h b/Classes/Retronator/Xni/Framework/Graphics/Enums.h index 06c6c3e..3062e1d 100644 --- a/Classes/Retronator/Xni/Framework/Graphics/Enums.h +++ b/Classes/Retronator/Xni/Framework/Graphics/Enums.h @@ -2,13 +2,10 @@ #import typedef enum { - DataTypeUnsignedByte = GL_UNSIGNED_BYTE, - DataTypeByte = GL_BYTE, - DataTypeUnsignedShort = GL_UNSIGNED_SHORT, - DataTypeShort = GL_SHORT, - DataTypeFixed = GL_FIXED, - DataTypeFloat = GL_FLOAT -} DataType; + ClearOptionsDepthBuffer = GL_DEPTH_BUFFER_BIT, + ClearOptionsStencil = GL_STENCIL_BUFFER_BIT, + ClearOptionsTarget = GL_COLOR_BUFFER_BIT +} ClearOptions; typedef enum { CompareFunctionAlways = GL_ALWAYS, @@ -22,10 +19,20 @@ typedef enum { } CompareFunction; typedef enum { - ClearOptionsDepthBuffer = GL_DEPTH_BUFFER_BIT, - ClearOptionsStencil = GL_STENCIL_BUFFER_BIT, - ClearOptionsTarget = GL_COLOR_BUFFER_BIT -} ClearOptions; + DataTypeUnsignedByte = GL_UNSIGNED_BYTE, + DataTypeByte = GL_BYTE, + DataTypeUnsignedShort = GL_UNSIGNED_SHORT, + DataTypeShort = GL_SHORT, + DataTypeFixed = GL_FIXED, + DataTypeFloat = GL_FLOAT +} DataType; + +typedef enum { + DepthFormatNone, + DepthFormatDepth16, + DepthFormatDepth24, + DepthFormatDepth24Stencil8 +} DepthFormat; typedef enum { FogModeNone = 0, @@ -34,11 +41,25 @@ typedef enum { FogModeExponentSquared = GL_EXP2 } FogMode; +typedef enum { + GraphicsProfileReach, + GraphicsProfileHiDef +} GraphicsProfile; + typedef enum { IndexElementSizeEightBits = 1, IndexElementSizeSixteenBits = 2, } IndexElementSize; +typedef enum { + PrimitiveTypePointList = GL_POINTS, + PrimitiveTypeLineList = GL_LINES, + PrimitiveTypeLineStrip = GL_LINE_STRIP, + PrimitiveTypeTriangleList = GL_TRIANGLES, + PrimitiveTypeTriangleStrip = GL_TRIANGLE_STRIP, + PrimitiveTypeTriangleFan = GL_TRIANGLE_FAN +} PrimitiveType; + typedef enum { ResourceUsageStatic = GL_STATIC_DRAW, ResourceUsageDynamic = GL_DYNAMIC_DRAW @@ -50,6 +71,29 @@ typedef enum { ResourceTypeVertexBuffer = GL_ARRAY_BUFFER } ResourceType; +typedef enum { + SurfaceFormatColor, + SurfaceFormatBgr565, + SurfaceFormatBgra5551, + SurfaceFormatBgra4444, + SurfaceFormatDxt1, + SurfaceFormatDxt3, + SurfaceFormatDxt5, + SurfaceFormatNormalizedByte2, + SurfaceFormatNormalizedByte4, + SurfaceFormatRgba1010102, + SurfaceFormatRg32, + SurfaceFormatRgba64, + SurfaceFormatAlpha8, + SurfaceFormatSingle, + SurfaceFormatVector2, + SurfaceFormatVector4, + SurfaceFormatHalfSingle, + SurfaceFormatHalfVector2, + SurfaceFormatHalfVector4, + SurfaceFormatHdrBlendable +} SurfaceFormat; + typedef enum { VertexElementFormatSingle, VertexElementFormatVector2, @@ -78,12 +122,3 @@ typedef enum { VertexElementUsageTextureCoordinate = GL_TEXTURE_COORD_ARRAY, VertexElementUsagePointSize = GL_POINT_SIZE_ARRAY_OES } VertexElementUsage; - -typedef enum { - PrimitiveTypePointList = GL_POINTS, - PrimitiveTypeLineList = GL_LINES, - PrimitiveTypeLineStrip = GL_LINE_STRIP, - PrimitiveTypeTriangleList = GL_TRIANGLES, - PrimitiveTypeTriangleStrip = GL_TRIANGLE_STRIP, - PrimitiveTypeTriangleFan = GL_TRIANGLE_FAN -} PrimitiveType; \ No newline at end of file diff --git a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.h b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.h index 915a2a4..d1acac4 100644 --- a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.h +++ b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.h @@ -26,6 +26,8 @@ GLuint defaultFramebuffer, colorRenderbuffer, depthRenderbuffer; } +@property (nonatomic, readonly) GraphicsProfile graphicsProfile; + - (id) initWithGame:(Game*) theGame; // Presentation diff --git a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m index 3d39c99..3ae4b83 100644 --- a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m +++ b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m @@ -48,6 +48,8 @@ return self; } +@synthesize graphicsProfile; + // Presentation - (void) reset { CAEAGLLayer *layer = (CAEAGLLayer*)game.window.handle; @@ -75,8 +77,19 @@ } // Render buffers -- (void) clearWithColor:(Color*)color {} -- (void) clearWithOptions:(ClearOptions)options color:(Color*)color depth:(float)depth stencil:(int)stencil {} + +- (void) clearWithColor:(Color *)color { + glClearColor(color.r / 255.0, color.g / 255.0, color.b / 255.0, color.a / 255.0); + glClearDepthf(1); + glClear(ClearOptionsTarget | ClearOptionsDepthBuffer); +} + +- (void) clearWithOptions:(ClearOptions)options color:(Color *)color depth:(float)depth stencil:(int)stencil { + glClearColor(color.r / 255.0, color.g / 255.0, color.b / 255.0, color.a / 255.0); + glClearDepthf(depth); + glClearStencil(stencil); + glClear(options); +} - (EAGLContext*) createContext { return nil; } diff --git a/Classes/Retronator/Xni/Framework/GraphicsDeviceManager.h b/Classes/Retronator/Xni/Framework/GraphicsDeviceManager.h new file mode 100644 index 0000000..60d11df --- /dev/null +++ b/Classes/Retronator/Xni/Framework/GraphicsDeviceManager.h @@ -0,0 +1,49 @@ +// +// GraphicsDeviceManager.h +// XNI +// +// Created by Matej Jan on 27.7.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import +#import "System.h" + +#import "IGraphicsDeviceManager.h" +#import "IGraphicsDeviceService.h" +#import "Retronator.Xni.Framework.classes.h" +#import "Retronator.Xni.Framework.Graphics.classes.h" + +@interface GraphicsDeviceManager : NSObject { + GraphicsProfile graphicsProfile; + BOOL isFullScreen; + BOOL preferMultiSampling; + SurfaceFormat preferedSurfaceFormat; + int preferedBackBufferWidth; + int preferedBackBufferHeight; + DepthFormat preferedDepthStencilFormat; + DisplayOrientation supportedOrientations; + + Game *game; + GraphicsDevice *graphicsDevice; + Event *deviceCreated; + Event *deviceDisposing; + Event *deviceReseting; + Event *deviceReset; +} + +- (id) initWithGame:(Game*)theGame; + +@property (nonatomic) GraphicsProfile graphicsProfile; +@property (nonatomic) BOOL isFullScreen; +@property (nonatomic) BOOL preferMultiSampling; +@property (nonatomic) SurfaceFormat preferedSurfaceFormat; +@property (nonatomic) int preferedBackBufferWidth; +@property (nonatomic) int preferedBackBufferHeight; +@property (nonatomic) DepthFormat preferedDepthStencilFormat; +@property (nonatomic) DisplayOrientation supportedOrientations; + +- (void) toggleFullscreen; +- (void) applyChanges; + +@end diff --git a/Classes/Retronator/Xni/Framework/GraphicsDeviceManager.m b/Classes/Retronator/Xni/Framework/GraphicsDeviceManager.m new file mode 100644 index 0000000..4bd477c --- /dev/null +++ b/Classes/Retronator/Xni/Framework/GraphicsDeviceManager.m @@ -0,0 +1,98 @@ +// +// GraphicsDeviceManager.m +// XNI +// +// Created by Matej Jan on 27.7.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import "GraphicsDeviceManager.h" + +#import "Retronator.Xni.Framework.h" +#import "Retronator.Xni.Framework.Graphics.h" + +@implementation GraphicsDeviceManager +- (id) initWithGame:(Game*)theGame { + if (self = [super init]) { + game = theGame; + + deviceCreated = [[Event alloc] init]; + deviceDisposing = [[Event alloc] init]; + deviceReseting = [[Event alloc] init]; + deviceReset = [[Event alloc] init]; + + [game.services addService:self ofType:[Protocols graphicsDeviceManager]]; + [game.services addService:self ofType:[Protocols graphicsDeviceService]]; + } + return self; +} + +@synthesize graphicsProfile; +@synthesize isFullScreen; +@synthesize preferMultiSampling; +@synthesize preferedSurfaceFormat; +@synthesize preferedBackBufferWidth; +@synthesize preferedBackBufferHeight; +@synthesize preferedDepthStencilFormat; +@synthesize supportedOrientations; + +@synthesize graphicsDevice; +@synthesize deviceCreated; +@synthesize deviceDisposing; +@synthesize deviceReseting; +@synthesize deviceReset; + +- (void) toggleFullscreen { + isFullScreen = !isFullScreen; + [self applyChanges]; +} + +- (void) createDevice { + [self applyChanges]; + + // Listen to client size change from now on. + [game.window.clientSizeChanged + subscribeDelegate:[Delegate delegateWithTarget:self Method:@selector(applyChanges)]]; +} + +- (BOOL) beginDraw { + return YES; +} + +- (void) endDraw { + [graphicsDevice present]; +} + +- (void) applyChanges { + [game.window beginScreenDeviceChangeWithFullscreen:isFullScreen]; + + if (graphicsDevice != nil && graphicsDevice.graphicsProfile != graphicsProfile) { + // Different graphics profile requested. + [deviceDisposing raiseWithSender:self]; + [graphicsDevice release]; + graphicsDevice = nil; + } + if (graphicsDevice == nil) { + // Create a new device. + graphicsDevice = [[GraphicsDevice alloc] initWithGame:game]; + [game.window endScreenDeviceChange]; + [deviceCreated raiseWithSender:self]; + } else { + // Reset the existing device. + [deviceReseting raiseWithSender:self]; + [graphicsDevice reset]; + [game.window endScreenDeviceChange]; + [deviceReset raiseWithSender:self]; + } +} + +- (void) dealloc { + [deviceDisposing raiseWithSender:self]; + [graphicsDevice release]; + [deviceCreated release]; + [deviceDisposing release]; + [deviceReseting release]; + [deviceReset release]; + [super dealloc]; +} +@end diff --git a/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.h b/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.h index c8f3bf0..3404a3c 100644 --- a/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.h +++ b/Classes/Retronator/Xni/Framework/Retronator.Xni.Framework.h @@ -9,7 +9,7 @@ #import "Game.h" #import "GameTime.h" #import "GameServiceContainer.h" -//#import "GraphicsDeviceManager.h" +#import "GraphicsDeviceManager.h" // Game host #import "GameHost.h" diff --git a/XNI.xcodeproj/project.pbxproj b/XNI.xcodeproj/project.pbxproj index 84fcb24..c5d484e 100644 --- a/XNI.xcodeproj/project.pbxproj +++ b/XNI.xcodeproj/project.pbxproj @@ -37,6 +37,8 @@ B5DDE8CA11FF2AD6000DB38B /* Enums.h in Headers */ = {isa = PBXBuildFile; fileRef = B5DDE8C911FF2AD6000DB38B /* Enums.h */; }; B5DDE90911FF352E000DB38B /* Color.h in Headers */ = {isa = PBXBuildFile; fileRef = B5DDE90711FF352E000DB38B /* Color.h */; }; B5DDE90A11FF352E000DB38B /* Color.m in Sources */ = {isa = PBXBuildFile; fileRef = B5DDE90811FF352E000DB38B /* Color.m */; }; + B5DDE99111FF546B000DB38B /* GraphicsDeviceManager.h in Headers */ = {isa = PBXBuildFile; fileRef = B5DDE98F11FF546B000DB38B /* GraphicsDeviceManager.h */; }; + B5DDE99211FF546B000DB38B /* GraphicsDeviceManager.m in Sources */ = {isa = PBXBuildFile; fileRef = B5DDE99011FF546B000DB38B /* GraphicsDeviceManager.m */; }; B5DE189811F8884A00BF3275 /* Delegate.h in Headers */ = {isa = PBXBuildFile; fileRef = B5DE189211F8884A00BF3275 /* Delegate.h */; }; B5DE189911F8884A00BF3275 /* Delegate.m in Sources */ = {isa = PBXBuildFile; fileRef = B5DE189311F8884A00BF3275 /* Delegate.m */; }; B5DE189A11F8884A00BF3275 /* Event.h in Headers */ = {isa = PBXBuildFile; fileRef = B5DE189411F8884A00BF3275 /* Event.h */; }; @@ -91,6 +93,8 @@ B5DDE8C911FF2AD6000DB38B /* Enums.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Enums.h; sourceTree = ""; }; B5DDE90711FF352E000DB38B /* Color.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Color.h; sourceTree = ""; }; B5DDE90811FF352E000DB38B /* Color.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Color.m; sourceTree = ""; }; + B5DDE98F11FF546B000DB38B /* GraphicsDeviceManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GraphicsDeviceManager.h; sourceTree = ""; }; + B5DDE99011FF546B000DB38B /* GraphicsDeviceManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GraphicsDeviceManager.m; sourceTree = ""; }; B5DE189211F8884A00BF3275 /* Delegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Delegate.h; sourceTree = ""; }; B5DE189311F8884A00BF3275 /* Delegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Delegate.m; sourceTree = ""; }; B5DE189411F8884A00BF3275 /* Event.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Event.h; sourceTree = ""; }; @@ -232,6 +236,8 @@ B5DDE7ED11FF06BB000DB38B /* GameServiceContainer.h */, B5DDE7EE11FF06BB000DB38B /* GameServiceContainer.m */, B5DDE82C11FF10FD000DB38B /* IGraphicsDeviceManager.h */, + B5DDE98F11FF546B000DB38B /* GraphicsDeviceManager.h */, + B5DDE99011FF546B000DB38B /* GraphicsDeviceManager.m */, B5DDE80111FF0AE3000DB38B /* IGameComponent.h */, B5DDE80411FF0B2D000DB38B /* IUpdatable.h */, B5DDE80611FF0B3C000DB38B /* IDrawable.h */, @@ -299,6 +305,7 @@ B5DDE8C711FF2A23000DB38B /* HiDefGraphicsDevice.h in Headers */, B5DDE8CA11FF2AD6000DB38B /* Enums.h in Headers */, B5DDE90911FF352E000DB38B /* Color.h in Headers */, + B5DDE99111FF546B000DB38B /* GraphicsDeviceManager.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -366,6 +373,7 @@ B5DDE8C411FF29E8000DB38B /* ReachGraphicsDevice.m in Sources */, B5DDE8C811FF2A23000DB38B /* HiDefGraphicsDevice.m in Sources */, B5DDE90A11FF352E000DB38B /* Color.m in Sources */, + B5DDE99211FF546B000DB38B /* GraphicsDeviceManager.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };