//----------------------------------------------------------------------- //----------------------------------------------------------------------- // file="ViewPort.cs" // Project: StgSharp // AuthorGroup: Nitload Space // Copyright (c) Nitload Space. All rights reserved. // // 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. // //----------------------------------------------------------------------- //----------------------------------------------------------------------- using StgSharp.Graphics; using StgSharp.Math; using StgSharp.Threading; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Threading; namespace StgSharp.MVVM { public class ViewPort { private static ConcurrentDictionary _handleToViewPortIndex = new ConcurrentDictionary( ); private int _shouldCloseMask, _closeRequestedMask; private int _width, _height, _newWidth, _newHeight; internal ViewPort() { _newHeight = 600; _newWidth = 800; FlushSize(); Name = new StackFrame( 1, false ). GetMethod()!.DeclaringType!.Name; Monitor = IntPtr.Zero; ViewPortID = InternalIO.glfwCreateWindow( _newWidth, _newHeight, Encoding.UTF8.GetBytes( Name ), Monitor, IntPtr.Zero ); if( ViewPortID == IntPtr.Zero ) { throw new InvalidOperationException( "Unable to create viewport handle." ); } _handleToViewPortIndex.TryAdd( ViewPortID, this ); } public ViewPort(int width, int height, string name, IntPtr monitor ) { _newWidth = width; _newHeight = height; FlushSize(); Name = name; Monitor = monitor; ViewPortID = InternalIO.glfwCreateWindow( _newWidth, _newHeight, Encoding.UTF8.GetBytes( Name ), Monitor, IntPtr.Zero ); if( ViewPortID == IntPtr.Zero ) { throw new InvalidOperationException( "Unable to create viewport handle." ); } _handleToViewPortIndex.TryAdd( ViewPortID, this ); } public unsafe int Height { get { return _height; } internal set { Interlocked.Exchange( ref _height, value ); } } public unsafe int Width { get { return _width; } internal set { Interlocked .Exchange( ref _width, value ); } } public IntPtr GraphicHandle { get; internal set; } public IntPtr Monitor { get; internal set; } public IntPtr ViewPortID { get; private set; } public string Name { get; set; } [MethodImpl( MethodImplOptions.AggressiveInlining )] public static bool GetExistedViewPort( IntPtr handle, out ViewPort port ) { return _handleToViewPortIndex.TryGetValue( handle, out port ); } internal unsafe void FlushSize() { Interlocked.Exchange( ref _height, _newHeight ); Interlocked.Exchange( ref _width, _newWidth ); } internal void RequestFlushSizeInNextFrame( int width, int height ) { Interlocked.Exchange( ref _newHeight, height ); Interlocked .Exchange( ref _newWidth, width ); } } }