/src/main.cpp
C++ | 103 lines | 63 code | 31 blank | 9 comment | 2 complexity | 735b83c6043c4808303b9667ef3ab8c7 MD5 | raw file
1#include "common.h" 2 3#include <stdio.h> 4#include <io.h> 5#include <fcntl.h> 6#include <windows.h> 7 8#include "network/network.h" 9#include "bindings/bind_all.h" 10#include "shared/log.h" 11 12 13// first link from google: http://www.halcyon.com/~ast/dload/guicon.htm 14 15enum { MAX_CONSOLE_LINES = 500 }; 16 17void create_console() 18{ 19 int hConHandle; 20 long lStdHandle; 21 22 CONSOLE_SCREEN_BUFFER_INFO coninfo; 23 FILE *fp; 24 25 AllocConsole(); 26 27 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); 28 coninfo.dwSize.Y = MAX_CONSOLE_LINES; 29 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); 30 31 // redirect unbuffered STDOUT to the console 32 lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); 33 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); 34 fp = _fdopen( hConHandle, "w" ); 35 36 *stdout = *fp; 37 setvbuf( stdout, NULL, _IONBF, 0 ); 38 39 // redirect unbuffered STDIN to the console 40 41 lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE); 42 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); 43 44 fp = _fdopen( hConHandle, "r" ); 45 *stdin = *fp; 46 setvbuf( stdin, NULL, _IONBF, 0 ); 47 48 // redirect unbuffered STDERR to the console 49 lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE); 50 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); 51 fp = _fdopen( hConHandle, "w" ); 52 *stderr = *fp; 53 setvbuf( stderr, NULL, _IONBF, 0 ); 54} 55 56 57 58// 59 60 61// 62// makes sure the current working directoy is where the exe is located 63// 64void init_working_dir() 65{ 66 wchar_t path[512], drive[10], dir[MAX_PATH], name[MAX_PATH], ext[128]; 67 GetModuleFileNameW( 0, path, 512 ); 68 _wsplitpath( path, drive, dir, name, ext ); 69 swprintf( path, 512, L"%s%s", drive, dir ); 70 if( !SetCurrentDirectoryW( path ) ) 71 { 72 ASSERT( !"Could not set exe directory!" ); 73 } 74} 75 76 77//////////////////////////////////////////////////////////////////////////////////////// 78 79 80int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 81{ 82#if ENABLE_CONSOLE 83 create_console(); 84#endif 85 86 init_working_dir(); 87 log_open("log.txt"); 88 89 int ret = luab::start_scripts(); 90 91#if ENABLE_CONSOLE 92 if( !IsDebuggerPresent() ) 93 { 94 system( "pause" ); 95 } 96#endif 97 98 log_close(); 99 100 return ret; 101 102 103}