2013-09-27 21:07:23 +00:00
|
|
|
#region License
|
|
|
|
//
|
|
|
|
// The Open Toolkit Library License
|
|
|
|
//
|
|
|
|
// Copyright (c) 2006 - 2013 Stefanos Apostolopoulos for the Open Toolkit library.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
// so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
#endregion
|
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Drawing;
|
|
|
|
using System.Drawing.Imaging;
|
2013-09-27 21:01:46 +00:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using OpenTK;
|
2013-09-27 12:41:37 +00:00
|
|
|
using OpenTK.Input;
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.SDL2
|
|
|
|
{
|
|
|
|
class Sdl2NativeWindow : INativeWindow, IInputDriver
|
|
|
|
{
|
|
|
|
Sdl2WindowInfo window;
|
2013-09-27 21:01:46 +00:00
|
|
|
uint window_id;
|
2013-09-27 12:41:37 +00:00
|
|
|
bool is_visible;
|
|
|
|
bool is_focused;
|
|
|
|
bool is_cursor_visible = true;
|
|
|
|
bool exists;
|
|
|
|
bool disposed;
|
2013-09-27 21:01:46 +00:00
|
|
|
WindowState window_state = WindowState.Normal;
|
|
|
|
WindowState previous_window_state = WindowState.Normal;
|
|
|
|
WindowBorder window_border = WindowBorder.Resizable;
|
2013-09-27 12:41:37 +00:00
|
|
|
Icon icon;
|
2013-10-01 20:01:27 +00:00
|
|
|
string window_title;
|
2013-09-27 12:41:37 +00:00
|
|
|
|
|
|
|
KeyboardDevice keyboard = new KeyboardDevice();
|
|
|
|
MouseDevice mouse = new MouseDevice();
|
|
|
|
IList<KeyboardDevice> keyboards = new List<KeyboardDevice>(1);
|
|
|
|
IList<MouseDevice> mice = new List<MouseDevice>(1);
|
|
|
|
|
2013-09-27 21:01:46 +00:00
|
|
|
static readonly SDL.SDL_EventFilter EventFilterDelegate = FilterEvents;
|
|
|
|
|
|
|
|
static readonly Dictionary<uint, Sdl2NativeWindow> windows =
|
|
|
|
new Dictionary<uint, Sdl2NativeWindow>();
|
|
|
|
|
|
|
|
static readonly Sdl2KeyMap map = new Sdl2KeyMap();
|
|
|
|
|
|
|
|
static Sdl2NativeWindow()
|
|
|
|
{
|
|
|
|
// store the filter delegate to protect it from the GC
|
2013-09-30 10:22:25 +00:00
|
|
|
SDL.SDL_AddEventWatch(EventFilterDelegate, IntPtr.Zero);
|
2013-09-27 21:01:46 +00:00
|
|
|
}
|
2013-09-27 12:41:37 +00:00
|
|
|
|
2013-09-27 16:57:05 +00:00
|
|
|
public Sdl2NativeWindow(int x, int y, int width, int height,
|
|
|
|
string title, GameWindowFlags options, DisplayDevice device)
|
2013-09-27 12:41:37 +00:00
|
|
|
{
|
|
|
|
var bounds = device.Bounds;
|
|
|
|
var flags = TranslateFlags(options);
|
2013-09-27 16:57:05 +00:00
|
|
|
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL;
|
|
|
|
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE;
|
2013-09-27 21:01:46 +00:00
|
|
|
|
|
|
|
if ((flags & SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP) != 0 ||
|
|
|
|
(flags & SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN) != 0)
|
|
|
|
window_state = WindowState.Fullscreen;
|
|
|
|
|
2013-09-27 16:57:05 +00:00
|
|
|
IntPtr handle = SDL.SDL_CreateWindow(title, bounds.Left + x, bounds.Top + y, width, height, flags);
|
2013-09-27 12:41:37 +00:00
|
|
|
window = new Sdl2WindowInfo(handle, null);
|
2013-09-27 21:01:46 +00:00
|
|
|
window_id = SDL.SDL_GetWindowID(handle);
|
|
|
|
windows.Add(window_id, this);
|
2013-10-01 20:01:27 +00:00
|
|
|
window_title = title;
|
2013-09-27 12:41:37 +00:00
|
|
|
|
|
|
|
keyboard.Description = "Standard Windows keyboard";
|
|
|
|
keyboard.NumberOfFunctionKeys = 12;
|
|
|
|
keyboard.NumberOfKeys = 101;
|
|
|
|
keyboard.NumberOfLeds = 3;
|
|
|
|
|
|
|
|
mouse.Description = "Standard Windows mouse";
|
|
|
|
mouse.NumberOfButtons = 3;
|
|
|
|
mouse.NumberOfWheels = 1;
|
|
|
|
|
|
|
|
keyboards.Add(keyboard);
|
|
|
|
mice.Add(mouse);
|
|
|
|
|
|
|
|
exists = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#region Private Members
|
|
|
|
|
|
|
|
static SDL.SDL_WindowFlags TranslateFlags(GameWindowFlags flags)
|
|
|
|
{
|
|
|
|
switch (flags)
|
|
|
|
{
|
|
|
|
case GameWindowFlags.Fullscreen:
|
2013-09-27 16:57:05 +00:00
|
|
|
return SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP;
|
2013-09-27 12:41:37 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return (SDL.SDL_WindowFlags)0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 11:51:31 +00:00
|
|
|
static Key TranslateKey(SDL.SDL_Scancode scan)
|
2013-09-27 12:41:37 +00:00
|
|
|
{
|
|
|
|
Key result = Key.Unknown;
|
2013-09-30 11:51:31 +00:00
|
|
|
if (map.ContainsKey(scan))
|
2013-09-27 12:41:37 +00:00
|
|
|
{
|
2013-09-30 11:51:31 +00:00
|
|
|
result = map[scan];
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-09-30 11:51:31 +00:00
|
|
|
static Key TranslateKey(SDL.SDL_Keycode key)
|
|
|
|
{
|
|
|
|
SDL.SDL_Scancode scan = SDL.SDL_GetScancodeFromKey(key);
|
|
|
|
return TranslateKey(scan);
|
|
|
|
}
|
|
|
|
|
2013-09-27 21:01:46 +00:00
|
|
|
static int FilterEvents(IntPtr user_data, IntPtr e)
|
|
|
|
{
|
|
|
|
var type = (SDL.SDL_EventType)Marshal.ReadInt32(e);
|
|
|
|
if (type == SDL.SDL_EventType.SDL_WINDOWEVENT)
|
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
SDL.SDL_Event ev = *(SDL.SDL_Event*)e;
|
|
|
|
|
|
|
|
// Dispatch this event to the correct window
|
|
|
|
Sdl2NativeWindow window;
|
|
|
|
if (windows.TryGetValue(ev.window.windowID, out window))
|
|
|
|
{
|
|
|
|
window.ProcessWindowEvent(ev.window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; // event processed, drop from queue
|
|
|
|
}
|
|
|
|
return 1; // event not processed, add to queue
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
void ProcessWindowEvent(SDL.SDL_WindowEvent e)
|
|
|
|
{
|
|
|
|
switch (e.windowEvent)
|
|
|
|
{
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE:
|
|
|
|
Closed(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_ENTER:
|
|
|
|
MouseEnter(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_LEAVE:
|
|
|
|
MouseLeave(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_EXPOSED:
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED:
|
|
|
|
is_focused = true;
|
|
|
|
FocusedChanged(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST:
|
|
|
|
is_focused = false;
|
|
|
|
FocusedChanged(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_HIDDEN:
|
|
|
|
is_visible = false;
|
|
|
|
VisibleChanged(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SHOWN:
|
|
|
|
is_visible = true;
|
|
|
|
VisibleChanged(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MAXIMIZED:
|
2013-09-27 21:01:46 +00:00
|
|
|
previous_window_state = window_state;
|
|
|
|
window_state = OpenTK.WindowState.Maximized;
|
|
|
|
WindowStateChanged(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MINIMIZED:
|
2013-09-27 21:01:46 +00:00
|
|
|
previous_window_state = window_state;
|
|
|
|
window_state = OpenTK.WindowState.Minimized;
|
|
|
|
WindowStateChanged(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESTORED:
|
2013-09-27 21:01:46 +00:00
|
|
|
window_state = previous_window_state;
|
2013-09-27 12:41:37 +00:00
|
|
|
WindowStateChanged(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MOVED:
|
|
|
|
Move(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED:
|
|
|
|
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED:
|
|
|
|
Resize(this, EventArgs.Empty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Debug.Print("SDL2 unhandled event: {0}", e.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 16:57:05 +00:00
|
|
|
void DestroyWindow()
|
|
|
|
{
|
|
|
|
exists = false;
|
2013-09-27 21:01:46 +00:00
|
|
|
|
2013-10-01 20:01:27 +00:00
|
|
|
SDL.SDL_DelEventWatch(EventFilterDelegate, IntPtr.Zero);
|
2013-09-27 21:01:46 +00:00
|
|
|
if (windows.ContainsKey(window_id))
|
|
|
|
{
|
|
|
|
windows.Remove(window_id);
|
|
|
|
}
|
2013-09-27 16:57:05 +00:00
|
|
|
SDL.SDL_DestroyWindow(window.Handle);
|
2013-09-27 21:01:46 +00:00
|
|
|
|
|
|
|
window_id = 0;
|
2013-09-27 16:57:05 +00:00
|
|
|
window.Handle = IntPtr.Zero;
|
|
|
|
}
|
|
|
|
|
2013-09-27 21:01:46 +00:00
|
|
|
void GrabCursor(bool grab)
|
|
|
|
{
|
|
|
|
SDL.SDL_ShowCursor(grab ? 0 : 1);
|
|
|
|
SDL.SDL_SetWindowGrab(window.Handle,
|
|
|
|
grab ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE);
|
|
|
|
SDL.SDL_SetRelativeMouseMode(
|
|
|
|
grab ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE);
|
|
|
|
|
2013-10-01 20:01:27 +00:00
|
|
|
//if (grab)
|
|
|
|
//{
|
|
|
|
// mouse.Position = new Point(0, 0);
|
|
|
|
//}
|
2013-09-27 21:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hack to force WindowState events to be pumped
|
|
|
|
void HideShowWindowHack()
|
|
|
|
{
|
|
|
|
SDL.SDL_HideWindow(window.Handle);
|
|
|
|
SDL.SDL_PumpEvents();
|
|
|
|
SDL.SDL_ShowWindow(window.Handle);
|
|
|
|
SDL.SDL_PumpEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revert to WindowState.Normal if necessary
|
|
|
|
void RestoreWindow()
|
|
|
|
{
|
|
|
|
WindowState state = WindowState;
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case WindowState.Fullscreen:
|
|
|
|
SDL.SDL_SetWindowFullscreen(window.Handle, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WindowState.Maximized:
|
|
|
|
SDL.SDL_RestoreWindow(window.Handle);
|
|
|
|
HideShowWindowHack();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WindowState.Minimized:
|
|
|
|
SDL.SDL_RestoreWindow(window.Handle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL.SDL_PumpEvents();
|
|
|
|
|
|
|
|
window_state = WindowState.Normal;
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region INativeWindow Members
|
|
|
|
|
|
|
|
public event EventHandler<EventArgs> Move = delegate { };
|
|
|
|
public event EventHandler<EventArgs> Resize = delegate { };
|
|
|
|
public event EventHandler<System.ComponentModel.CancelEventArgs> Closing = delegate { };
|
|
|
|
public event EventHandler<EventArgs> Closed = delegate { };
|
|
|
|
public event EventHandler<EventArgs> Disposed = delegate { };
|
|
|
|
public event EventHandler<EventArgs> IconChanged = delegate { };
|
|
|
|
public event EventHandler<EventArgs> TitleChanged = delegate { };
|
|
|
|
public event EventHandler<EventArgs> VisibleChanged = delegate { };
|
|
|
|
public event EventHandler<EventArgs> FocusedChanged = delegate { };
|
|
|
|
public event EventHandler<EventArgs> WindowBorderChanged = delegate { };
|
|
|
|
public event EventHandler<EventArgs> WindowStateChanged = delegate { };
|
|
|
|
public event EventHandler<KeyboardKeyEventArgs> KeyDown = delegate { };
|
|
|
|
public event EventHandler<KeyPressEventArgs> KeyPress = delegate { };
|
|
|
|
public event EventHandler<KeyboardKeyEventArgs> KeyUp = delegate { };
|
|
|
|
public event EventHandler<EventArgs> MouseEnter = delegate { };
|
|
|
|
public event EventHandler<EventArgs> MouseLeave = delegate { };
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
{
|
|
|
|
if (Exists)
|
|
|
|
{
|
|
|
|
Debug.Print("SDL2 destroying window {0}", window.Handle);
|
|
|
|
SDL.SDL_Event e = new SDL.SDL_Event();
|
|
|
|
e.type = SDL.SDL_EventType.SDL_QUIT;
|
|
|
|
SDL.SDL_PushEvent(ref e);
|
|
|
|
SDL.SDL_PumpEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ProcessEvents()
|
|
|
|
{
|
|
|
|
SDL.SDL_Event e;
|
|
|
|
while (SDL.SDL_PollEvent(out e) != 0)
|
|
|
|
{
|
|
|
|
switch (e.type)
|
|
|
|
{
|
|
|
|
case SDL.SDL_EventType.SDL_KEYDOWN:
|
|
|
|
case SDL.SDL_EventType.SDL_KEYUP:
|
|
|
|
bool key_pressed = e.key.state == SDL.SDL_PRESSED;
|
|
|
|
var key = e.key.keysym;
|
2013-09-30 11:51:31 +00:00
|
|
|
keyboard.SetKey(TranslateKey(key.scancode), (uint)key.scancode, key_pressed);
|
2013-09-27 12:41:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN:
|
|
|
|
case SDL.SDL_EventType.SDL_MOUSEBUTTONUP:
|
|
|
|
bool button_pressed = e.button.state == SDL.SDL_PRESSED;
|
|
|
|
switch (e.button.button)
|
|
|
|
{
|
|
|
|
case (byte)SDL.SDL_BUTTON_LEFT:
|
|
|
|
mouse[MouseButton.Left] = button_pressed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (byte)SDL.SDL_BUTTON_MIDDLE:
|
|
|
|
mouse[MouseButton.Middle] = button_pressed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (byte)SDL.SDL_BUTTON_RIGHT:
|
|
|
|
mouse[MouseButton.Right] = button_pressed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (byte)SDL.SDL_BUTTON_X1:
|
|
|
|
mouse[MouseButton.Button1] = button_pressed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (byte)SDL.SDL_BUTTON_X2:
|
|
|
|
mouse[MouseButton.Button2] = button_pressed;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_EventType.SDL_MOUSEMOTION:
|
|
|
|
if (CursorVisible)
|
|
|
|
{
|
|
|
|
mouse.Position = new Point(e.motion.x, e.motion.y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-27 21:01:46 +00:00
|
|
|
mouse.Position = new Point(mouse.X + e.motion.xrel, mouse.Y + e.motion.yrel);
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_EventType.SDL_MOUSEWHEEL:
|
|
|
|
mouse.Wheel += e.wheel.y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_EventType.SDL_QUIT:
|
|
|
|
var close_args = new System.ComponentModel.CancelEventArgs();
|
|
|
|
Closing(this, close_args);
|
|
|
|
if (!close_args.Cancel)
|
|
|
|
{
|
2013-09-27 16:57:05 +00:00
|
|
|
DestroyWindow();
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL.SDL_EventType.SDL_WINDOWEVENT:
|
2013-09-27 21:01:46 +00:00
|
|
|
// do nothing (this is processed in the event filter)
|
2013-09-27 12:41:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Point PointToClient(Point point)
|
|
|
|
{
|
|
|
|
var origin = DisplayDevice.Default.Bounds.Location;
|
|
|
|
var client = Location;
|
|
|
|
return new Point(point.X + client.X - origin.X, point.Y + client.Y - origin.Y);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Point PointToScreen(Point point)
|
|
|
|
{
|
|
|
|
var origin = DisplayDevice.Default.Bounds.Location;
|
|
|
|
var client = Location;
|
|
|
|
return new Point(point.X + origin.X - client.X, point.Y + origin.Y - client.Y);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Icon Icon
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2013-09-29 21:36:28 +00:00
|
|
|
// Set the new icon, if any, or clear the current
|
|
|
|
// icon if null
|
2013-09-27 12:41:37 +00:00
|
|
|
if (value != null)
|
|
|
|
{
|
|
|
|
using (Bitmap bmp = value.ToBitmap())
|
|
|
|
{
|
2013-09-29 21:36:28 +00:00
|
|
|
// Decode the icon into a SDL surface
|
2013-09-27 12:41:37 +00:00
|
|
|
var data = bmp.LockBits(
|
|
|
|
new Rectangle(0, 0, value.Width, value.Height),
|
|
|
|
ImageLockMode.ReadWrite,
|
|
|
|
PixelFormat.Format32bppArgb);
|
|
|
|
|
2013-09-29 21:36:28 +00:00
|
|
|
IntPtr surface = SDL.SDL_CreateRGBSurfaceFrom(
|
|
|
|
data.Scan0, data.Width, data.Height, 32, data.Stride,
|
2013-09-29 21:08:51 +00:00
|
|
|
0x00ff0000u, 0x0000ff00u, 0x000000ffu, 0xff000000u);
|
2013-09-27 12:41:37 +00:00
|
|
|
|
2013-09-29 21:36:28 +00:00
|
|
|
// Update the window icon
|
|
|
|
SDL.SDL_SetWindowIcon(window.Handle, surface);
|
|
|
|
|
|
|
|
// Free the SDL surface as it is no longer needed
|
|
|
|
SDL.SDL_FreeSurface(surface);
|
2013-09-27 12:41:37 +00:00
|
|
|
bmp.UnlockBits(data);
|
|
|
|
}
|
2013-09-29 21:36:28 +00:00
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
2013-09-29 21:36:28 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Clear the window icon
|
|
|
|
SDL.SDL_SetWindowIcon(window.Handle, IntPtr.Zero);
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
icon = value;
|
|
|
|
IconChanged(this, EventArgs.Empty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Title
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return SDL.SDL_GetWindowTitle(window.Handle);
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
SDL.SDL_SetWindowTitle(window.Handle, value);
|
2013-10-01 20:01:27 +00:00
|
|
|
window_title = value;
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Focused
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return is_focused;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Visible
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return is_visible;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
SDL.SDL_ShowWindow(window.Handle);
|
|
|
|
else
|
|
|
|
SDL.SDL_HideWindow(window.Handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Exists
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return exists;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IWindowInfo WindowInfo
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public WindowState WindowState
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-09-27 21:01:46 +00:00
|
|
|
return window_state;
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2013-09-27 21:01:46 +00:00
|
|
|
if (WindowState == value)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the new WindowState
|
2013-09-27 12:41:37 +00:00
|
|
|
switch (value)
|
|
|
|
{
|
|
|
|
case WindowState.Fullscreen:
|
2013-09-27 21:01:46 +00:00
|
|
|
RestoreWindow();
|
2013-09-27 16:57:05 +00:00
|
|
|
if (SDL.SDL_SetWindowFullscreen(window.Handle, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP) < 0)
|
2013-09-27 12:41:37 +00:00
|
|
|
{
|
2013-09-27 16:57:05 +00:00
|
|
|
if (SDL.SDL_SetWindowFullscreen(window.Handle, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN) < 0)
|
|
|
|
{
|
|
|
|
Debug.Print("SDL2 failed to enter fullscreen mode: {0}", SDL.SDL_GetError());
|
|
|
|
}
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
2013-09-27 21:01:46 +00:00
|
|
|
SDL.SDL_RaiseWindow(window.Handle);
|
|
|
|
// There is no "fullscreen" message in the event loop
|
|
|
|
// so we have to mark that ourselves
|
|
|
|
window_state = WindowState.Fullscreen;
|
2013-09-27 12:41:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WindowState.Maximized:
|
2013-09-27 21:01:46 +00:00
|
|
|
RestoreWindow();
|
2013-09-27 12:41:37 +00:00
|
|
|
SDL.SDL_MaximizeWindow(window.Handle);
|
2013-09-27 21:01:46 +00:00
|
|
|
HideShowWindowHack();
|
2013-09-27 12:41:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WindowState.Minimized:
|
|
|
|
SDL.SDL_MinimizeWindow(window.Handle);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WindowState.Normal:
|
2013-09-27 21:01:46 +00:00
|
|
|
RestoreWindow();
|
2013-09-27 12:41:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-09-27 21:01:46 +00:00
|
|
|
|
|
|
|
if (!CursorVisible)
|
|
|
|
GrabCursor(true);
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public WindowBorder WindowBorder
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-09-27 21:01:46 +00:00
|
|
|
return window_border;
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2013-09-27 21:01:46 +00:00
|
|
|
if (value != WindowBorder)
|
2013-09-27 12:41:37 +00:00
|
|
|
{
|
2013-09-27 21:01:46 +00:00
|
|
|
switch (value)
|
|
|
|
{
|
|
|
|
case WindowBorder.Resizable:
|
|
|
|
SDL.SDL_SetWindowBordered(window.Handle, SDL.SDL_bool.SDL_TRUE);
|
|
|
|
window_border = WindowBorder.Resizable;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WindowBorder.Hidden:
|
|
|
|
SDL.SDL_SetWindowBordered(window.Handle, SDL.SDL_bool.SDL_FALSE);
|
|
|
|
window_border = WindowBorder.Hidden;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WindowBorder.Fixed:
|
|
|
|
Debug.WriteLine("SDL2 cannot change to fixed-size windows at runtime.");
|
|
|
|
break;
|
|
|
|
}
|
2013-09-27 12:41:37 +00:00
|
|
|
|
2013-09-27 21:01:46 +00:00
|
|
|
WindowBorderChanged(this, EventArgs.Empty);
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Rectangle Bounds
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return new Rectangle(Location, Size);
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Size = value.Size;
|
|
|
|
Location = value.Location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Point Location
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
SDL.SDL_GetWindowPosition(window.Handle, out x, out y);
|
|
|
|
return new Point(x, y);
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
SDL.SDL_SetWindowPosition(window.Handle, value.X, value.Y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Size Size
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
int w, h;
|
|
|
|
SDL.SDL_GetWindowSize(window.Handle, out w, out h);
|
|
|
|
return new Size(w, h);
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
SDL.SDL_SetWindowSize(window.Handle, value.Width, value.Height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int X
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Location.X;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Location = new Point(value, Y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Y
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Location.Y;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Location = new Point(X, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Width
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Size.Width;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Size = new Size(value, Height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Height
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Size.Height;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Size = new Size(Width, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Rectangle ClientRectangle
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
// Todo: SDL2 does not have any APIs to get
|
|
|
|
// the border size or the bounds of the window
|
|
|
|
// (everything is defined in terms of the client
|
|
|
|
// area)
|
|
|
|
return Bounds;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Bounds = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Size ClientSize
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Size = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IInputDriver InputDriver
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CursorVisible
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return is_cursor_visible;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2013-09-27 21:01:46 +00:00
|
|
|
GrabCursor(!value);
|
2013-09-27 12:41:37 +00:00
|
|
|
is_cursor_visible = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IInputDriver Members
|
|
|
|
|
|
|
|
public void Poll()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IJoystickDriver Members
|
|
|
|
|
|
|
|
public IList<JoystickDevice> Joysticks
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IMouseDriver Members
|
|
|
|
|
|
|
|
public IList<MouseDevice> Mouse
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return mice;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IKeyboardDriver Members
|
|
|
|
|
|
|
|
public IList<KeyboardDevice> Keyboard
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return keyboards;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IDisposable implementation
|
|
|
|
|
|
|
|
void Dispose(bool manual)
|
|
|
|
{
|
|
|
|
if (manual)
|
|
|
|
{
|
|
|
|
if (!disposed)
|
|
|
|
{
|
2013-09-27 16:57:05 +00:00
|
|
|
if (Exists)
|
|
|
|
{
|
|
|
|
DestroyWindow();
|
|
|
|
}
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Print("[Warning] INativeWindow leaked ({0}). Did you forget to call INativeWindow.Dispose()?", this);
|
|
|
|
}
|
|
|
|
disposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Sdl2NativeWindow()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|