From e18978beeb67b4ae9122d04dc92a7df9cf0a2d4b Mon Sep 17 00:00:00 2001 From: ChrisDill Date: Mon, 22 Oct 2018 16:21:47 +0100 Subject: [PATCH] Menu for testing examples for Test.NetFX --- Test.NetCore/Test.NetCore.csproj | 4 +- Test.NetFX/Program.cs | 30 ++++++++- Test.NetFX/RayForm.cs | 101 +++++++++++++++++++++++++++++++ Test.NetFX/Test.NetFX.csproj | 10 ++- 4 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 Test.NetFX/RayForm.cs diff --git a/Test.NetCore/Test.NetCore.csproj b/Test.NetCore/Test.NetCore.csproj index 858df35..12753dc 100644 --- a/Test.NetCore/Test.NetCore.csproj +++ b/Test.NetCore/Test.NetCore.csproj @@ -4,6 +4,7 @@ netcoreapp2.1 raylib-cs.ico AnyCPU;x64;x86 + Test.NetCore.Program true @@ -11,7 +12,8 @@ - + + diff --git a/Test.NetFX/Program.cs b/Test.NetFX/Program.cs index fe9b2f6..16f2ef4 100644 --- a/Test.NetFX/Program.cs +++ b/Test.NetFX/Program.cs @@ -1,12 +1,38 @@ -using Test.Common; +using System; +using System.IO; +using System.Windows.Forms; +using Raylib; +using static Raylib.Raylib; namespace Test.NetFX { class Program { + [STAThread] static void Main(string[] args) { - Common.Test.Run(); + Console.WriteLine("Welcome to raylib-cs!"); + + var dir = Environment.CurrentDirectory + "../../../Examples"; + while (true) + { + var ofd = new OpenFileDialog + { + Filter = @"C#|*.cs", + Title = @"Select raylib example", + InitialDirectory = dir + }; + + if (ofd.ShowDialog() != DialogResult.OK) + return; + + var test = Path.GetFileNameWithoutExtension(ofd.FileName); + Console.WriteLine("Running example " + test + "..."); + + ChangeDirectory(Path.GetDirectoryName(ofd.FileName)); + Type.GetType(test)?.GetMethod("Main")?.Invoke(null, args); + Console.WriteLine(); + } } } } diff --git a/Test.NetFX/RayForm.cs b/Test.NetFX/RayForm.cs new file mode 100644 index 0000000..afc5562 --- /dev/null +++ b/Test.NetFX/RayForm.cs @@ -0,0 +1,101 @@ +using System; +using System.Drawing; +using System.Runtime.InteropServices; +using System.Windows.Forms; +using static Raylib.Raylib; + +namespace Test.NetFX +{ + public partial class RayForms : Form + { + private Panel gamePanel; + private bool windowAttached = false; + + #region WinAPI Entry Points + + [DllImport("user32.dll")] + private static extern IntPtr SetWindowPos(IntPtr handle, IntPtr handleAfter, int x, int y, int cx, int cy, uint flags); + [DllImport("user32.dll")] + private static extern IntPtr SetParent(IntPtr child, IntPtr newParent); + [DllImport("user32.dll")] + private static extern IntPtr ShowWindow(IntPtr handle, int command); + + #endregion + + public RayForms() + { + Size = new Size(1024, 700); + Text = "Rayforms"; + + gamePanel = new Panel(); + gamePanel.Size = new Size(800, 500); + gamePanel.Location = new Point(50, 50); + + Button button = new Button(); + button.Text = "Attach window"; + button.Size = new Size(150, 20); + button.Location = new Point( + (Size.Width / 2) - (button.Size.Width / 2), + gamePanel.Location.Y + gamePanel.Size.Height + 10 + ); + button.Click += new EventHandler(ClickedButton); + Controls.Add(button); + Controls.Add(gamePanel); + } + + private void ClickedButton(object sender, EventArgs e) + { + if (!windowAttached) + { + // new Thread(Test).Start(); + Test(); + } + } + + private void Test() + { + SetConfigFlags(FLAG_WINDOW_UNDECORATED); + InitWindow(800, 480, "Rayforms test"); + SetTargetFPS(60); + + IntPtr winHandle = GetWindowHandle(); + Invoke(new Action(() => + { + SetWindowPos(winHandle, Handle, 0, 0, 0, 0, 0x0401 /*NOSIZE | SHOWWINDOW */); + SetParent(winHandle, gamePanel.Handle); + ShowWindow(winHandle, 1); + windowAttached = true; + })); + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // TODO: Update your variables here + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText("Congrats! You created your first window!", 190, 200, 20, MAROON); + + DrawText(GetFrameTime().ToString(), 100, 10, 15, MAROON); + + DrawFPS(10, 10); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + CloseWindow(); + } + + public static void Run() + { + Application.Run(new RayForms()); + } + } +} diff --git a/Test.NetFX/Test.NetFX.csproj b/Test.NetFX/Test.NetFX.csproj index 789e5c8..1f60860 100644 --- a/Test.NetFX/Test.NetFX.csproj +++ b/Test.NetFX/Test.NetFX.csproj @@ -76,9 +76,14 @@ true true + + Test.NetFX.Program + + + @@ -89,6 +94,9 @@ + + Form + @@ -107,7 +115,7 @@ - +