Added GraphicsModeComparer for GraphicsMode comparisons.
Added WinInputBase as a base abstraction for all win32 input class.
This commit is contained in:
parent
e01ebf71e4
commit
6bb01d4cbb
3 changed files with 274 additions and 8 deletions
63
Source/OpenTK/Graphics/GraphicsModeComparer.cs
Normal file
63
Source/OpenTK/Graphics/GraphicsModeComparer.cs
Normal file
|
@ -0,0 +1,63 @@
|
|||
#region License
|
||||
//
|
||||
// The Open Toolkit Library License
|
||||
//
|
||||
// Copyright (c) 2006 - 2010 the Open Toolkit library.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do
|
||||
// so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenTK.Graphics
|
||||
{
|
||||
sealed class GraphicsModeComparer : IComparer<GraphicsMode>
|
||||
{
|
||||
#region IComparer<GraphicsMode> Members
|
||||
|
||||
public int Compare(GraphicsMode x, GraphicsMode y)
|
||||
{
|
||||
int result = x.ColorFormat.CompareTo(y.ColorFormat);
|
||||
if (result != 0)
|
||||
return result;
|
||||
result = x.Depth.CompareTo(y.Depth);
|
||||
if (result != 0)
|
||||
return result;
|
||||
result = x.Stencil.CompareTo(y.Stencil);
|
||||
if (result != 0)
|
||||
return result;
|
||||
result = x.Samples.CompareTo(y.Samples);
|
||||
if (result != 0)
|
||||
return result;
|
||||
result = x.Stereo.CompareTo(y.Stereo);
|
||||
if (result != 0)
|
||||
return result;
|
||||
result = x.Buffers.CompareTo(y.Buffers);
|
||||
if (result != 0)
|
||||
return result;
|
||||
return x.AccumulatorFormat.CompareTo(y.AccumulatorFormat);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
|
||||
<PropertyGroup>
|
||||
<ProjectType>Local</ProjectType>
|
||||
|
@ -116,10 +116,7 @@
|
|||
<Name>System.Drawing</Name>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Windows.Forms">
|
||||
<Name>System.Windows.Forms</Name>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml">
|
||||
<Name>System.Xml</Name>
|
||||
<Private>False</Private>
|
||||
|
@ -132,10 +129,12 @@
|
|||
<Compile Include="DisplayDevice.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Graphics\GraphicsModeComparer.cs" />
|
||||
<Compile Include="Input\IGamePadDriver.cs" />
|
||||
<Compile Include="Input\IInputDriver2.cs" />
|
||||
<Compile Include="Input\IKeyboardDriver2.cs" />
|
||||
<Compile Include="Input\IMouseDriver2.cs" />
|
||||
<Compile Include="Platform\Windows\WinInputBase.cs" />
|
||||
<Compile Include="WindowBorder.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
|
@ -758,13 +757,13 @@
|
|||
<None Include="..\..\OpenTK.snk">
|
||||
<Link>OpenTK.snk</Link>
|
||||
</None>
|
||||
<None Include="OpenTK.dll.config">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<Compile Include="Platform\X11\X11Keyboard.cs" />
|
||||
<Compile Include="Platform\X11\X11Mouse.cs" />
|
||||
<Compile Include="Input\ButtonState.cs" />
|
||||
<Compile Include="Platform\X11\XI2Mouse.cs" />
|
||||
<EmbeddedResource Include="OpenTK.dll.config">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<PropertyGroup>
|
||||
|
|
204
Source/OpenTK/Platform/Windows/WinInputBase.cs
Normal file
204
Source/OpenTK/Platform/Windows/WinInputBase.cs
Normal file
|
@ -0,0 +1,204 @@
|
|||
#region License
|
||||
//
|
||||
// The Open Toolkit Library License
|
||||
//
|
||||
// Copyright (c) 2006 - 2010 the Open Toolkit library.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do
|
||||
// so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using OpenTK.Input;
|
||||
|
||||
namespace OpenTK.Platform.Windows
|
||||
{
|
||||
abstract class WinInputBase : IInputDriver2
|
||||
{
|
||||
#region Fields
|
||||
|
||||
readonly WindowProcedure WndProc;
|
||||
readonly Thread InputThread;
|
||||
readonly AutoResetEvent InputReady = new AutoResetEvent(false);
|
||||
|
||||
IntPtr OldWndProc;
|
||||
INativeWindow native;
|
||||
|
||||
protected INativeWindow Native { get { return native; } private set { native = value; } }
|
||||
protected WinWindowInfo Parent { get { return (WinWindowInfo)Native.WindowInfo; } }
|
||||
|
||||
static readonly IntPtr Unhandled = new IntPtr(-1);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public WinInputBase()
|
||||
{
|
||||
WndProc = WindowProcedure;
|
||||
|
||||
InputThread = new Thread(ProcessEvents);
|
||||
InputThread.IsBackground = true;
|
||||
InputThread.Start();
|
||||
|
||||
InputReady.WaitOne();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Members
|
||||
|
||||
#region ConstructMessageWindow
|
||||
|
||||
INativeWindow ConstructMessageWindow()
|
||||
{
|
||||
Debug.WriteLine("Initializing input driver.");
|
||||
Debug.Indent();
|
||||
|
||||
// Create a new message-only window to retrieve WM_INPUT messages.
|
||||
INativeWindow native = new NativeWindow();
|
||||
native.ProcessEvents();
|
||||
WinWindowInfo parent = native.WindowInfo as WinWindowInfo;
|
||||
Functions.SetParent(parent.WindowHandle, Constants.MESSAGE_ONLY);
|
||||
native.ProcessEvents();
|
||||
|
||||
Debug.Unindent();
|
||||
return native;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region ProcessEvents
|
||||
|
||||
void ProcessEvents()
|
||||
{
|
||||
Native = ConstructMessageWindow();
|
||||
CreateDrivers();
|
||||
|
||||
// Subclass the window to retrieve the events we are interested in.
|
||||
OldWndProc = Functions.SetWindowLong(Parent.WindowHandle, WndProc);
|
||||
Debug.Print("Input window attached to {0}", Parent);
|
||||
|
||||
InputReady.Set();
|
||||
|
||||
MSG msg = new MSG();
|
||||
while (Native.Exists)
|
||||
{
|
||||
Native.ProcessEvents();
|
||||
//int ret = Functions.GetMessage(ref msg, Parent.WindowHandle, 0, 0);
|
||||
//if (ret == -1)
|
||||
//{
|
||||
// throw new PlatformException(String.Format(
|
||||
// "An error happened while processing the message queue. Windows error: {0}",
|
||||
// Marshal.GetLastWin32Error()));
|
||||
//}
|
||||
|
||||
//Functions.TranslateMessage(ref msg);
|
||||
//Functions.DispatchMessage(ref msg);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WndProcHandler
|
||||
|
||||
IntPtr WndProcHandler(
|
||||
IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
IntPtr ret = WindowProcedure(handle, message, wParam, lParam);
|
||||
if (ret == Unhandled)
|
||||
return Functions.CallWindowProc(OldWndProc, handle, message, wParam, lParam);
|
||||
else
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Members
|
||||
|
||||
#region WindowProcedure
|
||||
|
||||
protected virtual IntPtr WindowProcedure(
|
||||
IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
return Unhandled;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region CreateDrivers
|
||||
|
||||
// Note: this method is called through the input thread.
|
||||
protected abstract void CreateDrivers();
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region IInputDriver2 Members
|
||||
|
||||
public abstract IMouseDriver2 MouseDriver { get; }
|
||||
public abstract IKeyboardDriver2 KeyboardDriver { get; }
|
||||
public abstract IGamePadDriver GamePadDriver { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
protected bool Disposed;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool manual)
|
||||
{
|
||||
if (!Disposed)
|
||||
{
|
||||
if (manual)
|
||||
{
|
||||
if (Native != null)
|
||||
{
|
||||
Native.Close();
|
||||
Native.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
~WinInputBase()
|
||||
{
|
||||
Debug.Print("[Warning] Resource leaked: {0}.", this);
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue