CConsoleHandler.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #define VCMI_DLL
  2. #include "stdafx.h"
  3. #include "CConsoleHandler.h"
  4. #include "boost/function.hpp"
  5. #ifdef _WIN32
  6. #include <windows.h>
  7. HANDLE handleIn;
  8. HANDLE handleOut;
  9. #endif
  10. WORD defColor;
  11. /*
  12. * CConsoleHandler.cpp, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. void CConsoleHandler::setColor(int level)
  21. {
  22. WORD color;
  23. switch(level)
  24. {
  25. case -1:
  26. color = defColor;
  27. break;
  28. case 0:
  29. #ifdef _WIN32
  30. color = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  31. #else
  32. color = "\x1b[1;40;32m";
  33. #endif
  34. break;
  35. case 1:
  36. #ifdef _WIN32
  37. color = FOREGROUND_RED | FOREGROUND_INTENSITY;
  38. #else
  39. color = "\x1b[1;40;31m";
  40. #endif
  41. break;
  42. case 2:
  43. #ifdef _WIN32
  44. color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
  45. #else
  46. color = "\x1b[1;40;35m";
  47. #endif
  48. break;
  49. case 3:
  50. #ifdef _WIN32
  51. color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  52. #else
  53. color = "\x1b[1;40;32m";
  54. #endif
  55. break;
  56. case 4:
  57. #ifdef _WIN32
  58. color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
  59. #else
  60. color = "\x1b[1;40;39m";
  61. #endif
  62. break;
  63. case 5:
  64. #ifdef _WIN32
  65. color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
  66. #else
  67. color = "\x1b[0;40;39m";
  68. #endif
  69. break;
  70. default:
  71. color = defColor;
  72. break;
  73. }
  74. #ifdef _WIN32
  75. SetConsoleTextAttribute(handleOut,color);
  76. #else
  77. std::cout << color;
  78. #endif
  79. }
  80. int CConsoleHandler::run()
  81. {
  82. char buffer[5000];
  83. while(true)
  84. {
  85. std::cin.getline(buffer, 5000);
  86. if(cb && *cb)
  87. (*cb)(buffer);
  88. }
  89. return -1;
  90. }
  91. CConsoleHandler::CConsoleHandler()
  92. {
  93. #ifdef _WIN32
  94. handleIn = GetStdHandle(STD_INPUT_HANDLE);
  95. handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
  96. CONSOLE_SCREEN_BUFFER_INFO csbi;
  97. GetConsoleScreenBufferInfo(handleOut,&csbi);
  98. defColor = csbi.wAttributes;
  99. #else
  100. defColor = "\x1b[0m";
  101. #endif
  102. cb = new boost::function<void(const std::string &)>;
  103. }
  104. CConsoleHandler::~CConsoleHandler()
  105. {
  106. delete cb;
  107. }
  108. #ifndef _WIN32
  109. void CConsoleHandler::killConsole(pthread_t hThread)
  110. #else
  111. void CConsoleHandler::killConsole(void *hThread)
  112. #endif
  113. {
  114. tlog3 << "Killing console... ";
  115. _kill_thread(hThread,0);
  116. tlog3 << "done!\n";
  117. }