Add console window hiding for Windows

This commit is contained in:
2024-04-24 03:07:50 -05:00
parent 9cd4d39e89
commit 0ac915a865
4 changed files with 49 additions and 7 deletions

2
.gitignore vendored
View File

@@ -2,5 +2,7 @@
/dist
.idea
*.dll
*.lib
*.exe
node_modules
assets/build.css

22
Cargo.lock generated
View File

@@ -11,6 +11,17 @@ dependencies = [
"memchr",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "bitflags"
version = "1.3.2"
@@ -46,6 +57,15 @@ dependencies = [
"wasi",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@@ -105,6 +125,7 @@ checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
name = "pacman"
version = "0.1.0"
dependencies = [
"atty",
"colors-transform",
"lazy_static",
"libc",
@@ -114,6 +135,7 @@ dependencies = [
"tracing",
"tracing-error",
"tracing-subscriber",
"winapi",
]
[[package]]

View File

@@ -15,5 +15,9 @@ tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_leve
tracing-error = "0.2.0"
tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
[target.'cfg(target_os = "windows")'.dependencies]
atty = "0.2"
winapi = {version = "0.3", features = ["wincon", "winuser"]}
[target.'cfg(target_os = "emscripten")'.dependencies]
libc = "0.2.16"

View File

@@ -1,3 +1,4 @@
// #![windows_subsystem = "windows"]
use std::process;
use std::time::Duration;
@@ -55,22 +56,35 @@ fn ttf_context() -> &'static ttf::Sdl2TtfContext {
Box::leak(Box::new(ttf::init().unwrap()))
}
#[cfg(not(target_os = "windows"))]
fn hide_console_window() {}
#[cfg(target_os = "windows")]
fn hide_console_window() {
if !atty::is(atty::Stream::Stdout) {
unsafe { winapi::um::wincon::FreeConsole() };
}
}
fn main() {
hide_console_window();
let ctx = sdl2::init().unwrap();
let video_ctx = ctx.video().unwrap();
let window = match video_ctx
.window("rust-sdl2-emscripten", 640, 480)
.position_centered()
.resizable()
.allow_highdpi()
.opengl()
.build()
.window("rust-sdl2-emscripten", 640, 480)
.position_centered()
.resizable()
.allow_highdpi()
.opengl()
.build()
{
Ok(window) => window,
Err(err) => panic!("failed to create window: {}", err),
};
let mut canvas = match window.into_canvas().accelerated().build() {
Ok(canvas) => canvas,
Err(err) => panic!("failed to create canvas: {}", err),