diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index d2bba646ce..0ec16f8b1d 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -523,7 +523,7 @@ namespace OpenRA Bitmap bitmap; using (new PerfTimer("Renderer.TakeScreenshot")) - bitmap = Renderer.Window.TakeScreenshot(); + bitmap = Renderer.Context.TakeScreenshot(); ThreadPool.QueueUserWorkItem(_ => { diff --git a/OpenRA.Game/Graphics/HardwarePalette.cs b/OpenRA.Game/Graphics/HardwarePalette.cs index 0ea4376c15..3377f2b9a7 100644 --- a/OpenRA.Game/Graphics/HardwarePalette.cs +++ b/OpenRA.Game/Graphics/HardwarePalette.cs @@ -27,7 +27,7 @@ namespace OpenRA.Graphics public HardwarePalette() { - Texture = Game.Renderer.Window.CreateTexture(); + Texture = Game.Renderer.Context.CreateTexture(); readOnlyModifiablePalettes = modifiablePalettes.AsReadOnly(); } diff --git a/OpenRA.Game/Graphics/ModelRenderer.cs b/OpenRA.Game/Graphics/ModelRenderer.cs index b0ce131394..5d3e0cac99 100644 --- a/OpenRA.Game/Graphics/ModelRenderer.cs +++ b/OpenRA.Game/Graphics/ModelRenderer.cs @@ -300,14 +300,14 @@ namespace OpenRA.Graphics Game.Renderer.Flush(); fbo.Bind(); - Game.Renderer.Window.EnableDepthBuffer(); + Game.Renderer.Context.EnableDepthBuffer(); return fbo; } void DisableFrameBuffer(IFrameBuffer fbo) { Game.Renderer.Flush(); - Game.Renderer.Window.DisableDepthBuffer(); + Game.Renderer.Context.DisableDepthBuffer(); fbo.Unbind(); } @@ -356,7 +356,7 @@ namespace OpenRA.Graphics } var size = new Size(renderer.SheetSize, renderer.SheetSize); - var framebuffer = renderer.Window.CreateFrameBuffer(size); + var framebuffer = renderer.Context.CreateFrameBuffer(size); var sheet = new Sheet(SheetType.BGRA, framebuffer.Texture); mappedBuffers.Add(sheet, framebuffer); diff --git a/OpenRA.Game/Graphics/PlatformInterfaces.cs b/OpenRA.Game/Graphics/PlatformInterfaces.cs index 2c82d1c194..1f1d6b4a27 100644 --- a/OpenRA.Game/Graphics/PlatformInterfaces.cs +++ b/OpenRA.Game/Graphics/PlatformInterfaces.cs @@ -36,39 +36,40 @@ namespace OpenRA public interface IPlatformWindow : IDisposable { - IVertexBuffer CreateVertexBuffer(int length); - ITexture CreateTexture(Bitmap bitmap); - ITexture CreateTexture(); - IFrameBuffer CreateFrameBuffer(Size s); - IShader CreateShader(string name); + IGraphicsContext Context { get; } Size WindowSize { get; } float WindowScale { get; } event Action OnWindowScaleChanged; - void Clear(); - void Present(); - Bitmap TakeScreenshot(); void PumpInput(IInputHandler inputHandler); string GetClipboardText(); bool SetClipboardText(string text); - void DrawPrimitives(PrimitiveType type, int firstVertex, int numVertices); - - void EnableScissor(int left, int top, int width, int height); - void DisableScissor(); - - void EnableDepthBuffer(); - void DisableDepthBuffer(); - void ClearDepthBuffer(); - - void SetBlendMode(BlendMode mode); void GrabWindowMouseFocus(); void ReleaseWindowMouseFocus(); IHardwareCursor CreateHardwareCursor(string name, Size size, byte[] data, int2 hotspot); void SetHardwareCursor(IHardwareCursor cursor); + } + public interface IGraphicsContext : IDisposable + { + IVertexBuffer CreateVertexBuffer(int size); + ITexture CreateTexture(); + ITexture CreateTexture(Bitmap bitmap); + IFrameBuffer CreateFrameBuffer(Size s); + IShader CreateShader(string name); + void EnableScissor(int left, int top, int width, int height); + void DisableScissor(); + Bitmap TakeScreenshot(); + void Present(); + void DrawPrimitives(PrimitiveType pt, int firstVertex, int numVertices); + void Clear(); + void EnableDepthBuffer(); + void DisableDepthBuffer(); + void ClearDepthBuffer(); + void SetBlendMode(BlendMode mode); string GLVersion { get; } } diff --git a/OpenRA.Game/Graphics/Sheet.cs b/OpenRA.Game/Graphics/Sheet.cs index ec23905999..f07e845622 100644 --- a/OpenRA.Game/Graphics/Sheet.cs +++ b/OpenRA.Game/Graphics/Sheet.cs @@ -66,7 +66,7 @@ namespace OpenRA.Graphics { if (texture == null) { - texture = Game.Renderer.Window.CreateTexture(); + texture = Game.Renderer.Context.CreateTexture(); dirty = true; } diff --git a/OpenRA.Game/Graphics/SpriteRenderer.cs b/OpenRA.Game/Graphics/SpriteRenderer.cs index 81df72410e..9cbb16d608 100644 --- a/OpenRA.Game/Graphics/SpriteRenderer.cs +++ b/OpenRA.Game/Graphics/SpriteRenderer.cs @@ -43,10 +43,10 @@ namespace OpenRA.Graphics sheets[i] = null; } - renderer.Window.SetBlendMode(currentBlend); + renderer.Context.SetBlendMode(currentBlend); shader.PrepareRender(); renderer.DrawBatch(vertices, nv, PrimitiveType.TriangleList); - renderer.Window.SetBlendMode(BlendMode.None); + renderer.Context.SetBlendMode(BlendMode.None); nv = 0; ns = 0; @@ -131,10 +131,10 @@ namespace OpenRA.Graphics public void DrawVertexBuffer(IVertexBuffer buffer, int start, int length, PrimitiveType type, Sheet sheet, BlendMode blendMode) { shader.SetTexture("Texture0", sheet.GetTexture()); - renderer.Window.SetBlendMode(blendMode); + renderer.Context.SetBlendMode(blendMode); shader.PrepareRender(); renderer.DrawBatch(buffer, start, length, type); - renderer.Window.SetBlendMode(BlendMode.None); + renderer.Context.SetBlendMode(BlendMode.None); } // For RGBAColorRenderer diff --git a/OpenRA.Game/Graphics/TerrainSpriteLayer.cs b/OpenRA.Game/Graphics/TerrainSpriteLayer.cs index 6ba0689bef..698b1c1746 100644 --- a/OpenRA.Game/Graphics/TerrainSpriteLayer.cs +++ b/OpenRA.Game/Graphics/TerrainSpriteLayer.cs @@ -46,7 +46,7 @@ namespace OpenRA.Graphics rowStride = 6 * map.MapSize.X; vertices = new Vertex[rowStride * map.MapSize.Y]; - vertexBuffer = Game.Renderer.Window.CreateVertexBuffer(vertices.Length); + vertexBuffer = Game.Renderer.Context.CreateVertexBuffer(vertices.Length); emptySprite = new Sprite(sheet, Rectangle.Empty, TextureChannel.Alpha); wr.PaletteInvalidated += UpdatePaletteIndices; diff --git a/OpenRA.Game/Graphics/WorldRenderer.cs b/OpenRA.Game/Graphics/WorldRenderer.cs index 39f0b58318..48e4a26280 100644 --- a/OpenRA.Game/Graphics/WorldRenderer.cs +++ b/OpenRA.Game/Graphics/WorldRenderer.cs @@ -179,7 +179,7 @@ namespace OpenRA.Graphics Game.Renderer.EnableScissor(bounds); if (enableDepthBuffer) - Game.Renderer.Window.EnableDepthBuffer(); + Game.Renderer.Context.EnableDepthBuffer(); terrainRenderer.Draw(this, Viewport); Game.Renderer.Flush(); @@ -203,7 +203,7 @@ namespace OpenRA.Graphics a.Trait.RenderShroud(renderShroud, this); if (enableDepthBuffer) - Game.Renderer.Window.DisableDepthBuffer(); + Game.Renderer.Context.DisableDepthBuffer(); Game.Renderer.DisableScissor(); diff --git a/OpenRA.Game/Renderer.cs b/OpenRA.Game/Renderer.cs index 1ed593c461..9b9b784dcc 100644 --- a/OpenRA.Game/Renderer.cs +++ b/OpenRA.Game/Renderer.cs @@ -30,6 +30,8 @@ namespace OpenRA public IReadOnlyDictionary Fonts; internal IPlatformWindow Window { get; private set; } + internal IGraphicsContext Context { get; private set; } + internal int SheetSize { get; private set; } internal int TempBufferSize { get; private set; } @@ -52,19 +54,20 @@ namespace OpenRA var resolution = GetResolution(graphicSettings); Window = platform.CreateWindow(new Size(resolution.Width, resolution.Height), graphicSettings.Mode); + Context = Window.Context; TempBufferSize = graphicSettings.BatchSize; SheetSize = graphicSettings.SheetSize; - WorldSpriteRenderer = new SpriteRenderer(this, Window.CreateShader("combined")); + WorldSpriteRenderer = new SpriteRenderer(this, Context.CreateShader("combined")); WorldRgbaSpriteRenderer = new RgbaSpriteRenderer(WorldSpriteRenderer); WorldRgbaColorRenderer = new RgbaColorRenderer(WorldSpriteRenderer); - WorldModelRenderer = new ModelRenderer(this, Window.CreateShader("model")); - SpriteRenderer = new SpriteRenderer(this, Window.CreateShader("combined")); + WorldModelRenderer = new ModelRenderer(this, Context.CreateShader("model")); + SpriteRenderer = new SpriteRenderer(this, Context.CreateShader("combined")); RgbaSpriteRenderer = new RgbaSpriteRenderer(SpriteRenderer); RgbaColorRenderer = new RgbaColorRenderer(SpriteRenderer); - tempBuffer = Window.CreateVertexBuffer(TempBufferSize); + tempBuffer = Context.CreateVertexBuffer(TempBufferSize); } static Size GetResolution(GraphicSettings graphicsSettings) @@ -113,7 +116,7 @@ namespace OpenRA public void BeginFrame(int2 scroll, float zoom) { - Window.Clear(); + Context.Clear(); SetViewportParams(scroll, zoom); } @@ -154,7 +157,7 @@ namespace OpenRA { Flush(); Window.PumpInput(inputHandler); - Window.Present(); + Context.Present(); } public void DrawBatch(Vertex[] vertices, int numVertices, PrimitiveType type) @@ -168,7 +171,7 @@ namespace OpenRA where T : struct { vertices.Bind(); - Window.DrawPrimitives(type, firstVertex, numVertices); + Context.DrawPrimitives(type, firstVertex, numVertices); PerfHistory.Increment("batches", 1); } @@ -201,7 +204,7 @@ namespace OpenRA public IVertexBuffer CreateVertexBuffer(int length) { - return Window.CreateVertexBuffer(length); + return Context.CreateVertexBuffer(length); } public void EnableScissor(Rectangle rect) @@ -211,7 +214,7 @@ namespace OpenRA rect.Intersect(scissorState.Peek()); Flush(); - Window.EnableScissor(rect.Left, rect.Top, rect.Width, rect.Height); + Context.EnableScissor(rect.Left, rect.Top, rect.Width, rect.Height); scissorState.Push(rect); } @@ -224,28 +227,28 @@ namespace OpenRA if (scissorState.Any()) { var rect = scissorState.Peek(); - Window.EnableScissor(rect.Left, rect.Top, rect.Width, rect.Height); + Context.EnableScissor(rect.Left, rect.Top, rect.Width, rect.Height); } else - Window.DisableScissor(); + Context.DisableScissor(); } public void EnableDepthBuffer() { Flush(); - Window.EnableDepthBuffer(); + Context.EnableDepthBuffer(); } public void DisableDepthBuffer() { Flush(); - Window.DisableDepthBuffer(); + Context.DisableDepthBuffer(); } public void ClearDepthBuffer() { Flush(); - Window.ClearDepthBuffer(); + Context.ClearDepthBuffer(); } public void GrabWindowMouseFocus() @@ -282,7 +285,7 @@ namespace OpenRA public string GLVersion { - get { return Window.GLVersion; } + get { return Context.GLVersion; } } } } diff --git a/OpenRA.Platforms.Default/OpenRA.Platforms.Default.csproj b/OpenRA.Platforms.Default/OpenRA.Platforms.Default.csproj index aeeb61e796..1f594d7dbd 100644 --- a/OpenRA.Platforms.Default/OpenRA.Platforms.Default.csproj +++ b/OpenRA.Platforms.Default/OpenRA.Platforms.Default.csproj @@ -59,6 +59,7 @@ + diff --git a/OpenRA.Platforms.Default/Sdl2GraphicsContext.cs b/OpenRA.Platforms.Default/Sdl2GraphicsContext.cs new file mode 100644 index 0000000000..394962dfd5 --- /dev/null +++ b/OpenRA.Platforms.Default/Sdl2GraphicsContext.cs @@ -0,0 +1,256 @@ +#region Copyright & License Information +/* + * Copyright 2007-2018 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; +using System.Drawing; +using OpenRA.Graphics; +using SDL2; + +namespace OpenRA.Platforms.Default +{ + sealed class Sdl2GraphicsContext : ThreadAffine, IGraphicsContext + { + readonly Sdl2PlatformWindow window; + bool disposed; + IntPtr context; + + public Sdl2GraphicsContext(Sdl2PlatformWindow window) + { + this.window = window; + context = SDL.SDL_GL_CreateContext(window.Window); + if (context == IntPtr.Zero || SDL.SDL_GL_MakeCurrent(window.Window, context) < 0) + throw new InvalidOperationException("Can not create OpenGL context. (Error: {0})".F(SDL.SDL_GetError())); + + OpenGL.Initialize(); + + OpenGL.glEnableVertexAttribArray(Shader.VertexPosAttributeIndex); + OpenGL.CheckGLError(); + OpenGL.glEnableVertexAttribArray(Shader.TexCoordAttributeIndex); + OpenGL.CheckGLError(); + OpenGL.glEnableVertexAttribArray(Shader.TexMetadataAttributeIndex); + OpenGL.CheckGLError(); + } + + public IVertexBuffer CreateVertexBuffer(int size) + { + VerifyThreadAffinity(); + return new VertexBuffer(size); + } + + public ITexture CreateTexture() + { + VerifyThreadAffinity(); + return new Texture(); + } + + public ITexture CreateTexture(Bitmap bitmap) + { + VerifyThreadAffinity(); + return new Texture(bitmap); + } + + public IFrameBuffer CreateFrameBuffer(Size s) + { + VerifyThreadAffinity(); + return new FrameBuffer(s); + } + + public IShader CreateShader(string name) + { + VerifyThreadAffinity(); + return new Shader(name); + } + + public void EnableScissor(int left, int top, int width, int height) + { + VerifyThreadAffinity(); + + if (width < 0) + width = 0; + + if (height < 0) + height = 0; + + var windowSize = window.WindowSize; + var windowScale = window.WindowScale; + var surfaceSize = window.SurfaceSize; + + var bottom = windowSize.Height - (top + height); + if (windowSize != surfaceSize) + { + left = (int)Math.Round(windowScale * left); + bottom = (int)Math.Round(windowScale * bottom); + width = (int)Math.Round(windowScale * width); + height = (int)Math.Round(windowScale * height); + } + + OpenGL.glScissor(left, bottom, width, height); + OpenGL.CheckGLError(); + OpenGL.glEnable(OpenGL.GL_SCISSOR_TEST); + OpenGL.CheckGLError(); + } + + public void DisableScissor() + { + VerifyThreadAffinity(); + OpenGL.glDisable(OpenGL.GL_SCISSOR_TEST); + OpenGL.CheckGLError(); + } + + public Bitmap TakeScreenshot() + { + var rect = new Rectangle(Point.Empty, window.SurfaceSize); + var bitmap = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); + var data = bitmap.LockBits(rect, + System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); + + OpenGL.glPushClientAttrib(OpenGL.GL_CLIENT_PIXEL_STORE_BIT); + + OpenGL.glPixelStoref(OpenGL.GL_PACK_ROW_LENGTH, data.Stride / 4f); + OpenGL.glPixelStoref(OpenGL.GL_PACK_ALIGNMENT, 1); + + OpenGL.glReadPixels(rect.X, rect.Y, rect.Width, rect.Height, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, data.Scan0); + OpenGL.glFinish(); + + OpenGL.glPopClientAttrib(); + + bitmap.UnlockBits(data); + + // OpenGL standard defines the origin in the bottom left corner which is why this is upside-down by default. + bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); + + return bitmap; + } + + public void Present() + { + VerifyThreadAffinity(); + SDL.SDL_GL_SwapWindow(window.Window); + } + + static int ModeFromPrimitiveType(PrimitiveType pt) + { + switch (pt) + { + case PrimitiveType.PointList: return OpenGL.GL_POINTS; + case PrimitiveType.LineList: return OpenGL.GL_LINES; + case PrimitiveType.TriangleList: return OpenGL.GL_TRIANGLES; + } + + throw new NotImplementedException(); + } + + public void DrawPrimitives(PrimitiveType pt, int firstVertex, int numVertices) + { + VerifyThreadAffinity(); + OpenGL.glDrawArrays(ModeFromPrimitiveType(pt), firstVertex, numVertices); + OpenGL.CheckGLError(); + } + + public void Clear() + { + VerifyThreadAffinity(); + OpenGL.glClearColor(0, 0, 0, 1); + OpenGL.CheckGLError(); + OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); + OpenGL.CheckGLError(); + } + + public void EnableDepthBuffer() + { + VerifyThreadAffinity(); + OpenGL.glClear(OpenGL.GL_DEPTH_BUFFER_BIT); + OpenGL.CheckGLError(); + OpenGL.glEnable(OpenGL.GL_DEPTH_TEST); + OpenGL.CheckGLError(); + OpenGL.glDepthFunc(OpenGL.GL_LEQUAL); + OpenGL.CheckGLError(); + } + + public void DisableDepthBuffer() + { + VerifyThreadAffinity(); + OpenGL.glDisable(OpenGL.GL_DEPTH_TEST); + OpenGL.CheckGLError(); + } + + public void ClearDepthBuffer() + { + VerifyThreadAffinity(); + OpenGL.glClear(OpenGL.GL_DEPTH_BUFFER_BIT); + OpenGL.CheckGLError(); + } + + public void SetBlendMode(BlendMode mode) + { + VerifyThreadAffinity(); + OpenGL.glBlendEquation(OpenGL.GL_FUNC_ADD); + OpenGL.CheckGLError(); + + switch (mode) + { + case BlendMode.None: + OpenGL.glDisable(OpenGL.GL_BLEND); + break; + case BlendMode.Alpha: + OpenGL.glEnable(OpenGL.GL_BLEND); + OpenGL.CheckGLError(); + OpenGL.glBlendFunc(OpenGL.GL_ONE, OpenGL.GL_ONE_MINUS_SRC_ALPHA); + break; + case BlendMode.Additive: + case BlendMode.Subtractive: + OpenGL.glEnable(OpenGL.GL_BLEND); + OpenGL.CheckGLError(); + OpenGL.glBlendFunc(OpenGL.GL_ONE, OpenGL.GL_ONE); + if (mode == BlendMode.Subtractive) + { + OpenGL.CheckGLError(); + OpenGL.glBlendEquation(OpenGL.GL_FUNC_REVERSE_SUBTRACT); + } + + break; + case BlendMode.Multiply: + OpenGL.glEnable(OpenGL.GL_BLEND); + OpenGL.CheckGLError(); + OpenGL.glBlendFunc(OpenGL.GL_DST_COLOR, OpenGL.GL_ONE_MINUS_SRC_ALPHA); + OpenGL.CheckGLError(); + break; + case BlendMode.Multiplicative: + OpenGL.glEnable(OpenGL.GL_BLEND); + OpenGL.CheckGLError(); + OpenGL.glBlendFunc(OpenGL.GL_ZERO, OpenGL.GL_SRC_COLOR); + break; + case BlendMode.DoubleMultiplicative: + OpenGL.glEnable(OpenGL.GL_BLEND); + OpenGL.CheckGLError(); + OpenGL.glBlendFunc(OpenGL.GL_DST_COLOR, OpenGL.GL_SRC_COLOR); + break; + } + + OpenGL.CheckGLError(); + } + + public void Dispose() + { + if (disposed) + return; + + disposed = true; + if (context != IntPtr.Zero) + { + SDL.SDL_GL_DeleteContext(context); + context = IntPtr.Zero; + } + } + + public string GLVersion { get { return OpenGL.Version; } } + } +} diff --git a/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs b/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs index 77b9e239be..2e8bfa405d 100644 --- a/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs +++ b/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs @@ -12,16 +12,18 @@ using System; using System.Drawing; using System.Runtime.InteropServices; -using OpenRA.Graphics; using SDL2; namespace OpenRA.Platforms.Default { sealed class Sdl2PlatformWindow : ThreadAffine, IPlatformWindow { + readonly IGraphicsContext context; readonly Sdl2Input input; - IntPtr context, window; + public IGraphicsContext Context { get { return context; } } + + internal readonly IntPtr Window; bool disposed; public Size WindowSize { get; private set; } @@ -67,7 +69,7 @@ namespace OpenRA.Platforms.Default if (Platform.CurrentPlatform == PlatformType.OSX && windowMode == WindowMode.Fullscreen) SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1"); - window = SDL.SDL_CreateWindow("OpenRA", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, + Window = SDL.SDL_CreateWindow("OpenRA", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, WindowSize.Width, WindowSize.Height, windowFlags); SurfaceSize = WindowSize; @@ -79,7 +81,7 @@ namespace OpenRA.Platforms.Default // OSX defines the window size in "points", with a device-dependent number of pixels per point. // The window scale is simply the ratio of GL pixels / window points. int width, height; - SDL.SDL_GL_GetDrawableSize(window, out width, out height); + SDL.SDL_GL_GetDrawableSize(Window, out width, out height); SurfaceSize = new Size(width, height); WindowScale = width * 1f / WindowSize.Width; } @@ -112,7 +114,7 @@ namespace OpenRA.Platforms.Default if (windowMode == WindowMode.Fullscreen) { - SDL.SDL_SetWindowFullscreen(window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN); + SDL.SDL_SetWindowFullscreen(Window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN); // Fullscreen mode on OSX will ignore the configured display resolution // and instead always picks an arbitrary scaled resolution choice that may @@ -123,7 +125,7 @@ namespace OpenRA.Platforms.Default if (Platform.CurrentPlatform == PlatformType.OSX) { int width, height; - SDL.SDL_GetWindowSize(window, out width, out height); + SDL.SDL_GetWindowSize(Window, out width, out height); WindowSize = SurfaceSize = new Size(width, height); WindowScale = 1; } @@ -133,24 +135,13 @@ namespace OpenRA.Platforms.Default // Work around a visual glitch in OSX: the window is offset // partially offscreen if the dock is at the left of the screen if (Platform.CurrentPlatform == PlatformType.OSX) - SDL.SDL_SetWindowPosition(window, 0, 0); + SDL.SDL_SetWindowPosition(Window, 0, 0); - SDL.SDL_SetWindowFullscreen(window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP); + SDL.SDL_SetWindowFullscreen(Window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP); SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); } - context = SDL.SDL_GL_CreateContext(window); - if (context == IntPtr.Zero || SDL.SDL_GL_MakeCurrent(window, context) < 0) - throw new InvalidOperationException("Can not create OpenGL context. (Error: {0})".F(SDL.SDL_GetError())); - - OpenGL.Initialize(); - - OpenGL.glEnableVertexAttribArray(Shader.VertexPosAttributeIndex); - OpenGL.CheckGLError(); - OpenGL.glEnableVertexAttribArray(Shader.TexCoordAttributeIndex); - OpenGL.CheckGLError(); - OpenGL.glEnableVertexAttribArray(Shader.TexMetadataAttributeIndex); - OpenGL.CheckGLError(); + context = new Sdl2GraphicsContext(this); SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE); input = new Sdl2Input(); @@ -212,7 +203,7 @@ namespace OpenRA.Platforms.Default if (Platform.CurrentPlatform == PlatformType.OSX) { int width, height; - SDL.SDL_GL_GetDrawableSize(window, out width, out height); + SDL.SDL_GL_GetDrawableSize(Window, out width, out height); if (width != SurfaceSize.Width || height != SurfaceSize.Height) { @@ -231,196 +222,26 @@ namespace OpenRA.Platforms.Default return; disposed = true; - if (context != IntPtr.Zero) - { - SDL.SDL_GL_DeleteContext(context); - context = IntPtr.Zero; - } - if (window != IntPtr.Zero) - { - SDL.SDL_DestroyWindow(window); - window = IntPtr.Zero; - } + if (context != null) + context.Dispose(); + + if (Window != IntPtr.Zero) + SDL.SDL_DestroyWindow(Window); SDL.SDL_Quit(); } - static int ModeFromPrimitiveType(PrimitiveType pt) - { - switch (pt) - { - case PrimitiveType.PointList: return OpenGL.GL_POINTS; - case PrimitiveType.LineList: return OpenGL.GL_LINES; - case PrimitiveType.TriangleList: return OpenGL.GL_TRIANGLES; - } - - throw new NotImplementedException(); - } - - public void DrawPrimitives(PrimitiveType pt, int firstVertex, int numVertices) - { - VerifyThreadAffinity(); - OpenGL.glDrawArrays(ModeFromPrimitiveType(pt), firstVertex, numVertices); - OpenGL.CheckGLError(); - } - - public void Clear() - { - VerifyThreadAffinity(); - OpenGL.glClearColor(0, 0, 0, 1); - OpenGL.CheckGLError(); - OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); - OpenGL.CheckGLError(); - } - - public void EnableDepthBuffer() - { - VerifyThreadAffinity(); - OpenGL.glClear(OpenGL.GL_DEPTH_BUFFER_BIT); - OpenGL.CheckGLError(); - OpenGL.glEnable(OpenGL.GL_DEPTH_TEST); - OpenGL.CheckGLError(); - OpenGL.glDepthFunc(OpenGL.GL_LEQUAL); - OpenGL.CheckGLError(); - } - - public void DisableDepthBuffer() - { - VerifyThreadAffinity(); - OpenGL.glDisable(OpenGL.GL_DEPTH_TEST); - OpenGL.CheckGLError(); - } - - public void ClearDepthBuffer() - { - VerifyThreadAffinity(); - OpenGL.glClear(OpenGL.GL_DEPTH_BUFFER_BIT); - OpenGL.CheckGLError(); - } - - public void SetBlendMode(BlendMode mode) - { - VerifyThreadAffinity(); - OpenGL.glBlendEquation(OpenGL.GL_FUNC_ADD); - OpenGL.CheckGLError(); - - switch (mode) - { - case BlendMode.None: - OpenGL.glDisable(OpenGL.GL_BLEND); - break; - case BlendMode.Alpha: - OpenGL.glEnable(OpenGL.GL_BLEND); - OpenGL.CheckGLError(); - OpenGL.glBlendFunc(OpenGL.GL_ONE, OpenGL.GL_ONE_MINUS_SRC_ALPHA); - break; - case BlendMode.Additive: - case BlendMode.Subtractive: - OpenGL.glEnable(OpenGL.GL_BLEND); - OpenGL.CheckGLError(); - OpenGL.glBlendFunc(OpenGL.GL_ONE, OpenGL.GL_ONE); - if (mode == BlendMode.Subtractive) - { - OpenGL.CheckGLError(); - OpenGL.glBlendEquation(OpenGL.GL_FUNC_REVERSE_SUBTRACT); - } - - break; - case BlendMode.Multiply: - OpenGL.glEnable(OpenGL.GL_BLEND); - OpenGL.CheckGLError(); - OpenGL.glBlendFunc(OpenGL.GL_DST_COLOR, OpenGL.GL_ONE_MINUS_SRC_ALPHA); - OpenGL.CheckGLError(); - break; - case BlendMode.Multiplicative: - OpenGL.glEnable(OpenGL.GL_BLEND); - OpenGL.CheckGLError(); - OpenGL.glBlendFunc(OpenGL.GL_ZERO, OpenGL.GL_SRC_COLOR); - break; - case BlendMode.DoubleMultiplicative: - OpenGL.glEnable(OpenGL.GL_BLEND); - OpenGL.CheckGLError(); - OpenGL.glBlendFunc(OpenGL.GL_DST_COLOR, OpenGL.GL_SRC_COLOR); - break; - } - - OpenGL.CheckGLError(); - } - public void GrabWindowMouseFocus() { VerifyThreadAffinity(); - SDL.SDL_SetWindowGrab(window, SDL.SDL_bool.SDL_TRUE); + SDL.SDL_SetWindowGrab(Window, SDL.SDL_bool.SDL_TRUE); } public void ReleaseWindowMouseFocus() { VerifyThreadAffinity(); - SDL.SDL_SetWindowGrab(window, SDL.SDL_bool.SDL_FALSE); - } - - public void EnableScissor(int left, int top, int width, int height) - { - VerifyThreadAffinity(); - - if (width < 0) - width = 0; - - if (height < 0) - height = 0; - - var bottom = WindowSize.Height - (top + height); - if (WindowSize != SurfaceSize) - { - left = (int)Math.Round(WindowScale * left); - bottom = (int)Math.Round(WindowScale * bottom); - width = (int)Math.Round(WindowScale * width); - height = (int)Math.Round(WindowScale * height); - } - - OpenGL.glScissor(left, bottom, width, height); - OpenGL.CheckGLError(); - OpenGL.glEnable(OpenGL.GL_SCISSOR_TEST); - OpenGL.CheckGLError(); - } - - public void DisableScissor() - { - VerifyThreadAffinity(); - OpenGL.glDisable(OpenGL.GL_SCISSOR_TEST); - OpenGL.CheckGLError(); - } - - public Bitmap TakeScreenshot() - { - var rect = new Rectangle(Point.Empty, SurfaceSize); - var bitmap = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); - var data = bitmap.LockBits(rect, - System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); - - OpenGL.glPushClientAttrib(OpenGL.GL_CLIENT_PIXEL_STORE_BIT); - - OpenGL.glPixelStoref(OpenGL.GL_PACK_ROW_LENGTH, data.Stride / 4f); - OpenGL.glPixelStoref(OpenGL.GL_PACK_ALIGNMENT, 1); - - OpenGL.glReadPixels(rect.X, rect.Y, rect.Width, rect.Height, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, data.Scan0); - OpenGL.glFinish(); - - OpenGL.glPopClientAttrib(); - - bitmap.UnlockBits(data); - - // OpenGL standard defines the origin in the bottom left corner which is why this is upside-down by default. - bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); - - return bitmap; - } - - public void Present() - { - VerifyThreadAffinity(); - SDL.SDL_GL_SwapWindow(window); + SDL.SDL_SetWindowGrab(Window, SDL.SDL_bool.SDL_FALSE); } public void PumpInput(IInputHandler inputHandler) @@ -440,37 +261,5 @@ namespace OpenRA.Platforms.Default VerifyThreadAffinity(); return input.SetClipboardText(text); } - - public IVertexBuffer CreateVertexBuffer(int size) - { - VerifyThreadAffinity(); - return new VertexBuffer(size); - } - - public ITexture CreateTexture() - { - VerifyThreadAffinity(); - return new Texture(); - } - - public ITexture CreateTexture(Bitmap bitmap) - { - VerifyThreadAffinity(); - return new Texture(bitmap); - } - - public IFrameBuffer CreateFrameBuffer(Size s) - { - VerifyThreadAffinity(); - return new FrameBuffer(s); - } - - public IShader CreateShader(string name) - { - VerifyThreadAffinity(); - return new Shader(name); - } - - public string GLVersion { get { return OpenGL.Version; } } } }