HLSF use long as aloloc size

This commit is contained in:
Nitload
2026-02-23 12:32:15 +08:00
parent 4bd88ccbca
commit c155cab510
552 changed files with 154266 additions and 154330 deletions
+148 -148
View File
@@ -1,148 +1,148 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="DataBinding"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.HighPerformance;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
namespace StgSharp.MVVM.ViewModel
{
#nullable enable
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate ref object BindingDataGetter();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void BindingDataSetter(ref object value);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate bool MethodLookUpDelegate(string name, out Action callback);
public abstract partial class ViewModelBase
{
public bool MethodLookup(string methodName, out Action callback)
{
MethodInfo method = GetType().GetMethod(methodName)!;
if (method == null)
{
callback = null!;
return false;
}
try
{
callback = (Action)Delegate.CreateDelegate(typeof(Action), this, methodName);
return true;
}
catch (MissingMethodException)
{
callback = null!;
return false;
}
catch (MethodAccessException)
{
callback = null!;
return false;
}
catch (Exception)
{
throw;
}
}
}
[StructLayout(LayoutKind.Explicit)]
public unsafe struct DataBindingEntry
{
[FieldOffset(0)] private IntPtr getterPtr;
[FieldOffset(8)] private IntPtr setterPtr;
[FieldOffset(0)] private M128 _mask;
public DataBindingEntry(
[NotNull] BindingDataGetter gettingCallback,
[NotNull] BindingDataSetter settingCallback)
{
getterPtr = Marshal.GetFunctionPointerForDelegate(gettingCallback);
setterPtr = Marshal.GetFunctionPointerForDelegate(settingCallback);
}
public static DataBindingEntry NullBinding
{
get => new DataBindingEntry();
}
public bool Equals(DataBindingEntry other)
{
return (_mask == other._mask);
}
public override bool Equals(object obj)
{
if (obj is not DataBindingEntry other) {
return false;
}
return Equals(other);
}
public bool ExactEqual(DataBindingEntry other)
{
return (getterPtr == other.getterPtr) && (setterPtr == other.setterPtr);
}
public override int GetHashCode()
{
return _mask.GetHashCode();
}
public ref object GetValue()
{
return ref ((delegate* unmanaged[Cdecl]<ref object>)getterPtr)();
}
public void SetValue(ref object value)
{
((delegate* unmanaged[Cdecl]<ref object, void>)setterPtr)(ref value);
}
public static bool operator !=(DataBindingEntry left, DataBindingEntry right)
{
return left._mask != right._mask;
}
public static bool operator ==(DataBindingEntry left, DataBindingEntry right)
{
return left._mask == right._mask;
}
}
#nullable restore
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="DataBinding"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.HighPerformance;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
namespace StgSharp.MVVM.ViewModel
{
#nullable enable
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate ref object BindingDataGetter();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void BindingDataSetter(ref object value);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate bool MethodLookUpDelegate(string name, out Action callback);
public abstract partial class ViewModelBase
{
public bool MethodLookup(string methodName, out Action callback)
{
MethodInfo method = GetType().GetMethod(methodName)!;
if (method == null)
{
callback = null!;
return false;
}
try
{
callback = (Action)Delegate.CreateDelegate(typeof(Action), this, methodName);
return true;
}
catch (MissingMethodException)
{
callback = null!;
return false;
}
catch (MethodAccessException)
{
callback = null!;
return false;
}
catch (Exception)
{
throw;
}
}
}
[StructLayout(LayoutKind.Explicit)]
public unsafe struct DataBindingEntry
{
[FieldOffset(0)] private IntPtr getterPtr;
[FieldOffset(8)] private IntPtr setterPtr;
[FieldOffset(0)] private M128 _mask;
public DataBindingEntry(
[NotNull] BindingDataGetter gettingCallback,
[NotNull] BindingDataSetter settingCallback)
{
getterPtr = Marshal.GetFunctionPointerForDelegate(gettingCallback);
setterPtr = Marshal.GetFunctionPointerForDelegate(settingCallback);
}
public static DataBindingEntry NullBinding
{
get => new DataBindingEntry();
}
public bool Equals(DataBindingEntry other)
{
return (_mask == other._mask);
}
public override bool Equals(object obj)
{
if (obj is not DataBindingEntry other) {
return false;
}
return Equals(other);
}
public bool ExactEqual(DataBindingEntry other)
{
return (getterPtr == other.getterPtr) && (setterPtr == other.setterPtr);
}
public override int GetHashCode()
{
return _mask.GetHashCode();
}
public ref object GetValue()
{
return ref ((delegate* unmanaged[Cdecl]<ref object>)getterPtr)();
}
public void SetValue(ref object value)
{
((delegate* unmanaged[Cdecl]<ref object, void>)setterPtr)(ref value);
}
public static bool operator !=(DataBindingEntry left, DataBindingEntry right)
{
return left._mask != right._mask;
}
public static bool operator ==(DataBindingEntry left, DataBindingEntry right)
{
return left._mask == right._mask;
}
}
#nullable restore
}
+146 -146
View File
@@ -1,146 +1,146 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ShowViewPort"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.Controls;
using StgSharp.Graphics;
using StgSharp.MVVM;
using StgSharp.Threading;
using StgSharp.Timing;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace StgSharp.MVVM.ViewModel
{
public abstract partial class ViewModelBase
{
private volatile bool _closed = true;
private ViewBase _nextView, _currentView;
protected ViewBase CurrentView
{
get => _currentView;
set => _nextView = value;
}
/// <summary>
/// CustomizeInit this form and render it on screen, and respond with operation
/// </summary>
public unsafe void UseViewModel(
string viewEntryName
)
{
CustomizeInitialize();
UseViewInNextFrame(viewEntryName);
fpsCountProvider.TimeSpanRefreshed += CountFps;
fpsCountProvider.MissTimeSpanRefreshed += CountFps;
if (_nextView != _currentView) {
_ = Interlocked.Exchange(ref _currentView, _nextView);
}
Thread renderThread = new Thread((
token
) => {
try
{
ProcessFrameWithoutEvent();
}
catch (Exception ex)
{
DefaultLog.InternalWriteLog(
$"Critical error when rendering:\n" + $"{ex}", LogType.Error);
ShouldClose = true;
throw;
}
});
renderThread.Start();
/**/
while (_closed)
{
if (_nextView != _currentView) {
_ = Interlocked.Exchange(ref _currentView, _nextView);
}
GraphicFramework.glfwPollEvents();
frameTimeProvider.WaitNextSpan();
_closed = !ShouldClose;
}
/**/
renderThread.Join();
fpsCountProvider.Dispose();
frameTimeProvider.Dispose();
}
protected void UseViewInNextFrame(
string name
)
{
if (!allView.TryGetValue(name, out ViewBase v)) {
throw new KeyNotFoundException(
$"{$"Cannot find View named {name} "}{$"in current View Model of {GetType().FullName}"}");
}
CurrentView = v;
}
private void CountFps(
object sender,
EventArgs e
)
{
Interlocked.Exchange(ref _frameSpeed, _frameCount);
Interlocked.Exchange(ref _frameCount, 0);
}
private unsafe void PrivateShow()
{
if (0 == GraphicFramework.glfwWindowShouldClose(this.viewPortDisplay.ViewPortHandle))
{
CurrentView.Use();
RenderStream.PollEvents();
}
}
private void ProcessFrameWithoutEvent()
{
frameTimeProvider.TimeSpanRefreshed += (
_,
_
) => { };
while (_closed)
{
foreach (ViewPort context in CurrentView.Render) {
context.FlushSize();
}
CurrentView.Use();
frameTimeProvider.WaitNextSpan();
Interlocked.Add(ref _frameCount, 1);
}
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ShowViewPort"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.Controls;
using StgSharp.Graphics;
using StgSharp.MVVM;
using StgSharp.Threading;
using StgSharp.Timing;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace StgSharp.MVVM.ViewModel
{
public abstract partial class ViewModelBase
{
private volatile bool _closed = true;
private ViewBase _nextView, _currentView;
protected ViewBase CurrentView
{
get => _currentView;
set => _nextView = value;
}
/// <summary>
/// CustomizeInit this form and render it on screen, and respond with operation
/// </summary>
public unsafe void UseViewModel(
string viewEntryName
)
{
CustomizeInitialize();
UseViewInNextFrame(viewEntryName);
fpsCountProvider.TimeSpanRefreshed += CountFps;
fpsCountProvider.MissTimeSpanRefreshed += CountFps;
if (_nextView != _currentView) {
_ = Interlocked.Exchange(ref _currentView, _nextView);
}
Thread renderThread = new Thread((
token
) => {
try
{
ProcessFrameWithoutEvent();
}
catch (Exception ex)
{
DefaultLog.InternalWriteLog(
$"Critical error when rendering:\n" + $"{ex}", LogType.Error);
ShouldClose = true;
throw;
}
});
renderThread.Start();
/**/
while (_closed)
{
if (_nextView != _currentView) {
_ = Interlocked.Exchange(ref _currentView, _nextView);
}
GraphicFramework.glfwPollEvents();
frameTimeProvider.WaitNextSpan();
_closed = !ShouldClose;
}
/**/
renderThread.Join();
fpsCountProvider.Dispose();
frameTimeProvider.Dispose();
}
protected void UseViewInNextFrame(
string name
)
{
if (!allView.TryGetValue(name, out ViewBase v)) {
throw new KeyNotFoundException(
$"{$"Cannot find View named {name} "}{$"in current View Model of {GetType().FullName}"}");
}
CurrentView = v;
}
private void CountFps(
object sender,
EventArgs e
)
{
Interlocked.Exchange(ref _frameSpeed, _frameCount);
Interlocked.Exchange(ref _frameCount, 0);
}
private unsafe void PrivateShow()
{
if (0 == GraphicFramework.glfwWindowShouldClose(this.viewPortDisplay.ViewPortHandle))
{
CurrentView.Use();
RenderStream.PollEvents();
}
}
private void ProcessFrameWithoutEvent()
{
frameTimeProvider.TimeSpanRefreshed += (
_,
_
) => { };
while (_closed)
{
foreach (ViewPort context in CurrentView.Render) {
context.FlushSize();
}
CurrentView.Use();
frameTimeProvider.WaitNextSpan();
Interlocked.Add(ref _frameCount, 1);
}
}
}
}
+54 -54
View File
@@ -1,54 +1,54 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ViewManage"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.Graphics.OpenGL;
using StgSharp.MVVM;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Text;
namespace StgSharp.MVVM.ViewModel
{
public partial class ViewModelBase
{
private Dictionary<string, ViewBase> allView;
public T CreateAndBindView<T>(string name, (int x, int y, int z) unitCubeSize) where T: ViewBase, new()
{
T ret = new T();
ret.SelfName = name;
ret.InternalInitialize(name, unitCubeSize, this);
allView.Add(name, ret);
return ret;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ViewManage"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.Graphics.OpenGL;
using StgSharp.MVVM;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Text;
namespace StgSharp.MVVM.ViewModel
{
public partial class ViewModelBase
{
private Dictionary<string, ViewBase> allView;
public T CreateAndBindView<T>(string name, (int x, int y, int z) unitCubeSize) where T: ViewBase, new()
{
T ret = new T();
ret.SelfName = name;
ret.InternalInitialize(name, unitCubeSize, this);
allView.Add(name, ret);
return ret;
}
}
}
+271 -271
View File
@@ -1,271 +1,271 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ViewModel"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.Common.Timing;
using StgSharp.Controlling.UsrActivity;
using StgSharp.Graphics;
using StgSharp.Graphics.OpenGL;
using StgSharp.Mathematics;
using StgSharp.Mathematics.Graphic;
using StgSharp.MVVM;
using StgSharp.MVVM.ViewModel;
using StgSharp.Threading;
using StgSharp.Timing;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace StgSharp.MVVM.ViewModel
{
public abstract partial class ViewModelBase
{
private FrameBufferSizeHandler _resizeCallback;
private int _frameCount;
private int _frameSpeed;
private ViewPort viewPortDisplay;
// internal IntPtr monitor;
internal TimeSpanProvider fpsCountProvider;
internal TimeSpanProvider frameTimeProvider;
internal Vector4 backgroundColor;
protected ViewModelBase(
int frameSpeed
)
{
viewPortDisplay = new ViewPort();
allView = [];
fpsCountProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, 1.0 / frameSpeed), TimeSpanProvider.DefaultProvider);
_frameSpeed = 0;
_frameCount = 0;
long frameLength = (FrameSpeed > 0) ? (1000L * 1000L / FrameSpeed) : 0;
frameTimeProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, 1.0 / frameSpeed), TimeSpanProvider.DefaultProvider);
SetFrameSizeCallback(DefaultFrameResizeCallback);
}
protected ViewModelBase(
double frameLength
)
{
viewPortDisplay = new ViewPort();
allView = [];
fpsCountProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, frameLength), TimeSpanProvider.DefaultProvider);
_frameSpeed = 0;
_frameCount = 0;
long _frameLength = (FrameSpeed > 0) ? ((long)(1000 * frameLength)) : 0;
frameTimeProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, frameLength), TimeSpanProvider.DefaultProvider);
SetFrameSizeCallback(DefaultFrameResizeCallback);
}
public ViewModelBase(
ViewPort vBinding,
int frameSpeed
)
{
viewPortDisplay = vBinding;
allView = [];
fpsCountProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, 1.0), TimeSpanProvider.DefaultProvider);
_frameSpeed = 0;
_frameCount = 0;
long frameLength = (frameSpeed > 0) ? (1000L * 1000L / frameSpeed) : 0;
frameTimeProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, 1.0 / frameSpeed), TimeSpanProvider.DefaultProvider);
SetFrameSizeCallback(DefaultFrameResizeCallback);
}
public ViewModelBase(
ViewPort vBinding,
double frameLength
)
{
viewPortDisplay = vBinding;
allView = [];
fpsCountProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, 1.0), TimeSpanProvider.DefaultProvider);
_frameSpeed = 0;
_frameCount = 0;
long _frameLength = (FrameSpeed > 0) ? ((long)(1000 * frameLength)) : 0;
frameTimeProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, frameLength), TimeSpanProvider.DefaultProvider);
SetFrameSizeCallback(DefaultFrameResizeCallback);
}
public DataBindingEntry this[
string name
]
{
get
{
if (this.TryGetObjectReference(name, out DataBindingEntry entry)) {
return entry;
}
throw new KeyNotFoundException();
}
}
public bool ShouldClose
{
get => GraphicFramework.glfwWindowShouldClose(viewPortDisplay.ViewPortHandle) != 0;
set => GraphicFramework.glfwSetWindowShouldClose(
viewPortDisplay.ViewPortHandle, value ? 1 : 0);
}
public int CurrentFrameCount => frameTimeProvider.CurrentSpan;
public int FrameSpeed => _frameSpeed;
/// <summary>
/// Height of this form, represent in <see cref="int" />
/// </summary>
public int Height
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => viewPortDisplay.Height;
}
/// <summary>
/// Width of this form, represent in <see cref="int" />
/// </summary>
public int Width
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => viewPortDisplay.Width;
}
public unsafe IntPtr WindowID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => viewPortDisplay.ViewPortHandle;
}
public string Name
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => viewPortDisplay.Name;
}
/// <summary>
/// Width of this form, represent in <see cref="int" />
/// </summary>
public Vec2 Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec2(Width, Height);
}
public ViewPort ViewPortBinding
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => viewPortDisplay;
}
public abstract void CustomizeInitialize();
/// <summary>
/// Set the Size limit of current form
/// </summary>
/// <param _label="minSize">
/// Minimum Size of current form. In form of a <see cref="Vec2" /> of (width,height)
/// </param>
/// <param _label="maxSize">
/// Maximum Size of current form. In form of a <see cref="Vec2" /> of (width,height)
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetSizeLimit(
Vec2 minSize,
Vec2 maxSize
)
{
ThreadHelper.MainThreadAssert();
GraphicFramework.glfwSetWindowSizeLimits(this.viewPortDisplay.ViewPortHandle,
(int)minSize.X, (int)minSize.Y, (int)maxSize.X,
(int)maxSize.Y);
}
/// <summary>
/// Set the Size limit of current form
/// </summary>
/// <param _label="minWidth">
/// Minimum width of current form
/// </param>
/// <param _label="minHeight">
/// Minimum height of current form
/// </param>
/// <param _label="maxWidth">
/// Maximum width of current form
/// </param>
/// <param _label="maxHeight">
/// Maximum height of current form
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetSizeLimit(
int minWidth,
int minHeight,
int maxWidth,
int maxHeight
)
{
ThreadHelper.MainThreadAssert();
GraphicFramework.glfwSetWindowSizeLimits(this.viewPortDisplay.ViewPortHandle, minWidth,
minHeight, maxWidth, maxHeight);
}
public abstract bool TryGetObjectReference(
string name,
out DataBindingEntry entry
);
protected unsafe void SetFrameSizeCallback(
FrameBufferSizeHandler callback
)
{
_resizeCallback = callback;
IntPtr handlerPtr = Marshal.GetFunctionPointerForDelegate(callback);
_ = GraphicFramework.glfwSetFrameBufferSizeCallback(
viewPortDisplay.ViewPortHandle, (delegate* unmanaged[Cdecl]<IntPtr, int, int, void>)handlerPtr);
}
private void DefaultFrameResizeCallback(
IntPtr windowHandle,
int width,
int height
)
{
ViewPort.GetExistedViewPort(windowHandle, out ViewPort vp);
vp.RequestFlushSizeInNextFrame(width, height);
// Console.WriteLine($"{width} {height}");
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ViewModel"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.Common.Timing;
using StgSharp.Controlling.UsrActivity;
using StgSharp.Graphics;
using StgSharp.Graphics.OpenGL;
using StgSharp.Mathematics;
using StgSharp.Mathematics.Graphic;
using StgSharp.MVVM;
using StgSharp.MVVM.ViewModel;
using StgSharp.Threading;
using StgSharp.Timing;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace StgSharp.MVVM.ViewModel
{
public abstract partial class ViewModelBase
{
private FrameBufferSizeHandler _resizeCallback;
private int _frameCount;
private int _frameSpeed;
private ViewPort viewPortDisplay;
// internal IntPtr monitor;
internal TimeSpanProvider fpsCountProvider;
internal TimeSpanProvider frameTimeProvider;
internal Vector4 backgroundColor;
protected ViewModelBase(
int frameSpeed
)
{
viewPortDisplay = new ViewPort();
allView = [];
fpsCountProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, 1.0 / frameSpeed), TimeSpanProvider.DefaultProvider);
_frameSpeed = 0;
_frameCount = 0;
long frameLength = (FrameSpeed > 0) ? (1000L * 1000L / FrameSpeed) : 0;
frameTimeProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, 1.0 / frameSpeed), TimeSpanProvider.DefaultProvider);
SetFrameSizeCallback(DefaultFrameResizeCallback);
}
protected ViewModelBase(
double frameLength
)
{
viewPortDisplay = new ViewPort();
allView = [];
fpsCountProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, frameLength), TimeSpanProvider.DefaultProvider);
_frameSpeed = 0;
_frameCount = 0;
long _frameLength = (FrameSpeed > 0) ? ((long)(1000 * frameLength)) : 0;
frameTimeProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, frameLength), TimeSpanProvider.DefaultProvider);
SetFrameSizeCallback(DefaultFrameResizeCallback);
}
public ViewModelBase(
ViewPort vBinding,
int frameSpeed
)
{
viewPortDisplay = vBinding;
allView = [];
fpsCountProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, 1.0), TimeSpanProvider.DefaultProvider);
_frameSpeed = 0;
_frameCount = 0;
long frameLength = (frameSpeed > 0) ? (1000L * 1000L / frameSpeed) : 0;
frameTimeProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, 1.0 / frameSpeed), TimeSpanProvider.DefaultProvider);
SetFrameSizeCallback(DefaultFrameResizeCallback);
}
public ViewModelBase(
ViewPort vBinding,
double frameLength
)
{
viewPortDisplay = vBinding;
allView = [];
fpsCountProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, 1.0), TimeSpanProvider.DefaultProvider);
_frameSpeed = 0;
_frameCount = 0;
long _frameLength = (FrameSpeed > 0) ? ((long)(1000 * frameLength)) : 0;
frameTimeProvider = new TimeSpanProvider(TimeSequence.CreateLinear(double.PositiveInfinity, frameLength), TimeSpanProvider.DefaultProvider);
SetFrameSizeCallback(DefaultFrameResizeCallback);
}
public DataBindingEntry this[
string name
]
{
get
{
if (this.TryGetObjectReference(name, out DataBindingEntry entry)) {
return entry;
}
throw new KeyNotFoundException();
}
}
public bool ShouldClose
{
get => GraphicFramework.glfwWindowShouldClose(viewPortDisplay.ViewPortHandle) != 0;
set => GraphicFramework.glfwSetWindowShouldClose(
viewPortDisplay.ViewPortHandle, value ? 1 : 0);
}
public int CurrentFrameCount => frameTimeProvider.CurrentSpan;
public int FrameSpeed => _frameSpeed;
/// <summary>
/// Height of this form, represent in <see cref="int" />
/// </summary>
public int Height
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => viewPortDisplay.Height;
}
/// <summary>
/// Width of this form, represent in <see cref="int" />
/// </summary>
public int Width
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => viewPortDisplay.Width;
}
public unsafe IntPtr WindowID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => viewPortDisplay.ViewPortHandle;
}
public string Name
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => viewPortDisplay.Name;
}
/// <summary>
/// Width of this form, represent in <see cref="int" />
/// </summary>
public Vec2 Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec2(Width, Height);
}
public ViewPort ViewPortBinding
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => viewPortDisplay;
}
public abstract void CustomizeInitialize();
/// <summary>
/// Set the Size limit of current form
/// </summary>
/// <param _label="minSize">
/// Minimum Size of current form. In form of a <see cref="Vec2" /> of (width,height)
/// </param>
/// <param _label="maxSize">
/// Maximum Size of current form. In form of a <see cref="Vec2" /> of (width,height)
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetSizeLimit(
Vec2 minSize,
Vec2 maxSize
)
{
ThreadHelper.MainThreadAssert();
GraphicFramework.glfwSetWindowSizeLimits(this.viewPortDisplay.ViewPortHandle,
(int)minSize.X, (int)minSize.Y, (int)maxSize.X,
(int)maxSize.Y);
}
/// <summary>
/// Set the Size limit of current form
/// </summary>
/// <param _label="minWidth">
/// Minimum width of current form
/// </param>
/// <param _label="minHeight">
/// Minimum height of current form
/// </param>
/// <param _label="maxWidth">
/// Maximum width of current form
/// </param>
/// <param _label="maxHeight">
/// Maximum height of current form
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetSizeLimit(
int minWidth,
int minHeight,
int maxWidth,
int maxHeight
)
{
ThreadHelper.MainThreadAssert();
GraphicFramework.glfwSetWindowSizeLimits(this.viewPortDisplay.ViewPortHandle, minWidth,
minHeight, maxWidth, maxHeight);
}
public abstract bool TryGetObjectReference(
string name,
out DataBindingEntry entry
);
protected unsafe void SetFrameSizeCallback(
FrameBufferSizeHandler callback
)
{
_resizeCallback = callback;
IntPtr handlerPtr = Marshal.GetFunctionPointerForDelegate(callback);
_ = GraphicFramework.glfwSetFrameBufferSizeCallback(
viewPortDisplay.ViewPortHandle, (delegate* unmanaged[Cdecl]<IntPtr, int, int, void>)handlerPtr);
}
private void DefaultFrameResizeCallback(
IntPtr windowHandle,
int width,
int height
)
{
ViewPort.GetExistedViewPort(windowHandle, out ViewPort vp);
vp.RequestFlushSizeInNextFrame(width, height);
// Console.WriteLine($"{width} {height}");
}
}
}