2011-01-18 11:40:49 +00:00
|
|
|
// This code was written for the OpenTK library and has been released
|
|
|
|
// to the Public Domain.
|
|
|
|
// It is provided "as is" without express or implied warranty of any kind.
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2010-10-21 13:14:36 +00:00
|
|
|
[Example("GameWindow States", ExampleCategory.OpenTK, "GameWindow", 4, Documentation = "GameWindowStates")]
|
2009-02-22 10:43:35 +00:00
|
|
|
public class GameWindowStates : GameWindow
|
|
|
|
{
|
2010-10-21 13:14:36 +00:00
|
|
|
static readonly Font TextFont = new Font(FontFamily.GenericSansSerif, 11);
|
2013-09-27 20:59:56 +00:00
|
|
|
Bitmap TextBitmap = new Bitmap(1024, 1024);
|
2009-08-17 09:32:50 +00:00
|
|
|
int texture;
|
2009-10-27 23:58:29 +00:00
|
|
|
bool mouse_in_window = false;
|
2010-03-11 22:53:11 +00:00
|
|
|
bool viewport_changed = true;
|
|
|
|
bool refresh_text = true;
|
2010-10-21 13:14:36 +00:00
|
|
|
MouseState mouse, mouse_old;
|
2010-10-28 10:37:20 +00:00
|
|
|
KeyboardState keyboard, keyboard_old;
|
2010-03-11 22:53:11 +00:00
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
public GameWindowStates()
|
|
|
|
: base(800, 600)
|
|
|
|
{
|
2009-08-17 09:32:50 +00:00
|
|
|
VSync = VSyncMode.On;
|
2010-03-11 22:53:11 +00:00
|
|
|
Keyboard.KeyRepeat = true;
|
|
|
|
Keyboard.KeyDown += KeyDownHandler;
|
|
|
|
|
|
|
|
MouseEnter += delegate { mouse_in_window = true; };
|
|
|
|
MouseLeave += delegate { mouse_in_window = false; };
|
|
|
|
|
|
|
|
Move += delegate { refresh_text = true; };
|
|
|
|
Resize += delegate { refresh_text = true; };
|
|
|
|
WindowBorderChanged += delegate { refresh_text = true; };
|
|
|
|
WindowStateChanged += delegate { refresh_text = true; };
|
2010-10-28 10:37:20 +00:00
|
|
|
FocusedChanged += delegate { refresh_text = true; };
|
2010-10-18 15:25:25 +00:00
|
|
|
Mouse.Move += MouseMoveHandler;
|
|
|
|
Mouse.ButtonDown += MouseButtonHandler;
|
2010-10-18 15:48:32 +00:00
|
|
|
Mouse.ButtonUp += MouseButtonHandler;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
2010-03-11 22:53:11 +00:00
|
|
|
void KeyDownHandler(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
|
|
|
{
|
2010-10-19 09:20:59 +00:00
|
|
|
case OpenTK.Input.Key.Escape:
|
|
|
|
if (!CursorVisible)
|
|
|
|
CursorVisible = true;
|
|
|
|
else
|
|
|
|
Exit();
|
|
|
|
break;
|
2009-08-17 09:32:50 +00:00
|
|
|
|
|
|
|
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;
|
2010-03-11 22:53:11 +00:00
|
|
|
|
|
|
|
case Key.Left: X = X - 16; break;
|
|
|
|
case Key.Right: X = X + 16; break;
|
|
|
|
case Key.Up: Y = Y - 16; break;
|
|
|
|
case Key.Down: Y = Y + 16; break;
|
|
|
|
|
|
|
|
case Key.KeypadPlus:
|
|
|
|
case Key.Plus: Size += new Size(16, 16); break;
|
|
|
|
|
|
|
|
case Key.KeypadMinus:
|
|
|
|
case Key.Minus: Size -= new Size(16, 16); break;
|
2009-08-17 09:32:50 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-18 15:25:25 +00:00
|
|
|
|
|
|
|
void MouseMoveHandler(object sender, MouseMoveEventArgs e)
|
|
|
|
{
|
|
|
|
refresh_text = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseButtonHandler(object sender, MouseButtonEventArgs e)
|
|
|
|
{
|
|
|
|
refresh_text = true;
|
|
|
|
|
2010-10-19 09:20:59 +00:00
|
|
|
if (e.Button == MouseButton.Left && e.IsPressed)
|
2010-10-18 15:25:25 +00:00
|
|
|
{
|
2010-10-19 09:20:59 +00:00
|
|
|
CursorVisible = false;
|
2010-10-18 15:25:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-11 22:53:11 +00:00
|
|
|
static int Clamp(int val, int min, int max)
|
|
|
|
{
|
|
|
|
return val > max ? max : val < min ? min : val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DrawString(Graphics gfx, string str, int line)
|
|
|
|
{
|
|
|
|
gfx.DrawString(str, TextFont, Brushes.White, new PointF(0, line * TextFont.Height));
|
|
|
|
}
|
2009-08-17 09:32:50 +00:00
|
|
|
|
2010-11-01 07:57:21 +00:00
|
|
|
static void DrawString(Graphics gfx, string str, int line, float offset)
|
|
|
|
{
|
|
|
|
gfx.DrawString(str, TextFont, Brushes.White, new PointF(offset, line * TextFont.Height));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DrawKeyboard(Graphics gfx, KeyboardState keyboard, int line)
|
|
|
|
{
|
|
|
|
const string str = "Keys pressed:";
|
|
|
|
float space = gfx.MeasureString(" ", TextFont).Width;
|
|
|
|
float offset = gfx.MeasureString(str, TextFont).Width + space;
|
|
|
|
DrawString(gfx, str, line);
|
|
|
|
for (int i = 0; i < (int)Key.LastKey; i++)
|
|
|
|
{
|
|
|
|
Key k = (Key)i;
|
|
|
|
if (keyboard[k])
|
|
|
|
{
|
|
|
|
string key = k.ToString();
|
|
|
|
DrawString(gfx, key, line, offset);
|
|
|
|
offset += gfx.MeasureString(key, TextFont).Width + space;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DrawMouse(Graphics gfx, MouseState mouse, int line)
|
|
|
|
{
|
|
|
|
const string str = "Buttons pressed:";
|
|
|
|
float space = gfx.MeasureString(" ", TextFont).Width;
|
|
|
|
float offset = gfx.MeasureString(str, TextFont).Width + space;
|
|
|
|
DrawString(gfx, str, line);
|
|
|
|
for (int i = 0; i < (int)MouseButton.LastButton; i++)
|
|
|
|
{
|
|
|
|
MouseButton b = (MouseButton)i;
|
|
|
|
if (mouse[b])
|
|
|
|
{
|
|
|
|
string button = b.ToString();
|
|
|
|
DrawString(gfx, button, line, offset);
|
|
|
|
offset += gfx.MeasureString(button, TextFont).Width + space;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-11 22:53:11 +00:00
|
|
|
protected override void OnUpdateFrame(FrameEventArgs e)
|
2009-08-17 09:32:50 +00:00
|
|
|
{
|
2010-10-21 13:14:36 +00:00
|
|
|
mouse = OpenTK.Input.Mouse.GetState();
|
|
|
|
if (mouse != mouse_old)
|
|
|
|
refresh_text = true;
|
|
|
|
mouse_old = mouse;
|
2010-10-28 10:37:20 +00:00
|
|
|
keyboard = OpenTK.Input.Keyboard.GetState();
|
|
|
|
if (keyboard != keyboard_old)
|
|
|
|
refresh_text = true;
|
2010-10-28 11:11:19 +00:00
|
|
|
keyboard_old = keyboard;
|
2010-10-21 13:14:36 +00:00
|
|
|
|
2010-03-11 22:53:11 +00:00
|
|
|
if (refresh_text)
|
2009-08-17 09:32:50 +00:00
|
|
|
{
|
2010-03-11 22:53:11 +00:00
|
|
|
refresh_text = false;
|
|
|
|
|
|
|
|
using (Graphics gfx = Graphics.FromImage(TextBitmap))
|
|
|
|
{
|
|
|
|
int line = 0;
|
|
|
|
|
2010-10-22 09:29:41 +00:00
|
|
|
gfx.Clear(Color.Black);
|
|
|
|
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
|
2010-03-11 22:53:11 +00:00
|
|
|
|
|
|
|
DrawString(gfx, String.Format("[1 - 4]: change WindowState (current: {0}).", this.WindowState), line++);
|
|
|
|
DrawString(gfx, String.Format("[5 - 7]: change WindowBorder (current: {0}).", this.WindowBorder), line++);
|
|
|
|
DrawString(gfx, String.Format("Focused: {0}.", this.Focused), line++);
|
|
|
|
DrawString(gfx, String.Format("Mouse {0} window.", mouse_in_window ? "inside" : "outside of"), line++);
|
2010-10-18 15:25:25 +00:00
|
|
|
DrawString(gfx, String.Format("Mouse visible: {0}", CursorVisible), line++);
|
2010-10-21 13:14:36 +00:00
|
|
|
DrawString(gfx, String.Format("Mouse position (absolute): {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.Wheel)), line++);
|
|
|
|
DrawString(gfx, String.Format("Mouse position (relative): {0}", new Vector3(mouse.X, mouse.Y, mouse.WheelPrecise)), line++);
|
2010-03-11 22:53:11 +00:00
|
|
|
DrawString(gfx, String.Format("Window.Bounds: {0}", Bounds), line++);
|
|
|
|
DrawString(gfx, String.Format("Window.Location: {0}, Size: {1}", Location, Size), line++);
|
|
|
|
DrawString(gfx, String.Format("Window.{{X={0}, Y={1}, Width={2}, Height={3}}}", X, Y, Width, Height), line++);
|
|
|
|
DrawString(gfx, String.Format("Window.ClientRectangle: {0}", ClientRectangle), line++);
|
2010-11-01 07:57:21 +00:00
|
|
|
DrawKeyboard(gfx, keyboard, line++);
|
|
|
|
DrawMouse(gfx, mouse, line++);
|
2010-03-11 22:53:11 +00:00
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
2009-08-17 09:32:50 +00:00
|
|
|
|
2010-03-11 22:53:11 +00:00
|
|
|
System.Drawing.Imaging.BitmapData data = TextBitmap.LockBits(
|
|
|
|
new System.Drawing.Rectangle(0, 0, TextBitmap.Width, TextBitmap.Height),
|
2009-08-17 09:32:50 +00:00
|
|
|
System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
2010-03-11 22:53:11 +00:00
|
|
|
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, TextBitmap.Width, TextBitmap.Height, PixelFormat.Bgra,
|
|
|
|
PixelType.UnsignedByte, data.Scan0);
|
2009-08-17 09:32:50 +00:00
|
|
|
TextBitmap.UnlockBits(data);
|
|
|
|
}
|
2010-03-11 22:53:11 +00:00
|
|
|
|
2009-08-17 09:32:50 +00:00
|
|
|
|
2009-10-21 13:33:00 +00:00
|
|
|
protected override void OnLoad(EventArgs e)
|
2009-08-17 09:32:50 +00:00
|
|
|
{
|
2009-11-04 17:01:44 +00:00
|
|
|
base.OnLoad(e);
|
2010-03-11 22:53:11 +00:00
|
|
|
|
2009-08-17 09:32:50 +00:00
|
|
|
GL.ClearColor(Color.MidnightBlue);
|
|
|
|
|
|
|
|
GL.Enable(EnableCap.Texture2D);
|
2010-10-21 13:14:36 +00:00
|
|
|
GL.Enable(EnableCap.Blend);
|
2010-10-22 09:29:41 +00:00
|
|
|
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcColor);
|
2009-08-17 09:32:50 +00:00
|
|
|
|
|
|
|
texture = GL.GenTexture();
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, texture);
|
2010-03-11 22:53:11 +00:00
|
|
|
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, TextBitmap.Width, TextBitmap.Height,
|
|
|
|
0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
|
2009-08-17 09:32:50 +00:00
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Nearest);
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Nearest);
|
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
|
|
|
{
|
2009-11-04 17:01:44 +00:00
|
|
|
base.OnResize(e);
|
2010-03-11 22:53:11 +00:00
|
|
|
viewport_changed = true;
|
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
|
|
|
{
|
2009-11-04 17:01:44 +00:00
|
|
|
base.OnRenderFrame(e);
|
2010-03-11 22:53:11 +00:00
|
|
|
|
|
|
|
if (viewport_changed)
|
|
|
|
{
|
|
|
|
viewport_changed = false;
|
|
|
|
|
|
|
|
GL.Viewport(0, 0, Width, Height);
|
|
|
|
|
|
|
|
Matrix4 ortho_projection = Matrix4.CreateOrthographicOffCenter(0, Width, Height, 0, -1, 1);
|
|
|
|
GL.MatrixMode(MatrixMode.Projection);
|
|
|
|
GL.LoadMatrix(ref ortho_projection);
|
|
|
|
}
|
2009-11-04 17:01:44 +00:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Main()
|
|
|
|
{
|
|
|
|
using (GameWindowStates ex = new GameWindowStates())
|
|
|
|
{
|
|
|
|
Utilities.SetWindowTitle(ex);
|
2010-03-11 22:53:11 +00:00
|
|
|
ex.Run(30.0);
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|