2006-10-12 00:10:29 +02:00
#region License
2006-10-10 00:35:25 +02: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 23:42:22 +02:00
using OpenTK.OpenGL.Platform ;
2006-10-12 00:10:29 +02:00
using OpenTK.OpenGL ;
2006-10-10 00:35:25 +02:00
2006-10-15 22:28:57 +02:00
namespace OpenTK.Frameworks
2006-10-10 00:35:25 +02:00
{
2006-10-15 22:28:57 +02:00
public partial class Framework : Form , IDisposable
2006-10-10 00:35:25 +02:00
{
2006-10-15 22:28:57 +02:00
#region Public properties
2006-10-10 00:35:25 +02:00
#region Context
2006-10-15 22:28:57 +02:00
2006-10-10 00:35:25 +02:00
private GLContext _context ;
public GLContext Context
{
get { return _context ; }
2006-10-15 22:28:57 +02:00
protected set { _context = value ; }
}
#endregion
#region Fullscreen property
private bool _fullscreen ;
public bool Fullscreen
{
get { return _fullscreen ; }
set { _fullscreen = Implementation . ToggleFullscreen ( _fullscreen ) ; }
}
#endregion
#region DesktopResolution property
private Size _desktop_resolution ;
public Size DesktopResolution
{
get { return _desktop_resolution ; }
protected set { _desktop_resolution = value ; }
}
#endregion
#region DesktopRefreshRate property
private float _desktop_refresh_rate ;
public float DesktopRefreshRate
{
get { return _desktop_refresh_rate ; }
protected set { _desktop_refresh_rate = value ; }
2006-10-10 00:35:25 +02:00
}
2006-10-15 22:28:57 +02:00
#endregion
2006-10-10 00:35:25 +02:00
#endregion
2006-10-15 22:28:57 +02:00
FrameworkImplementation Implementation ;
2006-10-11 23:42:22 +02:00
2006-10-10 00:35:25 +02:00
#region Constructors
2006-10-11 23:42:22 +02:00
public Framework ( )
2006-10-10 00:35:25 +02:00
{
2006-10-15 22:28:57 +02:00
Setup ( null , 640 , 480 , 8 , 8 , 8 , 8 , 16 , 0 , false ) ;
2006-10-10 00:35:25 +02:00
}
2006-10-11 23:42:22 +02:00
public Framework ( string title , int width , int height , int red , int green , int blue , int alpha , int depth , int stencil , bool fullscreen )
{
2006-10-15 22:28:57 +02:00
Setup ( title , width , height , red , green , blue , alpha , depth , stencil , fullscreen ) ;
2006-10-11 23:42:22 +02:00
}
2006-10-12 00:10:29 +02:00
2006-10-10 00:35:25 +02:00
#endregion
2006-10-15 22:28:57 +02:00
public void Setup ( string title , int width , int height , int red , int green , int blue , int alpha , int depth , int stencil , bool fullscreen )
2006-10-11 23:42:22 +02:00
{
2006-10-15 22:28:57 +02:00
// Set platform.
2006-10-11 23:42:22 +02:00
try
{
2006-10-15 22:28:57 +02:00
if ( Environment . OSVersion . Platform = = PlatformID . Win32NT | | Environment . OSVersion . Platform = = PlatformID . Win32Windows )
2006-10-11 23:42:22 +02:00
{
2006-10-15 22:28:57 +02:00
Implementation = new WindowsImplementation ( this ) ;
2006-10-11 23:42:22 +02:00
}
else if ( Environment . OSVersion . Platform = = PlatformID . Unix )
{
2006-10-15 22:28:57 +02:00
Implementation = new X11Implementation ( ) ;
2006-10-11 23:42:22 +02: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 22:28:57 +02:00
throw e ;
2006-10-11 23:42:22 +02:00
}
2006-10-15 22:28:57 +02:00
Implementation . Setup ( ) ;
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();
//}
Context = GLContext . Create ( this , red , green , blue , alpha , depth , stencil ) ;
// 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
//this.SetStyle(ControlStyles.ResizeRedraw, true); // Redraw On Resize
this . SetStyle ( ControlStyles . UserPaint , true ) ; // We'll Handle Painting Ourselves
2006-10-12 00:48:39 +02:00
2006-10-10 00:35:25 +02:00
if ( title = = null )
2006-10-12 00:48:39 +02:00
title = "OpenTK Windows application" ;
2006-10-10 00:35:25 +02:00
this . Text = title ;
this . Size = new Size ( width , height ) ;
2006-10-12 00:48:39 +02:00
2006-10-15 22:28:57 +02:00
Application . Idle + = new EventHandler ( OnIdle ) ;
2006-10-10 00:35:25 +02:00
}
2006-10-11 23:42:22 +02:00
#region Event Handlers
2006-10-10 00:35:25 +02: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 22:28:57 +02:00
protected void OnIdle ( object sender , EventArgs args )
2006-10-10 00:35:25 +02:00
{
2006-10-15 22:28:57 +02:00
while ( Implementation . IsIdle ( ) )
2006-10-11 23:42:22 +02:00
{
if ( ActiveForm ! = this )
Thread . Sleep ( 100 ) ;
OnPaint ( null ) ;
}
2006-10-10 00:35:25 +02:00
}
#endregion
2006-10-12 00:48:39 +02:00
#region IDisposable Members
void IDisposable . Dispose ( )
{
//GC.SuppressFinalize(true);
Application . Idle - = OnIdle ;
}
#endregion
2006-10-10 00:35:25 +02:00
}
}