BsEditorExec.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <io.h>
  5. #include "BsEditorApplication.h"
  6. #include "BsPlatform.h"
  7. #include "BsCrashHandler.h"
  8. #if BS_PLATFORM == BS_PLATFORM_WIN32
  9. #include <windows.h>
  10. using namespace BansheeEngine;
  11. #if BS_DEBUG_MODE
  12. void InitializeDebugConsole()
  13. {
  14. //Create a console for this application
  15. AllocConsole();
  16. //Redirect unbuffered STDOUT to the console
  17. HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  18. int SystemOutput = _open_osfhandle(intptr_t(ConsoleOutput), _O_TEXT);
  19. FILE *COutputHandle = _fdopen(SystemOutput, "w");
  20. *stdout = *COutputHandle;
  21. setvbuf(stdout, NULL, _IONBF, 0);
  22. //Redirect unbuffered STDERR to the console
  23. HANDLE ConsoleError = GetStdHandle(STD_ERROR_HANDLE);
  24. int SystemError = _open_osfhandle(intptr_t(ConsoleError), _O_TEXT);
  25. FILE *CErrorHandle = _fdopen(SystemError, "w");
  26. *stderr = *CErrorHandle;
  27. setvbuf(stderr, NULL, _IONBF, 0);
  28. //Redirect unbuffered STDIN to the console
  29. HANDLE ConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
  30. int SystemInput = _open_osfhandle(intptr_t(ConsoleInput), _O_TEXT);
  31. FILE *CInputHandle = _fdopen(SystemInput, "r");
  32. *stdin = *CInputHandle;
  33. setvbuf(stdin, NULL, _IONBF, 0);
  34. //make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well
  35. std::ios::sync_with_stdio(true);
  36. }
  37. void ShutdownDebugConsole()
  38. {
  39. //Write "Press any key to exit"
  40. HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  41. DWORD CharsWritten;
  42. WriteConsole(ConsoleOutput, "\nPress any key to exit", 22, &CharsWritten, 0);
  43. //Disable line-based input mode so we can get a single character
  44. HANDLE ConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
  45. SetConsoleMode(ConsoleInput, 0);
  46. //Read a single character
  47. TCHAR InputBuffer;
  48. DWORD CharsRead;
  49. ReadConsole(ConsoleInput, &InputBuffer, 1, &CharsRead, 0);
  50. }
  51. #endif // End BS_DEBUG_MODE
  52. int CALLBACK WinMain(
  53. _In_ HINSTANCE hInstance,
  54. _In_ HINSTANCE hPrevInstance,
  55. _In_ LPSTR lpCmdLine,
  56. _In_ int nCmdShow
  57. )
  58. {
  59. #if BS_DEBUG_MODE
  60. InitializeDebugConsole();
  61. #endif
  62. CrashHandler::startUp();
  63. __try
  64. {
  65. EditorApplication::startUp(RenderAPIPlugin::DX9);
  66. EditorApplication::instance().runMainLoop();
  67. EditorApplication::shutDown();
  68. }
  69. __except (gCrashHandler().reportCrash(GetExceptionInformation()))
  70. {
  71. PlatformUtility::terminate(true);
  72. }
  73. #if BS_DEBUG_MODE
  74. ShutdownDebugConsole();
  75. #endif
  76. CrashHandler::shutDown();
  77. return 0;
  78. }
  79. #endif // End BS_PLATFORM