Bart van Strien avatar Bart van Strien committed 77d1546 Merge

Merged in bartbes/love-experiments/win-console (pull request #11)

Comments (0)

Files changed (1)

 	}
 
 	lua_setfield(L, -2, "_version_compat");
-	
+
 #ifdef LOVE_WINDOWS
 	lua_pushstring(L, "Windows");
 #elif defined(LOVE_MACOSX)
 int w__openConsole(lua_State * L)
 {
 	static bool is_open = false;
-
 	if (is_open)
 		return 0;
+	is_open = true;
 
-	static const int MAX_CONSOLE_LINES = 5000;
-	long std_handle;
-	int console_handle;
+	if (GetConsoleWindow() != NULL || AllocConsole() == 0)
+		return 0;
+
+	const int MAX_CONSOLE_LINES = 5000;
 	CONSOLE_SCREEN_BUFFER_INFO console_info;
-	FILE *fp;
-
-	AllocConsole();
 
 	// Set size.
 	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &console_info);
 
 	SetConsoleTitle(TEXT("LOVE Console"));
 
+	FILE * fp;
+
 	// Redirect stdout.
-	std_handle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
-	console_handle = _open_osfhandle(std_handle, _O_TEXT);
-	fp = _fdopen(console_handle, "w");
-	*stdout = *fp;
-	setvbuf(stdout, NULL, _IONBF, 0);
+	fp = freopen("CONOUT$", "w", stdout);
+	if (L && fp == NULL)
+		luaL_error(L, "Console redirection of stdout failed.");
 
 	// Redirect stdin.
-	std_handle = (long)GetStdHandle(STD_INPUT_HANDLE);
-	console_handle = _open_osfhandle(std_handle, _O_TEXT);
-	fp = _fdopen(console_handle, "r");
-	*stdin = *fp;
-	setvbuf(stdin, NULL, _IONBF, 0);
+	fp = freopen("CONIN$", "r", stdin);
+	if (L && fp == NULL)
+		luaL_error(L, "Console redirection of stdin failed.");
 
 	// Redirect stderr.
-	std_handle = (long)GetStdHandle(STD_ERROR_HANDLE);
-	console_handle = _open_osfhandle(std_handle, _O_TEXT);
-	fp = _fdopen(console_handle, "w");
-	*stderr = *fp;
-	setvbuf(stderr, NULL, _IONBF, 0);
-
-	// Sync std::cout, std::cerr, etc.
-	std::ios::sync_with_stdio();
-
-	is_open = true;
+	fp = freopen("CONOUT$", "w", stderr);
+	if (L && fp == NULL)
+		luaL_error(L, "Console redirection of stderr failed.");
 
 	return 0;
 }
 	// Boot
 	luaopen_love_boot(L);
 	lua_call(L, 0, 1);
-	
+
 	int retval = 0;
 	if (lua_isnumber(L, 1))
 		retval = (int) lua_tonumber(L, 1);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.