/src/main.cpp

https://bitbucket.org/vivkin/gam3b00bs/ · C++ · 103 lines · 63 code · 31 blank · 9 comment · 2 complexity · 735b83c6043c4808303b9667ef3ab8c7 MD5 · raw file

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