fixed #989: don't do renderer setup in a reflected call.

This commit is contained in:
Chris Forbes
2011-07-04 21:05:35 +12:00
committed by Paul Chote
parent a07e151ab5
commit dcd9627c22
5 changed files with 46 additions and 19 deletions

View File

@@ -21,12 +21,17 @@ namespace OpenRA.FileFormats.Graphics
public RendererAttribute( Type graphicsDeviceType ) public RendererAttribute( Type graphicsDeviceType )
{ {
if( !typeof( IGraphicsDevice ).IsAssignableFrom( graphicsDeviceType ) ) if( !typeof( IDeviceFactory ).IsAssignableFrom( graphicsDeviceType ) )
throw new InvalidOperationException( "Incorrect type in RendererAttribute" ); throw new InvalidOperationException( "Incorrect type in RendererAttribute" );
Type = graphicsDeviceType; Type = graphicsDeviceType;
} }
} }
public interface IDeviceFactory
{
IGraphicsDevice Create( Size size, WindowMode windowMode, bool vsync );
}
public interface IGraphicsDevice public interface IGraphicsDevice
{ {
IVertexBuffer<Vertex> CreateVertexBuffer( int length ); IVertexBuffer<Vertex> CreateVertexBuffer( int length );

View File

@@ -142,11 +142,11 @@ namespace OpenRA.Graphics
static IGraphicsDevice CreateDevice( Assembly rendererDll, int width, int height, WindowMode window, bool vsync ) 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 ) ) 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!"); throw new InvalidOperationException("Renderer DLL is missing RendererAttribute to tell us what type to use!");
} }

View File

@@ -16,10 +16,18 @@ using Tao.Cg;
using Tao.OpenGl; using Tao.OpenGl;
using Tao.Sdl; using Tao.Sdl;
[assembly: Renderer(typeof(OpenRA.Renderer.Cg.GraphicsDevice))] [assembly: Renderer(typeof(OpenRA.Renderer.Cg.DeviceFactory))]
namespace OpenRA.Renderer.Cg 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 public class GraphicsDevice : IGraphicsDevice
{ {
Size windowSize; Size windowSize;
@@ -75,7 +83,7 @@ namespace OpenRA.Renderer.Cg
throw new InvalidOperationException("CG Error. See graphics.log for details"); 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"); Console.WriteLine("Using Cg renderer");
Sdl.SDL_Init( Sdl.SDL_INIT_NOPARACHUTE | Sdl.SDL_INIT_VIDEO ); Sdl.SDL_Init( Sdl.SDL_INIT_NOPARACHUTE | Sdl.SDL_INIT_VIDEO );
@@ -102,7 +110,7 @@ namespace OpenRA.Renderer.Cg
break; 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_WM_SetCaption( "OpenRA", "OpenRA" );
Sdl.SDL_ShowCursor( 0 ); Sdl.SDL_ShowCursor( 0 );
@@ -111,7 +119,7 @@ namespace OpenRA.Renderer.Cg
CheckGlError(); CheckGlError();
windowSize = new Size( width, height ); windowSize = size;
cgContext = Tao.Cg.Cg.cgCreateContext(); cgContext = Tao.Cg.Cg.cgCreateContext();

View File

@@ -11,16 +11,23 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq;
using OpenRA.FileFormats.Graphics; using OpenRA.FileFormats.Graphics;
using Tao.Cg;
using Tao.OpenGl; using Tao.OpenGl;
using Tao.Sdl; using Tao.Sdl;
using System.Linq;
[assembly: Renderer(typeof(OpenRA.Renderer.Glsl.GraphicsDevice))] [assembly: Renderer(typeof(OpenRA.Renderer.Glsl.DeviceFactory))]
namespace OpenRA.Renderer.Glsl 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 public class GraphicsDevice : IGraphicsDevice
{ {
Size windowSize; Size windowSize;
@@ -65,7 +72,7 @@ namespace OpenRA.Renderer.Glsl
Log.Write("graphics", Gl.glGetString(Gl.GL_EXTENSIONS)); 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"); Console.WriteLine("Using Gl renderer");
Sdl.SDL_Init( Sdl.SDL_INIT_NOPARACHUTE | Sdl.SDL_INIT_VIDEO ); Sdl.SDL_Init( Sdl.SDL_INIT_NOPARACHUTE | Sdl.SDL_INIT_VIDEO );
@@ -89,7 +96,7 @@ namespace OpenRA.Renderer.Glsl
break; 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_WM_SetCaption( "OpenRA", "OpenRA" );
Sdl.SDL_ShowCursor( 0 ); Sdl.SDL_ShowCursor( 0 );
@@ -115,7 +122,7 @@ namespace OpenRA.Renderer.Glsl
throw new InvalidProgramException("Unsupported GPU. See graphics.log for details."); throw new InvalidProgramException("Unsupported GPU. See graphics.log for details.");
} }
windowSize = new Size( width, height ); windowSize = size;
Gl.glEnableClientState( Gl.GL_VERTEX_ARRAY ); Gl.glEnableClientState( Gl.GL_VERTEX_ARRAY );
CheckGlError(); CheckGlError();

View File

@@ -10,22 +10,29 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.IO;
using OpenRA.FileFormats.Graphics; using OpenRA.FileFormats.Graphics;
using OpenRA.Graphics; using OpenRA.Graphics;
[assembly: Renderer(typeof(OpenRA.Renderer.Null.NullGraphicsDevice))] [assembly: Renderer(typeof(OpenRA.Renderer.Null.DeviceFactory))]
namespace OpenRA.Renderer.Null 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 class NullGraphicsDevice : IGraphicsDevice
{ {
public Size WindowSize { get; internal set; } 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"); Console.WriteLine("Using Null renderer");
WindowSize = new Size(width, height); WindowSize = size;
} }
public void EnableScissor(int left, int top, int width, int height) { } public void EnableScissor(int left, int top, int width, int height) { }