1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Polished Ambient Lightning

This commit is contained in:
SpoinkyNL 2016-03-07 17:19:38 +01:00
parent ae66e7a63a
commit e93613520d
2 changed files with 35 additions and 16 deletions

View File

@ -39,6 +39,8 @@ namespace Artemis.Modules.Effects.AmbientLightning
public override void Dispose() public override void Dispose()
{ {
Initialized = false; Initialized = false;
_screenCapturer.Dispose();
_screenCapturer = null; _screenCapturer = null;
} }
@ -52,14 +54,16 @@ namespace Artemis.Modules.Effects.AmbientLightning
LinearGradientMode.Horizontal) {Height = MainManager.KeyboardManager.ActiveKeyboard.Height*Scale/2}; LinearGradientMode.Horizontal) {Height = MainManager.KeyboardManager.ActiveKeyboard.Height*Scale/2};
_botRect = new KeyboardRectangle(MainManager.KeyboardManager.ActiveKeyboard, 0, 0, new List<Color>(), _botRect = new KeyboardRectangle(MainManager.KeyboardManager.ActiveKeyboard, 0, 0, new List<Color>(),
LinearGradientMode.Horizontal); LinearGradientMode.Horizontal);
Initialized = true; Initialized = true;
} }
public override void Update() public override void Update()
{ {
var capture = _screenCapturer.Capture(); var capture = _screenCapturer.Capture();
if (capture == null)
return;
_rectangles = new List<Rectangle>(); _rectangles = new List<Rectangle>();
// Analise the result // Analise the result
// Chop the screen into 2 rows and 3 columns // Chop the screen into 2 rows and 3 columns
@ -80,7 +84,7 @@ namespace Artemis.Modules.Effects.AmbientLightning
{ {
var x = blockWidth/6*blockColumn + blockWidth/6/4 + blockBase.X; var x = blockWidth/6*blockColumn + blockWidth/6/4 + blockBase.X;
var y = blockHeight/6*blockRow + blockHeight/6/4 + blockBase.Y; var y = blockHeight/6*blockRow + blockHeight/6/4 + blockBase.Y;
samples.Add(ScreenCapture.GetColor(capture, new Point(x, y))); samples.Add(_screenCapturer.GetColor(capture, new Point(x, y)));
} }
} }

View File

@ -1,4 +1,8 @@
using System.Drawing; // Original code by Florian Schnell
// http://www.floschnell.de/computer-science/super-fast-screen-capture-with-windows-8.html
using System;
using System.Drawing;
using System.IO; using System.IO;
using SharpDX; using SharpDX;
using SharpDX.Direct3D; using SharpDX.Direct3D;
@ -11,14 +15,13 @@ using ResultCode = SharpDX.DXGI.ResultCode;
namespace Artemis.Utilities namespace Artemis.Utilities
{ {
internal class ScreenCapture internal class ScreenCapture : IDisposable
{ {
private readonly Device _device; private readonly Device _device;
private readonly OutputDuplication _duplicatedOutput; private readonly Factory1 _factory;
private readonly Texture2D _screenTexture; private readonly Texture2D _screenTexture;
private readonly uint numAdapter = 0; // # of graphics card adapter
private readonly uint numOutput = 0; // # of output device (i.e. monitor)
private DataStream _dataStream; private DataStream _dataStream;
private OutputDuplication _duplicatedOutput;
private Resource _screenResource; private Resource _screenResource;
private Surface _screenSurface; private Surface _screenSurface;
@ -26,7 +29,7 @@ namespace Artemis.Utilities
{ {
// Create device and factory // Create device and factory
_device = new Device(DriverType.Hardware); _device = new Device(DriverType.Hardware);
var factory = new Factory1(); _factory = new Factory1();
// Creating CPU-accessible texture resource // Creating CPU-accessible texture resource
var texdes = new Texture2DDescription var texdes = new Texture2DDescription
@ -34,8 +37,8 @@ namespace Artemis.Utilities
CpuAccessFlags = CpuAccessFlags.Read, CpuAccessFlags = CpuAccessFlags.Read,
BindFlags = BindFlags.None, BindFlags = BindFlags.None,
Format = Format.B8G8R8A8_UNorm, Format = Format.B8G8R8A8_UNorm,
Height = factory.Adapters1[numAdapter].Outputs[numOutput].Description.DesktopBounds.Bottom, Height = _factory.Adapters1[0].Outputs[0].Description.DesktopBounds.Bottom,
Width = factory.Adapters1[numAdapter].Outputs[numOutput].Description.DesktopBounds.Right, Width = _factory.Adapters1[0].Outputs[0].Description.DesktopBounds.Right,
OptionFlags = ResourceOptionFlags.None, OptionFlags = ResourceOptionFlags.None,
MipLevels = 1, MipLevels = 1,
ArraySize = 1, ArraySize = 1,
@ -49,12 +52,20 @@ namespace Artemis.Utilities
_screenTexture = new Texture2D(_device, texdes); _screenTexture = new Texture2D(_device, texdes);
// duplicate output stuff // duplicate output stuff
var output = new Output1(factory.Adapters1[numAdapter].Outputs[numOutput].NativePointer); var output = new Output1(_factory.Adapters1[0].Outputs[0].NativePointer);
_duplicatedOutput = output.DuplicateOutput(_device); _duplicatedOutput = output.DuplicateOutput(_device);
_screenResource = null; _screenResource = null;
_dataStream = null; _dataStream = null;
} }
public void Dispose()
{
_duplicatedOutput.Dispose();
_screenResource.Dispose();
_dataStream.Dispose();
_factory.Dispose();
}
public DataStream Capture() public DataStream Capture()
{ {
try try
@ -64,8 +75,10 @@ namespace Artemis.Utilities
} }
catch (SharpDXException e) catch (SharpDXException e)
{ {
if (e.ResultCode.Code == ResultCode.WaitTimeout.Result.Code) if (e.ResultCode.Code == ResultCode.WaitTimeout.Result.Code ||
return Capture(); e.ResultCode.Code == ResultCode.AccessDenied.Result.Code ||
e.ResultCode.Code == ResultCode.AccessLost.Result.Code)
return null;
throw; throw;
} }
@ -99,10 +112,12 @@ namespace Artemis.Utilities
/// <param name="surfaceDataStream"></param> /// <param name="surfaceDataStream"></param>
/// <param name="position">Given point on the screen.</param> /// <param name="position">Given point on the screen.</param>
/// <returns></returns> /// <returns></returns>
public static Color GetColor(DataStream surfaceDataStream, Point position) public Color GetColor(DataStream surfaceDataStream, Point position)
{ {
var data = new byte[4]; var data = new byte[4];
surfaceDataStream.Seek(position.Y*1920*4 + position.X*4, SeekOrigin.Begin); surfaceDataStream.Seek(
position.Y*_factory.Adapters1[0].Outputs[0].Description.DesktopBounds.Right*4 + position.X*4,
SeekOrigin.Begin);
surfaceDataStream.Read(data, 0, 4); surfaceDataStream.Read(data, 0, 4);
return Color.FromArgb(255, data[2], data[1], data[0]); return Color.FromArgb(255, data[2], data[1], data[0]);
} }