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;
|
2013-11-07 16:18:12 +00:00
|
|
|
using OpenTK.Graphics;
|
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);
|
2013-11-22 17:25:30 +00:00
|
|
|
StringBuilder TypedText = new StringBuilder();
|
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;
|
2014-01-14 12:27:09 +00:00
|
|
|
|
2014-05-06 07:12:08 +00:00
|
|
|
MouseCursor Pencil;
|
|
|
|
|
2014-05-04 22:43:58 +00:00
|
|
|
// legacy GameWindow.Mouse.* events
|
2014-05-04 06:33:51 +00:00
|
|
|
Vector4 mousedevice_pos;
|
|
|
|
int mousedevice_buttons;
|
|
|
|
MouseState mousedevice_state;
|
|
|
|
|
2014-05-04 22:43:58 +00:00
|
|
|
// new GameWindow.Mouse* events
|
2014-05-03 14:47:03 +00:00
|
|
|
Vector4 mouse_pos;
|
|
|
|
int mouse_buttons;
|
2014-05-04 06:33:51 +00:00
|
|
|
MouseState mouse_state;
|
2014-05-03 14:47:03 +00:00
|
|
|
|
2014-05-04 22:43:58 +00:00
|
|
|
// legacy GameWindow.Keyboard.Key* events
|
|
|
|
Dictionary<Key, int> legacy_keyboard_keys = new Dictionary<Key, int>();
|
|
|
|
KeyboardState legacy_keyboard_state;
|
|
|
|
KeyModifiers legacy_keyboard_modifiers;
|
|
|
|
|
|
|
|
//new GameWindow.Key* events
|
|
|
|
Dictionary<Key, int> keyboard_keys = new Dictionary<Key, int>();
|
|
|
|
KeyboardState keyboard_state;
|
|
|
|
KeyModifiers keyboard_modifiers;
|
|
|
|
|
2014-01-14 12:27:09 +00:00
|
|
|
// time drift
|
2014-01-07 00:01:00 +00:00
|
|
|
Stopwatch watch = new Stopwatch();
|
|
|
|
double update_time, render_time;
|
2010-03-11 22:53:11 +00:00
|
|
|
|
2014-01-14 12:27:09 +00:00
|
|
|
// timing information
|
|
|
|
double timestamp;
|
|
|
|
int update_count;
|
|
|
|
int update_fps;
|
|
|
|
int render_count;
|
|
|
|
int render_fps;
|
|
|
|
|
|
|
|
// position of moving objects on screen
|
|
|
|
double variable_update_timestep_pos = -1;
|
|
|
|
double variable_refresh_timestep_pos = -1;
|
|
|
|
double fixed_update_timestep_pos = -1;
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
public GameWindowStates()
|
2013-11-11 10:44:21 +00:00
|
|
|
: base(800, 600, GraphicsMode.Default)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2009-08-17 09:32:50 +00:00
|
|
|
VSync = VSyncMode.On;
|
2010-03-11 22:53:11 +00:00
|
|
|
Keyboard.KeyRepeat = true;
|
2013-11-22 17:25:30 +00:00
|
|
|
KeyDown += KeyDownHandler;
|
2014-02-17 22:28:29 +00:00
|
|
|
KeyUp += KeyUpHandler;
|
2013-11-22 17:25:30 +00:00
|
|
|
KeyPress += KeyPressHandler;
|
2014-05-04 22:43:58 +00:00
|
|
|
Keyboard.KeyDown += KeyboardDeviceDownHandler;
|
|
|
|
Keyboard.KeyUp += KeyboardDeviceUpHandler;
|
2014-01-14 12:27:09 +00:00
|
|
|
|
2010-03-11 22:53:11 +00:00
|
|
|
MouseEnter += delegate { mouse_in_window = true; };
|
|
|
|
MouseLeave += delegate { mouse_in_window = false; };
|
2014-01-14 12:27:09 +00:00
|
|
|
|
2014-05-04 06:33:51 +00:00
|
|
|
Mouse.Move += MouseDeviceMoveHandler;
|
|
|
|
Mouse.WheelChanged += MouseDeviceWheelHandler;
|
|
|
|
Mouse.ButtonDown += MouseDeviceButtonHandler;
|
|
|
|
Mouse.ButtonUp += MouseDeviceButtonHandler;
|
|
|
|
|
|
|
|
MouseMove += MouseMoveHandler;
|
|
|
|
MouseWheel += MouseWheelHandler;
|
|
|
|
MouseDown += MouseButtonHandler;
|
|
|
|
MouseUp += MouseButtonHandler;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-04 22:43:58 +00:00
|
|
|
#region Keyboard Events
|
|
|
|
|
|
|
|
void KeyPressHandler(object sender, KeyPressEventArgs e)
|
2013-11-22 17:25:30 +00:00
|
|
|
{
|
|
|
|
if (TypedText.Length > 32)
|
|
|
|
TypedText.Remove(0, 1);
|
|
|
|
|
|
|
|
TypedText.Append(e.KeyChar);
|
|
|
|
}
|
|
|
|
|
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;
|
2014-01-07 08:12:35 +00:00
|
|
|
|
|
|
|
case Key.V:
|
|
|
|
VSync = VSync == VSyncMode.On ? VSyncMode.Off : VSyncMode.On;
|
|
|
|
break;
|
2014-01-07 21:09:52 +00:00
|
|
|
|
|
|
|
case Key.BracketLeft: TargetUpdateFrequency--; break;
|
|
|
|
case Key.BracketRight: TargetUpdateFrequency++; break;
|
|
|
|
case Key.Comma: TargetRenderFrequency--; break;
|
|
|
|
case Key.Period: TargetRenderFrequency++; break;
|
2014-05-06 07:12:08 +00:00
|
|
|
|
2014-05-10 00:12:52 +00:00
|
|
|
case Key.Enter:
|
2014-05-06 07:12:08 +00:00
|
|
|
CursorVisible = !CursorVisible;
|
|
|
|
break;
|
2014-05-10 00:12:52 +00:00
|
|
|
|
|
|
|
case Key.C:
|
|
|
|
if (Cursor == MouseCursor.Default)
|
|
|
|
Cursor = Pencil;
|
|
|
|
else if (Cursor == Pencil)
|
|
|
|
Cursor = MouseCursor.Empty;
|
|
|
|
else
|
|
|
|
Cursor = MouseCursor.Default;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Key.Space:
|
|
|
|
Point p = new Point(Width / 2, Height / 2);
|
|
|
|
p = PointToScreen(p);
|
|
|
|
OpenTK.Input.Mouse.SetPosition(p.X, p.Y);
|
|
|
|
break;
|
2009-08-17 09:32:50 +00:00
|
|
|
}
|
2014-05-04 22:43:58 +00:00
|
|
|
|
|
|
|
if (!keyboard_keys.ContainsKey(e.Key))
|
|
|
|
{
|
|
|
|
keyboard_keys.Add(e.Key, 0);
|
|
|
|
}
|
|
|
|
keyboard_keys[e.Key] = e.IsRepeat ? 1 : 0;
|
|
|
|
keyboard_modifiers = e.Modifiers;
|
|
|
|
keyboard_state = e.Keyboard;
|
2014-02-17 22:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyUpHandler(object sender, KeyboardKeyEventArgs e)
|
|
|
|
{
|
2014-05-04 22:43:58 +00:00
|
|
|
keyboard_keys.Remove(e.Key);
|
|
|
|
keyboard_modifiers = e.Modifiers;
|
|
|
|
keyboard_state = e.Keyboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardDeviceDownHandler(object sender, KeyboardKeyEventArgs e)
|
|
|
|
{
|
|
|
|
if (!legacy_keyboard_keys.ContainsKey(e.Key))
|
|
|
|
{
|
|
|
|
legacy_keyboard_keys.Add(e.Key, 0);
|
|
|
|
}
|
|
|
|
legacy_keyboard_keys[e.Key] = e.IsRepeat ? 1 : 0;
|
|
|
|
legacy_keyboard_modifiers = e.Modifiers;
|
|
|
|
legacy_keyboard_state = e.Keyboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardDeviceUpHandler(object sender, KeyboardKeyEventArgs e)
|
|
|
|
{
|
|
|
|
legacy_keyboard_keys.Remove(e.Key);
|
|
|
|
legacy_keyboard_modifiers = e.Modifiers;
|
|
|
|
legacy_keyboard_state = e.Keyboard;
|
2009-08-17 09:32:50 +00:00
|
|
|
}
|
2010-10-18 15:25:25 +00:00
|
|
|
|
2014-05-04 22:43:58 +00:00
|
|
|
#endregion
|
|
|
|
|
2014-05-04 06:33:51 +00:00
|
|
|
#region MouseDevice events
|
|
|
|
|
|
|
|
void MouseDeviceMoveHandler(object sender, MouseMoveEventArgs e)
|
|
|
|
{
|
|
|
|
mousedevice_pos.X = e.X;
|
|
|
|
mousedevice_pos.Y = e.Y;
|
|
|
|
mousedevice_pos.Z = e.Mouse.Scroll.X;
|
|
|
|
mousedevice_pos.W = e.Mouse.Scroll.Y;
|
|
|
|
mousedevice_state = e.Mouse;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseDeviceButtonHandler(object sender, MouseButtonEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.IsPressed)
|
|
|
|
{
|
|
|
|
mousedevice_buttons |= 1 << (int)e.Button;
|
2014-05-06 07:12:08 +00:00
|
|
|
Cursor = Pencil;
|
2014-05-04 06:33:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mousedevice_buttons &= ~(1 << (int)e.Button);
|
2014-05-06 07:12:08 +00:00
|
|
|
Cursor = MouseCursor.Default;
|
2014-05-04 06:33:51 +00:00
|
|
|
}
|
|
|
|
mousedevice_state = e.Mouse;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseDeviceWheelHandler(object sender, MouseWheelEventArgs e)
|
|
|
|
{
|
2014-05-09 23:52:06 +00:00
|
|
|
mousedevice_pos.Z = e.Mouse.Scroll.X;
|
|
|
|
mousedevice_pos.W = e.Mouse.Scroll.Y;
|
2014-05-04 06:33:51 +00:00
|
|
|
mousedevice_state = e.Mouse;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Mouse events
|
|
|
|
|
2010-10-18 15:25:25 +00:00
|
|
|
void MouseMoveHandler(object sender, MouseMoveEventArgs e)
|
|
|
|
{
|
2014-05-03 14:47:03 +00:00
|
|
|
mouse_pos.X = e.X;
|
|
|
|
mouse_pos.Y = e.Y;
|
2014-05-03 23:29:55 +00:00
|
|
|
mouse_pos.Z = e.Mouse.Scroll.X;
|
|
|
|
mouse_pos.W = e.Mouse.Scroll.Y;
|
2014-05-04 06:33:51 +00:00
|
|
|
mouse_state = e.Mouse;
|
2010-10-18 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MouseButtonHandler(object sender, MouseButtonEventArgs e)
|
|
|
|
{
|
2014-05-03 14:47:03 +00:00
|
|
|
if (e.IsPressed)
|
|
|
|
{
|
|
|
|
mouse_buttons |= 1 << (int)e.Button;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mouse_buttons &= ~(1 << (int)e.Button);
|
|
|
|
}
|
2014-05-04 06:33:51 +00:00
|
|
|
mouse_state = e.Mouse;
|
2014-05-03 14:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MouseWheelHandler(object sender, MouseWheelEventArgs e)
|
|
|
|
{
|
2014-05-09 23:52:06 +00:00
|
|
|
mouse_pos.Z = e.Mouse.Scroll.X;
|
|
|
|
mouse_pos.W = e.Mouse.Scroll.Y;
|
2014-05-04 06:33:51 +00:00
|
|
|
mouse_state = e.Mouse;
|
2010-10-18 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
2014-05-04 06:33:51 +00:00
|
|
|
#endregion
|
|
|
|
|
2014-05-04 22:43:58 +00:00
|
|
|
#region Private Members
|
|
|
|
|
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;
|
|
|
|
}
|
2014-01-14 12:27:09 +00:00
|
|
|
|
2014-01-06 23:48:09 +00:00
|
|
|
static float DrawString(Graphics gfx, string str, int line)
|
2010-03-11 22:53:11 +00:00
|
|
|
{
|
2014-01-06 23:48:09 +00:00
|
|
|
return DrawString(gfx, str, line, 0);
|
2010-03-11 22:53:11 +00:00
|
|
|
}
|
2009-08-17 09:32:50 +00:00
|
|
|
|
2014-01-06 23:48:09 +00:00
|
|
|
static float DrawString(Graphics gfx, string str, int line, float offset)
|
2010-11-01 07:57:21 +00:00
|
|
|
{
|
|
|
|
gfx.DrawString(str, TextFont, Brushes.White, new PointF(offset, line * TextFont.Height));
|
2014-01-06 23:48:09 +00:00
|
|
|
return offset + gfx.MeasureString(str, TextFont).Width;
|
2010-11-01 07:57:21 +00:00
|
|
|
}
|
|
|
|
|
2014-05-04 22:56:19 +00:00
|
|
|
static void KeyboardStateToString(KeyboardState state, StringBuilder sb)
|
|
|
|
{
|
|
|
|
for (int key_index = 0; key_index < (int)Key.LastKey; key_index++)
|
|
|
|
{
|
|
|
|
Key k = (Key)key_index;
|
|
|
|
if (state[k])
|
|
|
|
{
|
|
|
|
sb.Append(k);
|
|
|
|
sb.Append(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 22:28:29 +00:00
|
|
|
int DrawKeyboards(Graphics gfx, int line)
|
2010-11-01 07:57:21 +00:00
|
|
|
{
|
2014-01-06 23:48:09 +00:00
|
|
|
line++;
|
2014-05-04 22:43:58 +00:00
|
|
|
DrawString(gfx, "Keyboard:", line++);
|
2014-01-06 23:48:09 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
2010-11-01 07:57:21 +00:00
|
|
|
{
|
2014-01-06 23:48:09 +00:00
|
|
|
var state = OpenTK.Input.Keyboard.GetState(i);
|
|
|
|
if (state.IsConnected)
|
2010-11-01 07:57:21 +00:00
|
|
|
{
|
2014-01-06 23:48:09 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.Append(i);
|
|
|
|
sb.Append(": ");
|
2014-05-04 22:56:19 +00:00
|
|
|
KeyboardStateToString(state, sb);
|
2014-01-06 23:48:09 +00:00
|
|
|
DrawString(gfx, sb.ToString(), line++);
|
2010-11-01 07:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-06 23:48:09 +00:00
|
|
|
return line;
|
2010-11-01 07:57:21 +00:00
|
|
|
}
|
|
|
|
|
2014-01-06 23:48:09 +00:00
|
|
|
static int DrawMice(Graphics gfx, int line)
|
2010-11-01 07:57:21 +00:00
|
|
|
{
|
2014-01-06 23:48:09 +00:00
|
|
|
line++;
|
2014-05-09 23:30:46 +00:00
|
|
|
DrawString(gfx, String.Format("Cursor: {0}", OpenTK.Input.Mouse.GetCursorState()), line++);
|
2014-01-06 23:48:09 +00:00
|
|
|
DrawString(gfx, "Mouse:", line++);
|
|
|
|
for (int i = 0; i < 4; i++)
|
2010-11-01 07:57:21 +00:00
|
|
|
{
|
2014-01-06 23:48:09 +00:00
|
|
|
var state = OpenTK.Input.Mouse.GetState(i);
|
|
|
|
if (state.IsConnected)
|
2010-11-01 07:57:21 +00:00
|
|
|
{
|
2014-01-06 23:48:09 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
Vector3 pos = new Vector3(state.X, state.Y, state.WheelPrecise);
|
|
|
|
sb.Append(i);
|
|
|
|
sb.Append(": ");
|
|
|
|
sb.Append(pos);
|
|
|
|
for (int button_index = 0; button_index < (int)MouseButton.LastButton; button_index++)
|
|
|
|
{
|
|
|
|
MouseButton b = (MouseButton)button_index;
|
|
|
|
if (state[b])
|
|
|
|
{
|
|
|
|
sb.Append(b);
|
|
|
|
sb.Append(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DrawString(gfx, sb.ToString(), line++);
|
2010-11-01 07:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-06 23:48:09 +00:00
|
|
|
return line;
|
2010-11-01 07:57:21 +00:00
|
|
|
}
|
|
|
|
|
2014-05-04 22:43:58 +00:00
|
|
|
int DrawKeyboardDevice(Graphics gfx, int line)
|
|
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.Append("KeyboardDevice: ");
|
|
|
|
for (Key key = 0; key < Key.LastKey; key++)
|
|
|
|
{
|
|
|
|
if (Keyboard[key])
|
|
|
|
{
|
|
|
|
sb.Append(key);
|
|
|
|
sb.Append(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DrawString(gfx, sb.ToString(), line++);
|
|
|
|
|
|
|
|
sb.Remove(0, sb.Length);
|
|
|
|
sb.Append("KeyboardDevice events: [");
|
|
|
|
sb.Append(legacy_keyboard_modifiers);
|
|
|
|
sb.Append("] ");
|
|
|
|
foreach (var pair in legacy_keyboard_keys)
|
|
|
|
{
|
|
|
|
sb.Append(pair.Key);
|
|
|
|
sb.Append(":");
|
|
|
|
sb.Append(pair.Value);
|
|
|
|
sb.Append(" ");
|
|
|
|
}
|
|
|
|
DrawString(gfx, sb.ToString(), line++);
|
|
|
|
|
|
|
|
sb.Remove(0, sb.Length);
|
|
|
|
sb.Append("KeyboardDevice state: ");
|
2014-05-04 22:56:19 +00:00
|
|
|
KeyboardStateToString(legacy_keyboard_state, sb);
|
2014-05-04 22:43:58 +00:00
|
|
|
DrawString(gfx, sb.ToString(), line++);
|
|
|
|
|
|
|
|
sb.Remove(0, sb.Length);
|
|
|
|
sb.Append("Keyboard events: [");
|
|
|
|
sb.Append(keyboard_modifiers);
|
|
|
|
sb.Append("] ");
|
|
|
|
foreach (var pair in keyboard_keys)
|
|
|
|
{
|
|
|
|
sb.Append(pair.Key);
|
|
|
|
sb.Append(":");
|
|
|
|
sb.Append(pair.Value);
|
|
|
|
sb.Append(" ");
|
|
|
|
}
|
|
|
|
DrawString(gfx, sb.ToString(), line++);
|
|
|
|
|
|
|
|
sb.Remove(0, sb.Length);
|
|
|
|
sb.Append("Keyboard state: ");
|
2014-05-04 22:56:19 +00:00
|
|
|
KeyboardStateToString(keyboard_state, sb);
|
2014-05-04 22:43:58 +00:00
|
|
|
DrawString(gfx, sb.ToString(), line++);
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2014-05-03 14:47:03 +00:00
|
|
|
int DrawMouseDevice(Graphics gfx, int line)
|
|
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.Append("MouseDevice: ");
|
2014-05-04 06:33:51 +00:00
|
|
|
sb.AppendFormat("[{0}, {1}, {2:0.00}] ",
|
|
|
|
Mouse.X, Mouse.Y, Mouse.WheelPrecise);
|
2014-05-03 14:47:03 +00:00
|
|
|
for (var i = MouseButton.Left; i < MouseButton.LastButton; i++)
|
|
|
|
{
|
|
|
|
if (Mouse[i])
|
|
|
|
{
|
|
|
|
sb.Append(i);
|
|
|
|
sb.Append(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.AppendLine();
|
|
|
|
DrawString(gfx, sb.ToString(), line++);
|
|
|
|
|
|
|
|
sb.Remove(0, sb.Length);
|
2014-05-04 06:33:51 +00:00
|
|
|
sb.Append("MouseDevice events: ");
|
|
|
|
sb.AppendFormat("[{0}, {1}, {2:0.00}, {3:0.00}] ",
|
|
|
|
mousedevice_pos.X, mousedevice_pos.Y,
|
|
|
|
mousedevice_pos.Z, mousedevice_pos.W);
|
|
|
|
for (var i = MouseButton.Left; i < MouseButton.LastButton; i++)
|
|
|
|
{
|
|
|
|
if ((mousedevice_buttons & (1 << (int)i)) != 0)
|
|
|
|
{
|
|
|
|
sb.Append(i);
|
|
|
|
sb.Append(" ");
|
|
|
|
}
|
|
|
|
}
|
2014-05-03 14:47:03 +00:00
|
|
|
sb.Append(" ");
|
2014-05-04 06:33:51 +00:00
|
|
|
sb.AppendLine(mousedevice_state.ToString());
|
|
|
|
DrawString(gfx, sb.ToString(), line++);
|
|
|
|
|
|
|
|
sb.Remove(0, sb.Length);
|
|
|
|
sb.Append("Mouse events: ");
|
|
|
|
sb.AppendFormat("[{0}, {1}, {2:0.00}, {3:0.00}] ",
|
|
|
|
mouse_pos.X, mouse_pos.Y,
|
|
|
|
mouse_pos.Z, mouse_pos.W);
|
2014-05-03 14:47:03 +00:00
|
|
|
for (var i = MouseButton.Left; i < MouseButton.LastButton; i++)
|
|
|
|
{
|
|
|
|
if ((mouse_buttons & (1 << (int)i)) != 0)
|
|
|
|
{
|
|
|
|
sb.Append(i);
|
|
|
|
sb.Append(" ");
|
|
|
|
}
|
|
|
|
}
|
2014-05-04 06:33:51 +00:00
|
|
|
sb.Append(" ");
|
|
|
|
sb.AppendLine(mouse_state.ToString());
|
2014-05-03 14:47:03 +00:00
|
|
|
DrawString(gfx, sb.ToString(), line++);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2014-01-06 23:48:09 +00:00
|
|
|
static int DrawLegacyJoysticks(Graphics gfx, IList<JoystickDevice> joysticks, int line)
|
2009-08-17 09:32:50 +00:00
|
|
|
{
|
2014-01-06 23:48:09 +00:00
|
|
|
line++;
|
|
|
|
DrawString(gfx, "Legacy Joystick:", line++);
|
2013-10-04 00:37:05 +00:00
|
|
|
|
2014-01-06 23:48:09 +00:00
|
|
|
int joy_index = -1;
|
2013-10-04 00:37:05 +00:00
|
|
|
foreach (var joy in joysticks)
|
|
|
|
{
|
2014-01-06 23:48:09 +00:00
|
|
|
joy_index++;
|
|
|
|
if (!String.IsNullOrEmpty(joy.Description))
|
2013-10-04 00:37:05 +00:00
|
|
|
{
|
2014-01-06 23:48:09 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.Append(joy_index);
|
|
|
|
sb.Append(": '");
|
|
|
|
sb.Append(joy.Description);
|
|
|
|
sb.Append("' ");
|
|
|
|
|
|
|
|
for (int i = 0; i < joy.Axis.Count; i++)
|
|
|
|
{
|
|
|
|
sb.Append(joy.Axis[i]);
|
|
|
|
sb.Append(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < joy.Button.Count; i++)
|
|
|
|
{
|
|
|
|
sb.Append(joy.Button[i]);
|
|
|
|
sb.Append(" ");
|
|
|
|
}
|
|
|
|
DrawString(gfx, sb.ToString(), line++);
|
2013-10-04 00:37:05 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-23 00:29:12 +00:00
|
|
|
|
|
|
|
return line;
|
2013-10-04 00:37:05 +00:00
|
|
|
}
|
|
|
|
|
2014-05-04 22:43:58 +00:00
|
|
|
#endregion
|
|
|
|
|
2013-10-04 00:37:05 +00:00
|
|
|
protected override void OnUpdateFrame(FrameEventArgs e)
|
2013-12-23 21:00:10 +00:00
|
|
|
{
|
2014-01-07 00:01:00 +00:00
|
|
|
double clock_time = watch.Elapsed.TotalSeconds;
|
|
|
|
update_time += e.Time;
|
2014-01-14 12:27:09 +00:00
|
|
|
timestamp += e.Time;
|
|
|
|
update_count++;
|
2014-01-07 00:01:00 +00:00
|
|
|
|
2013-12-23 21:00:10 +00:00
|
|
|
using (Graphics gfx = Graphics.FromImage(TextBitmap))
|
2009-08-17 09:32:50 +00:00
|
|
|
{
|
2013-12-23 21:00:10 +00:00
|
|
|
int line = 0;
|
|
|
|
|
|
|
|
gfx.Clear(Color.Black);
|
|
|
|
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
|
|
|
|
|
2014-01-07 08:12:35 +00:00
|
|
|
// OpenGL information
|
2013-12-23 21:00:10 +00:00
|
|
|
DrawString(gfx, GL.GetString(StringName.Renderer), line++);
|
2014-01-06 23:48:09 +00:00
|
|
|
DrawString(gfx, GL.GetString(StringName.Version), line++);
|
2013-12-23 21:00:10 +00:00
|
|
|
DrawString(gfx, Context.GraphicsMode.ToString(), line++);
|
|
|
|
|
2014-01-07 08:12:35 +00:00
|
|
|
// GameWindow information
|
|
|
|
line++;
|
2014-01-06 23:48:09 +00:00
|
|
|
DrawString(gfx, "GameWindow:", line++);
|
2014-01-07 08:12:35 +00:00
|
|
|
DrawString(gfx, String.Format("[1 - 4]:[5 - 7]: WindowState.{0}:WindowBorder.{1}",
|
|
|
|
this.WindowState, this.WindowBorder), line++);
|
|
|
|
DrawString(gfx, String.Format("[V]: VSync.{0}.", VSync), line++);
|
|
|
|
DrawString(gfx, String.Format("Bounds: {0}", Bounds), line++);
|
|
|
|
DrawString(gfx, String.Format("ClientRectangle: {0}", ClientRectangle), line++);
|
2014-01-06 23:48:09 +00:00
|
|
|
DrawString(gfx, String.Format("Mouse {0} and {1}. {2}.",
|
|
|
|
mouse_in_window ? "inside" : "outside",
|
|
|
|
CursorVisible ? "visible" : "hidden",
|
|
|
|
Focused ? "Focused" : "Not focused"), line++);
|
2014-05-03 14:47:03 +00:00
|
|
|
|
2014-05-04 22:43:58 +00:00
|
|
|
line = DrawKeyboardDevice(gfx, line);
|
2014-05-03 14:47:03 +00:00
|
|
|
line = DrawMouseDevice(gfx, line);
|
2014-01-07 08:12:35 +00:00
|
|
|
|
|
|
|
// Timing information
|
|
|
|
line++;
|
|
|
|
DrawString(gfx, "Timing:", line++);
|
2014-01-14 12:27:09 +00:00
|
|
|
DrawString(gfx,
|
|
|
|
String.Format("Frequency: update {4} ({0:f2}/{1:f2}); render {5} ({2:f2}/{3:f2})",
|
|
|
|
UpdateFrequency, TargetUpdateFrequency,
|
|
|
|
RenderFrequency, TargetRenderFrequency,
|
|
|
|
update_fps, render_fps),
|
|
|
|
line++);
|
|
|
|
DrawString(gfx,
|
|
|
|
String.Format("Period: update {4:N4} ({0:f4}/{1:f4}); render {5:N4} ({2:f4}/{3:f4})",
|
|
|
|
UpdatePeriod, TargetUpdatePeriod,
|
|
|
|
RenderPeriod, TargetRenderPeriod,
|
|
|
|
1.0 / update_fps, 1.0 / render_fps),
|
|
|
|
line++);
|
2014-01-07 08:12:35 +00:00
|
|
|
DrawString(gfx, String.Format("Time: update {0:f4}; render {1:f4}",
|
2014-01-14 12:27:09 +00:00
|
|
|
UpdateTime, RenderTime), line++);
|
2014-01-07 08:12:35 +00:00
|
|
|
DrawString(gfx, String.Format("Drift: clock {0:f4}; update {1:f4}; render {2:f4}",
|
2014-01-07 00:01:00 +00:00
|
|
|
clock_time, clock_time - update_time, clock_time - render_time), line++);
|
|
|
|
DrawString(gfx, String.Format("Text: {0}", TypedText.ToString()), line++);
|
2009-08-17 09:32:50 +00:00
|
|
|
|
2014-01-14 12:27:09 +00:00
|
|
|
if (timestamp >= 1)
|
|
|
|
{
|
|
|
|
timestamp -= 1;
|
|
|
|
update_fps = update_count;
|
|
|
|
render_fps = render_count;
|
|
|
|
update_count = 0;
|
|
|
|
render_count = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-07 08:12:35 +00:00
|
|
|
// Input information
|
2014-01-06 23:48:09 +00:00
|
|
|
line = DrawKeyboards(gfx, line);
|
|
|
|
line = DrawMice(gfx, line);
|
|
|
|
line = DrawJoysticks(gfx, line);
|
|
|
|
line = DrawLegacyJoysticks(gfx, Joysticks, line);
|
|
|
|
}
|
2014-01-14 12:27:09 +00:00
|
|
|
|
|
|
|
fixed_update_timestep_pos += TargetUpdatePeriod;
|
|
|
|
variable_update_timestep_pos += e.Time;
|
|
|
|
if (fixed_update_timestep_pos >= 1)
|
|
|
|
fixed_update_timestep_pos -= 2;
|
|
|
|
if (variable_update_timestep_pos >= 1)
|
|
|
|
variable_update_timestep_pos -= 2;
|
2009-08-17 09:32:50 +00:00
|
|
|
}
|
2013-12-22 21:07:40 +00:00
|
|
|
|
2014-01-06 23:48:09 +00:00
|
|
|
int DrawJoysticks(Graphics gfx, int line)
|
2013-12-22 21:07:40 +00:00
|
|
|
{
|
2013-12-24 14:01:33 +00:00
|
|
|
line++;
|
2014-01-06 23:48:09 +00:00
|
|
|
DrawString(gfx, "GamePad:", line++);
|
2013-12-22 21:07:40 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
GamePadCapabilities caps = GamePad.GetCapabilities(i);
|
|
|
|
GamePadState state = GamePad.GetState(i);
|
2013-12-24 14:01:33 +00:00
|
|
|
if (state.IsConnected)
|
|
|
|
{
|
2014-01-05 20:32:24 +00:00
|
|
|
DrawString(gfx, String.Format("{0}: {1}", i, caps), line++);
|
2013-12-24 14:01:33 +00:00
|
|
|
DrawString(gfx, state.ToString(), line++);
|
|
|
|
}
|
|
|
|
}
|
2014-01-06 23:48:09 +00:00
|
|
|
|
2013-12-24 14:01:33 +00:00
|
|
|
line++;
|
2014-01-06 23:48:09 +00:00
|
|
|
DrawString(gfx, "Joystick:", line++);
|
2013-12-24 14:01:33 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
JoystickCapabilities caps = Joystick.GetCapabilities(i);
|
|
|
|
JoystickState state = Joystick.GetState(i);
|
|
|
|
if (state.IsConnected)
|
|
|
|
{
|
2014-01-05 20:32:24 +00:00
|
|
|
DrawString(gfx, String.Format("{0}: {1}", i, caps), line++);
|
2013-12-24 14:01:33 +00:00
|
|
|
DrawString(gfx, state.ToString(), line++);
|
|
|
|
}
|
2013-12-22 21:07:40 +00:00
|
|
|
}
|
2013-12-24 14:01:33 +00:00
|
|
|
|
2013-12-22 21:07:40 +00:00
|
|
|
return line;
|
|
|
|
}
|
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
|
|
|
{
|
2014-01-07 00:01:00 +00:00
|
|
|
watch.Start();
|
|
|
|
|
2014-05-06 07:12:08 +00:00
|
|
|
using (var bitmap = new Bitmap("Data/Textures/cursor.png"))
|
|
|
|
{
|
|
|
|
var data = bitmap.LockBits(
|
|
|
|
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
|
|
|
|
System.Drawing.Imaging.ImageLockMode.ReadOnly,
|
|
|
|
System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
|
|
|
|
|
|
|
|
Pencil = new OpenTK.MouseCursor(
|
|
|
|
2, 21, data.Width, data.Height, data.Scan0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-01-07 00:01:00 +00:00
|
|
|
render_time += e.Time;
|
2014-01-14 12:27:09 +00:00
|
|
|
render_count++;
|
2014-01-06 23:48:09 +00:00
|
|
|
|
2014-01-14 12:27:09 +00:00
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
2014-01-06 23:48:09 +00:00
|
|
|
|
2010-03-11 22:53:11 +00:00
|
|
|
if (viewport_changed)
|
|
|
|
{
|
|
|
|
viewport_changed = false;
|
|
|
|
GL.Viewport(0, 0, Width, Height);
|
|
|
|
}
|
2009-11-04 17:01:44 +00:00
|
|
|
|
2014-01-14 12:27:09 +00:00
|
|
|
DrawText();
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2014-01-14 12:27:09 +00:00
|
|
|
DrawMovingObjects();
|
|
|
|
|
|
|
|
variable_refresh_timestep_pos += e.Time;
|
|
|
|
if (variable_refresh_timestep_pos >= 1)
|
|
|
|
variable_refresh_timestep_pos -= 2;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2014-01-14 12:27:09 +00:00
|
|
|
SwapBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uploads our text Bitmap to an OpenGL texture
|
|
|
|
// and displays is to screen.
|
|
|
|
private void DrawText()
|
|
|
|
{
|
|
|
|
System.Drawing.Imaging.BitmapData data = TextBitmap.LockBits(
|
|
|
|
new System.Drawing.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);
|
|
|
|
|
|
|
|
Matrix4 text_projection = Matrix4.CreateOrthographicOffCenter(0, Width, Height, 0, -1, 1);
|
|
|
|
GL.MatrixMode(MatrixMode.Projection);
|
|
|
|
GL.LoadMatrix(ref text_projection);
|
|
|
|
GL.MatrixMode(MatrixMode.Modelview);
|
|
|
|
GL.LoadIdentity();
|
|
|
|
|
|
|
|
GL.Color4(Color4.White);
|
|
|
|
GL.Enable(EnableCap.Texture2D);
|
|
|
|
GL.Begin(PrimitiveType.Quads);
|
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);
|
|
|
|
GL.End();
|
2014-01-14 12:27:09 +00:00
|
|
|
GL.Disable(EnableCap.Texture2D);
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2014-01-14 12:27:09 +00:00
|
|
|
// Draws three moving objects, using three different timing methods:
|
|
|
|
// 1. fixed framerate based on TargetUpdatePeriod
|
|
|
|
// 2. variable framerate based on UpdateFrame e.Time
|
|
|
|
// 3. variable framerate based on RenderFrame e.Time
|
|
|
|
// If the timing implementation is correct, all three objects
|
|
|
|
// should be moving at the same speed, regardless of the current
|
|
|
|
// UpdatePeriod and RenderPeriod.
|
|
|
|
void DrawMovingObjects()
|
|
|
|
{
|
|
|
|
Matrix4 thing_projection = Matrix4.CreateOrthographic(2, 2, -1, 1);
|
|
|
|
GL.MatrixMode(MatrixMode.Projection);
|
|
|
|
GL.LoadMatrix(ref thing_projection);
|
|
|
|
|
|
|
|
GL.MatrixMode(MatrixMode.Modelview);
|
|
|
|
GL.LoadIdentity();
|
|
|
|
GL.Translate(fixed_update_timestep_pos, -0.2, 0);
|
|
|
|
GL.Color4(Color4.Red);
|
|
|
|
DrawRectangle();
|
|
|
|
|
|
|
|
GL.MatrixMode(MatrixMode.Modelview);
|
|
|
|
GL.LoadIdentity();
|
|
|
|
GL.Translate(variable_update_timestep_pos, -0.4, 0);
|
|
|
|
GL.Color4(Color4.DarkGoldenrod);
|
|
|
|
DrawRectangle();
|
|
|
|
|
|
|
|
GL.MatrixMode(MatrixMode.Modelview);
|
|
|
|
GL.LoadIdentity();
|
|
|
|
GL.Translate(variable_refresh_timestep_pos, -0.8, 0);
|
|
|
|
GL.Color4(Color4.DarkGreen);
|
|
|
|
DrawRectangle();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DrawRectangle()
|
|
|
|
{
|
|
|
|
GL.Begin(PrimitiveType.Quads);
|
|
|
|
GL.Vertex2(-0.05, -0.05);
|
|
|
|
GL.Vertex2(+0.05, -0.05);
|
|
|
|
GL.Vertex2(+0.05, +0.05);
|
|
|
|
GL.Vertex2(-0.05, +0.05);
|
|
|
|
GL.End();
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Main()
|
|
|
|
{
|
|
|
|
using (GameWindowStates ex = new GameWindowStates())
|
|
|
|
{
|
|
|
|
Utilities.SetWindowTitle(ex);
|
2014-01-07 21:09:52 +00:00
|
|
|
ex.Run(30.0);
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|