91 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			91 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using OpenTK.Windowing.Desktop;
 | |
| using OpenTK.Windowing.GraphicsLibraryFramework;
 | |
| using Quik.CommandMachine;
 | |
| using Quik.Media;
 | |
| using Quik.OpenGL;
 | |
| using Quik.PAL;
 | |
| 
 | |
| namespace Quik.OpenTK
 | |
| {
 | |
|     public class OpenTKPlatform : IQuikPlatform
 | |
|     {
 | |
|         private readonly List<OpenTKPort> _ports = new List<OpenTKPort>();
 | |
| 
 | |
|         // These shall remain a sad nop for now.
 | |
|         public string Title { get; set; }
 | |
|         public QImage Icon { get; set; }
 | |
| 
 | |
|         public event EventHandler EventRaised;
 | |
| 
 | |
|         public NativeWindowSettings DefaultSettings { get; set; } = NativeWindowSettings.Default;
 | |
| 
 | |
|         public IReadOnlyList<OpenTKPort> Ports => _ports;
 | |
| 
 | |
|         private bool IsGLInitialized = false;
 | |
| 
 | |
|         public IQuikPortHandle CreatePort()
 | |
|         {
 | |
|             NativeWindow window = new NativeWindow(DefaultSettings);
 | |
|             OpenTKPort port = new OpenTKPort(window);
 | |
|             _ports.Add(port);
 | |
| 
 | |
|             if (!IsGLInitialized)
 | |
|             {
 | |
|                 window.Context.MakeCurrent();
 | |
|                 GL.LoadBindings(GLFW.GetProcAddress);
 | |
|                 IsGLInitialized = true;
 | |
|             }
 | |
| 
 | |
|             window.Closing += (ea) =>
 | |
|             {
 | |
|                 Environment.Exit(0);
 | |
|             };
 | |
| 
 | |
|             return port;
 | |
|         }
 | |
| 
 | |
|         public void Dispose()
 | |
|         {
 | |
|             // FIXME: dispose pattern here!
 | |
| 
 | |
|             // Copy the array to prevent collection modification exceptions.
 | |
|             foreach (OpenTKPort port in _ports.ToArray())
 | |
|             {
 | |
|                 port.Dispose();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public void ProcessEvents(bool block)
 | |
|         {
 | |
|             NativeWindow.ProcessWindowEvents(block);
 | |
|         }
 | |
| 
 | |
|         public void DestroyPort(IQuikPortHandle port) => ((OpenTKPort)port).Dispose();
 | |
| 
 | |
|         public string PortGetTitle(IQuikPortHandle port) => ((OpenTKPort)port).Title;
 | |
| 
 | |
|         public void PortSetTitle(IQuikPortHandle port, string title) => ((OpenTKPort)port).Title = title;
 | |
| 
 | |
|         public QVec2 PortGetSize(IQuikPortHandle port) => ((OpenTKPort)port).Size;
 | |
| 
 | |
|         public void PortSetSize(IQuikPortHandle port, QVec2 size) => ((OpenTKPort)port).Size = size;
 | |
| 
 | |
|         public QVec2 PortGetPosition(IQuikPortHandle port) => ((OpenTKPort)port).Position;
 | |
| 
 | |
|         public void PortSetPosition(IQuikPortHandle port, QVec2 position) => ((OpenTKPort)port).Position = position;
 | |
| 
 | |
|         public bool PortIsValid(IQuikPortHandle port) => ((OpenTKPort)port).IsValid;
 | |
| 
 | |
|         public void PortSubscribeEvent(IQuikPortHandle port, EventHandler handler) => ((OpenTKPort)port).EventRaised += handler;
 | |
| 
 | |
|         public void PortUnsubscribeEvent(IQuikPortHandle port, EventHandler handler) => ((OpenTKPort)port).EventRaised -= handler;
 | |
| 
 | |
|         public void PortFocus(IQuikPortHandle port) => ((OpenTKPort)port).Focus();
 | |
| 
 | |
|         public void PortShow(IQuikPortHandle port, bool shown = true) => ((OpenTKPort)port).Show(shown);
 | |
| 
 | |
|         public void PortPaint(IQuikPortHandle port, CommandList commands) => ((OpenTKPort)port).Paint(commands);
 | |
|     }
 | |
| } |