From 73468f8fa34cf09c2117493906157eda5f09fc52 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Mon, 15 Oct 2007 11:15:34 +0000 Subject: [PATCH] Added CreateContext and DestroyContext functions. Better handling of DisplayModes. Added Mode property. --- Source/OpenTK/GLControl.cs | 91 +++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 17 deletions(-) diff --git a/Source/OpenTK/GLControl.cs b/Source/OpenTK/GLControl.cs index 7379ff6d..f179c707 100644 --- a/Source/OpenTK/GLControl.cs +++ b/Source/OpenTK/GLControl.cs @@ -23,15 +23,23 @@ namespace OpenTK { IGLContext context; IPlatformIdle idle; + DisplayMode display_mode; #region --- Constructor --- public GLControl() + : this(null) + { + } + + public GLControl(DisplayMode mode) { InitializeComponent(); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); + + this.display_mode = mode; } #endregion @@ -41,28 +49,13 @@ namespace OpenTK protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); - - // For maximum compatibility we do not set a specific depth for the DisplayMode. - // The driver is free to find the best match. - WindowInfo info = new WindowInfo(this); - if (!this.DesignMode) - { - context = new GLContext(new DisplayMode(), info); - context.CreateContext(); - idle = new PlatformIdle(info); - } - else - { - context = new DummyGLContext(); - idle = new DummyPlatformIdle(); - } + this.CreateContext(); } protected override void OnHandleDestroyed(EventArgs e) { base.OnHandleDestroyed(e); - - context.Dispose(); + this.DestroyContext(); } #endregion @@ -74,6 +67,7 @@ namespace OpenTK /// /// Gets a value indicating whether the current thread contains pending system messages. /// + [Browsable(false)] public bool IsIdle { get { return idle.IsIdle; } @@ -86,9 +80,11 @@ namespace OpenTK /// /// Gets an interface to the underlying GLContext used by this GLControl. /// + [Browsable(false)] public IGLContext Context { get { return context; } + private set { context = value; } } #endregion @@ -98,6 +94,7 @@ namespace OpenTK /// /// Gets the aspect ratio of this GLControl. /// + [Description("The aspect ratio of the client area of this GLControl.")] public float AspectRatio { get @@ -113,6 +110,7 @@ namespace OpenTK /// /// Gets or sets a value indicating whether vsync is active for this GLControl. /// + [Description("Indicates whether GLControl updates are synced to the monitor's refresh.")] public bool VSync { get @@ -130,6 +128,21 @@ namespace OpenTK #endregion + #region public DisplayMode Mode + + /// + /// Gets the DisplayMode of the GLContext attached to this GLControl. + /// + /// + /// You cannot change the DisplayMode of an existing GLContext. + /// + public DisplayMode Mode + { + get { return Context.Mode; } + } + + #endregion + #endregion #region --- Public Methods --- @@ -159,6 +172,50 @@ namespace OpenTK #endregion + #region public void CreateContext() + + /// + /// Creates a GLContext and attaches it to this GLControl. + /// + public void CreateContext() + { + if (display_mode == null) + display_mode = new DisplayMode(); + WindowInfo info = new WindowInfo(this); + + if (!this.DesignMode) + { + // Mono's implementation of Windows.Forms on X11 does not allow the context to + // have a different colordepth from the parent. To combat this, we do not set a + // specific depth for the DisplayMode - we let the driver select one instead. + display_mode.Color = new ColorMode(0); + context = new GLContext(display_mode, info); + context.CreateContext(); + idle = new PlatformIdle(info); + } + else + { + context = new DummyGLContext(display_mode); + idle = new DummyPlatformIdle(); + } + } + + #endregion + + #region public void DestroyContext() + + /// + /// Destroys the GLContext attached to this GLControl. + /// + /// Occurs when no GLContext is attached. + public void DestroyContext() + { + Context.Dispose(); + Context = null; + } + + #endregion + #endregion }