- added missing ProjectData.cs

- fixed OGL window size, swap crash when window is closed
- closing the run loop when window closed
This commit is contained in:
SND\AstrorEnales_cp 2011-11-10 21:17:03 +00:00
parent 71bec46d54
commit 2961704067
4 changed files with 424 additions and 289 deletions

View File

@ -57,248 +57,252 @@ namespace ANX.Framework.Windows.GL3
/// <summary> /// <summary>
/// Native OpenGL implementation for a graphics device. /// Native OpenGL implementation for a graphics device.
/// </summary> /// </summary>
public class GraphicsDeviceWindowsGL3 : INativeGraphicsDevice public class GraphicsDeviceWindowsGL3 : INativeGraphicsDevice
{ {
#region Constants #region Constants
private const float ColorMultiplier = 1f / 255f; private const float ColorMultiplier = 1f / 255f;
#endregion #endregion
#region Private #region Private
/// <summary> /// <summary>
/// Native graphics context. /// Native graphics context.
/// </summary> /// </summary>
private GraphicsContext nativeContext; private GraphicsContext nativeContext;
/// <summary> /// <summary>
/// The OpenTK window info helper class to provide window informations /// The OpenTK window info helper class to provide window informations
/// to the graphics device. /// to the graphics device.
/// </summary> /// </summary>
private IWindowInfo nativeWindowInfo; private IWindowInfo nativeWindowInfo;
#endregion #endregion
#region Constructor #region Constructor
/// <summary> /// <summary>
/// Create a new OpenGL graphics context. /// Create a new OpenGL graphics context.
/// </summary> /// </summary>
/// <param name="presentationParameters">Parameters for the window /// <param name="presentationParameters">Parameters for the window
/// and graphics context.</param> /// and graphics context.</param>
internal GraphicsDeviceWindowsGL3( internal GraphicsDeviceWindowsGL3(
PresentationParameters presentationParameters) PresentationParameters presentationParameters)
{ {
ResetDevice(presentationParameters); ResetDevice(presentationParameters);
} }
#endregion #endregion
#region ResetDevice #region ResetDevice
/// <summary> /// <summary>
/// Reset the graphics device with the given presentation paramters. /// Reset the graphics device with the given presentation paramters.
/// If a device is currently set, then we dispose the old one. /// If a device is currently set, then we dispose the old one.
/// </summary> /// </summary>
/// <param name="presentationParameters">Parameters for the /// <param name="presentationParameters">Parameters for the
/// graphics device.</param> /// graphics device.</param>
private void ResetDevice(PresentationParameters presentationParameters) private void ResetDevice(PresentationParameters presentationParameters)
{ {
#region Validation #region Validation
if (nativeContext != null) if (nativeContext != null)
{ {
nativeContext.Dispose(); nativeContext.Dispose();
nativeContext = null; nativeContext = null;
} }
if (nativeWindowInfo != null) if (nativeWindowInfo != null)
{ {
nativeWindowInfo.Dispose(); nativeWindowInfo.Dispose();
nativeWindowInfo = null; nativeWindowInfo = null;
} }
#endregion #endregion
// OpenGL Depth Buffer Size: 0/16/24/32 // OpenGL Depth Buffer Size: 0/16/24/32
int depth = 0; int depth = 0;
int stencil = 0; int stencil = 0;
switch (presentationParameters.DepthStencilFormat) switch (presentationParameters.DepthStencilFormat)
{ {
case DepthFormat.None: case DepthFormat.None:
break; break;
case DepthFormat.Depth16: case DepthFormat.Depth16:
depth = 16; depth = 16;
break; break;
case DepthFormat.Depth24: case DepthFormat.Depth24:
depth = 24; depth = 24;
break; break;
case DepthFormat.Depth24Stencil8: case DepthFormat.Depth24Stencil8:
depth = 24; depth = 24;
stencil = 8; stencil = 8;
break; break;
} }
nativeWindowInfo = Utilities.CreateWindowsWindowInfo( nativeWindowInfo = Utilities.CreateWindowsWindowInfo(
presentationParameters.DeviceWindowHandle); presentationParameters.DeviceWindowHandle);
GraphicsMode graphicsMode = new GraphicsMode( GraphicsMode graphicsMode = new GraphicsMode(
DatatypesMapping.SurfaceToColorFormat( DatatypesMapping.SurfaceToColorFormat(
presentationParameters.BackBufferFormat), presentationParameters.BackBufferFormat),
depth, stencil, depth, stencil,
// AntiAlias Samples: 2/4/8/16/32 // AntiAlias Samples: 2/4/8/16/32
presentationParameters.MultiSampleCount); presentationParameters.MultiSampleCount);
nativeContext = new GraphicsContext(graphicsMode, nativeWindowInfo); nativeContext = new GraphicsContext(graphicsMode, nativeWindowInfo);
nativeContext.MakeCurrent(nativeWindowInfo); nativeContext.MakeCurrent(nativeWindowInfo);
nativeContext.LoadAll(); nativeContext.LoadAll();
} }
#endregion #endregion
#region SetViewport #region SetViewport
/// <summary> /// <summary>
/// Set the OpenGL viewport. /// Set the OpenGL viewport.
/// </summary> /// </summary>
/// <param name="viewport">Viewport data to set natively.</param> /// <param name="viewport">Viewport data to set natively.</param>
public void SetViewport(Viewport viewport) public void SetViewport(Viewport viewport)
{ {
GL.Viewport(viewport.X, viewport.Y, viewport.Width, viewport.Height); GL.Viewport(viewport.X, viewport.Y, viewport.Width, viewport.Height);
} }
#endregion #endregion
#region Clear #region Clear
private uint lastClearColor; private uint lastClearColor;
/// <summary> /// <summary>
/// Clear the current screen by the specified clear color. /// Clear the current screen by the specified clear color.
/// </summary> /// </summary>
/// <param name="color">Clear color.</param> /// <param name="color">Clear color.</param>
public void Clear(ref Color color) public void Clear(ref Color color)
{ {
uint newClearColor = color.PackedValue; uint newClearColor = color.PackedValue;
if (lastClearColor != newClearColor) if (lastClearColor != newClearColor)
{ {
lastClearColor = newClearColor; lastClearColor = newClearColor;
GL.ClearColor(color.R * ColorMultiplier, color.G * ColorMultiplier, GL.ClearColor(color.R * ColorMultiplier, color.G * ColorMultiplier,
color.B * ColorMultiplier, color.A * ColorMultiplier); color.B * ColorMultiplier, color.A * ColorMultiplier);
} }
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
} }
/// <summary> /// <summary>
/// Clear the current screen by the specified clear color and options. /// Clear the current screen by the specified clear color and options.
/// </summary> /// </summary>
/// <param name="options">Clear options defining which components /// <param name="options">Clear options defining which components
/// should be cleared.</param> /// should be cleared.</param>
/// <param name="color">Clear color.</param> /// <param name="color">Clear color.</param>
/// <param name="depth">Depth value.</param> /// <param name="depth">Depth value.</param>
/// <param name="stencil">Stencil value.</param> /// <param name="stencil">Stencil value.</param>
public void Clear(ClearOptions options, Vector4 color, float depth, public void Clear(ClearOptions options, Vector4 color, float depth,
int stencil) int stencil)
{ {
Color anxColor; Color anxColor;
DatatypesMapping.Convert(ref color, out anxColor); DatatypesMapping.Convert(ref color, out anxColor);
uint newClearColor = anxColor.PackedValue; uint newClearColor = anxColor.PackedValue;
if (lastClearColor != newClearColor) if (lastClearColor != newClearColor)
{ {
lastClearColor = newClearColor; lastClearColor = newClearColor;
GL.ClearColor(anxColor.R * ColorMultiplier, anxColor.G * ColorMultiplier, GL.ClearColor(anxColor.R * ColorMultiplier, anxColor.G * ColorMultiplier,
anxColor.B * ColorMultiplier, anxColor.A * ColorMultiplier); anxColor.B * ColorMultiplier, anxColor.A * ColorMultiplier);
} }
ClearBufferMask mask = (ClearBufferMask)0; ClearBufferMask mask = (ClearBufferMask)0;
if ((options | ClearOptions.Target) == options) if ((options | ClearOptions.Target) == options)
{ {
mask |= ClearBufferMask.ColorBufferBit; mask |= ClearBufferMask.ColorBufferBit;
} }
if ((options | ClearOptions.Stencil) == options) if ((options | ClearOptions.Stencil) == options)
{ {
mask |= ClearBufferMask.StencilBufferBit; mask |= ClearBufferMask.StencilBufferBit;
} }
if ((options | ClearOptions.DepthBuffer) == options) if ((options | ClearOptions.DepthBuffer) == options)
{ {
mask |= ClearBufferMask.DepthBufferBit; mask |= ClearBufferMask.DepthBufferBit;
} }
GL.ClearDepth(depth); GL.ClearDepth(depth);
GL.ClearStencil(stencil); GL.ClearStencil(stencil);
GL.Clear(mask); GL.Clear(mask);
} }
#endregion #endregion
#region Present #region Present
/// <summary> /// <summary>
/// Swap the graphics buffers. /// Swap the graphics buffers.
/// </summary> /// </summary>
public void Present() public void Present()
{ {
nativeContext.SwapBuffers(); if (WindowsGameWindow.Form != null &&
} WindowsGameWindow.Form.IsDisposed == false)
#endregion {
nativeContext.SwapBuffers();
}
}
#endregion
public void DrawIndexedPrimitives(PrimitiveType primitiveType, public void DrawIndexedPrimitives(PrimitiveType primitiveType,
int baseVertex, int minVertexIndex, int numVertices, int startIndex, int baseVertex, int minVertexIndex, int numVertices, int startIndex,
int primitiveCount) int primitiveCount)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void DrawInstancedPrimitives(PrimitiveType primitiveType, public void DrawInstancedPrimitives(PrimitiveType primitiveType,
int baseVertex, int minVertexIndex, int numVertices, int startIndex, int baseVertex, int minVertexIndex, int numVertices, int startIndex,
int primitiveCount, int instanceCount) int primitiveCount, int instanceCount)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void DrawUserIndexedPrimitives<T>(PrimitiveType primitiveType, public void DrawUserIndexedPrimitives<T>(PrimitiveType primitiveType,
T[] vertexData, int vertexOffset, int numVertices, Array indexData, T[] vertexData, int vertexOffset, int numVertices, Array indexData,
int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration, int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration,
IndexElementSize indexFormat) where T : struct, IVertexType IndexElementSize indexFormat) where T : struct, IVertexType
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void DrawUserPrimitives<T>(PrimitiveType primitiveType, public void DrawUserPrimitives<T>(PrimitiveType primitiveType,
T[] vertexData, int vertexOffset, int primitiveCount, T[] vertexData, int vertexOffset, int primitiveCount,
VertexDeclaration vertexDeclaration) where T : struct, IVertexType VertexDeclaration vertexDeclaration) where T : struct, IVertexType
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void DrawPrimitives(PrimitiveType primitiveType, int vertexOffset, public void DrawPrimitives(PrimitiveType primitiveType, int vertexOffset,
int primitiveCount) int primitiveCount)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void SetVertexBuffers(VertexBufferBinding[] vertexBuffers) public void SetVertexBuffers(VertexBufferBinding[] vertexBuffers)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void SetIndexBuffer(IndexBuffer indexBuffer) public void SetIndexBuffer(IndexBuffer indexBuffer)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void SetRenderTargets(params RenderTargetBinding[] renderTargets) public void SetRenderTargets(params RenderTargetBinding[] renderTargets)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void GetBackBufferData<T>(Rectangle? rect, T[] data, public void GetBackBufferData<T>(Rectangle? rect, T[] data,
int startIndex, int elementCount) where T : struct int startIndex, int elementCount) where T : struct
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void GetBackBufferData<T>(T[] data) where T : struct public void GetBackBufferData<T>(T[] data) where T : struct
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void GetBackBufferData<T>(T[] data, int startIndex, int elementCount) where T : struct public void GetBackBufferData<T>(T[] data, int startIndex, int elementCount) where T : struct
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void ResizeBuffers(PresentationParameters presentationParameters) public void ResizeBuffers(PresentationParameters presentationParameters)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
} }

View File

@ -53,10 +53,13 @@ namespace ANX.Framework.Windows.GL3
{ {
public class WindowsGameHost : GameHost public class WindowsGameHost : GameHost
{ {
#region Private
private Game game; private Game game;
private WindowsGameWindow gameWindow; private WindowsGameWindow gameWindow;
private bool isQuitting; private bool isQuitting;
#endregion
#region Public
public override GameWindow Window public override GameWindow Window
{ {
get get
@ -64,16 +67,15 @@ namespace ANX.Framework.Windows.GL3
return this.gameWindow; return this.gameWindow;
} }
} }
#endregion
#region Constructor
public WindowsGameHost(Game setGame) public WindowsGameHost(Game setGame)
: base(setGame) : base(setGame)
{ {
isQuitting = false; isQuitting = false;
game = setGame; game = setGame;
gameWindow = new WindowsGameWindow(); gameWindow = new WindowsGameWindow();
//Mouse.WindowHandle = gameWindow.Handle;
//TouchPanel.WindowHandle = gameWindow.Handle;
//gameWindow.IsMouseVisible = game.IsMouseVisible;
gameWindow.Activated += delegate gameWindow.Activated += delegate
{ {
OnActivated(); OnActivated();
@ -82,13 +84,17 @@ namespace ANX.Framework.Windows.GL3
{ {
OnDeactivated(); OnDeactivated();
}; };
//gameWindow.Suspend += GameWindowSuspend; WindowsGameWindow.Form.FormClosing += delegate
//gameWindow.Resume += GameWindowResume; {
isQuitting = true;
};
} }
#endregion
#region Run
public override void Run() public override void Run()
{ {
gameWindow.Form.Show(); WindowsGameWindow.Form.Show();
while (isQuitting == false) while (isQuitting == false)
{ {
Application.DoEvents(); Application.DoEvents();
@ -96,10 +102,13 @@ namespace ANX.Framework.Windows.GL3
} }
gameWindow.Close(); gameWindow.Close();
} }
#endregion
#region Exit
public override void Exit() public override void Exit()
{ {
isQuitting = true; isQuitting = true;
} }
#endregion
} }
} }

View File

@ -52,94 +52,118 @@ using OpenTK.Platform.Windows;
namespace ANX.Framework.Windows.GL3 namespace ANX.Framework.Windows.GL3
{ {
internal class WindowsGameWindow : ANX.Framework.GameWindow internal class WindowsGameWindow : ANX.Framework.GameWindow
{
#region Public
internal static Form Form
{ {
#region Public get;
public Form Form private set;
}
public override IntPtr Handle
{
get
{ {
get; return Form.Handle;
private set;
} }
}
public override IntPtr Handle public override bool IsMinimized
{
get
{ {
get return Form.WindowState == FormWindowState.Minimized;
{
return Form.Handle;
}
} }
}
public override bool IsMinimized #region AllowUserResizing
public override bool AllowUserResizing
{
get
{ {
get return Form.FormBorderStyle == FormBorderStyle.Sizable;
{
return Form.WindowState == FormWindowState.Minimized;
}
} }
#endregion set
#region Constructor
internal WindowsGameWindow()
{ {
Form = new Form() Form.FormBorderStyle = value ?
{ FormBorderStyle.Sizable :
Text = "ANX Framework", FormBorderStyle.Fixed3D;
};
} }
#endregion }
#endregion
#region Close #region ClientBounds
public void Close() public override Rectangle ClientBounds
{
get
{ {
if (Form != null) System.Drawing.Rectangle rect = Form.ClientRectangle;
{ return new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
Form.Close();
Form.Dispose();
Form = null;
}
} }
#endregion }
#endregion
public override void BeginScreenDeviceChange(bool willBeFullScreen) #region CurrentOrientation
{ public override DisplayOrientation CurrentOrientation
throw new NotImplementedException(); {
} get
{
return DisplayOrientation.Default;
}
}
#endregion
#endregion
public override void EndScreenDeviceChange(string screenDeviceName, int clientWidth, int clientHeight) #region Constructor
{ internal WindowsGameWindow()
throw new NotImplementedException(); {
} Form = new Form()
{
Text = "ANX Framework",
MaximizeBox = false,
FormBorderStyle = FormBorderStyle.Fixed3D,
ClientSize = new System.Drawing.Size(800, 600),
};
}
#endregion
protected override void SetTitle(string title) #region Close
{ public void Close()
throw new NotImplementedException(); {
} if (Form != null)
{
Form.Close();
Form.Dispose();
Form = null;
}
}
#endregion
public override bool AllowUserResizing #region SetTitle
{ protected override void SetTitle(string title)
get {
{ Form.Text = title;
throw new NotImplementedException(); }
} #endregion
set
{
throw new NotImplementedException();
}
}
public override Rectangle ClientBounds public override void BeginScreenDeviceChange(bool willBeFullScreen)
{ {
get { throw new NotImplementedException(); } throw new NotImplementedException();
} }
public override string ScreenDeviceName public override void EndScreenDeviceChange(string screenDeviceName,
{ int clientWidth, int clientHeight)
get { throw new NotImplementedException(); } {
} throw new NotImplementedException();
}
public override DisplayOrientation CurrentOrientation public override string ScreenDeviceName
{ {
get { throw new NotImplementedException(); } get
} {
} throw new NotImplementedException();
}
}
}
} }

View File

@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace XNAToANXConverter
{
public static class ProjectData
{
private const string XnaBaseName = "Microsoft.Xna.Framework";
private const string AnxBaseName = "ANX.Framework";
#region Convert
public static void Convert(string sourceFilepath, string destinationFilepath)
{
string sourceFolderPath =
sourceFilepath.Replace(Path.GetFileName(sourceFilepath), "");
string destinationFolderPath =
destinationFilepath.Replace(Path.GetFileName(destinationFilepath), "");
if (Directory.Exists(destinationFolderPath) == false)
{
Directory.CreateDirectory(destinationFolderPath);
}
XDocument doc = XDocument.Load(sourceFilepath);
XElement root = doc.Root;
foreach (XElement propertyGroup in root.Elements())
{
if (propertyGroup.Name.LocalName != "ItemGroup")
{
continue;
}
List<XElement> elements = new List<XElement>(propertyGroup.Elements());
foreach (XElement item in elements)
{
if (item.Name.LocalName == "Reference")
{
#region Process Reference
string assemblyPath = item.Attribute("Include").Value;
if (assemblyPath.Contains(XnaBaseName))
{
item.Remove();
string anxPath = assemblyPath.Replace(XnaBaseName, AnxBaseName);
if (anxPath.Contains(", Version"))
{
anxPath = anxPath.Substring(0, anxPath.IndexOf(',')) + ".dll";
}
propertyGroup.Add(new XElement("Reference",
new XAttribute("Include", anxPath)));
}
#endregion
}
else if (item.Name.LocalName == "Compile")
{
#region Process Compile
string codeFilepath = item.Attribute("Include").Value;
string absolutePath = Path.Combine(sourceFolderPath, codeFilepath);
string text = File.ReadAllText(absolutePath);
text = text.Replace(XnaBaseName, AnxBaseName);
string destCodeFolderPath = codeFilepath.Replace(
Path.GetFileName(codeFilepath), "");
destCodeFolderPath = Path.Combine(destinationFolderPath,
destCodeFolderPath);
if (Directory.Exists(destCodeFolderPath) == false)
{
Directory.CreateDirectory(destCodeFolderPath);
}
File.WriteAllText(Path.Combine(destinationFolderPath,
codeFilepath), text);
#endregion
}
else if (item.Name.LocalName == "BootstrapperPackage")
{
#region Process BootstrapperPackage
// Remove all bootstrapper tasks for XNA.
string includeName = item.Attribute("Include").Value;
if (includeName.Contains(XnaBaseName))
{
item.Remove();
}
#endregion
}
}
}
doc.Save(destinationFilepath);
}
#endregion
}
}