diff --git a/CMakeLists.txt b/CMakeLists.txt index b19c598f..56eb0f4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.10) set(MOORDYN_MAJOR_VERSION 2) -set(MOORDYN_MINOR_VERSION 4) -set(MOORDYN_PATCH_VERSION 1) +set(MOORDYN_MINOR_VERSION 5) +set(MOORDYN_PATCH_VERSION 0) set(MOORDYN_VERSION ${MOORDYN_MAJOR_VERSION}.${MOORDYN_MINOR_VERSION}) project(Moordyn VERSION ${MOORDYN_VERSION}) diff --git a/source/MoorDyn.cpp b/source/MoorDyn.cpp index 5a43b045..5291c216 100644 --- a/source/MoorDyn.cpp +++ b/source/MoorDyn.cpp @@ -66,6 +66,9 @@ int OwnConsoleWindow = 0; #endif +// Default is false, meaning the console will open +int disableConsole = 0; + /** * @} */ @@ -85,63 +88,65 @@ MoorDyn md_singleton = NULL; int DECLDIR MoorDynInit(const double x[], const double xd[], const char* infilename) { + if (!disableConsole) { #ifdef WIN32 - // ------------ create console window for messages if none already available - // ----------------- adapted from Andrew S. Tucker, "Adding Console I/O to a - // Win32 GUI App" in Windows Developer Journal, December 1997. source code - // at http://dslweb.nwnexus.com/~ast/dload/guicon.htm - - FILE* fp; - // get pointer to environment variable "PROMPT" (NULL if not in console) - PromptPtr = getenv("PROMPT"); - - // TODO: simplify this to just keep the output parts I need - - HWND consoleWnd = GetConsoleWindow(); - if (!consoleWnd) { - // if not in console, create our own - OwnConsoleWindow = 1; - - // allocate a console for this app - if (AllocConsole()) { - // set the screen buffer to be big enough to let us scroll text - static const WORD MAX_CONSOLE_LINES = 500; - CONSOLE_SCREEN_BUFFER_INFO coninfo; - GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), - &coninfo); - coninfo.dwSize.Y = MAX_CONSOLE_LINES; - SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), - coninfo.dwSize); - - // redirect unbuffered STDOUT to the console - // lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); - lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE); - hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); - fp = _fdopen(hConHandle, "w"); - *stdout = *fp; - setvbuf(stdout, NULL, _IONBF, 0); - - // redirect unbuffered STDERR to the console - lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE); - hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); - fp = _fdopen(hConHandle, "w"); - *stderr = *fp; - setvbuf(stderr, NULL, _IONBF, 0); - - // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog - // point to console as well - std::ios::sync_with_stdio(); - - std::cout << "(MoorDyn-initiated console window)" << std::endl; - } else { - // This is not a likely scenario, but we've run into some situations - // where you can neither get the console nor allocate a console. - // So just fall back to using whatever cout and cerr were before. - std::cout << "AllocConsole failed" << std::endl; - OwnConsoleWindow = 0; + // ------------ create console window for messages if none already available + // ----------------- adapted from Andrew S. Tucker, "Adding Console I/O to a + // Win32 GUI App" in Windows Developer Journal, December 1997. source code + // at http://dslweb.nwnexus.com/~ast/dload/guicon.htm + + FILE* fp; + // get pointer to environment variable "PROMPT" (NULL if not in console) + PromptPtr = getenv("PROMPT"); + + // TODO: simplify this to just keep the output parts I need + + HWND consoleWnd = GetConsoleWindow(); + if (!consoleWnd) { + // if not in console, create our own + OwnConsoleWindow = 1; + + // allocate a console for this app + if (AllocConsole()) { + // set the screen buffer to be big enough to let us scroll text + static const WORD MAX_CONSOLE_LINES = 500; + CONSOLE_SCREEN_BUFFER_INFO coninfo; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), + &coninfo); + coninfo.dwSize.Y = MAX_CONSOLE_LINES; + SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), + coninfo.dwSize); + + // redirect unbuffered STDOUT to the console + // lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); + lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE); + hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); + fp = _fdopen(hConHandle, "w"); + *stdout = *fp; + setvbuf(stdout, NULL, _IONBF, 0); + + // redirect unbuffered STDERR to the console + lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE); + hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); + fp = _fdopen(hConHandle, "w"); + *stderr = *fp; + setvbuf(stderr, NULL, _IONBF, 0); + + // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog + // point to console as well + std::ios::sync_with_stdio(); + + std::cout << "(MoorDyn-initiated console window)" << std::endl; + } else { + // This is not a likely scenario, but we've run into some situations + // where you can neither get the console nor allocate a console. + // So just fall back to using whatever cout and cerr were before. + std::cout << "AllocConsole failed" << std::endl; + OwnConsoleWindow = 0; + } } - } #endif + } MoorDyn instance = MoorDyn_Create(infilename); if (!instance) @@ -192,6 +197,7 @@ MoorDynClose(void) std::cin.get(); FreeConsole(); } + OwnConsoleWindow = 0; #endif return 0; @@ -291,4 +297,10 @@ AllOutput(double t, double dt) MOORDYN_MSG_LEVEL, "In version 2, AllOutput is automatically called by " "MoorDynInit and MoorDynStep"); +} + +void DECLDIR +SetDisableConsole(int disable) +{ + disableConsole = disable; } \ No newline at end of file diff --git a/source/MoorDyn.h b/source/MoorDyn.h index 7742ff5b..d40a10cb 100644 --- a/source/MoorDyn.h +++ b/source/MoorDyn.h @@ -152,6 +152,14 @@ GetNodePos(int LineNum, int NodeNum, double pos[3]); void DECLDIR AllOutput(double, double); +/** @brief Set the variable to disable the console window. + * + * Use this function to control display of the console window popup. + * @param disable Set disable to 1 to disable the console window. + */ +void DECLDIR +SetDisableConsole(int disable); + /** * @} */