From 1b2ad307590ec1360025ed0c5cbad758d9a55666 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Sun, 12 Feb 2023 17:32:43 +0100 Subject: [PATCH] Add togglable console window --- Capy64/Assets/bios.lua | 9 ++++++++ Capy64/Extensions/Bindings/Common.cs | 25 +++++++++++++++++++++ Capy64/Extensions/GameWindowExtensions.cs | 15 +++++++++++++ Capy64/Runtime/RuntimeManager.cs | 27 +++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 Capy64/Extensions/Bindings/Common.cs diff --git a/Capy64/Assets/bios.lua b/Capy64/Assets/bios.lua index c4b05a6..14b57e3 100644 --- a/Capy64/Assets/bios.lua +++ b/Capy64/Assets/bios.lua @@ -142,6 +142,11 @@ local function installOS() promptKey() end +local function toggleConsole() + local status = getConsole() + setConsole(not status) +end + term.setBlink(false) local function setupScreen() @@ -150,6 +155,10 @@ local function setupScreen() "Open data folder", openDataFolder, }, + { + "Toggle console window", + toggleConsole, + }, { "Install default OS", installOS, diff --git a/Capy64/Extensions/Bindings/Common.cs b/Capy64/Extensions/Bindings/Common.cs new file mode 100644 index 0000000..539ff01 --- /dev/null +++ b/Capy64/Extensions/Bindings/Common.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace Capy64.Extensions.Bindings; + +public partial class Common +{ + public const int SW_HIDE = 0; + public const int SW_SHOW = 5; + + [LibraryImport("kernel32.dll")] + public static partial IntPtr GetConsoleWindow(); + + [LibraryImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static partial bool IsWindowVisible(IntPtr hWnd); + + [LibraryImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static partial bool ShowWindow(IntPtr hWnd, int nCmdShow); +} diff --git a/Capy64/Extensions/GameWindowExtensions.cs b/Capy64/Extensions/GameWindowExtensions.cs index 94005e4..486a339 100644 --- a/Capy64/Extensions/GameWindowExtensions.cs +++ b/Capy64/Extensions/GameWindowExtensions.cs @@ -54,4 +54,19 @@ public static class GameWindowExtensions { return window.GetWindowFlags().HasFlag(WindowFlags.SDL_WINDOW_MAXIMIZED); } + + public static void ToggleConsole(this GameWindow _, bool show) + { + var console = Common.GetConsoleWindow(); + if (console != IntPtr.Zero) + Common.ShowWindow(console, show ? Common.SW_SHOW : Common.SW_HIDE); + } + + public static bool IsConsoleVisible(this GameWindow _) + { + var console = Common.GetConsoleWindow(); + if (console != IntPtr.Zero) + return false; + return Common.IsWindowVisible(console); + } } diff --git a/Capy64/Runtime/RuntimeManager.cs b/Capy64/Runtime/RuntimeManager.cs index f944f2f..81271d8 100644 --- a/Capy64/Runtime/RuntimeManager.cs +++ b/Capy64/Runtime/RuntimeManager.cs @@ -1,5 +1,6 @@ using Capy64.API; using Capy64.Eventing.Events; +using Capy64.Extensions; using Capy64.Runtime.Libraries; using KeraLua; using System; @@ -113,6 +114,12 @@ internal class RuntimeManager : IPlugin luaState.Thread.PushCFunction(L_Exit); luaState.Thread.SetGlobal("exit"); + luaState.Thread.PushCFunction(L_SetConsole); + luaState.Thread.SetGlobal("setConsole"); + + luaState.Thread.PushCFunction(L_GetConsole); + luaState.Thread.SetGlobal("getConsole"); + var status = luaState.Thread.LoadFile("Assets/bios.lua"); if (status != LuaStatus.OK) { @@ -203,6 +210,26 @@ internal class RuntimeManager : IPlugin return 0; } + private static int L_SetConsole(IntPtr state) + { + var L = Lua.FromIntPtr(state); + + var status = L.ToBoolean(1); + _game.Window.ToggleConsole(status); + + return 0; + } + + private static int L_GetConsole(IntPtr state) + { + var L = Lua.FromIntPtr(state); + + var status = _game.Window.IsConsoleVisible(); + L.PushBoolean(status); + + return 1; + } + private void OnInit(object sender, EventArgs e) { Start();