From 6736bb56ece3b05dca03a17ffc76e1a6bbe4e1e5 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 28 Jan 2024 22:37:50 +0900 Subject: [PATCH] internal/ui: make a separate package to hide a console Updates #2896 --- go.mod | 1 + go.sum | 2 + internal/ui/hideconsole_windows.go | 73 ------------------------------ internal/ui/ui.go | 2 + 4 files changed, 5 insertions(+), 73 deletions(-) delete mode 100644 internal/ui/hideconsole_windows.go diff --git a/go.mod b/go.mod index 133bb87ee..361877fcd 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/hajimehoshi/ebiten/v2 go 1.18 require ( + github.com/ebitengine/hideconsole v0.1.0 github.com/ebitengine/oto/v3 v3.2.0-alpha.3 github.com/ebitengine/purego v0.6.0-alpha.4.0.20240127153756-e7ad88ddfffe github.com/go-text/typesetting v0.1.1-0.20231231232151-8d81c02dc157 diff --git a/go.sum b/go.sum index ef033e517..4c43bc02e 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/ebitengine/hideconsole v0.1.0 h1:pNW5M6xbD+icVaTg+vUl9ZsPTV3BopSTjEWEQitM1WI= +github.com/ebitengine/hideconsole v0.1.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A= github.com/ebitengine/oto/v3 v3.2.0-alpha.3 h1:gDsHiEBKofH9scQ7rf+3jE1XOutgB0KvTkvP74LaRH4= github.com/ebitengine/oto/v3 v3.2.0-alpha.3/go.mod h1:JtMbxJHZBDXfS8BmVYwzWk9Z6r7jsjwsHzOuZrEkfs4= github.com/ebitengine/purego v0.6.0-alpha.4.0.20240127153756-e7ad88ddfffe h1:bac7sG+vDG+mBmjElwI0p1a2L09rxkY8xcmv5QEFyGA= diff --git a/internal/ui/hideconsole_windows.go b/internal/ui/hideconsole_windows.go deleted file mode 100644 index 2d267020c..000000000 --- a/internal/ui/hideconsole_windows.go +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2017 The Ebiten Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package ui - -import ( - "fmt" - - "golang.org/x/sys/windows" -) - -var ( - kernel32 = windows.NewLazySystemDLL("kernel32.dll") - - procFreeConsoleWindow = kernel32.NewProc("FreeConsole") - procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow") -) - -func freeConsole() error { - r, _, e := procFreeConsoleWindow.Call() - if int32(r) == 0 { - if e != nil && e != windows.ERROR_SUCCESS { - return fmt.Errorf("ui: FreeConsole failed: %w", e) - } - return fmt.Errorf("ui: FreeConsole returned 0") - } - return nil -} - -func getConsoleWindow() windows.HWND { - r, _, _ := procGetConsoleWindow.Call() - return windows.HWND(r) -} - -// hideConsoleWindow will hide the console window that is showing when -// compiling on Windows without specifying the '-ldflags "-Hwindowsgui"' flag. -func hideConsoleWindow() { - // In Xbox, GetWindowThreadProcessId might not exist. - if user32.NewProc("GetWindowThreadProcessId").Find() != nil { - return - } - - pid := windows.GetCurrentProcessId() - - // Get the process ID of the console's creator. - var cpid uint32 - if _, err := windows.GetWindowThreadProcessId(getConsoleWindow(), &cpid); err != nil { - // Even if closing the console fails, this is not harmful. - // Ignore error. - return - } - - if pid == cpid { - // The current process created its own console. Hide this. - // Ignore error. - _ = freeConsole() - } -} - -func init() { - hideConsoleWindow() -} diff --git a/internal/ui/ui.go b/internal/ui/ui.go index fd4a3ab45..83ef4f1ea 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -20,6 +20,8 @@ import ( "sync" "sync/atomic" + _ "github.com/ebitengine/hideconsole" + "github.com/hajimehoshi/ebiten/v2/internal/atlas" "github.com/hajimehoshi/ebiten/v2/internal/mipmap" "github.com/hajimehoshi/ebiten/v2/internal/thread"