diff --git a/OpenRA.Renderer.SdlCommon/ErrorHandler.cs b/OpenRA.Renderer.SdlCommon/ErrorHandler.cs index 3d67c88a24..8282bbb3de 100644 --- a/OpenRA.Renderer.SdlCommon/ErrorHandler.cs +++ b/OpenRA.Renderer.SdlCommon/ErrorHandler.cs @@ -31,7 +31,7 @@ namespace OpenRA.Renderer.SdlCommon public static void CheckGlError() { var n = Gl.glGetError(); - if( n != Gl.GL_NO_ERROR ) + if (n != Gl.GL_NO_ERROR) { var error = "GL Error: {0}\n{1}".F((GlError)n, new StackTrace()); WriteGraphicsLog(error); @@ -53,5 +53,4 @@ namespace OpenRA.Renderer.SdlCommon Log.Write("graphics", Gl.glGetString(Gl.GL_EXTENSIONS)); } } -} - +} \ No newline at end of file diff --git a/OpenRA.Renderer.SdlCommon/FrameBuffer.cs b/OpenRA.Renderer.SdlCommon/FrameBuffer.cs index ca96757b68..4c3c2f175f 100644 --- a/OpenRA.Renderer.SdlCommon/FrameBuffer.cs +++ b/OpenRA.Renderer.SdlCommon/FrameBuffer.cs @@ -64,6 +64,22 @@ namespace OpenRA.Renderer.SdlCommon ErrorHandler.CheckGlError(); } + static int[] ViewportRectangle() + { + var v = new int[4]; + unsafe + { + fixed (int* ptr = &v[0]) + { + IntPtr intPtr = new IntPtr((void*)ptr); + Gl.glGetIntegerv(Gl.GL_VIEWPORT, intPtr); + } + } + + ErrorHandler.CheckGlError(); + return v; + } + void FinalizeInner() { Gl.glDeleteFramebuffersEXT(1, ref framebuffer); @@ -74,22 +90,6 @@ namespace OpenRA.Renderer.SdlCommon ~FrameBuffer() { Game.RunAfterTick(FinalizeInner); } - static int[] ViewportRectangle() - { - int[] v = new int[4]; - unsafe - { - fixed (int *ptr = &v[0]) - { - IntPtr intPtr = new IntPtr((void*)ptr); - Gl.glGetIntegerv(Gl.GL_VIEWPORT, intPtr); - } - } - ErrorHandler.CheckGlError(); - - return v; - } - int[] cv = new int[4]; public void Bind() { diff --git a/OpenRA.Renderer.SdlCommon/MultiTapDetection.cs b/OpenRA.Renderer.SdlCommon/MultiTapDetection.cs index 75255e03e7..33bc647f28 100644 --- a/OpenRA.Renderer.SdlCommon/MultiTapDetection.cs +++ b/OpenRA.Renderer.SdlCommon/MultiTapDetection.cs @@ -14,33 +14,29 @@ using OpenRA.FileFormats; public static class MultiTapDetection { - static Cache KeyHistoryCache = + static Cache keyHistoryCache = new Cache(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1))); - static Cache ClickHistoryCache = + static Cache clickHistoryCache = new Cache(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1))); - public static int DetectFromMouse(byte MBName, int2 xy) + public static int DetectFromMouse(byte button, int2 xy) { - var clickHistory = ClickHistoryCache[MBName]; - return clickHistory.GetTapCount(xy); + return clickHistoryCache[button].GetTapCount(xy); } - public static int InfoFromMouse(byte MBName) + public static int InfoFromMouse(byte button) { - var clickHistory = ClickHistoryCache[MBName]; - return clickHistory.LastTapCount(); + return clickHistoryCache[button].LastTapCount(); } - public static int DetectFromKeyboard(string KeyName) + public static int DetectFromKeyboard(string key) { - var keyHistory = KeyHistoryCache[KeyName]; - return keyHistory.GetTapCount(int2.Zero); + return keyHistoryCache[key].GetTapCount(int2.Zero); } - public static int InfoFromKeyboard(string KeyName) + public static int InfoFromKeyboard(string key) { - var keyHistory = KeyHistoryCache[KeyName]; - return keyHistory.LastTapCount(); + return keyHistoryCache[key].LastTapCount(); } } @@ -50,12 +46,12 @@ class TapHistory public TapHistory(DateTime now) { - FirstRelease = SecondRelease = ThirdRelease = Pair.New( now, int2.Zero ); + FirstRelease = SecondRelease = ThirdRelease = Pair.New(now, int2.Zero); } static bool CloseEnough(Pair a, Pair b) { - return a.First - b.First < TimeSpan.FromMilliseconds( 250 ) + return a.First - b.First < TimeSpan.FromMilliseconds(250) && (a.Second - b.Second).Length < 4; } @@ -65,15 +61,21 @@ class TapHistory SecondRelease = ThirdRelease; ThirdRelease = Pair.New(DateTime.Now, xy); - if (!CloseEnough(ThirdRelease, SecondRelease)) return 1; - if (!CloseEnough(SecondRelease, FirstRelease)) return 2; + if (!CloseEnough(ThirdRelease, SecondRelease)) + return 1; + if (!CloseEnough(SecondRelease, FirstRelease)) + return 2; + return 3; } public int LastTapCount() { - if (!CloseEnough(ThirdRelease, SecondRelease)) return 1; - if (!CloseEnough(SecondRelease, FirstRelease)) return 2; + if (!CloseEnough(ThirdRelease, SecondRelease)) + return 1; + if (!CloseEnough(SecondRelease, FirstRelease)) + return 2; + return 3; } } \ No newline at end of file diff --git a/OpenRA.Renderer.SdlCommon/SdlGraphics.cs b/OpenRA.Renderer.SdlCommon/SdlGraphics.cs index 2fe9891249..08bb0fcc15 100644 --- a/OpenRA.Renderer.SdlCommon/SdlGraphics.cs +++ b/OpenRA.Renderer.SdlCommon/SdlGraphics.cs @@ -43,13 +43,13 @@ namespace OpenRA.Renderer.SdlCommon 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 ); - Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_RED_SIZE, 8 ); - Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_GREEN_SIZE, 8 ); - Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_BLUE_SIZE, 8 ); - Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_ALPHA_SIZE, 0 ); - Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_STENCIL_SIZE, 1 ); + Sdl.SDL_Init(Sdl.SDL_INIT_NOPARACHUTE | Sdl.SDL_INIT_VIDEO); + Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DOUBLEBUFFER, 1); + Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_RED_SIZE, 8); + Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_GREEN_SIZE, 8); + Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_BLUE_SIZE, 8); + Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_ALPHA_SIZE, 0); + Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_STENCIL_SIZE, 1); int windowFlags = 0; switch (window) @@ -59,16 +59,16 @@ namespace OpenRA.Renderer.SdlCommon break; case WindowMode.PseudoFullscreen: windowFlags |= Sdl.SDL_NOFRAME; - Environment.SetEnvironmentVariable( "SDL_VIDEO_WINDOW_POS", "0,0" ); + Environment.SetEnvironmentVariable("SDL_VIDEO_WINDOW_POS", "0,0"); break; case WindowMode.Windowed: - Environment.SetEnvironmentVariable( "SDL_VIDEO_CENTERED", "1" ); + Environment.SetEnvironmentVariable("SDL_VIDEO_CENTERED", "1"); break; default: break; } - var info = (Sdl.SDL_VideoInfo) Marshal.PtrToStructure( + var info = (Sdl.SDL_VideoInfo)Marshal.PtrToStructure( Sdl.SDL_GetVideoInfo(), typeof(Sdl.SDL_VideoInfo)); Console.WriteLine("Desktop resolution: {0}x{1}", info.current_w, info.current_h); @@ -76,7 +76,7 @@ namespace OpenRA.Renderer.SdlCommon if (size.Width == 0 && size.Height == 0) { Console.WriteLine("No custom resolution provided, using desktop resolution"); - size = new Size( info.current_w, info.current_h ); + size = new Size(info.current_w, info.current_h); } Console.WriteLine("Using resolution: {0}x{1}", size.Width, size.Height); @@ -110,13 +110,14 @@ namespace OpenRA.Renderer.SdlCommon int ModeFromPrimitiveType(PrimitiveType pt) { - switch(pt) + switch (pt) { case PrimitiveType.PointList: return Gl.GL_POINTS; case PrimitiveType.LineList: return Gl.GL_LINES; case PrimitiveType.TriangleList: return Gl.GL_TRIANGLES; case PrimitiveType.QuadList: return Gl.GL_QUADS; } + throw new NotImplementedException(); } @@ -193,5 +194,4 @@ namespace OpenRA.Renderer.SdlCommon public IFrameBuffer CreateFrameBuffer(Size s) { return new FrameBuffer(s); } public abstract IShader CreateShader(string name); } -} - +} \ No newline at end of file diff --git a/OpenRA.Renderer.SdlCommon/SdlInput.cs b/OpenRA.Renderer.SdlCommon/SdlInput.cs index 17675ce829..5d72f992a8 100644 --- a/OpenRA.Renderer.SdlCommon/SdlInput.cs +++ b/OpenRA.Renderer.SdlCommon/SdlInput.cs @@ -19,9 +19,9 @@ namespace OpenRA.Renderer.SdlCommon MouseButton lastButtonBits = (MouseButton)0; IntPtr surface; - public SdlInput( IntPtr surface ) { this.surface = surface; } + public SdlInput(IntPtr surface) { this.surface = surface; } - MouseButton MakeButton( byte b ) + MouseButton MakeButton(byte b) { return b == Sdl.SDL_BUTTON_LEFT ? MouseButton.Left : b == Sdl.SDL_BUTTON_RIGHT ? MouseButton.Right @@ -31,26 +31,26 @@ namespace OpenRA.Renderer.SdlCommon : 0; } - Modifiers MakeModifiers( int raw ) + Modifiers MakeModifiers(int raw) { - return ( ( raw & Sdl.KMOD_ALT ) != 0 ? Modifiers.Alt : 0 ) - | ( ( raw & Sdl.KMOD_CTRL ) != 0 ? Modifiers.Ctrl : 0 ) - | ( ( raw & Sdl.KMOD_META ) != 0 ? Modifiers.Meta : 0 ) - | ( ( raw & Sdl.KMOD_SHIFT ) != 0 ? Modifiers.Shift : 0 ); + return ((raw & Sdl.KMOD_ALT) != 0 ? Modifiers.Alt : 0) + | ((raw & Sdl.KMOD_CTRL) != 0 ? Modifiers.Ctrl : 0) + | ((raw & Sdl.KMOD_META) != 0 ? Modifiers.Meta : 0) + | ((raw & Sdl.KMOD_SHIFT) != 0 ? Modifiers.Shift : 0); } - public void PumpInput( IInputHandler inputHandler ) + public void PumpInput(IInputHandler inputHandler) { - Game.HasInputFocus = 0 != ( Sdl.SDL_GetAppState() & Sdl.SDL_APPINPUTFOCUS ); + Game.HasInputFocus = 0 != (Sdl.SDL_GetAppState() & Sdl.SDL_APPINPUTFOCUS); - var mods = MakeModifiers( Sdl.SDL_GetModState() ); - inputHandler.ModifierKeys( mods ); + var mods = MakeModifiers(Sdl.SDL_GetModState()); + inputHandler.ModifierKeys(mods); MouseInput? pendingMotion = null; Sdl.SDL_Event e; - while( Sdl.SDL_PollEvent( out e ) != 0 ) + while (Sdl.SDL_PollEvent(out e) != 0) { - switch( e.type ) + switch (e.type) { case Sdl.SDL_QUIT: OpenRA.Game.Exit(); @@ -58,118 +58,121 @@ namespace OpenRA.Renderer.SdlCommon case Sdl.SDL_MOUSEBUTTONDOWN: { - if( pendingMotion != null ) + if (pendingMotion != null) { - inputHandler.OnMouseInput( pendingMotion.Value ); + inputHandler.OnMouseInput(pendingMotion.Value); pendingMotion = null; } - var button = MakeButton( e.button.button ); + var button = MakeButton(e.button.button); lastButtonBits |= button; - var pos = new int2( e.button.x, e.button.y ); + var pos = new int2(e.button.x, e.button.y); inputHandler.OnMouseInput(new MouseInput( - MouseInputEvent.Down, button, pos, mods, - MultiTapDetection.DetectFromMouse(e.button.button, pos) - )); - } break; + MouseInputEvent.Down, button, pos, mods, + MultiTapDetection.DetectFromMouse(e.button.button, pos))); + + break; + } case Sdl.SDL_MOUSEBUTTONUP: { - if( pendingMotion != null ) + if (pendingMotion != null) { - inputHandler.OnMouseInput( pendingMotion.Value ); + inputHandler.OnMouseInput(pendingMotion.Value); pendingMotion = null; } - var button = MakeButton( e.button.button ); + var button = MakeButton(e.button.button); lastButtonBits &= ~button; - var pos = new int2( e.button.x, e.button.y ); + var pos = new int2(e.button.x, e.button.y); inputHandler.OnMouseInput(new MouseInput( MouseInputEvent.Up, button, pos, mods, - MultiTapDetection.InfoFromMouse(e.button.button) - )); - } break; + MultiTapDetection.InfoFromMouse(e.button.button))); + + break; + } case Sdl.SDL_MOUSEMOTION: { pendingMotion = new MouseInput( - MouseInputEvent.Move, - lastButtonBits, - new int2( e.motion.x, e.motion.y ), - mods, 0 ); - } break; + MouseInputEvent.Move, lastButtonBits, + new int2(e.motion.x, e.motion.y), mods, 0); + + break; + } case Sdl.SDL_KEYDOWN: { - var keyName = Sdl.SDL_GetKeyName( e.key.keysym.sym ); + var keyName = Sdl.SDL_GetKeyName(e.key.keysym.sym); var keyEvent = new KeyInput { Event = KeyInputEvent.Down, Modifiers = mods, UnicodeChar = (char)e.key.keysym.unicode, - KeyName = Sdl.SDL_GetKeyName( e.key.keysym.sym ), + KeyName = Sdl.SDL_GetKeyName(e.key.keysym.sym), VirtKey = e.key.keysym.sym, MultiTapCount = MultiTapDetection.DetectFromKeyboard(keyName) }; - if( !HandleSpecialKey( keyEvent ) ) - inputHandler.OnKeyInput( keyEvent ); - } break; + if (!HandleSpecialKey(keyEvent)) + inputHandler.OnKeyInput(keyEvent); + + break; + } case Sdl.SDL_KEYUP: { - var keyName = Sdl.SDL_GetKeyName( e.key.keysym.sym ); - + var keyName = Sdl.SDL_GetKeyName(e.key.keysym.sym); var keyEvent = new KeyInput { Event = KeyInputEvent.Up, Modifiers = mods, UnicodeChar = (char)e.key.keysym.unicode, - KeyName = Sdl.SDL_GetKeyName( e.key.keysym.sym ), + KeyName = Sdl.SDL_GetKeyName(e.key.keysym.sym), VirtKey = e.key.keysym.sym, MultiTapCount = MultiTapDetection.InfoFromKeyboard(keyName) }; - inputHandler.OnKeyInput( keyEvent ); - } break; + inputHandler.OnKeyInput(keyEvent); + break; + } } } - if( pendingMotion != null ) + if (pendingMotion != null) { - inputHandler.OnMouseInput( pendingMotion.Value ); + inputHandler.OnMouseInput(pendingMotion.Value); pendingMotion = null; } ErrorHandler.CheckGlError(); } - bool HandleSpecialKey( KeyInput k ) + bool HandleSpecialKey(KeyInput k) { - switch( k.VirtKey ) + switch (k.VirtKey) { case Sdl.SDLK_F13: - var path = Environment.GetFolderPath( Environment.SpecialFolder.Personal ) - + Path.DirectorySeparatorChar + DateTime.UtcNow.ToString( "OpenRA-yyyy-MM-ddThhmmssZ" ) + ".bmp"; - Sdl.SDL_SaveBMP( surface, path ); + var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + + Path.DirectorySeparatorChar + DateTime.UtcNow.ToString("OpenRA-yyyy-MM-ddThhmmssZ") + ".bmp"; + Sdl.SDL_SaveBMP(surface, path); return true; case Sdl.SDLK_F4: - if( k.Modifiers.HasModifier( Modifiers.Alt ) ) + if (k.Modifiers.HasModifier(Modifiers.Alt)) { OpenRA.Game.Exit(); return true; } - return false; + return false; default: return false; } } } -} - +} \ No newline at end of file diff --git a/OpenRA.Renderer.SdlCommon/Texture.cs b/OpenRA.Renderer.SdlCommon/Texture.cs index 915d0edf33..978895147e 100644 --- a/OpenRA.Renderer.SdlCommon/Texture.cs +++ b/OpenRA.Renderer.SdlCommon/Texture.cs @@ -84,14 +84,14 @@ namespace OpenRA.Renderer.SdlCommon int height = colors.GetUpperBound(0) + 1; if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height)) - throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width,height)); + throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height)); size = new Size(width, height); unsafe { - fixed (uint* ptr = &colors[0,0]) + fixed (uint* ptr = &colors[0, 0]) { - IntPtr intPtr = new IntPtr((void *) ptr); + IntPtr intPtr = new IntPtr((void*)ptr); PrepareTexture(); Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, width, height, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, intPtr); @@ -107,8 +107,7 @@ namespace OpenRA.Renderer.SdlCommon size = new Size(bitmap.Width, bitmap.Height); var bits = bitmap.LockBits(bitmap.Bounds(), - ImageLockMode.ReadOnly, - PixelFormat.Format32bppArgb); + ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); PrepareTexture(); Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, bits.Width, bits.Height, @@ -119,18 +118,20 @@ namespace OpenRA.Renderer.SdlCommon public byte[] GetData() { - var data = new byte[4*size.Width * size.Height]; + var data = new byte[4 * size.Width * size.Height]; ErrorHandler.CheckGlError(); Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture); unsafe { - fixed (byte *ptr = &data[0]) + fixed (byte* ptr = &data[0]) { IntPtr intPtr = new IntPtr((void*)ptr); Gl.glGetTexImage(Gl.GL_TEXTURE_2D, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, intPtr); } } + + ErrorHandler.CheckGlError(); return data; } diff --git a/OpenRA.Renderer.SdlCommon/VertexBuffer.cs b/OpenRA.Renderer.SdlCommon/VertexBuffer.cs index f80d907136..30d84787b8 100644 --- a/OpenRA.Renderer.SdlCommon/VertexBuffer.cs +++ b/OpenRA.Renderer.SdlCommon/VertexBuffer.cs @@ -18,8 +18,8 @@ namespace OpenRA.Renderer.SdlCommon public class VertexBuffer : IVertexBuffer where T : struct { + static readonly int VertexSize = Marshal.SizeOf(typeof(T)); int buffer; - static readonly int vertexSize = Marshal.SizeOf(typeof(T)); public VertexBuffer(int size) { @@ -27,8 +27,8 @@ namespace OpenRA.Renderer.SdlCommon ErrorHandler.CheckGlError(); Bind(); Gl.glBufferDataARB(Gl.GL_ARRAY_BUFFER_ARB, - new IntPtr(vertexSize * size), - new T[ size ], + new IntPtr(VertexSize * size), + new T[size], Gl.GL_DYNAMIC_DRAW_ARB); ErrorHandler.CheckGlError(); } @@ -38,7 +38,7 @@ namespace OpenRA.Renderer.SdlCommon Bind(); Gl.glBufferSubDataARB(Gl.GL_ARRAY_BUFFER_ARB, IntPtr.Zero, - new IntPtr(vertexSize * length), + new IntPtr(VertexSize * length), data); ErrorHandler.CheckGlError(); } @@ -47,13 +47,13 @@ namespace OpenRA.Renderer.SdlCommon { Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, buffer); ErrorHandler.CheckGlError(); - Gl.glVertexPointer(3, Gl.GL_FLOAT, vertexSize, IntPtr.Zero); + Gl.glVertexPointer(3, Gl.GL_FLOAT, VertexSize, IntPtr.Zero); ErrorHandler.CheckGlError(); - Gl.glTexCoordPointer(4, Gl.GL_FLOAT, vertexSize, new IntPtr(12)); + Gl.glTexCoordPointer(4, Gl.GL_FLOAT, VertexSize, new IntPtr(12)); ErrorHandler.CheckGlError(); } - void FinalizeInner() { Gl.glDeleteBuffersARB( 1, ref buffer ); } - ~VertexBuffer() { Game.RunAfterTick( FinalizeInner ); } + void FinalizeInner() { Gl.glDeleteBuffersARB(1, ref buffer); } + ~VertexBuffer() { Game.RunAfterTick(FinalizeInner); } } }