diff --git a/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs b/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs index 0e51dc109d..7fb27b27b2 100755 --- a/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs +++ b/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs @@ -21,12 +21,17 @@ namespace OpenRA.FileFormats.Graphics public RendererAttribute( Type graphicsDeviceType ) { - if( !typeof( IGraphicsDevice ).IsAssignableFrom( graphicsDeviceType ) ) + if( !typeof( IDeviceFactory ).IsAssignableFrom( graphicsDeviceType ) ) throw new InvalidOperationException( "Incorrect type in RendererAttribute" ); Type = graphicsDeviceType; } } + public interface IDeviceFactory + { + IGraphicsDevice Create( Size size, WindowMode windowMode, bool vsync ); + } + public interface IGraphicsDevice { IVertexBuffer CreateVertexBuffer( int length ); diff --git a/OpenRA.Game/Graphics/Renderer.cs b/OpenRA.Game/Graphics/Renderer.cs index 337989e939..3675d6b08c 100644 --- a/OpenRA.Game/Graphics/Renderer.cs +++ b/OpenRA.Game/Graphics/Renderer.cs @@ -142,11 +142,11 @@ namespace OpenRA.Graphics static IGraphicsDevice CreateDevice( Assembly rendererDll, int width, int height, WindowMode window, bool vsync ) { - var argTypes = new Type[] { typeof( int ), typeof( int ), typeof( WindowMode ), typeof( bool ) }; - var argValues = new object[] { width, height, window, vsync }; - foreach( RendererAttribute r in rendererDll.GetCustomAttributes( typeof( RendererAttribute ), false ) ) - return (IGraphicsDevice)r.Type.GetConstructor( argTypes ).Invoke( argValues ); + { + var factory = (IDeviceFactory) r.Type.GetConstructor( Type.EmptyTypes ).Invoke( null ); + return factory.Create( new Size( width, height ), window, vsync ); + } throw new InvalidOperationException("Renderer DLL is missing RendererAttribute to tell us what type to use!"); } diff --git a/OpenRA.Renderer.Cg/GraphicsDevice.cs b/OpenRA.Renderer.Cg/GraphicsDevice.cs index 31eef6a948..e6962992f0 100755 --- a/OpenRA.Renderer.Cg/GraphicsDevice.cs +++ b/OpenRA.Renderer.Cg/GraphicsDevice.cs @@ -16,10 +16,18 @@ using Tao.Cg; using Tao.OpenGl; using Tao.Sdl; -[assembly: Renderer(typeof(OpenRA.Renderer.Cg.GraphicsDevice))] +[assembly: Renderer(typeof(OpenRA.Renderer.Cg.DeviceFactory))] namespace OpenRA.Renderer.Cg { + public class DeviceFactory : IDeviceFactory + { + public IGraphicsDevice Create(Size size, WindowMode windowMode, bool vsync) + { + return new GraphicsDevice( size, windowMode, vsync ); + } + } + public class GraphicsDevice : IGraphicsDevice { Size windowSize; @@ -75,7 +83,7 @@ namespace OpenRA.Renderer.Cg throw new InvalidOperationException("CG Error. See graphics.log for details"); }; - public GraphicsDevice( int width, int height, WindowMode window, bool vsync ) + public GraphicsDevice( Size size, WindowMode window, bool vsync ) { Console.WriteLine("Using Cg renderer"); Sdl.SDL_Init( Sdl.SDL_INIT_NOPARACHUTE | Sdl.SDL_INIT_VIDEO ); @@ -102,7 +110,7 @@ namespace OpenRA.Renderer.Cg break; } - surf = Sdl.SDL_SetVideoMode( width, height, 0, Sdl.SDL_OPENGL | windowFlags ); + surf = Sdl.SDL_SetVideoMode( size.Width, size.Height, 0, Sdl.SDL_OPENGL | windowFlags ); Sdl.SDL_WM_SetCaption( "OpenRA", "OpenRA" ); Sdl.SDL_ShowCursor( 0 ); @@ -111,7 +119,7 @@ namespace OpenRA.Renderer.Cg CheckGlError(); - windowSize = new Size( width, height ); + windowSize = size; cgContext = Tao.Cg.Cg.cgCreateContext(); diff --git a/OpenRA.Renderer.Gl/GraphicsDevice.cs b/OpenRA.Renderer.Gl/GraphicsDevice.cs index d58fb6c7df..7033f27de8 100755 --- a/OpenRA.Renderer.Gl/GraphicsDevice.cs +++ b/OpenRA.Renderer.Gl/GraphicsDevice.cs @@ -11,16 +11,23 @@ using System; using System.Drawing; using System.IO; +using System.Linq; using OpenRA.FileFormats.Graphics; -using Tao.Cg; using Tao.OpenGl; using Tao.Sdl; -using System.Linq; -[assembly: Renderer(typeof(OpenRA.Renderer.Glsl.GraphicsDevice))] +[assembly: Renderer(typeof(OpenRA.Renderer.Glsl.DeviceFactory))] namespace OpenRA.Renderer.Glsl { + public class DeviceFactory : IDeviceFactory + { + public IGraphicsDevice Create(Size size, WindowMode windowMode, bool vsync) + { + return new GraphicsDevice( size, windowMode, vsync ); + } + } + public class GraphicsDevice : IGraphicsDevice { Size windowSize; @@ -65,7 +72,7 @@ namespace OpenRA.Renderer.Glsl Log.Write("graphics", Gl.glGetString(Gl.GL_EXTENSIONS)); } - public GraphicsDevice( int width, int height, WindowMode window, bool vsync ) + public GraphicsDevice( Size size, WindowMode window, bool vsync ) { Console.WriteLine("Using Gl renderer"); Sdl.SDL_Init( Sdl.SDL_INIT_NOPARACHUTE | Sdl.SDL_INIT_VIDEO ); @@ -89,7 +96,7 @@ namespace OpenRA.Renderer.Glsl break; } - surf = Sdl.SDL_SetVideoMode( width, height, 0, Sdl.SDL_OPENGL | windowFlags ); + surf = Sdl.SDL_SetVideoMode( size.Width, size.Height, 0, Sdl.SDL_OPENGL | windowFlags ); Sdl.SDL_WM_SetCaption( "OpenRA", "OpenRA" ); Sdl.SDL_ShowCursor( 0 ); @@ -115,7 +122,7 @@ namespace OpenRA.Renderer.Glsl throw new InvalidProgramException("Unsupported GPU. See graphics.log for details."); } - windowSize = new Size( width, height ); + windowSize = size; Gl.glEnableClientState( Gl.GL_VERTEX_ARRAY ); CheckGlError(); diff --git a/OpenRA.Renderer.Null/NullGraphicsDevice.cs b/OpenRA.Renderer.Null/NullGraphicsDevice.cs index fa507b4726..cbd1739dd9 100644 --- a/OpenRA.Renderer.Null/NullGraphicsDevice.cs +++ b/OpenRA.Renderer.Null/NullGraphicsDevice.cs @@ -10,22 +10,29 @@ using System; using System.Drawing; -using System.IO; using OpenRA.FileFormats.Graphics; using OpenRA.Graphics; -[assembly: Renderer(typeof(OpenRA.Renderer.Null.NullGraphicsDevice))] +[assembly: Renderer(typeof(OpenRA.Renderer.Null.DeviceFactory))] namespace OpenRA.Renderer.Null { + public class DeviceFactory : IDeviceFactory + { + public IGraphicsDevice Create(Size size, WindowMode windowMode, bool vsync) + { + return new NullGraphicsDevice( size, windowMode, vsync ); + } + } + public class NullGraphicsDevice : IGraphicsDevice { public Size WindowSize { get; internal set; } - public NullGraphicsDevice(int width, int height, WindowMode window, bool vsync) + public NullGraphicsDevice(Size size, WindowMode window, bool vsync) { Console.WriteLine("Using Null renderer"); - WindowSize = new Size(width, height); + WindowSize = size; } public void EnableScissor(int left, int top, int width, int height) { }