From 816e9319d719b500a85d66cd99e97f4e32299e6d Mon Sep 17 00:00:00 2001 From: Larpon Date: Thu, 29 Sep 2022 14:02:50 +0200 Subject: [PATCH] cgen: remove the need for [console] for gg or ui programs on windows (#15912) --- doc/docs.md | 8 +++++--- examples/clock/clock.v | 2 -- examples/fireworks/fireworks.v | 2 -- examples/gg/mandelbrot.v | 1 - examples/sokol/01_cubes/cube.v | 2 -- examples/sokol/02_cubes_glsl/cube_glsl.v | 1 - examples/sokol/03_march_tracing_glsl/rt_glsl.v | 1 - examples/sokol/04_multi_shader_glsl/rt_glsl.v | 1 - examples/sokol/05_instancing_glsl/rt_glsl.v | 1 - examples/sokol/06_obj_viewer/show_obj.v | 2 -- examples/sokol/fonts.v | 1 - examples/sokol/freetype_raven.v | 1 - examples/ttf_font/example_ttf.v | 1 - examples/viewer/view.v | 16 +++++++--------- vlib/gg/testdata/draw_simple_polygons.vv | 1 - vlib/gg/testdata/remove_image_from_cache.vv | 1 - vlib/v/fmt/tests/attrs_expected.vv | 2 +- vlib/v/fmt/tests/attrs_input.vv | 2 +- vlib/v/gen/c/cmain.v | 14 ++++++++++++++ vlib/v/tests/skip_unused/gg_code.vv | 1 - vlib/x/ttf/README.md | 4 ---- 21 files changed, 28 insertions(+), 37 deletions(-) diff --git a/doc/docs.md b/doc/docs.md index 732882780..825b58556 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -6175,9 +6175,11 @@ fn C.DefWindowProc(hwnd int, msg int, lparam int, wparam int) type FastFn = fn (int) bool // Windows only: -// If a default graphics library is imported (ex. gg, ui), then the graphical window takes -// priority and no console window is created, effectively disabling println() statements. -// Use to explicitly create console window. Valid before main() only. +// Without this attribute all graphical apps will have the following behavior on Windows: +// If run from a console or terminal; keep the terminal open so all (e)println statements can be viewed. +// If run from e.g. Explorer, by double-click; app is opened, but no terminal is opened, and no (e)println output can be seen. +// Use it to force-open a terminal to view output in, even if the app is started from Explorer. +// Valid before main() only. [console] fn main() { } diff --git a/examples/clock/clock.v b/examples/clock/clock.v index da8f6ba1a..8d503907a 100644 --- a/examples/clock/clock.v +++ b/examples/clock/clock.v @@ -147,8 +147,6 @@ fn on_init(mut app App) { app.resize() } -// is needed for easier diagnostics on windows -[console] fn main() { println("Press 'q' to quit.") mut font_path := os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'RobotoMono-Regular.ttf')) diff --git a/examples/fireworks/fireworks.v b/examples/fireworks/fireworks.v index cff2bf434..64022a3c7 100644 --- a/examples/fireworks/fireworks.v +++ b/examples/fireworks/fireworks.v @@ -93,8 +93,6 @@ fn (mut app App) resize() { app.ui.height = size.height } -// is needed for easier diagnostics on windows -[console] fn main() { mut font_path := os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'RobotoMono-Regular.ttf')) $if android { diff --git a/examples/gg/mandelbrot.v b/examples/gg/mandelbrot.v index a9d0ed4b9..1e8661ede 100644 --- a/examples/gg/mandelbrot.v +++ b/examples/gg/mandelbrot.v @@ -222,7 +222,6 @@ fn graphics_keydown(code gg.KeyCode, mod gg.Modifier, mut state AppState) { } } -[console] fn main() { mut state := &AppState{} state.gg = gg.new_context( diff --git a/examples/sokol/01_cubes/cube.v b/examples/sokol/01_cubes/cube.v index 6137d2b7f..0810ff40b 100644 --- a/examples/sokol/01_cubes/cube.v +++ b/examples/sokol/01_cubes/cube.v @@ -403,8 +403,6 @@ fn my_event_manager(mut ev gg.Event, mut app App) { * Main * ******************************************************************************/ -// is needed for easier diagnostics on windows -[console] fn main() { // App init mut app := &App{ diff --git a/examples/sokol/02_cubes_glsl/cube_glsl.v b/examples/sokol/02_cubes_glsl/cube_glsl.v index 7b863f079..68c04f593 100644 --- a/examples/sokol/02_cubes_glsl/cube_glsl.v +++ b/examples/sokol/02_cubes_glsl/cube_glsl.v @@ -577,7 +577,6 @@ fn my_event_manager(mut ev gg.Event, mut app App) { * Main * ******************************************************************************/ -[console] // is needed for easier diagnostics on windows fn main() { // App init mut app := &App{ diff --git a/examples/sokol/03_march_tracing_glsl/rt_glsl.v b/examples/sokol/03_march_tracing_glsl/rt_glsl.v index 640288686..671ebd206 100644 --- a/examples/sokol/03_march_tracing_glsl/rt_glsl.v +++ b/examples/sokol/03_march_tracing_glsl/rt_glsl.v @@ -392,7 +392,6 @@ fn my_event_manager(mut ev gg.Event, mut app App) { /****************************************************************************** * Main ******************************************************************************/ -[console] // is needed for easier diagnostics on windows fn main() { // App init mut app := &App{ diff --git a/examples/sokol/04_multi_shader_glsl/rt_glsl.v b/examples/sokol/04_multi_shader_glsl/rt_glsl.v index fd817e7b8..1c1e6e490 100644 --- a/examples/sokol/04_multi_shader_glsl/rt_glsl.v +++ b/examples/sokol/04_multi_shader_glsl/rt_glsl.v @@ -586,7 +586,6 @@ fn my_event_manager(mut ev gg.Event, mut app App) { /****************************************************************************** * Main ******************************************************************************/ -[console] // is needed for easier diagnostics on windows fn main() { // App init mut app := &App{ diff --git a/examples/sokol/05_instancing_glsl/rt_glsl.v b/examples/sokol/05_instancing_glsl/rt_glsl.v index 2cab6627f..2ae944111 100644 --- a/examples/sokol/05_instancing_glsl/rt_glsl.v +++ b/examples/sokol/05_instancing_glsl/rt_glsl.v @@ -475,7 +475,6 @@ fn my_event_manager(mut ev gg.Event, mut app App) { /****************************************************************************** * Main ******************************************************************************/ -[console] // is needed for easier diagnostics on windows fn main(){ // App init mut app := &App{ diff --git a/examples/sokol/06_obj_viewer/show_obj.v b/examples/sokol/06_obj_viewer/show_obj.v index 3b0253c75..394a4551f 100644 --- a/examples/sokol/06_obj_viewer/show_obj.v +++ b/examples/sokol/06_obj_viewer/show_obj.v @@ -265,8 +265,6 @@ fn my_event_manager(mut ev gg.Event, mut app App) { /****************************************************************************** * Main ******************************************************************************/ -// is needed for easier diagnostics on windows -[console] fn main() { /* obj.tst() diff --git a/examples/sokol/fonts.v b/examples/sokol/fonts.v index 131f485b3..e8048b75d 100644 --- a/examples/sokol/fonts.v +++ b/examples/sokol/fonts.v @@ -13,7 +13,6 @@ mut: font_normal int } -[console] fn main() { mut color_action := gfx.ColorAttachmentAction{ action: .clear diff --git a/examples/sokol/freetype_raven.v b/examples/sokol/freetype_raven.v index 2d72511a1..7c68642a3 100644 --- a/examples/sokol/freetype_raven.v +++ b/examples/sokol/freetype_raven.v @@ -61,7 +61,6 @@ mut: inited bool } -[console] fn main() { mut color_action := gfx.ColorAttachmentAction{ action: .clear diff --git a/examples/ttf_font/example_ttf.v b/examples/ttf_font/example_ttf.v index 144257582..78f1a5ced 100644 --- a/examples/ttf_font/example_ttf.v +++ b/examples/ttf_font/example_ttf.v @@ -116,7 +116,6 @@ fn my_event_manager(mut ev gg.Event, mut app App_data) { } } -[console] fn main() { mut app := &App_data{ gg: 0 diff --git a/examples/viewer/view.v b/examples/viewer/view.v index 07b50f213..f4cb2a934 100644 --- a/examples/viewer/view.v +++ b/examples/viewer/view.v @@ -444,7 +444,7 @@ fn frame(mut app App) { if app.show_info_flag == true && app.scale > 1 { mut bw := f32(0.25) mut bh := f32(0.25 / app.img_ratio) - + // manage the rotations if rotation & 1 == 1 { bw,bh = bh,bw @@ -454,11 +454,11 @@ fn frame(mut app App) { if rotation & 1 == 1 { bx,by = by,bx } - + bh_old1 := bh bh *= ratio by += (bh_old1 - bh) - + // draw the zoom icon sgl.begin_quads() r := int(u32(rotation) << 1) @@ -467,17 +467,17 @@ fn frame(mut app App) { sgl.v2f_t2f_c3b(bx + bw, by + bh, uv[(4 + r) & 7] , uv[(5 + r) & 7], c[0], c[1], c[2]) sgl.v2f_t2f_c3b(bx , by + bh, uv[(6 + r) & 7] , uv[(7 + r) & 7], c[0], c[1], c[2]) sgl.end() - + // draw the zoom rectangle sgl.disable_texture() - + bw_old := bw bh_old := bh bw /= app.scale bh /= app.scale bx += (bw_old - bw) / 2 - (tr_x / 8) / app.scale by += (bh_old - bh) / 2 - ((tr_y / 8) / app.scale) * ratio - + c = [u8(255),255,0]! // yellow sgl.begin_line_strip() sgl.v2f_c3b(bx , by , c[0], c[1], c[2]) @@ -517,7 +517,7 @@ fn frame(mut app App) { scale_str := "${app.scale:.2}" text := "${num}/${of_num} [${app.img_w},${app.img_h}]=>[${x_screen},${y_screen}] ${app.item_list.lst[app.item_list.item_index].name} scale: ${scale_str} rotation: ${rotation_angle}" //text := "${num}/${of_num}" - draw_text(mut app, text, 10, 10, 20) + draw_text(mut app, text, 10, 10, 20) unsafe{ text.free() } @@ -761,8 +761,6 @@ fn load_and_show(file_list []string, mut app App) { * Main * ******************************************************************************/ -// is needed for easier diagnostics on windows -[console] fn main() { // mut font_path := os.resource_abs_path(os.join_path('../assets/fonts/', 'RobotoMono-Regular.ttf')) font_name := 'RobotoMono-Regular.ttf' diff --git a/vlib/gg/testdata/draw_simple_polygons.vv b/vlib/gg/testdata/draw_simple_polygons.vv index 9493eba18..70b2026b2 100644 --- a/vlib/gg/testdata/draw_simple_polygons.vv +++ b/vlib/gg/testdata/draw_simple_polygons.vv @@ -10,7 +10,6 @@ mut: edge int = 3 } -[console] fn main() { println('rotation: left arrow key, right arrow key') println('center polygon edge: up arrow key, down arrow key') diff --git a/vlib/gg/testdata/remove_image_from_cache.vv b/vlib/gg/testdata/remove_image_from_cache.vv index 01eed818d..f0bb8a898 100644 --- a/vlib/gg/testdata/remove_image_from_cache.vv +++ b/vlib/gg/testdata/remove_image_from_cache.vv @@ -3,7 +3,6 @@ module main import gg import sokol.gfx -[console] fn main() { mut context := gg.new_context( frame_fn: frame diff --git a/vlib/v/fmt/tests/attrs_expected.vv b/vlib/v/fmt/tests/attrs_expected.vv index 1660ee55d..a05fd1e82 100644 --- a/vlib/v/fmt/tests/attrs_expected.vv +++ b/vlib/v/fmt/tests/attrs_expected.vv @@ -3,7 +3,7 @@ [direct_array_access; inline; unsafe] fn heavily_tagged() {} -// console attribute for easier diagnostics on windows +// console attribute to force-open a console for easier diagnostics on windows // also it's not safe to use [console; unsafe] fn dangerous_console() {} diff --git a/vlib/v/fmt/tests/attrs_input.vv b/vlib/v/fmt/tests/attrs_input.vv index bfffc55c9..27079eddb 100644 --- a/vlib/v/fmt/tests/attrs_input.vv +++ b/vlib/v/fmt/tests/attrs_input.vv @@ -5,6 +5,6 @@ [tom: 'jerry'] fn heavily_tagged() {} -[console] // console attribute for easier diagnostics on windows +[console] // console attribute to force-open a console for easier diagnostics on windows [unsafe] // also it's not safe to use fn dangerous_console() {} diff --git a/vlib/v/gen/c/cmain.v b/vlib/v/gen/c/cmain.v index ec8601a67..1b7f8c7dc 100644 --- a/vlib/v/gen/c/cmain.v +++ b/vlib/v/gen/c/cmain.v @@ -69,6 +69,20 @@ fn (mut g Gen) gen_c_main_function_only_header() { g.writeln('\tcmd_line_to_argv CommandLineToArgvW = (cmd_line_to_argv)GetProcAddress(shell32_module, "CommandLineToArgvW");') g.writeln('\tint ___argc;') g.writeln('\twchar_t** ___argv = CommandLineToArgvW(full_cmd_line, &___argc);') + + g.writeln('BOOL con_valid = FALSE;') + if g.force_main_console { + g.writeln('con_valid = AllocConsole();') + } else { + g.writeln('con_valid = AttachConsole(ATTACH_PARENT_PROCESS);') + } + g.writeln('if (con_valid) {') + g.writeln('\tFILE* res_fp = 0;') + g.writeln('\terrno_t err;') + g.writeln('\terr = freopen_s(&res_fp, "CON", "w", stdout);') + g.writeln('\terr = freopen_s(&res_fp, "CON", "w", stderr);') + g.writeln('\t(void)err;') + g.writeln('}') return } // Console application diff --git a/vlib/v/tests/skip_unused/gg_code.vv b/vlib/v/tests/skip_unused/gg_code.vv index df821c44b..b27124c10 100644 --- a/vlib/v/tests/skip_unused/gg_code.vv +++ b/vlib/v/tests/skip_unused/gg_code.vv @@ -3,7 +3,6 @@ module main import gg import gx -[console] fn main() { x := gg.new_context( bg_color: gx.rgb(174, 198, 255) diff --git a/vlib/x/ttf/README.md b/vlib/x/ttf/README.md index d875f1608..fdde1dda7 100644 --- a/vlib/x/ttf/README.md +++ b/vlib/x/ttf/README.md @@ -57,7 +57,6 @@ Let's start with a simple snippet of code: import os import x.ttf -[console] fn main() { mut ttf_font := ttf.TTF_File{} ttf_font.buf = os.read_bytes('arial.ttf') or { panic(err) } @@ -75,7 +74,6 @@ At this point we can render a simple text: import os import x.ttf -[console] fn main() { mut ttf_font := ttf.TTF_File{} ttf_font.buf = os.read_bytes('arial.ttf') or { panic(err) } @@ -139,7 +137,6 @@ Draw text block draw a justified and indented block of multiline text in the bit import os import x.ttf -[console] fn main() { mut ttf_font := ttf.TTF_File{} ttf_font.buf = os.read_bytes('arial.ttf') or { panic(err) } @@ -268,7 +265,6 @@ fn draw_frame(mut app App_data) { app.gg.end() } -[console] fn main() { mut app := &App_data{ gg: 0 -- 2.30.2