Opentk/Source/Framework/Framework.cs

173 lines
5.4 KiB
C#
Raw Normal View History

#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;
using OpenTK.OpenGL.Platform;
using OpenTK.OpenGL;
2006-10-10 00:35:25 +02:00
namespace OpenTK.Frameworks
2006-10-10 00:35:25 +02:00
{
public partial class Framework : Form, IDisposable
2006-10-10 00:35:25 +02:00
{
#region Public properties
2006-10-10 00:35:25 +02:00
#region Context
2006-10-10 00:35:25 +02:00
private GLContext _context;
public GLContext Context
{
get { return _context; }
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
}
#endregion
2006-10-10 00:35:25 +02:00
#endregion
FrameworkImplementation Implementation;
2006-10-10 00:35:25 +02:00
#region Constructors
public Framework()
2006-10-10 00:35:25 +02:00
{
Setup(null, 640, 480, 8, 8, 8, 8, 16, 0, false);
2006-10-10 00:35:25 +02:00
}
public Framework(string title, int width, int height, int red, int green, int blue, int alpha, int depth, int stencil, bool fullscreen)
{
Setup(title, width, height, red, green, blue, alpha, depth, stencil, fullscreen);
}
2006-10-10 00:35:25 +02:00
#endregion
public void Setup(string title, int width, int height, int red, int green, int blue, int alpha, int depth, int stencil, bool fullscreen)
{
// Set platform.
try
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows)
{
Implementation = new WindowsImplementation(this);
}
else if (Environment.OSVersion.Platform == PlatformID.Unix)
{
Implementation = new X11Implementation();
}
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());
throw e;
}
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-10 00:35:25 +02:00
if (title == null)
title = "OpenTK Windows application";
2006-10-10 00:35:25 +02:00
this.Text = title;
this.Size = new Size(width, height);
Application.Idle += new EventHandler(OnIdle);
2006-10-10 00:35:25 +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>
protected void OnIdle(object sender, EventArgs args)
2006-10-10 00:35:25 +02:00
{
while (Implementation.IsIdle())
{
if (ActiveForm != this)
Thread.Sleep(100);
OnPaint(null);
}
2006-10-10 00:35:25 +02:00
}
#endregion
#region IDisposable Members
void IDisposable.Dispose()
{
//GC.SuppressFinalize(true);
Application.Idle -= OnIdle;
}
#endregion
2006-10-10 00:35:25 +02:00
}
}