CConsoleHandler.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 _MSC_VER
  21. #include <windows.h>
  22. HANDLE handleIn;
  23. HANDLE handleOut;
  24. WORD defColor;
  25. #endif
  26. void CConsoleHandler::setColor(int level)
  27. {
  28. #ifdef _MSC_VER
  29. WORD color;
  30. switch(level)
  31. {
  32. case -1:
  33. color = defColor;
  34. break;
  35. case 0:
  36. color = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  37. break;
  38. case 1:
  39. color = FOREGROUND_RED | FOREGROUND_INTENSITY;
  40. break;
  41. case 2:
  42. color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
  43. break;
  44. case 3:
  45. color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  46. break;
  47. case 4:
  48. color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
  49. break;
  50. case 5:
  51. color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
  52. break;
  53. default:
  54. color = defColor;
  55. break;
  56. }
  57. SetConsoleTextAttribute(handleOut,color);
  58. #endif
  59. }
  60. int CConsoleHandler::run()
  61. {
  62. char buffer[500];
  63. std::string readed;
  64. while(true)
  65. {
  66. std::cin.getline(buffer, 500);
  67. if(cb && *cb)
  68. (*cb)(buffer);
  69. }
  70. return -1;
  71. }
  72. CConsoleHandler::CConsoleHandler()
  73. {
  74. #ifdef _MSC_VER
  75. handleIn = GetStdHandle(STD_INPUT_HANDLE);
  76. handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
  77. CONSOLE_SCREEN_BUFFER_INFO csbi;
  78. GetConsoleScreenBufferInfo(handleOut,&csbi);
  79. defColor = csbi.wAttributes;
  80. #endif
  81. cb = new boost::function<void(const std::string &)>;
  82. }
  83. CConsoleHandler::~CConsoleHandler()
  84. {
  85. delete cb;
  86. }
  87. void CConsoleHandler::killConsole(void *hThread)
  88. {
  89. #ifdef _MSC_VER
  90. log3 << "Killing console... ";
  91. TerminateThread(hThread,0);
  92. log3 << "done!\n";
  93. #endif
  94. }