Opentk/Source/OpenTK/Platform/SDL2/Sdl2DisplayDeviceDriver.cs
thefiddler 6d0b5eb49f Implemented SDL2 platform using sdl2-cs
This is a new platform that can be used then sdl2 is installed on the target system. SDL2 is commercially supported by Valve and provides better support for MacOS compared to our current implementation (Cocoa vs Carbon). It will also help us introduce faster support for new platforms. Existing platforms remain as a fallback and will be automatically used if sdl2 is not installed.

Please note that this is still a work in progress. The new mouse and keyboard API is not supported yet. Due to limitations of sdl2, multiple mice/keyboards are also not supported.
2013-09-27 14:41:37 +02:00

94 lines
3 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
namespace OpenTK.Platform.SDL2
{
class Sdl2DisplayDeviceDriver : DisplayDeviceBase
{
public Sdl2DisplayDeviceDriver()
{
int displays = SDL.SDL_GetNumVideoDisplays();
for (int d = 0; d < displays; d++)
{
SDL.SDL_Rect bounds;
SDL.SDL_GetDisplayBounds(d, out bounds);
SDL.SDL_DisplayMode current_mode;
SDL.SDL_GetCurrentDisplayMode(d, out current_mode);
var mode_list = new List<DisplayResolution>();
int num_modes = SDL.SDL_GetNumDisplayModes(d);
for (int m = 0; m < num_modes; m++)
{
SDL.SDL_DisplayMode sdl_mode;
SDL.SDL_GetDisplayMode(d, m, out sdl_mode);
mode_list.Add(new DisplayResolution(
bounds.x, bounds.y,
sdl_mode.w, sdl_mode.h,
TranslateFormat(sdl_mode.format),
sdl_mode.refresh_rate));
}
var current_resolution = new DisplayResolution(
bounds.x, bounds.y,
current_mode.w, current_mode.h,
TranslateFormat(current_mode.format),
current_mode.refresh_rate);
var device = new DisplayDevice(
current_resolution, d == 0, mode_list, TranslateBounds(bounds), d);
AvailableDevices.Add(device);
if (d == 0)
Primary = device;
}
}
#region Private Members
int TranslateFormat(uint format)
{
int bpp;
uint a, r, g, b;
SDL.SDL_PixelFormatEnumToMasks(format, out bpp, out r, out g, out b, out a);
return bpp;
}
Rectangle TranslateBounds(SDL.SDL_Rect rect)
{
return new Rectangle(rect.x, rect.y, rect.w, rect.h);
}
#endregion
#region DisplayDeviceBase Members
public override bool TryChangeResolution(DisplayDevice device, DisplayResolution resolution)
{
// Todo: we need a temporary window to change resolutions, most probably
Trace.WriteLine("SDL2 driver does not implement TryChangeResolution");
return true;
//SDL2.SDL_DisplayMode desired, closest;
//desired.w = resolution.Width;
//desired.h = resolution.Height;
//desired.format = SDL.SDL_PIXELFORMAT_BGRA8888;
//SDL2.SDL_GetClosestDisplayMode((int)device.Id, ref desired, out closest);
//SDL2.SDL_SetWindowDisplayMode(IntPtr.Zero, ref closest);
}
public override bool TryRestoreResolution(DisplayDevice device)
{
Trace.WriteLine("SDL2 driver does not support TryRestoreResolution");
return true;
//throw new NotImplementedException();
}
#endregion
}
}