From 21e8e3a78d83462253733eb5302eb68e8be0dd5e Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 4 Mar 2013 20:17:22 +1300 Subject: [PATCH] Remove unnecessary duplication between renderers. --- .../Graphics/IGraphicsDevice.cs | 1 - OpenRA.Renderer.Cg/GraphicsDevice.cs | 77 +++---------------- OpenRA.Renderer.Gl/GraphicsDevice.cs | 76 +++--------------- OpenRA.Renderer.Null/NullGraphicsDevice.cs | 2 - OpenRA.Renderer.SdlCommon/SdlGraphics.cs | 73 +++++++++++++++--- 5 files changed, 83 insertions(+), 146 deletions(-) diff --git a/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs b/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs index 94b2bc4abd..12231dadca 100755 --- a/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs +++ b/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs @@ -40,7 +40,6 @@ namespace OpenRA.FileFormats.Graphics IShader CreateShader( string name ); Size WindowSize { get; } - int GpuMemoryUsed { get; } void Clear(); void Present(); diff --git a/OpenRA.Renderer.Cg/GraphicsDevice.cs b/OpenRA.Renderer.Cg/GraphicsDevice.cs index b0e6153679..0ac9a1dff2 100755 --- a/OpenRA.Renderer.Cg/GraphicsDevice.cs +++ b/OpenRA.Renderer.Cg/GraphicsDevice.cs @@ -24,21 +24,23 @@ namespace OpenRA.Renderer.Cg { public IGraphicsDevice Create(Size size, WindowMode windowMode) { + Console.WriteLine("Using Cg renderer"); return new GraphicsDevice(size, windowMode); } } - public class GraphicsDevice : IGraphicsDevice + public class GraphicsDevice : SdlGraphics { - Size windowSize; + static string[] RequiredExtensions = + { + "GL_ARB_vertex_program", + "GL_ARB_fragment_program", + "GL_ARB_vertex_buffer_object" + }; + internal IntPtr cgContext; internal int vertexProfile, fragmentProfile; - IntPtr surf; - SdlInput input; - - public Size WindowSize { get { return windowSize; } } - static Tao.Cg.Cg.CGerrorCallbackFuncDelegate CgErrorCallback = () => { var err = Tao.Cg.Cg.cgGetError(); @@ -48,19 +50,8 @@ namespace OpenRA.Renderer.Cg }; public GraphicsDevice(Size size, WindowMode window) + : base(size, window, RequiredExtensions) { - Console.WriteLine("Using Cg renderer"); - windowSize = size; - - var extensions = new [] - { - "GL_ARB_vertex_program", - "GL_ARB_fragment_program", - "GL_ARB_vertex_buffer_object", - }; - - surf = SdlGraphics.InitializeSdlGl(ref windowSize, window, extensions); - cgContext = Tao.Cg.Cg.cgCreateContext(); Tao.Cg.Cg.cgSetErrorCallback(CgErrorCallback); @@ -69,54 +60,8 @@ namespace OpenRA.Renderer.Cg Tao.Cg.CgGl.cgGLSetManageTextureParameters(cgContext, true); vertexProfile = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_VERTEX); fragmentProfile = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_FRAGMENT); - - Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY); - ErrorHandler.CheckGlError(); - Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY); - ErrorHandler.CheckGlError(); - - Sdl.SDL_SetModState(0); // i have had enough. - - input = new SdlInput(surf); } - public void EnableScissor(int left, int top, int width, int height) - { - if (width < 0) width = 0; - if (height < 0) height = 0; - - Gl.glScissor(left, windowSize.Height - ( top + height ), width, height); - ErrorHandler.CheckGlError(); - Gl.glEnable(Gl.GL_SCISSOR_TEST); - ErrorHandler.CheckGlError(); - } - - public void DisableScissor() - { - Gl.glDisable(Gl.GL_SCISSOR_TEST); - ErrorHandler.CheckGlError(); - } - - public void Clear() { SdlGraphics.Clear(); } - public void Present() { Sdl.SDL_GL_SwapBuffers(); } - public void PumpInput(IInputHandler inputHandler) { input.PumpInput(inputHandler); } - - public void DrawPrimitives(PrimitiveType pt, int firstVertex, int numVertices) - { - SdlGraphics.DrawPrimitives(pt, firstVertex, numVertices); - } - - public void SetLineWidth(float width) - { - Gl.glLineWidth(width); - ErrorHandler.CheckGlError(); - } - - public IVertexBuffer CreateVertexBuffer(int size) { return new VertexBuffer(size); } - public ITexture CreateTexture() { return new Texture(); } - public ITexture CreateTexture(Bitmap bitmap) { return new Texture(bitmap); } - public IShader CreateShader(string name) { return new Shader(this, name); } - - public int GpuMemoryUsed { get { return 0; } } + public override IShader CreateShader(string name) { return new Shader(this, name); } } } diff --git a/OpenRA.Renderer.Gl/GraphicsDevice.cs b/OpenRA.Renderer.Gl/GraphicsDevice.cs index 5728ddc123..b9a091bac8 100755 --- a/OpenRA.Renderer.Gl/GraphicsDevice.cs +++ b/OpenRA.Renderer.Gl/GraphicsDevice.cs @@ -23,79 +23,23 @@ namespace OpenRA.Renderer.Glsl { public IGraphicsDevice Create(Size size, WindowMode windowMode) { + Console.WriteLine("Using Gl renderer"); return new GraphicsDevice(size, windowMode); } } - public class GraphicsDevice : IGraphicsDevice + public class GraphicsDevice : SdlGraphics { - Size windowSize; - IntPtr surf; - SdlInput input; - - public Size WindowSize { get { return windowSize; } } + static string[] RequiredExtensions = + { + "GL_ARB_vertex_shader", + "GL_ARB_fragment_shader", + "GL_ARB_vertex_buffer_object" + }; public GraphicsDevice(Size size, WindowMode window) - { - Console.WriteLine("Using Gl renderer"); - windowSize = size; + : base(size, window, RequiredExtensions) {} - var extensions = new [] - { - "GL_ARB_vertex_shader", - "GL_ARB_fragment_shader", - "GL_ARB_vertex_buffer_object", - }; - - surf = SdlGraphics.InitializeSdlGl(ref windowSize, window, extensions); - - Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY); - ErrorHandler.CheckGlError(); - Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY); - ErrorHandler.CheckGlError(); - - Sdl.SDL_SetModState(0); - - input = new SdlInput(surf); - } - - public void EnableScissor(int left, int top, int width, int height) - { - if (width < 0) width = 0; - if (height < 0) height = 0; - - Gl.glScissor(left, windowSize.Height - ( top + height ), width, height); - ErrorHandler.CheckGlError(); - Gl.glEnable(Gl.GL_SCISSOR_TEST); - ErrorHandler.CheckGlError(); - } - - public void DisableScissor() - { - Gl.glDisable(Gl.GL_SCISSOR_TEST); - ErrorHandler.CheckGlError(); - } - - public void Clear() { SdlGraphics.Clear(); } - public void Present() { Sdl.SDL_GL_SwapBuffers(); } - public void PumpInput(IInputHandler inputHandler) { input.PumpInput(inputHandler); } - - public void DrawPrimitives(PrimitiveType pt, int firstVertex, int numVertices) - { - SdlGraphics.DrawPrimitives(pt, firstVertex, numVertices); - } - - public void SetLineWidth( float width ) - { - Gl.glLineWidth(width); - ErrorHandler.CheckGlError(); - } - - public IVertexBuffer CreateVertexBuffer( int size ) { return new VertexBuffer( size ); } - public ITexture CreateTexture() { return new Texture(); } - public ITexture CreateTexture( Bitmap bitmap ) { return new Texture( bitmap ); } - public IShader CreateShader( string name ) { return new Shader( this, name ); } - - public int GpuMemoryUsed { get; internal set; } + public override IShader CreateShader(string name) { return new Shader( this, name ); } } } diff --git a/OpenRA.Renderer.Null/NullGraphicsDevice.cs b/OpenRA.Renderer.Null/NullGraphicsDevice.cs index 56f3627e92..742ac7f6b9 100644 --- a/OpenRA.Renderer.Null/NullGraphicsDevice.cs +++ b/OpenRA.Renderer.Null/NullGraphicsDevice.cs @@ -54,8 +54,6 @@ namespace OpenRA.Renderer.Null public ITexture CreateTexture() { return new NullTexture(); } public ITexture CreateTexture(Bitmap bitmap) { return new NullTexture(); } public IShader CreateShader(string name) { return new NullShader(); } - - public int GpuMemoryUsed { get { return 0; } } } public class NullShader : IShader diff --git a/OpenRA.Renderer.SdlCommon/SdlGraphics.cs b/OpenRA.Renderer.SdlCommon/SdlGraphics.cs index fdccf07e2d..e274c54a78 100644 --- a/OpenRA.Renderer.SdlCommon/SdlGraphics.cs +++ b/OpenRA.Renderer.SdlCommon/SdlGraphics.cs @@ -18,9 +18,30 @@ using Tao.Sdl; namespace OpenRA.Renderer.SdlCommon { - public static class SdlGraphics + public abstract class SdlGraphics : IGraphicsDevice { - public static IntPtr InitializeSdlGl( ref Size size, WindowMode window, string[] requiredExtensions ) + Size windowSize; + IntPtr surf; + SdlInput input; + + public Size WindowSize { get { return windowSize; } } + + public SdlGraphics(Size size, WindowMode window, string[] extensions) + { + windowSize = size; + surf = InitializeSdlGl(ref windowSize, window, extensions); + + Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY); + ErrorHandler.CheckGlError(); + Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY); + ErrorHandler.CheckGlError(); + + Sdl.SDL_SetModState(0); + + input = new SdlInput(surf); + } + + IntPtr InitializeSdlGl(ref Size size, WindowMode window, string[] requiredExtensions) { Sdl.SDL_Init( Sdl.SDL_INIT_NOPARACHUTE | Sdl.SDL_INIT_VIDEO ); Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_DOUBLEBUFFER, 1 ); @@ -30,7 +51,7 @@ namespace OpenRA.Renderer.SdlCommon Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_ALPHA_SIZE, 0 ); int windowFlags = 0; - switch( window ) + switch (window) { case WindowMode.Fullscreen: windowFlags |= Sdl.SDL_FULLSCREEN; @@ -59,14 +80,14 @@ namespace OpenRA.Renderer.SdlCommon Console.WriteLine("Using resolution: {0}x{1}", size.Width, size.Height); - var surf = Sdl.SDL_SetVideoMode( size.Width, size.Height, 0, Sdl.SDL_OPENGL | windowFlags ); + var surf = Sdl.SDL_SetVideoMode(size.Width, size.Height, 0, Sdl.SDL_OPENGL | windowFlags); if (surf == IntPtr.Zero) Console.WriteLine("Failed to set video mode."); - Sdl.SDL_WM_SetCaption( "OpenRA", "OpenRA" ); - Sdl.SDL_ShowCursor( 0 ); - Sdl.SDL_EnableUNICODE( 1 ); - Sdl.SDL_EnableKeyRepeat( Sdl.SDL_DEFAULT_REPEAT_DELAY, Sdl.SDL_DEFAULT_REPEAT_INTERVAL ); + Sdl.SDL_WM_SetCaption("OpenRA", "OpenRA"); + Sdl.SDL_ShowCursor(0); + Sdl.SDL_EnableUNICODE(1); + Sdl.SDL_EnableKeyRepeat(Sdl.SDL_DEFAULT_REPEAT_DELAY, Sdl.SDL_DEFAULT_REPEAT_INTERVAL); ErrorHandler.CheckGlError(); @@ -86,7 +107,7 @@ namespace OpenRA.Renderer.SdlCommon return surf; } - static int ModeFromPrimitiveType(PrimitiveType pt) + int ModeFromPrimitiveType(PrimitiveType pt) { switch(pt) { @@ -98,19 +119,49 @@ namespace OpenRA.Renderer.SdlCommon throw new NotImplementedException(); } - public static void DrawPrimitives(PrimitiveType pt, int firstVertex, int numVertices) + public void DrawPrimitives(PrimitiveType pt, int firstVertex, int numVertices) { Gl.glDrawArrays(ModeFromPrimitiveType(pt), firstVertex, numVertices); ErrorHandler.CheckGlError(); } - public static void Clear() + public void Clear() { Gl.glClearColor(0, 0, 0, 0); ErrorHandler.CheckGlError(); Gl.glClear(Gl.GL_COLOR_BUFFER_BIT); ErrorHandler.CheckGlError(); } + + public void EnableScissor(int left, int top, int width, int height) + { + if (width < 0) width = 0; + if (height < 0) height = 0; + + Gl.glScissor(left, windowSize.Height - (top + height), width, height); + ErrorHandler.CheckGlError(); + Gl.glEnable(Gl.GL_SCISSOR_TEST); + ErrorHandler.CheckGlError(); + } + + public void DisableScissor() + { + Gl.glDisable(Gl.GL_SCISSOR_TEST); + ErrorHandler.CheckGlError(); + } + + public void SetLineWidth(float width) + { + Gl.glLineWidth(width); + ErrorHandler.CheckGlError(); + } + + public void Present() { Sdl.SDL_GL_SwapBuffers(); } + public void PumpInput(IInputHandler inputHandler) { input.PumpInput(inputHandler); } + public IVertexBuffer CreateVertexBuffer(int size) { return new VertexBuffer(size); } + public ITexture CreateTexture() { return new Texture(); } + public ITexture CreateTexture(Bitmap bitmap) { return new Texture(bitmap); } + public abstract IShader CreateShader(string name); } }