From ed4a035574eacdab027f90aacf40c92e15b72c81 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 12 Feb 2019 20:02:04 -0800 Subject: [PATCH] ImGui up and running --- .gitignore | 1 + getFNA.sh | 50 +++++++++++- .../ImGui/ImGuiFinalRenderDelegate.cs | 78 +++++++++++++++++++ project_name/project_name.csproj | 24 +++++- 4 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 project_name/ImGui/ImGuiFinalRenderDelegate.cs diff --git a/.gitignore b/.gitignore index 0cb3aef..bf09f5c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .DS_Store FNA/ +ImGui.NET/ fnalibs/ project_name/bin project_name/obj diff --git a/getFNA.sh b/getFNA.sh index 34abdb6..c0a480b 100755 --- a/getFNA.sh +++ b/getFNA.sh @@ -62,6 +62,35 @@ function updateFNA() fi } + +# Clones FNA from the git master branch +function downloadImGui() +{ + checkGit + echo "Downloading ImGui..." + git -C $MY_DIR clone https://github.com/mellinoe/ImGui.NET.git --recursive + if [ $? -eq 0 ]; then + echo "Finished downloading!\n" + else + echo >&2 "ERROR: Unable to download successfully. Maybe try again later?" + fi +} + +# Pulls FNA from the git master branch +function updateImGui() +{ + checkGit + echo "Updating to the latest git version of ImGui.NET..." + git -C "$MY_DIR/ImGui.NET" pull --recurse-submodules + if [ $? -eq 0 ]; then + echo "Finished updating!\n" + else + echo >&2 "ERROR: Unable to update." + exit 1 + fi +} + + # Downloads and extracts prepackaged archive of native libraries ("fnalibs") function getLibs() { @@ -114,17 +143,36 @@ if [[ $shouldDownloadLibs =~ ^[Yy]$ ]]; then getLibs fi +# Dear ImGui +if [ ! -d "$MY_DIR/ImGui.NET" ]; then + read -p "Download ImGui.NET (y/n)? " shouldDownload + if [[ $shouldDownload =~ ^[Yy]$ ]]; then + downloadImGui + fi +else + read -p "Update ImGui.NET (y/n)? " shouldUpdate + if [[ $shouldUpdate =~ ^[Yy]$ ]]; then + updateImGui + fi +fi + # install t4 engine installT4 -# Rename project + +# Only proceed from here if we have not yet renamed the project if [ ! -d "$MY_DIR/project_name" ]; then # old project_name folder already renamed so we are all done here exit 1 fi +# copy over ImGui files before renaming the project +echo "Copying ImGui renderer to project..." +cp "$MY_DIR/ImGui.NET/src/ImGui.NET.SampleProgram.XNA/DrawVertDeclaration.cs" "$MY_DIR/project_name/ImGui" +cp "$MY_DIR/ImGui.NET/src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs" "$MY_DIR/project_name/ImGui" +sed -i '' "s/cimgui/cimgui.dylib/g" ImGui.NET/src/ImGui.NET/Generated/ImGuiNative.gen.cs read -p "Enter the project name to use for your folder and csproj file or 'exit' to quit: " newProjectName if [[ $newProjectName = 'exit' || -z "$newProjectName" ]]; then diff --git a/project_name/ImGui/ImGuiFinalRenderDelegate.cs b/project_name/ImGui/ImGuiFinalRenderDelegate.cs new file mode 100644 index 0000000..29510c7 --- /dev/null +++ b/project_name/ImGui/ImGuiFinalRenderDelegate.cs @@ -0,0 +1,78 @@ +using System; +using System.Reflection; +using ImGuiNET; +using ImGuiNET.SampleProgram.XNA; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace Nez +{ + public class ImGuiFinalRenderDelegate : IFinalRenderDelegate + { + public Scene scene { get; set; } + + ImGuiRenderer _imGuiRenderer; + RenderTarget2D _lastRenderTarget; + IntPtr _renderTargetId; + + public ImGuiFinalRenderDelegate() + { + var core = typeof(Core).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null) as Core; + _imGuiRenderer = new ImGuiRenderer(core); + _imGuiRenderer.RebuildFontAtlas(); + } + + public void handleFinalRender( Color letterboxColor, RenderTarget2D source, Rectangle finalRenderDestinationRect, SamplerState samplerState ) + { + if(_lastRenderTarget != source) + { + // unbind the old texture if we had one + if(_lastRenderTarget != null) + _imGuiRenderer.UnbindTexture(_renderTargetId); + + // bind the new texture + _lastRenderTarget = source; + _renderTargetId = _imGuiRenderer.BindTexture(source); + } + + Core.graphicsDevice.setRenderTarget( null ); + Core.graphicsDevice.Clear( letterboxColor ); + + + _imGuiRenderer.BeforeLayout(new GameTime(TimeSpan.FromDays(0), TimeSpan.FromMilliseconds(Time.deltaTime * 1000))); + layoutGui(); + _imGuiRenderer.AfterLayout(); + } + + void layoutGui() + { + ImGui.ShowDemoWindow(); + + var maxSize = new System.Numerics.Vector2(_lastRenderTarget.Width, _lastRenderTarget.Height); + var minSize = maxSize / 4; + unsafe + { + ImGui.SetNextWindowSizeConstraints(minSize, maxSize, data => + { + var size = (*data).CurrentSize; + var ratio = size.X / _lastRenderTarget.Width; + (*data).DesiredSize.Y = ratio * _lastRenderTarget.Height; + }); + } + + ImGui.SetNextWindowPos(new System.Numerics.Vector2(0, 0), ImGuiCond.FirstUseEver); + ImGui.Begin("Game Window"); + ImGui.Image(_renderTargetId, ImGui.GetContentRegionAvail()); + ImGui.End(); + } + + public void onAddedToScene() + {} + + public void onSceneBackBufferSizeChanged( int newWidth, int newHeight ) + {} + + public void unload() + {} + } +} diff --git a/project_name/project_name.csproj b/project_name/project_name.csproj index 01f3c9c..fc687f1 100644 --- a/project_name/project_name.csproj +++ b/project_name/project_name.csproj @@ -2,17 +2,19 @@ Exe - net46 + net462 false AnyCPU project_name DesktopGL + True + @@ -66,5 +68,25 @@ PreserveNewest + + + + + osx\%(RecursiveDir)%(Filename)%(Extension) + PreserveNewest + + + osx\%(RecursiveDir)%(Filename)%(Extension) + PreserveNewest + + + osx\%(RecursiveDir)%(Filename)%(Extension) + PreserveNewest + + + osx\%(RecursiveDir)%(Filename)%(Extension) + PreserveNewest + + \ No newline at end of file