2009-02-22 10:43:35 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
using OpenTK;
|
2009-08-14 13:13:28 +00:00
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
|
|
|
|
|
namespace Examples.Tests
|
|
|
|
|
{
|
2009-04-21 13:33:25 +00:00
|
|
|
|
[Example("GameWindow states", ExampleCategory.OpenTK, "Test", Documentation="GameWindowStates")]
|
2009-02-22 10:43:35 +00:00
|
|
|
|
public class GameWindowStates : GameWindow
|
|
|
|
|
{
|
2009-08-17 09:32:50 +00:00
|
|
|
|
readonly Font TextFont = new Font(FontFamily.GenericSansSerif, 16);
|
|
|
|
|
readonly Bitmap TextBitmap = new Bitmap(512, 512);
|
|
|
|
|
int texture;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
public GameWindowStates()
|
|
|
|
|
: base(800, 600)
|
|
|
|
|
{
|
2009-08-17 09:32:50 +00:00
|
|
|
|
VSync = VSyncMode.On;
|
|
|
|
|
Keyboard.KeyUp += KeyUpHandler;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-08-17 09:32:50 +00:00
|
|
|
|
WindowBorderChanged += WindowBorderOrStateChangedHandler;
|
|
|
|
|
WindowStateChanged += WindowBorderOrStateChangedHandler;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-04 22:10:50 +00:00
|
|
|
|
void KeyUpHandler(object sender, KeyboardKeyEventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2009-09-04 22:10:50 +00:00
|
|
|
|
switch (e.Key)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2009-08-17 09:32:50 +00:00
|
|
|
|
case OpenTK.Input.Key.Escape: this.Exit(); break;
|
|
|
|
|
|
|
|
|
|
case Key.Number1: WindowState = WindowState.Normal; break;
|
|
|
|
|
case Key.Number2: WindowState = WindowState.Maximized; break;
|
|
|
|
|
case Key.Number3: WindowState = WindowState.Fullscreen; break;
|
|
|
|
|
case Key.Number4: WindowState = WindowState.Minimized; break;
|
|
|
|
|
|
|
|
|
|
case Key.Number5: WindowBorder = WindowBorder.Resizable; break;
|
|
|
|
|
case Key.Number6: WindowBorder = WindowBorder.Fixed; break;
|
|
|
|
|
case Key.Number7: WindowBorder = WindowBorder.Hidden; break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowBorderOrStateChangedHandler(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
using (Graphics gfx = Graphics.FromImage(TextBitmap))
|
|
|
|
|
{
|
|
|
|
|
gfx.Clear(Color.MidnightBlue);
|
|
|
|
|
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
|
|
|
|
|
|
|
|
|
|
gfx.DrawString(String.Format("[1 - 4]: change WindowState (current: {0}).", this.WindowState), TextFont, Brushes.White, new PointF(0, 0));
|
|
|
|
|
gfx.DrawString(String.Format("[5 - 7]: change WindowBorder (current: {0}).", this.WindowBorder), TextFont, Brushes.White, new PointF(0, TextFont.Height));
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
2009-08-17 09:32:50 +00:00
|
|
|
|
|
|
|
|
|
System.Drawing.Imaging.BitmapData data = TextBitmap.LockBits(new Rectangle(0, 0, TextBitmap.Width, TextBitmap.Height),
|
|
|
|
|
System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
|
|
|
|
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, TextBitmap.Width, TextBitmap.Height, PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
|
|
|
|
|
TextBitmap.UnlockBits(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnLoad(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
GL.ClearColor(Color.MidnightBlue);
|
|
|
|
|
|
|
|
|
|
GL.Enable(EnableCap.Texture2D);
|
|
|
|
|
|
|
|
|
|
texture = GL.GenTexture();
|
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, texture);
|
|
|
|
|
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, TextBitmap.Width, TextBitmap.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
|
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Nearest);
|
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Nearest);
|
|
|
|
|
|
|
|
|
|
// Make sure text is displayed when the application starts.
|
|
|
|
|
WindowBorderOrStateChangedHandler(this, EventArgs.Empty);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-02 15:49:39 +00:00
|
|
|
|
protected override void OnResize(EventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
GL.Viewport(0, 0, Width, Height);
|
2009-08-17 09:32:50 +00:00
|
|
|
|
|
|
|
|
|
Matrix4 ortho_projection = Matrix4.CreateOrthographicOffCenter(0, Width, Height, 0, -1, 1);
|
|
|
|
|
GL.MatrixMode(MatrixMode.Projection);
|
|
|
|
|
GL.LoadMatrix(ref ortho_projection);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-02 15:49:39 +00:00
|
|
|
|
protected override void OnRenderFrame(FrameEventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
|
|
|
|
|
2009-08-17 09:32:50 +00:00
|
|
|
|
GL.Begin(BeginMode.Quads);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-08-17 09:32:50 +00:00
|
|
|
|
GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
|
|
|
|
|
GL.TexCoord2(1, 0); GL.Vertex2(TextBitmap.Width, 0);
|
|
|
|
|
GL.TexCoord2(1, 1); GL.Vertex2(TextBitmap.Width, TextBitmap.Height);
|
|
|
|
|
GL.TexCoord2(0, 1); GL.Vertex2(0, TextBitmap.Height);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-08-17 09:32:50 +00:00
|
|
|
|
GL.End();
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
SwapBuffers();
|
|
|
|
|
Thread.Sleep(5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
|
|
|
|
using (GameWindowStates ex = new GameWindowStates())
|
|
|
|
|
{
|
|
|
|
|
Utilities.SetWindowTitle(ex);
|
|
|
|
|
ex.Run(20.0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|