2
0

main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "PolycodeWindowsPlayer.h"
  2. #include <Polycode.h>
  3. #include "PolycodePlayerView.h"
  4. #include "windows.h"
  5. #include "resource.h"
  6. #include <io.h>
  7. #include <fcntl.h>
  8. using namespace Polycode;
  9. extern Win32Core *core;
  10. void wtoc(char* Dest, TCHAR* Source, int SourceSize)
  11. {
  12. for(int i = 0; i < SourceSize; ++i)
  13. Dest[i] = (char)Source[i];
  14. }
  15. static void OpenConsole()
  16. {
  17. int outHandle, errHandle, inHandle;
  18. FILE *outFile, *errFile, *inFile;
  19. AllocConsole();
  20. CONSOLE_SCREEN_BUFFER_INFO coninfo;
  21. GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
  22. coninfo.dwSize.Y = 9999;
  23. SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
  24. outHandle = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
  25. errHandle = _open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE),_O_TEXT);
  26. inHandle = _open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE),_O_TEXT );
  27. outFile = _fdopen(outHandle, "w" );
  28. errFile = _fdopen(errHandle, "w");
  29. inFile = _fdopen(inHandle, "r");
  30. *stdout = *outFile;
  31. *stderr = *errFile;
  32. *stdin = *inFile;
  33. setvbuf( stdout, NULL, _IONBF, 0 );
  34. setvbuf( stderr, NULL, _IONBF, 0 );
  35. setvbuf( stdin, NULL, _IONBF, 0 );
  36. std::ios::sync_with_stdio();
  37. }
  38. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  39. {
  40. String args = String(GetCommandLineW());
  41. String fileName;
  42. for(int i=0; i < args.length(); i++) {
  43. if(args[i] != '\"')
  44. fileName += args.substr(i, 1);
  45. if(args[i] == '\"' && i != args.length()-1)
  46. fileName = "";
  47. }
  48. if(fileName == " ")
  49. fileName = "";
  50. if(fileName.length() > 1) {
  51. fileName = fileName.replace(":", "");
  52. fileName = fileName.replace("\\", "/");
  53. fileName = fileName.substr(1, fileName.length() - 1);
  54. }
  55. char path[2049];
  56. TCHAR tpath[2049];
  57. GetModuleFileName(NULL, (LPWSTR)tpath, 2048);
  58. wtoc(path, tpath, 2048);
  59. String basePath = path;
  60. vector<String> cpts = basePath.split("\\");
  61. String installPath = "";
  62. for(int i=0; i < cpts.size() - 1; i++) {
  63. installPath = installPath + cpts[i];
  64. installPath += String("\\");
  65. }
  66. SetCurrentDirectory(installPath.getWDataWithEncoding(String::ENCODING_UTF8));
  67. PolycodePlayerView *view = new PolycodePlayerView(true, hInstance, nCmdShow, L"");
  68. PolycodeWindowsPlayer *player = new PolycodeWindowsPlayer(view, "main.polyapp", false);
  69. player->addEventListener(view, PolycodeDebugEvent::EVENT_ERROR);
  70. player->addEventListener(view, PolycodeDebugEvent::EVENT_PRINT);
  71. //OpenConsole();
  72. player->runPlayer();
  73. core = (Win32Core*)player->getCore();
  74. MSG Msg;
  75. do {
  76. if(PeekMessage(&Msg, NULL, 0,0,PM_REMOVE)) {
  77. TranslateMessage(&Msg);
  78. DispatchMessage(&Msg);
  79. }
  80. } while(player->Update());
  81. return Msg.wParam;
  82. }