cocos2dx Win32下添加控制台输出

前端之家收集整理的这篇文章主要介绍了cocos2dx Win32下添加控制台输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

只需要在main函数里面添加一句代码,再重新编译运行就好

  1. // uncomment below line,open debug console
  2. #define USE_WIN32_CONSOLE
  3.  
  4.  
  5. int WINAPI _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
  6. {
  7. UNREFERENCED_PARAMETER(hPrevInstance);
  8. UNREFERENCED_PARAMETER(lpCmdLine);
  9. #ifdef USE_WIN32_CONSOLE
  10. AllocConsole();
  11. freopen("CONIN$","r",stdin);
  12. freopen("CONOUT$","w",stdout);
  13. freopen("CONOUT$",stderr);
  14. #endif
  15.  
  16.  
  17. auto simulator = SimulatorWin::getInstance();
  18. int ret = simulator->run();
  19.  
  20.  
  21. #ifdef USE_WIN32_CONSOLE
  22. FreeConsole();
  23. #endif
  24.  
  25.  
  26. return ret;
  27. }
其中起作用的就是这几句了
  1. #ifdef USE_WIN32_CONSOLE
  2. AllocConsole();
  3. freopen("CONIN$",stderr);
  4. #endif
  5.  
  6. #ifdef USE_WIN32_CONSOLE
  7. FreeConsole();
  8. #endif

这个宏定义就是开关
  1. // uncomment below line,open debug console
  2. #define USE_WIN32_CONSOLE

当然,新版里面有一个SimulatorWin.cpp 文件,这个文件里面定义了一个宏

  1. #define SIMULATOR_WITH_CONSOLE_AND_MENU 0

这个宏要是等于0,那么就不会显示控制台输出,只要将这个宏的值改为 0 以上的数,控制台就完美的显示出来了

将上面宏的值换成下面这个

  1. #define SIMULATOR_WITH_CONSOLE_AND_MENU 1

因为在这个文件的 248 行有这么一个判断

  1. #if (SIMULATOR_WITH_CONSOLE_AND_MENU > 0)
  2. // create console window
  3. if (_project.isShowConsole())
  4. {
  5. AllocConsole();
  6. _hwndConsole = GetConsoleWindow();
  7. if (_hwndConsole != NULL)
  8. {
  9. ShowWindow(_hwndConsole,SW_SHOW);
  10. BringWindowToTop(_hwndConsole);
  11. freopen("CONOUT$","wt",stdout);
  12. freopen("CONOUT$",stderr);
  13.  
  14. HMENU hmenu = GetSystemMenu(_hwndConsole,FALSE);
  15. if (hmenu != NULL)
  16. {
  17. DeleteMenu(hmenu,SC_CLOSE,MF_BYCOMMAND);
  18. }
  19. }
  20. }
  21. #endif

猜你在找的Cocos2d-x相关文章