Added an option to attach a console in Windows.

This commit is contained in:
rude
2009-11-07 14:41:35 +01:00
parent 0dc6d985ee
commit fa3b226edc
5 changed files with 804 additions and 621 deletions
+2 -1
View File
@@ -63,7 +63,8 @@
#endif
#if defined(LOVE_WINDOWS)
# define LOVE_LEGENDARY_UTF8_ARGV_HACK
# define LOVE_LEGENDARY_UTF8_ARGV_HACK
# define LOVE_LEGENDARY_CONSOLE_IO_HACK
#endif
#endif // LOVE_CONFIG_H
+87 -4
View File
@@ -24,9 +24,16 @@
#include <common/runtime.h>
#include <common/MemoryData.h>
#ifdef LOVE_LEGENDARY_UTF8_ARGV_HACK
#ifdef LOVE_WINDOWS
#include <windows.h>
#endif // #ifdef LOVE_LEGENDARY_UTF8_ARGV_HACK
#endif // LOVE_WINDOWS
#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <fstream>
#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK
#ifdef LOVE_BUILD_EXE
@@ -74,6 +81,10 @@ static const luaL_Reg modules[] = {
#endif // LOVE_BUILD_STANDALONE
#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
int w__openConsole(lua_State * L);
#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK
extern "C" LOVE_EXPORT int luaopen_love(lua_State * L)
{
love::luax_insistglobal(L, "love");
@@ -88,6 +99,11 @@ extern "C" LOVE_EXPORT int luaopen_love(lua_State * L)
lua_pushstring(L, love::VERSION_CODENAME);
lua_setfield(L, -2, "_version_codename");
#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
lua_pushcfunction(L, w__openConsole);
lua_setfield(L, -2, "_openConsole");
#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK
lua_newtable(L);
for(int i = 0; love::VERSION_COMPATIBILITY[i] != 0; ++i)
@@ -161,6 +177,61 @@ void get_utf8_arguments(int & argc, char **& argv)
#endif // LOVE_LEGENDARY_UTF8_ARGV_HACK
#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
int w__openConsole(lua_State * L)
{
static bool is_open = false;
if(is_open)
return 0;
static const int MAX_CONSOLE_LINES = 5000;
long std_handle;
int console_handle;
CONSOLE_SCREEN_BUFFER_INFO console_info;
FILE *fp;
AllocConsole();
// Set size.
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &console_info);
console_info.dwSize.Y = MAX_CONSOLE_LINES;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), console_info.dwSize);
SetConsoleTitle(TEXT("LOVE Console"));
// 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);
// 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);
// 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;
return 0;
}
#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK
#ifdef LOVE_BUILD_EXE
int main(int argc, char ** argv)
@@ -176,9 +247,10 @@ int main(int argc, char ** argv)
// Oh, you just want the version? Okay!
if(argc > 1 && strcmp(argv[1],"--version") == 0) {
printf("This is LOVE %s (%s), the unquestionably awesome 2D game engine.\n", love::VERSION_STR, love::VERSION_CODENAME);
printf("LOVE %s (%s)\n", love::VERSION_STR, love::VERSION_CODENAME);
return 0;
}
// Create the virtual machine.
lua_State * L = lua_open();
luaL_openlibs(L);
@@ -190,11 +262,22 @@ int main(int argc, char ** argv)
// Add command line arguments to global arg (like stand-alone Lua).
{
lua_newtable(L);
for(int i = 0;i<argc;i++)
if(argc > 0)
{
lua_pushstring(L, argv[0]);
lua_rawseti(L, -2, -2);
}
lua_pushstring(L, "embedded boot.lua");
lua_rawseti(L, -2, -1);
for(int i = 1; i<argc; i++)
{
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i);
}
lua_setglobal(L, "arg");
}
+56 -4
View File
@@ -86,6 +86,46 @@ function love.arg.getLow(a)
return a[m]
end
love.arg.options = {
console = { a = 0 },
game = { a = 1 }
}
function love.arg.parse_option(m, i)
m.set = true
if m.a > 0 then
m.arg = {}
for j=i,i+m.a-1 do
table.insert(m.arg, arg[j])
i = j
end
end
return i
end
function love.arg.parse_options()
local last = 0
local argc = #arg
for i=1,argc do
-- Look for options.
local s, e, m = string.find(arg[i], "%-%-(.+)")
if m and love.arg.options[m] then
i = love.arg.parse_option(love.arg.options[m], i+1)
end
last = i
end
if not love.arg.options.game.set then
love.arg.parse_option(love.arg.options.game, last)
end
end
function love.createhandlers()
-- Standard callback handlers.
@@ -122,9 +162,13 @@ function love.boot()
require("love")
require("love.filesystem")
if arg and arg[1] then
love.arg.parse_options()
local o = love.arg.options
if o.game.set and o.game.arg[1] then
love.filesystem.init(love.path.getfull(love.arg.getLow(arg)))
local full_source = love.path.getfull(arg[1])
local full_source = love.path.getfull(o.game.arg[1])
local leaf = love.path.leaf(full_source)
love.filesystem.setIdentity(leaf)
if not pcall(love.filesystem.setSource, full_source) then
@@ -162,6 +206,7 @@ function love.init()
physics = true,
sound = true,
},
console = false, -- Only relevant for windows.
}
-- If config file exists, load it and allow it to update config table.
@@ -175,6 +220,10 @@ function love.init()
love.conf(c)
end
if love.arg.options.console.set then
c.console = true
end
-- Gets desired modules.
for k,v in pairs(c.modules) do
if v then
@@ -196,6 +245,11 @@ function love.init()
if love.filesystem and love.filesystem.exists("main.lua") then require("main.lua") end
-- Console hack
if c.console and love._openConsole then
love._openConsole()
end
end
function love.run()
@@ -716,5 +770,3 @@ result = xpcall(love.init, love.errhand)
if not result then return end
result = xpcall(love.run, love.errhand)
if not result then return end
print("Done.")
+658 -611
View File
File diff suppressed because it is too large Load Diff