2006-10-11 22:10:29 +00:00
#region License
2006-10-09 22:35:25 +00:00
/ * Copyright ( c ) 2006 Stephen Apostolopoulos
* See license . txt for license info
* /
#endregion
using System ;
using System.Collections.Generic ;
using System.Text ;
using System.Windows.Forms ;
using System.Drawing ;
using System.Threading ;
using OpenTK.Platform.Windows ;
using System.Runtime.InteropServices ;
2006-10-11 21:42:22 +00:00
using OpenTK.OpenGL.Platform ;
2006-10-11 22:10:29 +00:00
using OpenTK.OpenGL ;
2006-10-09 22:35:25 +00:00
2006-10-15 20:28:57 +00:00
namespace OpenTK.Frameworks
2006-10-09 22:35:25 +00:00
{
2006-10-15 20:28:57 +00:00
public partial class Framework : Form , IDisposable
2006-10-09 22:35:25 +00:00
{
2006-10-15 20:28:57 +00:00
#region Public properties
2006-10-09 22:35:25 +00:00
#region Context
2006-10-15 20:28:57 +00:00
2006-10-09 22:35:25 +00:00
private GLContext _context ;
public GLContext Context
{
get { return _context ; }
2006-10-17 19:34:49 +00:00
private set { _context = value ; }
2006-10-15 20:28:57 +00:00
}
#endregion
#region Fullscreen property
private bool _fullscreen ;
public bool Fullscreen
{
get { return _fullscreen ; }
2006-10-15 23:31:37 +00:00
private set { _fullscreen = value ; }
}
#endregion
#region ColorDepth property
private OpenTK . OpenGL . ColorDepth _color_depth ;
public OpenTK . OpenGL . ColorDepth ColorDepth
{
get { return _color_depth ; }
2006-10-17 19:34:49 +00:00
private set { _color_depth = value ; }
2006-10-15 23:31:37 +00:00
}
#endregion
#region ZDepth property
private int _z_depth ;
public int ZDepth
{
get { return _z_depth ; }
2006-10-17 19:34:49 +00:00
private set { _z_depth = value ; }
2006-10-15 23:31:37 +00:00
}
#endregion
#region StencilDepth property
private int _stencil_depth ;
public int StencilDepth
{
get { return _stencil_depth ; }
2006-10-17 19:34:49 +00:00
private set { _stencil_depth = value ; }
2006-10-15 20:28:57 +00:00
}
#endregion
#region DesktopResolution property
private Size _desktop_resolution ;
public Size DesktopResolution
{
get { return _desktop_resolution ; }
2006-10-17 19:34:49 +00:00
private set { _desktop_resolution = value ; }
2006-10-15 20:28:57 +00:00
}
#endregion
#region DesktopRefreshRate property
private float _desktop_refresh_rate ;
public float DesktopRefreshRate
{
get { return _desktop_refresh_rate ; }
2006-10-17 19:34:49 +00:00
private set { _desktop_refresh_rate = value ; }
}
#endregion
#region DesktopColorDepth property
private OpenTK . OpenGL . ColorDepth _desktop_color_depth ;
public OpenTK . OpenGL . ColorDepth DesktopColorDepth
{
get { return _desktop_color_depth ; }
private set { _desktop_color_depth = value ; }
2006-10-09 22:35:25 +00:00
}
2006-10-15 20:28:57 +00:00
#endregion
2006-10-09 22:35:25 +00:00
#endregion
2006-10-15 20:28:57 +00:00
FrameworkImplementation Implementation ;
2006-10-11 21:42:22 +00:00
2006-10-09 22:35:25 +00:00
#region Constructors
2006-10-11 21:42:22 +00:00
public Framework ( )
2006-10-09 22:35:25 +00:00
{
2006-10-17 19:34:49 +00:00
Setup ( null , 800 , 600 , new OpenTK . OpenGL . ColorDepth ( 8 , 8 , 8 , 8 ) , 16 , 0 , false ) ;
2006-10-09 22:35:25 +00:00
}
2006-10-11 21:42:22 +00:00
2006-10-15 23:31:37 +00:00
public Framework ( string title , int width , int height , OpenTK . OpenGL . ColorDepth color , int depth , int stencil , bool fullscreen )
2006-10-11 21:42:22 +00:00
{
2006-10-15 23:31:37 +00:00
Setup ( title , width , height , color , depth , stencil , fullscreen ) ;
2006-10-11 21:42:22 +00:00
}
2006-10-11 22:10:29 +00:00
2006-10-09 22:35:25 +00:00
#endregion
2006-10-17 19:34:49 +00:00
#region Setup ( string title , int width , int height , OpenTK . OpenGL . ColorDepth color , int depth , int stencil , bool fullscreen )
private void Setup ( string title , int width , int height , OpenTK . OpenGL . ColorDepth color , int depth , int stencil , bool fullscreen )
2006-10-11 21:42:22 +00:00
{
2006-10-15 20:28:57 +00:00
// Set platform.
2006-10-11 21:42:22 +00:00
try
{
2006-10-15 20:28:57 +00:00
if ( Environment . OSVersion . Platform = = PlatformID . Win32NT | | Environment . OSVersion . Platform = = PlatformID . Win32Windows )
2006-10-11 21:42:22 +00:00
{
2006-10-15 20:28:57 +00:00
Implementation = new WindowsImplementation ( this ) ;
2006-10-11 21:42:22 +00:00
}
else if ( Environment . OSVersion . Platform = = PlatformID . Unix )
{
2006-10-17 19:34:49 +00:00
//Implementation = new X11Implementation();
throw new PlatformNotSupportedException ( "The platform on which you are trying to run this program is not currently supported. Sorry for the inconvenience." ) ;
2006-10-11 21:42:22 +00:00
}
else
{
throw new PlatformNotSupportedException ( "The platform on which you are trying to run this program is not currently supported. Sorry for the inconvenience." ) ;
}
}
catch ( Exception e )
{
MessageBox . Show ( e . ToString ( ) ) ;
2006-10-15 20:28:57 +00:00
throw e ;
2006-10-11 21:42:22 +00:00
}
2006-10-15 20:28:57 +00:00
this . HandleCreated + = new EventHandler ( Implementation . OnHandleCreated ) ;
//Type xplatui = Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms");
//if (xplatui != null)
//{
// Context = GLContext.Create(this, 8, 8, 8, 8, 16, 0);
// //Context.MakeCurrent();
//}
2006-10-15 23:31:37 +00:00
2006-10-17 19:34:49 +00:00
// Initialise components.
ColorDepth = color ;
ZDepth = depth ;
StencilDepth = stencil ;
2006-10-15 23:31:37 +00:00
Context = GLContext . Create ( this , color , depth , stencil ) ;
2006-10-15 20:28:57 +00:00
// Code taken from NeHe tutorials
this . CreateParams . Style | = ( int ) Api . WindowClassStyle . HRedraw | ( int ) Api . WindowClassStyle . VRedraw | ( int ) Api . WindowClassStyle . OwnDC ;
this . SetStyle ( ControlStyles . AllPaintingInWmPaint , true ) ; // No Need To Erase Form Background
this . SetStyle ( ControlStyles . Opaque , true ) ; // No Need To Draw Form Background
//this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // Buffer Control
2006-10-15 23:31:37 +00:00
//this.SetStyle(ControlStyles.ResizeRedraw, true); // Redraw On Resize
2006-10-15 20:28:57 +00:00
this . SetStyle ( ControlStyles . UserPaint , true ) ; // We'll Handle Painting Ourselves
2006-10-11 22:48:39 +00:00
2006-10-17 19:34:49 +00:00
Implementation . SetResolution ( width , height , color , fullscreen ) ;
2006-10-15 23:31:37 +00:00
2006-10-09 22:35:25 +00:00
if ( title = = null )
2006-10-11 22:48:39 +00:00
title = "OpenTK Windows application" ;
2006-10-09 22:35:25 +00:00
this . Text = title ;
2006-10-15 20:28:57 +00:00
Application . Idle + = new EventHandler ( OnIdle ) ;
2006-10-09 22:35:25 +00:00
}
2006-10-17 19:34:49 +00:00
#endregion
2006-10-11 21:42:22 +00:00
#region Event Handlers
2006-10-09 22:35:25 +00:00
/// <summary>
/// Called when all pending messages have been processed, this is where the application 'Main Loop' resides.
/// </summary>
/// <param name="sender">Not used.</param>
/// <param name="e">Not used.</param>
2006-10-15 20:28:57 +00:00
protected void OnIdle ( object sender , EventArgs args )
2006-10-09 22:35:25 +00:00
{
2006-10-15 20:28:57 +00:00
while ( Implementation . IsIdle ( ) )
2006-10-11 21:42:22 +00:00
{
2006-10-17 19:34:49 +00:00
//if (ActiveForm != this)
// Thread.Sleep(100);
2006-10-11 21:42:22 +00:00
OnPaint ( null ) ;
}
2006-10-09 22:35:25 +00:00
}
#endregion
2006-10-11 22:48:39 +00:00
2006-10-17 19:34:49 +00:00
#region Public member functions
/// <summary>
/// Requests mode change. The parameters are hints for the mode, which may or may not be
/// possible according to the hardware.
/// </summary>
/// <param name="width">The horizontal resolution in pixels.</param>
/// <param name="height">The vertical resolution in pixels.</param>
/// <param name="color">The color depth.</param>
/// <param name="fullscreen">Set to true to set a fullscreen mode or false to set a windowed mode.</param>
/// <returns>True if the mode set was fullscreen, false otherwise.</returns></returns>
public void SetResolution ( int width , int height , OpenTK . OpenGL . ColorDepth color , bool fullscreen )
{
Implementation . SetResolution ( width , height , color , fullscreen ) ;
}
#endregion
2006-10-11 22:48:39 +00:00
#region IDisposable Members
void IDisposable . Dispose ( )
{
//GC.SuppressFinalize(true);
Application . Idle - = OnIdle ;
}
#endregion
2006-10-09 22:35:25 +00:00
}
}