Split IGraphicsContext from IPlatformWindow.

This commit is contained in:
Paul Chote
2018-06-06 20:27:32 +00:00
parent ab14a86d39
commit 9ff177359b
12 changed files with 326 additions and 276 deletions

View File

@@ -523,7 +523,7 @@ namespace OpenRA
Bitmap bitmap; Bitmap bitmap;
using (new PerfTimer("Renderer.TakeScreenshot")) using (new PerfTimer("Renderer.TakeScreenshot"))
bitmap = Renderer.Window.TakeScreenshot(); bitmap = Renderer.Context.TakeScreenshot();
ThreadPool.QueueUserWorkItem(_ => ThreadPool.QueueUserWorkItem(_ =>
{ {

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Graphics
public HardwarePalette() public HardwarePalette()
{ {
Texture = Game.Renderer.Window.CreateTexture(); Texture = Game.Renderer.Context.CreateTexture();
readOnlyModifiablePalettes = modifiablePalettes.AsReadOnly(); readOnlyModifiablePalettes = modifiablePalettes.AsReadOnly();
} }

View File

@@ -300,14 +300,14 @@ namespace OpenRA.Graphics
Game.Renderer.Flush(); Game.Renderer.Flush();
fbo.Bind(); fbo.Bind();
Game.Renderer.Window.EnableDepthBuffer(); Game.Renderer.Context.EnableDepthBuffer();
return fbo; return fbo;
} }
void DisableFrameBuffer(IFrameBuffer fbo) void DisableFrameBuffer(IFrameBuffer fbo)
{ {
Game.Renderer.Flush(); Game.Renderer.Flush();
Game.Renderer.Window.DisableDepthBuffer(); Game.Renderer.Context.DisableDepthBuffer();
fbo.Unbind(); fbo.Unbind();
} }
@@ -356,7 +356,7 @@ namespace OpenRA.Graphics
} }
var size = new Size(renderer.SheetSize, renderer.SheetSize); 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); var sheet = new Sheet(SheetType.BGRA, framebuffer.Texture);
mappedBuffers.Add(sheet, framebuffer); mappedBuffers.Add(sheet, framebuffer);

View File

@@ -36,39 +36,40 @@ namespace OpenRA
public interface IPlatformWindow : IDisposable public interface IPlatformWindow : IDisposable
{ {
IVertexBuffer<Vertex> CreateVertexBuffer(int length); IGraphicsContext Context { get; }
ITexture CreateTexture(Bitmap bitmap);
ITexture CreateTexture();
IFrameBuffer CreateFrameBuffer(Size s);
IShader CreateShader(string name);
Size WindowSize { get; } Size WindowSize { get; }
float WindowScale { get; } float WindowScale { get; }
event Action<float, float> OnWindowScaleChanged; event Action<float, float> OnWindowScaleChanged;
void Clear();
void Present();
Bitmap TakeScreenshot();
void PumpInput(IInputHandler inputHandler); void PumpInput(IInputHandler inputHandler);
string GetClipboardText(); string GetClipboardText();
bool SetClipboardText(string text); 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 GrabWindowMouseFocus();
void ReleaseWindowMouseFocus(); void ReleaseWindowMouseFocus();
IHardwareCursor CreateHardwareCursor(string name, Size size, byte[] data, int2 hotspot); IHardwareCursor CreateHardwareCursor(string name, Size size, byte[] data, int2 hotspot);
void SetHardwareCursor(IHardwareCursor cursor); void SetHardwareCursor(IHardwareCursor cursor);
}
public interface IGraphicsContext : IDisposable
{
IVertexBuffer<Vertex> 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; } string GLVersion { get; }
} }

View File

@@ -66,7 +66,7 @@ namespace OpenRA.Graphics
{ {
if (texture == null) if (texture == null)
{ {
texture = Game.Renderer.Window.CreateTexture(); texture = Game.Renderer.Context.CreateTexture();
dirty = true; dirty = true;
} }

View File

@@ -43,10 +43,10 @@ namespace OpenRA.Graphics
sheets[i] = null; sheets[i] = null;
} }
renderer.Window.SetBlendMode(currentBlend); renderer.Context.SetBlendMode(currentBlend);
shader.PrepareRender(); shader.PrepareRender();
renderer.DrawBatch(vertices, nv, PrimitiveType.TriangleList); renderer.DrawBatch(vertices, nv, PrimitiveType.TriangleList);
renderer.Window.SetBlendMode(BlendMode.None); renderer.Context.SetBlendMode(BlendMode.None);
nv = 0; nv = 0;
ns = 0; ns = 0;
@@ -131,10 +131,10 @@ namespace OpenRA.Graphics
public void DrawVertexBuffer(IVertexBuffer<Vertex> buffer, int start, int length, PrimitiveType type, Sheet sheet, BlendMode blendMode) public void DrawVertexBuffer(IVertexBuffer<Vertex> buffer, int start, int length, PrimitiveType type, Sheet sheet, BlendMode blendMode)
{ {
shader.SetTexture("Texture0", sheet.GetTexture()); shader.SetTexture("Texture0", sheet.GetTexture());
renderer.Window.SetBlendMode(blendMode); renderer.Context.SetBlendMode(blendMode);
shader.PrepareRender(); shader.PrepareRender();
renderer.DrawBatch(buffer, start, length, type); renderer.DrawBatch(buffer, start, length, type);
renderer.Window.SetBlendMode(BlendMode.None); renderer.Context.SetBlendMode(BlendMode.None);
} }
// For RGBAColorRenderer // For RGBAColorRenderer

View File

@@ -46,7 +46,7 @@ namespace OpenRA.Graphics
rowStride = 6 * map.MapSize.X; rowStride = 6 * map.MapSize.X;
vertices = new Vertex[rowStride * map.MapSize.Y]; 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); emptySprite = new Sprite(sheet, Rectangle.Empty, TextureChannel.Alpha);
wr.PaletteInvalidated += UpdatePaletteIndices; wr.PaletteInvalidated += UpdatePaletteIndices;

View File

@@ -179,7 +179,7 @@ namespace OpenRA.Graphics
Game.Renderer.EnableScissor(bounds); Game.Renderer.EnableScissor(bounds);
if (enableDepthBuffer) if (enableDepthBuffer)
Game.Renderer.Window.EnableDepthBuffer(); Game.Renderer.Context.EnableDepthBuffer();
terrainRenderer.Draw(this, Viewport); terrainRenderer.Draw(this, Viewport);
Game.Renderer.Flush(); Game.Renderer.Flush();
@@ -203,7 +203,7 @@ namespace OpenRA.Graphics
a.Trait.RenderShroud(renderShroud, this); a.Trait.RenderShroud(renderShroud, this);
if (enableDepthBuffer) if (enableDepthBuffer)
Game.Renderer.Window.DisableDepthBuffer(); Game.Renderer.Context.DisableDepthBuffer();
Game.Renderer.DisableScissor(); Game.Renderer.DisableScissor();

View File

@@ -30,6 +30,8 @@ namespace OpenRA
public IReadOnlyDictionary<string, SpriteFont> Fonts; public IReadOnlyDictionary<string, SpriteFont> Fonts;
internal IPlatformWindow Window { get; private set; } internal IPlatformWindow Window { get; private set; }
internal IGraphicsContext Context { get; private set; }
internal int SheetSize { get; private set; } internal int SheetSize { get; private set; }
internal int TempBufferSize { get; private set; } internal int TempBufferSize { get; private set; }
@@ -52,19 +54,20 @@ namespace OpenRA
var resolution = GetResolution(graphicSettings); var resolution = GetResolution(graphicSettings);
Window = platform.CreateWindow(new Size(resolution.Width, resolution.Height), graphicSettings.Mode); Window = platform.CreateWindow(new Size(resolution.Width, resolution.Height), graphicSettings.Mode);
Context = Window.Context;
TempBufferSize = graphicSettings.BatchSize; TempBufferSize = graphicSettings.BatchSize;
SheetSize = graphicSettings.SheetSize; SheetSize = graphicSettings.SheetSize;
WorldSpriteRenderer = new SpriteRenderer(this, Window.CreateShader("combined")); WorldSpriteRenderer = new SpriteRenderer(this, Context.CreateShader("combined"));
WorldRgbaSpriteRenderer = new RgbaSpriteRenderer(WorldSpriteRenderer); WorldRgbaSpriteRenderer = new RgbaSpriteRenderer(WorldSpriteRenderer);
WorldRgbaColorRenderer = new RgbaColorRenderer(WorldSpriteRenderer); WorldRgbaColorRenderer = new RgbaColorRenderer(WorldSpriteRenderer);
WorldModelRenderer = new ModelRenderer(this, Window.CreateShader("model")); WorldModelRenderer = new ModelRenderer(this, Context.CreateShader("model"));
SpriteRenderer = new SpriteRenderer(this, Window.CreateShader("combined")); SpriteRenderer = new SpriteRenderer(this, Context.CreateShader("combined"));
RgbaSpriteRenderer = new RgbaSpriteRenderer(SpriteRenderer); RgbaSpriteRenderer = new RgbaSpriteRenderer(SpriteRenderer);
RgbaColorRenderer = new RgbaColorRenderer(SpriteRenderer); RgbaColorRenderer = new RgbaColorRenderer(SpriteRenderer);
tempBuffer = Window.CreateVertexBuffer(TempBufferSize); tempBuffer = Context.CreateVertexBuffer(TempBufferSize);
} }
static Size GetResolution(GraphicSettings graphicsSettings) static Size GetResolution(GraphicSettings graphicsSettings)
@@ -113,7 +116,7 @@ namespace OpenRA
public void BeginFrame(int2 scroll, float zoom) public void BeginFrame(int2 scroll, float zoom)
{ {
Window.Clear(); Context.Clear();
SetViewportParams(scroll, zoom); SetViewportParams(scroll, zoom);
} }
@@ -154,7 +157,7 @@ namespace OpenRA
{ {
Flush(); Flush();
Window.PumpInput(inputHandler); Window.PumpInput(inputHandler);
Window.Present(); Context.Present();
} }
public void DrawBatch(Vertex[] vertices, int numVertices, PrimitiveType type) public void DrawBatch(Vertex[] vertices, int numVertices, PrimitiveType type)
@@ -168,7 +171,7 @@ namespace OpenRA
where T : struct where T : struct
{ {
vertices.Bind(); vertices.Bind();
Window.DrawPrimitives(type, firstVertex, numVertices); Context.DrawPrimitives(type, firstVertex, numVertices);
PerfHistory.Increment("batches", 1); PerfHistory.Increment("batches", 1);
} }
@@ -201,7 +204,7 @@ namespace OpenRA
public IVertexBuffer<Vertex> CreateVertexBuffer(int length) public IVertexBuffer<Vertex> CreateVertexBuffer(int length)
{ {
return Window.CreateVertexBuffer(length); return Context.CreateVertexBuffer(length);
} }
public void EnableScissor(Rectangle rect) public void EnableScissor(Rectangle rect)
@@ -211,7 +214,7 @@ namespace OpenRA
rect.Intersect(scissorState.Peek()); rect.Intersect(scissorState.Peek());
Flush(); Flush();
Window.EnableScissor(rect.Left, rect.Top, rect.Width, rect.Height); Context.EnableScissor(rect.Left, rect.Top, rect.Width, rect.Height);
scissorState.Push(rect); scissorState.Push(rect);
} }
@@ -224,28 +227,28 @@ namespace OpenRA
if (scissorState.Any()) if (scissorState.Any())
{ {
var rect = scissorState.Peek(); 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 else
Window.DisableScissor(); Context.DisableScissor();
} }
public void EnableDepthBuffer() public void EnableDepthBuffer()
{ {
Flush(); Flush();
Window.EnableDepthBuffer(); Context.EnableDepthBuffer();
} }
public void DisableDepthBuffer() public void DisableDepthBuffer()
{ {
Flush(); Flush();
Window.DisableDepthBuffer(); Context.DisableDepthBuffer();
} }
public void ClearDepthBuffer() public void ClearDepthBuffer()
{ {
Flush(); Flush();
Window.ClearDepthBuffer(); Context.ClearDepthBuffer();
} }
public void GrabWindowMouseFocus() public void GrabWindowMouseFocus()
@@ -282,7 +285,7 @@ namespace OpenRA
public string GLVersion public string GLVersion
{ {
get { return Window.GLVersion; } get { return Context.GLVersion; }
} }
} }
} }

View File

@@ -59,6 +59,7 @@
<Compile Include="OpenAlSoundEngine.cs" /> <Compile Include="OpenAlSoundEngine.cs" />
<Compile Include="OpenGL.cs" /> <Compile Include="OpenGL.cs" />
<Compile Include="Sdl2HardwareCursor.cs" /> <Compile Include="Sdl2HardwareCursor.cs" />
<Compile Include="Sdl2GraphicsContext.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>

View File

@@ -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<Vertex> CreateVertexBuffer(int size)
{
VerifyThreadAffinity();
return new VertexBuffer<Vertex>(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; } }
}
}

View File

@@ -12,16 +12,18 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using OpenRA.Graphics;
using SDL2; using SDL2;
namespace OpenRA.Platforms.Default namespace OpenRA.Platforms.Default
{ {
sealed class Sdl2PlatformWindow : ThreadAffine, IPlatformWindow sealed class Sdl2PlatformWindow : ThreadAffine, IPlatformWindow
{ {
readonly IGraphicsContext context;
readonly Sdl2Input input; readonly Sdl2Input input;
IntPtr context, window; public IGraphicsContext Context { get { return context; } }
internal readonly IntPtr Window;
bool disposed; bool disposed;
public Size WindowSize { get; private set; } public Size WindowSize { get; private set; }
@@ -67,7 +69,7 @@ namespace OpenRA.Platforms.Default
if (Platform.CurrentPlatform == PlatformType.OSX && windowMode == WindowMode.Fullscreen) if (Platform.CurrentPlatform == PlatformType.OSX && windowMode == WindowMode.Fullscreen)
SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1"); 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); WindowSize.Width, WindowSize.Height, windowFlags);
SurfaceSize = WindowSize; 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. // 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. // The window scale is simply the ratio of GL pixels / window points.
int width, height; 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); SurfaceSize = new Size(width, height);
WindowScale = width * 1f / WindowSize.Width; WindowScale = width * 1f / WindowSize.Width;
} }
@@ -112,7 +114,7 @@ namespace OpenRA.Platforms.Default
if (windowMode == WindowMode.Fullscreen) 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 // Fullscreen mode on OSX will ignore the configured display resolution
// and instead always picks an arbitrary scaled resolution choice that may // and instead always picks an arbitrary scaled resolution choice that may
@@ -123,7 +125,7 @@ namespace OpenRA.Platforms.Default
if (Platform.CurrentPlatform == PlatformType.OSX) if (Platform.CurrentPlatform == PlatformType.OSX)
{ {
int width, height; 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); WindowSize = SurfaceSize = new Size(width, height);
WindowScale = 1; WindowScale = 1;
} }
@@ -133,24 +135,13 @@ namespace OpenRA.Platforms.Default
// Work around a visual glitch in OSX: the window is offset // Work around a visual glitch in OSX: the window is offset
// partially offscreen if the dock is at the left of the screen // partially offscreen if the dock is at the left of the screen
if (Platform.CurrentPlatform == PlatformType.OSX) 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"); SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
} }
context = SDL.SDL_GL_CreateContext(window); context = new Sdl2GraphicsContext(this);
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();
SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE); SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE);
input = new Sdl2Input(); input = new Sdl2Input();
@@ -212,7 +203,7 @@ namespace OpenRA.Platforms.Default
if (Platform.CurrentPlatform == PlatformType.OSX) if (Platform.CurrentPlatform == PlatformType.OSX)
{ {
int width, height; 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) if (width != SurfaceSize.Width || height != SurfaceSize.Height)
{ {
@@ -231,196 +222,26 @@ namespace OpenRA.Platforms.Default
return; return;
disposed = true; disposed = true;
if (context != IntPtr.Zero)
{
SDL.SDL_GL_DeleteContext(context);
context = IntPtr.Zero;
}
if (window != IntPtr.Zero) if (context != null)
{ context.Dispose();
SDL.SDL_DestroyWindow(window);
window = IntPtr.Zero; if (Window != IntPtr.Zero)
} SDL.SDL_DestroyWindow(Window);
SDL.SDL_Quit(); 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() public void GrabWindowMouseFocus()
{ {
VerifyThreadAffinity(); VerifyThreadAffinity();
SDL.SDL_SetWindowGrab(window, SDL.SDL_bool.SDL_TRUE); SDL.SDL_SetWindowGrab(Window, SDL.SDL_bool.SDL_TRUE);
} }
public void ReleaseWindowMouseFocus() public void ReleaseWindowMouseFocus()
{ {
VerifyThreadAffinity(); VerifyThreadAffinity();
SDL.SDL_SetWindowGrab(window, SDL.SDL_bool.SDL_FALSE); 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);
} }
public void PumpInput(IInputHandler inputHandler) public void PumpInput(IInputHandler inputHandler)
@@ -440,37 +261,5 @@ namespace OpenRA.Platforms.Default
VerifyThreadAffinity(); VerifyThreadAffinity();
return input.SetClipboardText(text); return input.SetClipboardText(text);
} }
public IVertexBuffer<Vertex> CreateVertexBuffer(int size)
{
VerifyThreadAffinity();
return new VertexBuffer<Vertex>(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; } }
} }
} }