Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})

Expand Down
120 changes: 66 additions & 54 deletions source/MoorDyn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ int OwnConsoleWindow = 0;

#endif

// Default is false, meaning the console will open
int disableConsole = 0;

/**
* @}
*/
Expand All @@ -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)
Expand Down Expand Up @@ -192,6 +197,7 @@ MoorDynClose(void)
std::cin.get();
FreeConsole();
}
OwnConsoleWindow = 0;
#endif

return 0;
Expand Down Expand Up @@ -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;
}
8 changes: 8 additions & 0 deletions source/MoorDyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
* @}
*/
Expand Down
Loading