CConsoleHandler.cpp 2.5 KB

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