Compare commits

...

4 Commits

Author SHA1 Message Date
max
4b824f3205 Add License 2024-07-12 16:38:54 +02:00
max
9890026656 Fix viewports 2024-07-12 16:38:42 +02:00
max
d8b41b0827 Fix viewports 2024-07-12 16:38:30 +02:00
max
42b978e8c9 working on window handling 2024-07-10 23:18:56 +02:00
7 changed files with 173 additions and 102 deletions

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2024 Nerfed Engine
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.

View File

@ -19,7 +19,6 @@ public static class Engine
private static Stopwatch gameTimer;
private static long previousTicks = 0;
private static TimeSpan accumulatedUpdateTime = TimeSpan.Zero;
private static TimeSpan accumulatedDrawTime = TimeSpan.Zero;
// must be a power of 2 so we can do a bitmask optimization when checking worst case
@ -65,6 +64,10 @@ internal static void Run(string[] args)
throw new Exception("Failed to claim window");
}
MainWindow.OnCloseEvent += (w) => {
Quit();
};
AudioDevice = new AudioDevice();
Controller = new Gui.GuiController(GraphicsDevice, MainWindow, Color.DarkOliveGreen);

View File

@ -37,7 +37,7 @@ internal class GuiController : IDisposable
private readonly Shader imGuiFragmentShader;
private readonly Sampler imGuiSampler;
private readonly GuiTextureStorage textureStorage = new GuiTextureStorage();
private readonly Dictionary<Window, GCHandle> windows = new Dictionary<Window, GCHandle>(16);
private readonly GuiViewportWindow mainViewportWindow;
private Texture fontTexture = null;
private uint vertexCount = 0;
@ -48,8 +48,8 @@ internal class GuiController : IDisposable
public GuiController(GraphicsDevice graphicsDevice, Window mainWindow, Color clearColor, ImGuiConfigFlags configFlags = ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.DockingEnable | ImGuiConfigFlags.ViewportsEnable)
{
this.mainWindow = mainWindow;
this.graphicsDevice = graphicsDevice;
this.mainWindow = mainWindow;
this.clearColor = clearColor;
resourceUploader = new ResourceUploader(graphicsDevice);
@ -100,7 +100,6 @@ public GuiController(GraphicsDevice graphicsDevice, Window mainWindow, Color cle
BuildFontAtlas();
io.ConfigFlags = configFlags;
//io.MouseDrawCursor = true;
if (!OperatingSystem.IsWindows())
{
@ -111,9 +110,7 @@ public GuiController(GraphicsDevice graphicsDevice, Window mainWindow, Color cle
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
ImGuiViewportPtr mainViewport = platformIO.Viewports[0];
mainViewport.PlatformHandle = mainWindow.Handle;
GCHandle handle = GCHandle.Alloc(mainWindow);
mainViewport.PlatformUserData = (IntPtr)handle;
AddWindow(mainWindow, mainViewport, handle);
mainViewportWindow = new GuiViewportWindow(graphicsDevice, mainViewport, mainWindow);
unsafe
{
@ -167,11 +164,6 @@ public void Update(float deltaTime)
OnGui?.Invoke();
{ // Debug
ImGuiIOPtr io = ImGui.GetIO();
ImGui.Text($"mouse pos: {io.MousePos}");
}
ImGui.EndFrame();
}
@ -183,7 +175,7 @@ private void UpdatePerFrameImGuiData(float deltaSeconds)
io.DeltaTime = deltaSeconds; // DeltaTime is in seconds.
}
private void UpdateInput()
private static void UpdateInput()
{
ImGuiIOPtr io = ImGui.GetIO();
@ -248,7 +240,7 @@ private void UpdateInput()
}
}
private void UpdateCursor()
private static void UpdateCursor()
{
ImGuiIOPtr io = ImGui.GetIO();
@ -261,7 +253,7 @@ private void UpdateCursor()
if (imGuiCursor == ImGuiMouseCursor.None || io.MouseDrawCursor)
{
SDL2.SDL.SDL_ShowCursor(0);
_ = SDL2.SDL.SDL_ShowCursor(0);
}
else
{
@ -279,7 +271,7 @@ private void UpdateCursor()
_ => SDL2.SDL.SDL_CreateSystemCursor(SDL2.SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_ARROW),
};
SDL2.SDL.SDL_SetCursor(sdlCursor);
SDL2.SDL.SDL_ShowCursor(1);
_ = SDL2.SDL.SDL_ShowCursor(1);
}
}
@ -324,9 +316,9 @@ public void Render()
for (int i = 0; i < platformIO.Viewports.Size; i++)
{
ImGuiViewportPtr vp = platformIO.Viewports[i];
Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
GuiViewportWindow window = (GuiViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
if (!window.Claimed)
if (!window.Window.Claimed)
{
continue;
}
@ -334,7 +326,7 @@ public void Render()
UpdateImGuiBuffers(vp.DrawData);
CommandBuffer commandBuffer = graphicsDevice.AcquireCommandBuffer();
Texture swapchainTexture = commandBuffer.AcquireSwapchainTexture(window);
Texture swapchainTexture = commandBuffer.AcquireSwapchainTexture(window.Window);
if (swapchainTexture != null)
{
@ -363,7 +355,7 @@ public void Render()
{
RenderCommandLists(commandBuffer, swapchainTexture, drawDataPtr);
graphicsDevice.Submit(commandBuffer);
graphicsDevice.Wait();
//graphicsDevice.Wait();
}
}
}
@ -540,82 +532,33 @@ out int bytesPerPixel
#region Window
private void CreateWindow(ImGuiViewportPtr vp)
{
// TODO: Handle all flags.
ScreenMode screenMode = vp.Flags.HasFlag(ImGuiViewportFlags.NoDecoration) ? ScreenMode.WindowedBorderless : ScreenMode.Windowed;
bool systemResizable = !vp.Flags.HasFlag(ImGuiViewportFlags.NoDecoration);
WindowCreateInfo info = new WindowCreateInfo("Window Title", (uint)vp.Pos.X, (uint)vp.Pos.Y, screenMode, systemResizable, false);
Window window = new Window(graphicsDevice, info);
graphicsDevice.ClaimWindow(window, SwapchainComposition.SDR, PresentMode.Immediate); // What PresentMode do we need?
GCHandle handle = GCHandle.Alloc(window);
vp.PlatformUserData = (IntPtr)handle;
AddWindow(window, vp, handle);
GuiViewportWindow window = new GuiViewportWindow(graphicsDevice, vp);
}
private void DestroyWindow(ImGuiViewportPtr vp)
{
if (vp.PlatformUserData == IntPtr.Zero) return;
Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
graphicsDevice.UnclaimWindow(window);
if (windows.TryGetValue(window, out GCHandle handle))
{
handle.Free();
windows.Remove(window);
}
//graphicsDevice.Wait();
GuiViewportWindow window = (GuiViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
window.Dispose();
vp.PlatformUserData = IntPtr.Zero;
}
private void AddWindow(Window window, ImGuiViewportPtr vp, GCHandle handle)
{
window.OnResizedEvent += ((win, w, h) => {
vp.PlatformRequestResize = true;
});
window.OnMovedEvent += ((win, x, y) =>
{
vp.PlatformRequestMove = true;
});
window.OnCloseEvent += (win) =>
{
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
for (int i = 0; i < platformIO.Viewports.Capacity; i++)
{
ImGuiViewportPtr vp = platformIO.Viewports[i];
if(win == (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target)
{
DestroyWindow(vp);
}
}
};
windows.Add(window, handle);
}
private void ShowWindow(ImGuiViewportPtr vp)
{
if (vp.PlatformUserData == IntPtr.Zero) return;
Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_ShowWindow(window.Handle);
GuiViewportWindow window = (GuiViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_ShowWindow(window.Window.Handle);
}
private unsafe void GetWindowPos(ImGuiViewportPtr vp, Vector2* outPos)
{
if (vp.PlatformUserData == IntPtr.Zero) return;
Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_GetWindowPosition(window.Handle, out int x, out int y);
GuiViewportWindow window = (GuiViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_GetWindowPosition(window.Window.Handle, out int x, out int y);
*outPos = new Vector2(x, y);
}
@ -623,24 +566,24 @@ private void SetWindowPos(ImGuiViewportPtr vp, Vector2 pos)
{
if (vp.PlatformUserData == IntPtr.Zero) return;
Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_SetWindowPosition(window.Handle, (int)pos.X, (int)pos.Y);
GuiViewportWindow window = (GuiViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_SetWindowPosition(window.Window.Handle, (int)pos.X, (int)pos.Y);
}
private void SetWindowSize(ImGuiViewportPtr vp, Vector2 size)
{
if (vp.PlatformUserData == IntPtr.Zero) return;
Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_SetWindowSize(window.Handle, (int)size.X, (int)size.Y);
GuiViewportWindow window = (GuiViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_SetWindowSize(window.Window.Handle, (int)size.X, (int)size.Y);
}
private unsafe void GetWindowSize(ImGuiViewportPtr vp, Vector2* outSize)
{
if (vp.PlatformUserData == IntPtr.Zero) return;
Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_GetWindowSize(window.Handle, out int w, out int h);
GuiViewportWindow window = (GuiViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_GetWindowSize(window.Window.Handle, out int w, out int h);
*outSize = new Vector2(w, h);
}
@ -648,26 +591,28 @@ private void SetWindowFocus(ImGuiViewportPtr vp)
{
if (vp.PlatformUserData == IntPtr.Zero) return;
Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
GuiViewportWindow window = (GuiViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
//SDL2.SDL.SDL_SetWindowInputFocus(window.Handle);
SDL2.SDL.SDL_RaiseWindow(window.Handle);
SDL2.SDL.SDL_RaiseWindow(window.Window.Handle);
}
private byte GetWindowFocus(ImGuiViewportPtr vp)
{
if (vp.PlatformUserData == IntPtr.Zero) return (byte)0;
Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_WindowFlags flags = (SDL2.SDL.SDL_WindowFlags)SDL2.SDL.SDL_GetWindowFlags(window.Handle);
GuiViewportWindow window = (GuiViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_WindowFlags flags = (SDL2.SDL.SDL_WindowFlags)SDL2.SDL.SDL_GetWindowFlags(window.Window.Handle);
return (flags & SDL2.SDL.SDL_WindowFlags.SDL_WINDOW_INPUT_FOCUS) != 0 ? (byte)1 : (byte)0;
}
private byte GetWindowMinimized(ImGuiViewportPtr vp)
{
if (vp.PlatformUserData == IntPtr.Zero) return (byte)0;
if (vp.PlatformUserData == IntPtr.Zero) return 0;
GuiViewportWindow window = (GuiViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_WindowFlags flags = (SDL2.SDL.SDL_WindowFlags)SDL2.SDL.SDL_GetWindowFlags(window.Window.Handle);
Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
SDL2.SDL.SDL_WindowFlags flags = (SDL2.SDL.SDL_WindowFlags)SDL2.SDL.SDL_GetWindowFlags(window.Handle);
return (flags & SDL2.SDL.SDL_WindowFlags.SDL_WINDOW_MINIMIZED) != 0 ? (byte)1 : (byte)0;
}
@ -675,14 +620,14 @@ private unsafe void SetWindowTitle(ImGuiViewportPtr vp, IntPtr title)
{
if (vp.PlatformUserData == IntPtr.Zero) return;
Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
GuiViewportWindow window = (GuiViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target;
byte* titlePtr = (byte*)title;
int count = 0;
while (titlePtr[count] != 0)
{
count += 1;
}
SDL2.SDL.SDL_SetWindowTitle(window.Handle, System.Text.Encoding.ASCII.GetString(titlePtr, count));
SDL2.SDL.SDL_SetWindowTitle(window.Window.Handle, System.Text.Encoding.ASCII.GetString(titlePtr, count));
}
#endregion
@ -696,15 +641,5 @@ public void Dispose()
imGuiPipeline?.Dispose();
imGuiSampler?.Dispose();
resourceUploader?.Dispose();
foreach (KeyValuePair<Window, GCHandle> window in windows)
{
if (window.Key == mainWindow) continue;
graphicsDevice.UnclaimWindow(window.Key);
window.Key.Dispose();
window.Value.Free();
}
windows.Clear();
}
}

View File

@ -0,0 +1,87 @@
using ImGuiNET;
using Nerfed.Runtime.Graphics;
using System.Runtime.InteropServices;
namespace Nerfed.Runtime.Gui
{
internal class GuiViewportWindow
{
public Window Window => window;
private readonly GCHandle gcHandle;
private readonly GraphicsDevice graphicsDevice;
private readonly ImGuiViewportPtr vp;
private readonly Window window;
public GuiViewportWindow(GraphicsDevice graphicsDevice, ImGuiViewportPtr vp, Window window)
{
this.graphicsDevice = graphicsDevice;
this.vp = vp;
this.window = window;
gcHandle = GCHandle.Alloc(this);
if (!window.Claimed)
{
graphicsDevice.ClaimWindow(window, SwapchainComposition.SDR, PresentMode.Immediate); // What PresentMode do we need?
}
vp.PlatformUserData = (IntPtr)gcHandle;
}
public GuiViewportWindow(GraphicsDevice graphicsDevice, ImGuiViewportPtr vp)
{
this.graphicsDevice = graphicsDevice;
this.vp = vp;
gcHandle = GCHandle.Alloc(this);
// TODO: Handle all flags.
ScreenMode screenMode = ScreenMode.Windowed;
bool systemResizable = true;
if ((vp.Flags & ImGuiViewportFlags.NoDecoration) == ImGuiViewportFlags.NoDecoration)
{
screenMode = ScreenMode.WindowedBorderless;
systemResizable = false;
}
WindowCreateInfo info = new WindowCreateInfo("Window Title", (uint)vp.Pos.X, (uint)vp.Pos.Y, screenMode, systemResizable, false);
window = new Window(graphicsDevice, info);
graphicsDevice.ClaimWindow(window, SwapchainComposition.SDR, PresentMode.Immediate); // What PresentMode do we need?
window.OnMovedEvent += HandleOnMovedEvent;
window.OnResizedEvent += HandleOnResizedEvent;
window.OnCloseEvent += HandleOnCloseEvent;
vp.PlatformUserData = (IntPtr)gcHandle;
}
public void Dispose()
{
window.OnMovedEvent -= HandleOnMovedEvent;
window.OnResizedEvent -= HandleOnResizedEvent;
window.OnCloseEvent -= HandleOnCloseEvent;
graphicsDevice.UnclaimWindow(window);
window.Dispose();
gcHandle.Free();
}
private void HandleOnMovedEvent(Window window, int x, int y)
{
vp.PlatformRequestMove = true;
}
private void HandleOnResizedEvent(Window window, uint w, uint h)
{
vp.PlatformRequestResize = true;
}
private void HandleOnCloseEvent(Window window)
{
vp.PlatformRequestClose = true;
}
}
}

View File

@ -136,9 +136,9 @@ private void ProcessMovedChangedEvent(ref SDL.SDL_WindowEvent ev)
private void ProcessCloseEvent(ref SDL.SDL_WindowEvent ev)
{
OnCloseEvent?.Invoke(this);
Engine.GraphicsDevice.UnclaimWindow(this);
Dispose();
OnCloseEvent?.Invoke(this);
}
/// <summary>

View File

@ -1,3 +1,11 @@
# Nerfed
nerfed game engine
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## Third-Party Licenses
This project includes third-party libraries with their respective licenses, which can be found in the [THIRD_PARTY_LICENSES](THIRD_PARTY_LICENSES) file.

29
THIRD_PARTY_LICENSES Normal file
View File

@ -0,0 +1,29 @@
# Third-Party Licenses
## ImGui
**Name:** Dear ImGui
**License:** MIT License
MIT License
Copyright (c) 2014-2022 Omar Cornut
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.