Advertisement
sapitando

console.h

Sep 16th, 2016
1,930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Autor : Tiago Portela
  2.    Email : sapitando@gmail.com
  3.    Sobre : Compilado com TDM-GCC 5.10 64 bit. Funções para console Windows.
  4.    Obs : Apenas tentando aprender algoritimos, sozinho, por hobby. */
  5.  
  6. #ifndef _WINDOWS_
  7. #define WIN32_LEAN_AND_MEAN
  8. #include <windows.h>
  9. #endif
  10. #ifndef _INC_STDIO
  11. #include <stdio.h>
  12. #endif
  13. #ifndef _INC_STDLIB
  14. #include <stdlib.h>
  15. #endif
  16.  
  17. // F_ means foreground.
  18. #define F_BLACK                 0
  19. #define F_BLUE                  1
  20. #define F_GREEN                 2
  21. #define F_CYAN                  3
  22. #define F_RED                   4
  23. #define F_MAGENT                5
  24. #define F_YELLOW                6
  25. #define F_WHITE                 7
  26. #define F_LIGHT                 8
  27. #define F_LIGHTBLUE             9
  28. #define F_LIGHTGREEN           10
  29. #define F_LIGHTCYAN            11
  30. #define F_LIGHTRED             12
  31. #define F_LIGHTMAGENT          13
  32. #define F_LIGHTYELLOW          14
  33. #define F_LIGHTWHITE           15
  34. // B_ means background.
  35. #define B_BLACK                 0
  36. #define B_BLUE                 16
  37. #define B_GREEN                32
  38. #define B_CYAN                 48
  39. #define B_RED                  64
  40. #define B_MAGENT               80
  41. #define B_YELLOW               96
  42. #define B_WHITE               112
  43. #define B_LIGHT               128
  44. #define B_LIGHTBLUE           144
  45. #define B_LIGHTGREEN          160
  46. #define B_LIGHTCYAN           176
  47. #define B_LIGHTRED            192
  48. #define B_LIGHTMAGENT         208
  49. #define B_LIGHTYELLOW         224
  50. #define B_LIGHTWHITE          240
  51. // Current color.
  52. #define ATTR                  (sFBColors.wAttributes)
  53. // Current foreground color.
  54. #define F_ATTR                (sFBColors.Color.Foreground)
  55. // Current background color.
  56. #define B_ATTR                (sFBColors.wAttributes & 240)
  57. // Reverse background and foreground colors.
  58. #define REVERSE               ((sFBColors.Color.Foreground << 4) | (sFBColors.Color.Background))
  59.  
  60. // Intern use.
  61. #define CLEAR_LEFT_TO_RIGHT   1
  62. #define CLEAR_TOP_TO_BOTTOM   2
  63. #define INSERT_LEFT_TO_RIGHT  3
  64. #define INSERT_TOP_TO_BOTTOM  4
  65. #define INSERT_RIGHT_TO_LEFT  5
  66. #define INSERT_BOTTOM_TO_TOP  6
  67. #define REPLACE_LEFT_TO_RIGHT 7
  68. #define REPLACE_TOP_TO_BOTTOM 8
  69.  
  70.  
  71. typedef struct _WindowInfo {
  72.     COORD Min;
  73.     COORD Max;
  74.     COORD Size;
  75. } Window_t;
  76.  
  77. typedef struct _ScreenInfo {
  78.     Window_t Window;
  79.     Window_t Boundary;
  80. } Screen_t;
  81.  
  82. typedef struct _Color {
  83.     DWORD Foreground : 4;
  84.     DWORD Background : 4;
  85.     DWORD : 24;
  86. } Color_t;
  87.  
  88. typedef union _ForegroundBackgroundColor {
  89.     DWORD wAttributes;
  90.     Color_t Color;
  91. } FBColor_t;
  92.  
  93.  
  94. HANDLE     hConsoleOutput = NULL;
  95. Window_t   sWindow        = {{0,0},{0,0},{0,0}};
  96. Screen_t   sScreen        = {{{0,0},{0,0},{0,0}},{{0,0},{0,0},{0,0}}};
  97. FBColor_t  sFBColors      = {.wAttributes = 7};
  98. BOOL       _iOK           = FALSE;
  99.  
  100. // Intern use.
  101. void _Initialize(void);
  102. COORD _Where(void);
  103. void _Block(const Window_t sWind_,
  104.             const UCHAR cHowInsert,
  105.             const SHORT iHowMany,
  106.             const DWORD wAttributes,
  107.             const UCHAR ucChar);
  108.  
  109. // Functions.
  110. SHORT WhereWindowX(void)
  111. {
  112.     return (_Where().X - sWindow.Min.X) + 1;
  113. }
  114.  
  115. SHORT WhereWindowY(void)
  116. {
  117.     return (_Where().Y - sWindow.Min.Y) + 1;
  118. }
  119.  
  120. SHORT WhereScreenX(void)
  121. {
  122.     return _Where().X + 1;
  123. }
  124.  
  125. SHORT WhereScreenY(void)
  126. {
  127.     return _Where().Y + 1;
  128. }
  129.  
  130. SHORT WindowMaxX(void)
  131. {
  132.     if(!_iOK) {_Initialize();}
  133.     return sWindow.Size.X;
  134. }
  135.  
  136. SHORT WindowMaxY(void)
  137. {
  138.     if(!_iOK) {_Initialize();}
  139.     return sWindow.Size.Y;
  140. }
  141.  
  142. SHORT ScreenMaxX(void)
  143. {
  144.     if(!_iOK) {_Initialize();}
  145.     return sScreen.Boundary.Size.X;
  146. }
  147.  
  148. SHORT ScreenMaxY(void)
  149. {
  150.     if(!_iOK) {_Initialize();}
  151.     return sScreen.Boundary.Size.Y;
  152. }
  153.  
  154. void GotoXY(const SHORT iX,
  155.             const SHORT iY)
  156. {
  157.     if(!_iOK) {_Initialize();}
  158.     COORD dwCurPos = {.X = (iX + sWindow.Min.X) - 1,
  159.                               .Y = (iY + sWindow.Min.Y) - 1};
  160.     if((dwCurPos.X >= sWindow.Min.X)
  161.             && (dwCurPos.Y >= sWindow.Min.Y)
  162.             && (dwCurPos.X <= sWindow.Max.X)
  163.             && (dwCurPos.Y <= sWindow.Max.Y))
  164.     {
  165.         SetConsoleCursorPosition(hConsoleOutput, dwCurPos);
  166.     }
  167. }
  168.  
  169. void GotoFullXY(const SHORT iX,
  170.                 const SHORT iY)
  171. {
  172.     if(!_iOK) {_Initialize();}
  173.     COORD dwCurPos = {.X = iX - 1,
  174.                       .Y = iY - 1};
  175.     if((dwCurPos.X >= sScreen.Boundary.Min.X)
  176.             && (dwCurPos.Y >= sScreen.Boundary.Min.Y)
  177.             && (dwCurPos.X <= sScreen.Boundary.Max.X)
  178.             && (dwCurPos.Y <= sScreen.Boundary.Max.Y))
  179.     {
  180.         SetConsoleCursorPosition(hConsoleOutput, dwCurPos);
  181.     }
  182. }
  183.  
  184. void Window(const SHORT iX1,
  185.             const SHORT iY1,
  186.             const SHORT iX2,
  187.             const SHORT iY2)
  188. {
  189.     if(!_iOK) {_Initialize();}
  190.     Window_t sWind_ = {.Min.X = iX1 - 1,
  191.                        .Min.Y = iY1 - 1,
  192.                        .Max.X = iX2 - 1,
  193.                        .Max.Y = iY2 - 1,
  194.                        .Size.X = (iX2 - iX1) + 1,
  195.                        .Size.Y = (iY2 - iY1) + 1};
  196.     if((sWind_.Min.X <= sWind_.Max.X)
  197.             && (sWind_.Min.Y <= sWind_.Max.Y)
  198.             && (sWind_.Min.X >= sScreen.Boundary.Min.X)
  199.             && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
  200.             && (sWind_.Max.X <= sScreen.Boundary.Max.X)
  201.             && (sWind_.Max.Y <= sScreen.Boundary.Max.Y))
  202.     {
  203.         sWindow = sWind_;
  204.     }
  205.     GotoXY(1,1);
  206. }
  207.  
  208. void WindowFmt(const SHORT iX1,
  209.                const SHORT iY1,
  210.                const SHORT iSizeX,
  211.                const SHORT iSizeY)
  212. {
  213.     Window(iX1, iY1, iSizeX + iX1 - 1, iSizeY + iY1 - 1);
  214. }
  215.  
  216. void SetColors(const DWORD wColorFlags)
  217. {
  218.     if(!_iOK) {_Initialize();}
  219.     sFBColors.wAttributes = wColorFlags;
  220.     SetConsoleTextAttribute(hConsoleOutput, sFBColors.wAttributes);
  221. }
  222.  
  223. void ClrScr(const DWORD wColorFlags,
  224.             const UCHAR ucChar)
  225. {
  226.     if(!_iOK) {_Initialize();}
  227.     Window_t sWind_ = {.Min.X  = sWindow.Min.X,
  228.                        .Min.Y  = sWindow.Min.Y,
  229.                        .Max.X  = sWindow.Max.X,
  230.                        .Max.Y  = sWindow.Max.Y,
  231.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  232.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  233.     _Block(sWind_, CLEAR_TOP_TO_BOTTOM, sWind_.Size.Y, wColorFlags, ucChar);
  234. }
  235.  
  236. void ClrFullScr(const DWORD wColorFlags,
  237.                 const UCHAR ucChar)
  238. {
  239.     if(!_iOK) {_Initialize();}
  240.     Window_t sWind_ = {.Min.X  = sScreen.Boundary.Min.X,
  241.                        .Min.Y  = sScreen.Boundary.Min.Y,
  242.                        .Max.X  = sScreen.Boundary.Max.X,
  243.                        .Max.Y  = sScreen.Boundary.Max.Y,
  244.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  245.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  246.     _Block(sWind_, CLEAR_TOP_TO_BOTTOM, sWind_.Size.Y, wColorFlags, ucChar);
  247. }
  248.  
  249. void ClrEol(const DWORD wColorFlags,
  250.             const UCHAR ucChar)
  251. {
  252.     if(!_iOK) {_Initialize();}
  253.     Window_t sWind_ = {.Min.X  = _Where().X,
  254.                        .Min.Y  = _Where().Y,
  255.                        .Max.X  = sWindow.Max.X,
  256.                        .Max.Y  = sWind_.Min.Y,
  257.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  258.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  259.     if((sWind_.Min.X >= sWindow.Min.X)
  260.             && (sWind_.Min.X <= sWindow.Max.X)
  261.             && (sWind_.Min.Y >= sWindow.Min.Y)
  262.             && (sWind_.Min.Y <= sWindow.Max.Y))
  263.     {
  264.         _Block(sWind_, CLEAR_TOP_TO_BOTTOM, 1, wColorFlags, ucChar);
  265.     }
  266. }
  267.  
  268. void ClrLine(const SHORT iY,
  269.              const SHORT iLines,
  270.              const DWORD wColorFlags,
  271.              const UCHAR ucChar)
  272. {
  273.     if(!_iOK) {_Initialize();}
  274.     Window_t sWind_ = {.Min.X  = sWindow.Min.X,
  275.                        .Min.Y  = (iY - 1) + sWindow.Min.Y,
  276.                        .Max.X  = sWindow.Max.X,
  277.                        .Max.Y  = (sWind_.Min.Y + iLines) - 1,
  278.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  279.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  280.     if((iLines > 0)
  281.             && (sWind_.Min.Y >= sWindow.Min.Y)
  282.             && (sWind_.Max.Y <= sWindow.Max.Y))
  283.     {
  284.         _Block(sWind_, CLEAR_TOP_TO_BOTTOM, iLines, wColorFlags, ucChar);
  285.     }
  286. }
  287.  
  288. void ClrFullLine(const SHORT iY,
  289.                  const SHORT iLines,
  290.                  const DWORD wColorFlags,
  291.                  const UCHAR ucChar)
  292. {
  293.     if(!_iOK) {_Initialize();}
  294.     Window_t sWind_ = {.Min.X  = sScreen.Boundary.Min.X,
  295.                        .Min.Y  = iY - 1,
  296.                        .Max.X  = sScreen.Boundary.Max.X,
  297.                        .Max.Y  = (sWind_.Min.Y + iLines) - 1,
  298.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  299.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  300.     if((iLines > 0)
  301.             && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
  302.             && (sWind_.Max.Y <= sScreen.Boundary.Max.Y))
  303.     {
  304.         _Block(sWind_, CLEAR_TOP_TO_BOTTOM, iLines, wColorFlags, ucChar);
  305.     }
  306. }
  307.  
  308. void ClrEoc(const DWORD wColorFlags,
  309.             const UCHAR ucChar)
  310. {
  311.     if(!_iOK) {_Initialize();}
  312.     Window_t sWind_ = {.Min.X  = _Where().X,
  313.                        .Min.Y  = _Where().Y,
  314.                        .Max.X  = sWind_.Min.X,
  315.                        .Max.Y  = sWindow.Max.Y,
  316.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  317.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  318.     if((sWind_.Min.X >= sWindow.Min.X)
  319.             && (sWind_.Min.X <= sWindow.Max.X)
  320.             && (sWind_.Min.Y >= sWindow.Min.Y)
  321.             && (sWind_.Min.Y <= sWindow.Max.Y))
  322.     {
  323.         _Block(sWind_, CLEAR_LEFT_TO_RIGHT, 1, wColorFlags, ucChar);
  324.     }
  325. }
  326.  
  327. void ClrColumn(const SHORT iX,
  328.                const SHORT iColumns,
  329.                const DWORD wColorFlags,
  330.                const UCHAR ucChar)
  331. {
  332.     if(!_iOK) {_Initialize();}
  333.     Window_t sWind_ = {.Min.X  = (iX - 1) + sWindow.Min.X,
  334.                        .Min.Y  = sWindow.Min.Y,
  335.                        .Max.X  = (sWind_.Min.X + iColumns) - 1,
  336.                        .Max.Y  = sWindow.Max.Y,
  337.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  338.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  339.     if((iColumns > 0)
  340.             && (sWind_.Min.X >= sWindow.Min.X)
  341.             && (sWind_.Max.X <= sWindow.Max.X))
  342.     {
  343.         _Block(sWind_, CLEAR_LEFT_TO_RIGHT, iColumns, wColorFlags, ucChar);
  344.     }
  345. }
  346.  
  347. void ClrFullColumn(const SHORT iX,
  348.                    const SHORT iColumns,
  349.                    const DWORD wColorFlags,
  350.                    const UCHAR ucChar)
  351. {
  352.     if(!_iOK) {_Initialize();}
  353.     Window_t sWind_ = {.Min.X  = iX - 1,
  354.                        .Min.Y  = sScreen.Boundary.Min.Y,
  355.                        .Max.X  = (sWind_.Min.X + iColumns) - 1,
  356.                        .Max.Y  = sScreen.Boundary.Max.Y,
  357.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  358.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  359.     if((iColumns > 0)
  360.             && (sWind_.Min.X >= sScreen.Boundary.Min.X)
  361.             && (sWind_.Max.X <= sScreen.Boundary.Max.X))
  362.     {
  363.         _Block(sWind_, CLEAR_LEFT_TO_RIGHT, iColumns, wColorFlags, ucChar);
  364.     }
  365. }
  366.  
  367. void InsLine(const SHORT iY,
  368.              const SHORT iLines,
  369.              const DWORD wColorFlags,
  370.              const UCHAR ucChar)
  371. {
  372.     if(!_iOK) {_Initialize();}
  373.     Window_t sWind_ = {.Min.X  = sWindow.Min.X,
  374.                        .Min.Y  = (iY - 1) + sWindow.Min.Y,
  375.                        .Max.X  = sWindow.Max.X,
  376.                        .Max.Y  = sWindow.Max.Y,
  377.                        .Size.X = sWindow.Size.X,
  378.                        .Size.Y = (sWindow.Max.Y - sWind_.Min.Y) + 1};
  379.     if((iLines > 0)
  380.             && (iLines <= sWind_.Size.Y)
  381.             && (sWind_.Min.Y >= sWindow.Min.Y)
  382.             && (sWind_.Min.Y <= sWindow.Max.Y))
  383.     {
  384.         _Block(sWind_, INSERT_TOP_TO_BOTTOM, iLines, wColorFlags, ucChar);
  385.     }
  386.  
  387. }
  388.  
  389. void InsFullLine(const SHORT iY,
  390.                  const SHORT iLines,
  391.                  const DWORD wColorFlags,
  392.                  const UCHAR ucChar)
  393. {
  394.     if(!_iOK) {_Initialize();}
  395.     Window_t sWind_ = {.Min.X  = sScreen.Boundary.Min.X,
  396.                        .Min.Y  = iY - 1,
  397.                        .Max.X  = sScreen.Boundary.Max.X,
  398.                        .Max.Y  = sScreen.Boundary.Max.Y,
  399.                        .Size.X = sScreen.Boundary.Size.X,
  400.                        .Size.Y = (sScreen.Boundary.Max.Y - sWind_.Min.Y) + 1};
  401.     if((iLines > 0)
  402.             && (iLines <= sWind_.Size.Y)
  403.             && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
  404.             && (sWind_.Min.Y <= sScreen.Boundary.Max.Y))
  405.     {
  406.         _Block(sWind_, INSERT_TOP_TO_BOTTOM, iLines, wColorFlags, ucChar);
  407.     }
  408.  
  409. }
  410.  
  411. void InsColumn(const SHORT iX,
  412.                const SHORT iColumns,
  413.                const DWORD wColorFlags,
  414.                const UCHAR ucChar)
  415. {
  416.     if(!_iOK) {_Initialize();}
  417.     Window_t sWind_ = {.Min.X  = (iX - 1) + sWindow.Min.X,
  418.                        .Min.Y  = sWindow.Min.Y,
  419.                        .Max.X  = sWindow.Max.X,
  420.                        .Max.Y  = sWindow.Max.Y,
  421.                        .Size.X = (sWindow.Max.X - sWind_.Min.X) + 1,
  422.                        .Size.Y = sWindow.Size.Y};
  423.     if((iColumns > 0)
  424.             && (iColumns <= sWind_.Size.X)
  425.             && (sWind_.Min.X >= sWindow.Min.X)
  426.             && (sWind_.Min.X <= sWindow.Max.X))
  427.     {
  428.         _Block(sWind_, INSERT_LEFT_TO_RIGHT, iColumns, wColorFlags, ucChar);
  429.     }
  430. }
  431.  
  432. void InsFullColumn(const SHORT iX,
  433.                    const SHORT iColumns,
  434.                    const DWORD wColorFlags,
  435.                    const UCHAR ucChar)
  436. {
  437.     if(!_iOK) {_Initialize();}
  438.     Window_t sWind_ = {.Min.X  = iX - 1,
  439.                        .Min.Y  = sScreen.Boundary.Min.Y,
  440.                        .Max.X  = sScreen.Boundary.Max.X,
  441.                        .Max.Y  = sScreen.Boundary.Max.Y,
  442.                        .Size.X = (sScreen.Boundary.Max.X - sWind_.Min.X) + 1,
  443.                        .Size.Y = sScreen.Boundary.Size.Y};
  444.     if((iColumns > 0)
  445.             && (iColumns <= sWind_.Size.X)
  446.             && (sWind_.Min.X >= sScreen.Boundary.Min.X)
  447.             && (sWind_.Min.X <= sScreen.Boundary.Max.X))
  448.     {
  449.         _Block(sWind_, INSERT_LEFT_TO_RIGHT, iColumns, wColorFlags, ucChar);
  450.     }
  451. }
  452.  
  453. void DelLine(const SHORT iY,
  454.              const SHORT iLines,
  455.              const DWORD wColorFlags,
  456.              const UCHAR ucChar)
  457. {
  458.     if(!_iOK) {_Initialize();}
  459.     Window_t sWind_ = {.Min.X  = sWindow.Min.X,
  460.                        .Min.Y  = (iY - 1) + sWindow.Min.Y,
  461.                        .Max.X  = sWindow.Max.X,
  462.                        .Max.Y  = sWindow.Max.Y,
  463.                        .Size.X = sWindow.Size.X,
  464.                        .Size.Y = (sWindow.Max.Y - sWind_.Min.Y) + 1};
  465.     if((iLines > 0)
  466.             && (iLines <= sWind_.Size.Y)
  467.             && (sWind_.Min.Y >= sWindow.Min.Y)
  468.             && (sWind_.Min.Y <= sWindow.Max.Y))
  469.     {
  470.         _Block(sWind_, INSERT_BOTTOM_TO_TOP, iLines, wColorFlags, ucChar);
  471.     }
  472.  
  473. }
  474.  
  475. void DelFullLine(const SHORT iY,
  476.                  const SHORT iLines,
  477.                  const DWORD wColorFlags,
  478.                  const UCHAR ucChar)
  479. {
  480.     if(!_iOK) {_Initialize();}
  481.     Window_t sWind_ = {.Min.X  = sScreen.Boundary.Min.X,
  482.                        .Min.Y  = iY - 1,
  483.                        .Max.X  = sScreen.Boundary.Max.X,
  484.                        .Max.Y  = sScreen.Boundary.Max.Y,
  485.                        .Size.X = sScreen.Boundary.Size.X,
  486.                        .Size.Y = (sScreen.Boundary.Max.Y - sWind_.Min.Y) + 1};
  487.     if((iLines > 0)
  488.             && (iLines <= sWind_.Size.Y)
  489.             && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
  490.             && (sWind_.Min.Y <= sScreen.Boundary.Max.Y))
  491.     {
  492.         _Block(sWind_, INSERT_BOTTOM_TO_TOP, iLines, wColorFlags, ucChar);
  493.     }
  494.  
  495. }
  496.  
  497. void DelColumn(const SHORT iX,
  498.                const SHORT iColumns,
  499.                const DWORD wColorFlags,
  500.                const UCHAR ucChar)
  501. {
  502.     if(!_iOK) {_Initialize();}
  503.     Window_t sWind_ = {.Min.X  = (iX - 1) + sWindow.Min.X,
  504.                        .Min.Y  = sWindow.Min.Y,
  505.                        .Max.X  = sWindow.Max.X,
  506.                        .Max.Y  = sWindow.Max.Y,
  507.                        .Size.X = (sWindow.Max.X - sWind_.Min.X) + 1,
  508.                        .Size.Y = sWindow.Size.Y};
  509.     if((iColumns > 0)
  510.             && (iColumns <= sWind_.Size.X)
  511.             && (sWind_.Min.X >= sWindow.Min.X)
  512.             && (sWind_.Min.X <= sWindow.Max.X))
  513.     {
  514.         _Block(sWind_, INSERT_RIGHT_TO_LEFT, iColumns, wColorFlags, ucChar);
  515.     }
  516. }
  517.  
  518. void DelFullColumn(const SHORT iX,
  519.                    const SHORT iColumns,
  520.                    const DWORD wColorFlags,
  521.                    const UCHAR ucChar)
  522. {
  523.     if(!_iOK) {_Initialize();}
  524.     Window_t sWind_ = {.Min.X  = iX - 1,
  525.                        .Min.Y  = sScreen.Boundary.Min.Y,
  526.                        .Max.X  = sScreen.Boundary.Max.X,
  527.                        .Max.Y  = sScreen.Boundary.Max.Y,
  528.                        .Size.X = (sScreen.Boundary.Max.X - sWind_.Min.X) + 1,
  529.                        .Size.Y = sScreen.Boundary.Size.Y};
  530.     if((iColumns > 0)
  531.             && (iColumns <= sWind_.Size.X)
  532.             && (sWind_.Min.X >= sScreen.Boundary.Min.X)
  533.             && (sWind_.Min.X <= sScreen.Boundary.Max.X))
  534.     {
  535.         _Block(sWind_, INSERT_RIGHT_TO_LEFT, iColumns, wColorFlags, ucChar);
  536.     }
  537. }
  538.  
  539. void DrawRect(const SHORT iX1,
  540.               const SHORT iY1,
  541.               const SHORT iX2,
  542.               const SHORT iY2,
  543.               const DWORD wColorFlags,
  544.               const UCHAR ucChar)
  545. {
  546.     if(!_iOK) {_Initialize();}
  547.     Window_t sWind_ = {.Min.X  = iX1 - 1,
  548.                        .Min.Y  = iY1 - 1,
  549.                        .Max.X  = iX2 - 1,
  550.                        .Max.Y  = iY2 - 1,
  551.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  552.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  553.     if((sWind_.Min.X <= sWind_.Max.X)
  554.             && (sWind_.Min.Y <= sWind_.Max.Y)
  555.             && (sWind_.Min.X >= sScreen.Boundary.Min.X)
  556.             && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
  557.             && (sWind_.Max.X <= sScreen.Boundary.Max.X)
  558.             && (sWind_.Max.Y <= sScreen.Boundary.Max.Y))
  559.     {
  560.         _Block(sWind_, CLEAR_TOP_TO_BOTTOM, sWind_.Size.Y, wColorFlags, ucChar);
  561.     }
  562. }
  563.  
  564. void DrawRectFmt(const SHORT iX1,
  565.                  const SHORT iY1,
  566.                  const SHORT iSizeX,
  567.                  const SHORT iSizeY,
  568.                  const DWORD wColorFlags,
  569.                  const UCHAR ucChar)
  570. {
  571.     DrawRect(iX1, iY1, (iX1 + iSizeX) - 1, (iY1 + iSizeY) - 1, wColorFlags, ucChar);
  572. }
  573.  
  574. void RpColor(const DWORD wColorFlags)
  575. {
  576.     if(!_iOK) {_Initialize();}
  577.     Window_t sWind_ = {.Min.X  = sWindow.Min.X,
  578.                        .Min.Y  = sWindow.Min.Y,
  579.                        .Max.X  = sWindow.Max.X, .Max.Y = sWindow.Max.Y,
  580.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  581.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  582.     _Block(sWind_, REPLACE_TOP_TO_BOTTOM, sWind_.Size.Y, wColorFlags, ' ');
  583. }
  584.  
  585. void RpColorFull(const DWORD wColorFlags)
  586. {
  587.     if(!_iOK) {_Initialize();}
  588.     Window_t sWind_ = {.Min.X  = sScreen.Boundary.Min.X,
  589.                        .Min.Y  = sScreen.Boundary.Min.Y,
  590.                        .Max.X  = sScreen.Boundary.Max.X,
  591.                        .Max.Y  = sScreen.Boundary.Max.Y,
  592.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  593.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  594.     _Block(sWind_, REPLACE_TOP_TO_BOTTOM, sWind_.Size.Y, wColorFlags, ' ');
  595. }
  596.  
  597. void RpColorEol(const DWORD wColorFlags)
  598. {
  599.     if(!_iOK) {_Initialize();}
  600.     Window_t sWind_ = {.Min.X  = _Where().X,
  601.                        .Min.Y  = _Where().Y,
  602.                        .Max.X  = sWindow.Max.X,
  603.                        .Max.Y  = sWind_.Min.Y,
  604.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  605.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  606.     if((sWind_.Min.X >= sWindow.Min.X)
  607.             && (sWind_.Min.X <= sWindow.Max.X)
  608.             && (sWind_.Min.Y >= sWindow.Min.Y)
  609.             && (sWind_.Min.Y <= sWindow.Max.Y))
  610.     {
  611.         _Block(sWind_, REPLACE_TOP_TO_BOTTOM, 1, wColorFlags, ' ');
  612.     }
  613. }
  614.  
  615. void RpColorLine(const SHORT iY,
  616.                  const SHORT iLines,
  617.                  const DWORD wColorFlags)
  618. {
  619.     if(!_iOK) {_Initialize();}
  620.     Window_t sWind_ = {.Min.X  = sWindow.Min.X,
  621.                        .Min.Y  = (iY - 1) + sWindow.Min.Y,
  622.                        .Max.X  = sWindow.Max.X,
  623.                        .Max.Y  = (sWind_.Min.Y + iLines) - 1,
  624.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  625.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  626.     if((iLines > 0)
  627.             && (sWind_.Min.Y >= sWindow.Min.Y)
  628.             && (sWind_.Max.Y <= sWindow.Max.Y))
  629.     {
  630.         _Block(sWind_, REPLACE_TOP_TO_BOTTOM, iLines, wColorFlags, ' ');
  631.     }
  632. }
  633.  
  634. void RpColorFullLine(const SHORT iY,
  635.                      const SHORT iLines,
  636.                      const DWORD wColorFlags)
  637. {
  638.     if(!_iOK) {_Initialize();}
  639.     Window_t sWind_ = {.Min.X  = sScreen.Boundary.Min.X,
  640.                        .Min.Y  = iY - 1,
  641.                        .Max.X  = sScreen.Boundary.Max.X,
  642.                        .Max.Y  = (sWind_.Min.Y + iLines) - 1,
  643.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  644.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  645.     if((iLines > 0)
  646.             && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
  647.             && (sWind_.Max.Y <= sScreen.Boundary.Max.Y))
  648.     {
  649.         _Block(sWind_, REPLACE_TOP_TO_BOTTOM, iLines, wColorFlags, ' ');
  650.     }
  651. }
  652.  
  653. void RpColorEoc(const DWORD wColorFlags)
  654. {
  655.     if(!_iOK) {_Initialize();}
  656.     Window_t sWind_ = {.Min.X  = _Where().X,
  657.                        .Min.Y  = _Where().Y,
  658.                        .Max.X  = sWind_.Min.X,
  659.                        .Max.Y  = sWindow.Max.Y,
  660.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  661.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  662.     if((sWind_.Min.X >= sWindow.Min.X)
  663.             && (sWind_.Min.X <= sWindow.Max.X)
  664.             && (sWind_.Min.Y >= sWindow.Min.Y)
  665.             && (sWind_.Min.Y <= sWindow.Max.Y))
  666.     {
  667.         _Block(sWind_, REPLACE_LEFT_TO_RIGHT, 1, wColorFlags, ' ');
  668.     }
  669. }
  670.  
  671. void RpColorColumn(const SHORT iX,
  672.                    const SHORT iColumns,
  673.                    const DWORD wColorFlags)
  674. {
  675.     if(!_iOK) {_Initialize();}
  676.     Window_t sWind_ = {.Min.X  = (iX - 1) + sWindow.Min.X,
  677.                        .Min.Y  = sWindow.Min.Y,
  678.                        .Max.X  = (sWind_.Min.X + iColumns) - 1,
  679.                        .Max.Y  = sWindow.Max.Y,
  680.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  681.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  682.     if((iColumns > 0)
  683.             && (sWind_.Min.X >= sWindow.Min.X)
  684.             && (sWind_.Max.X <= sWindow.Max.X))
  685.     {
  686.         _Block(sWind_, REPLACE_LEFT_TO_RIGHT, iColumns, wColorFlags, ' ');
  687.     }
  688. }
  689.  
  690. void RpColorFullColumn(const SHORT iX,
  691.                        const SHORT iColumns,
  692.                        const DWORD wColorFlags)
  693. {
  694.     if(!_iOK) {_Initialize();}
  695.     Window_t sWind_ = {.Min.X  = iX - 1,
  696.                        .Min.Y  = sScreen.Boundary.Min.Y,
  697.                        .Max.X  = (sWind_.Min.X + iColumns) - 1,
  698.                        .Max.Y  = sScreen.Boundary.Max.Y,
  699.                        .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
  700.                        .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
  701.     if((iColumns > 0)
  702.             && (sWind_.Min.X >= sScreen.Boundary.Min.X)
  703.             && (sWind_.Max.X <= sScreen.Boundary.Max.X))
  704.     {
  705.         _Block(sWind_, REPLACE_LEFT_TO_RIGHT, iColumns, wColorFlags, ' ');
  706.     }
  707. }
  708.  
  709. // Intern use.
  710. void _Initialize(void)
  711. {
  712.     hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  713.     CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
  714.     GetConsoleScreenBufferInfo(hConsoleOutput, &ConsoleScreenBufferInfo);
  715.     memcpy(&sWindow.Min, &ConsoleScreenBufferInfo.srWindow, sizeof(SMALL_RECT));
  716.     sWindow.Size.X = (sWindow.Max.X - sWindow.Min.X) + 1;
  717.     sWindow.Size.Y = (sWindow.Max.Y - sWindow.Min.Y) + 1;
  718.     memcpy(&sScreen.Window.Min, &sWindow.Min, sizeof(Window_t));
  719.     memcpy(&sScreen.Boundary.Min, &sWindow.Min, sizeof(COORD));
  720.     sScreen.Boundary.Max.X = ConsoleScreenBufferInfo.dwSize.X - 1;
  721.     sScreen.Boundary.Max.Y = ConsoleScreenBufferInfo.dwSize.Y - 1;
  722.     sScreen.Boundary.Size.X = (sScreen.Boundary.Max.X - sScreen.Boundary.Min.X) + 1;
  723.     sScreen.Boundary.Size.Y = (sScreen.Boundary.Max.Y - sScreen.Boundary.Min.Y) + 1;
  724.     _iOK = TRUE;
  725. }
  726.  
  727. COORD _Where(void)
  728. {
  729.     if(!_iOK) {_Initialize();}
  730.     CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
  731.     GetConsoleScreenBufferInfo(hConsoleOutput, &ConsoleScreenBufferInfo);
  732.     return ConsoleScreenBufferInfo.dwCursorPosition;
  733. }
  734.  
  735. void _Block(const Window_t sWind_,
  736.             const UCHAR cHowInsert,
  737.             const SHORT iHowMany,
  738.             const DWORD wAttributes,
  739.             const UCHAR ucChar)
  740. {
  741.     if(!_iOK) {_Initialize();}
  742.     SHORT iBlockSize = iHowMany;
  743.     switch(cHowInsert)
  744.     {
  745.         case CLEAR_LEFT_TO_RIGHT   :
  746.         case INSERT_RIGHT_TO_LEFT  :
  747.         case INSERT_LEFT_TO_RIGHT  :
  748.         case REPLACE_LEFT_TO_RIGHT : iBlockSize *= sWind_.Size.Y;
  749.                                      break;
  750.         case CLEAR_TOP_TO_BOTTOM   :
  751.         case INSERT_BOTTOM_TO_TOP  :
  752.         case INSERT_TOP_TO_BOTTOM  :
  753.         case REPLACE_TOP_TO_BOTTOM : iBlockSize *= sWind_.Size.X;
  754.                                      break;
  755.     }
  756.     COORD dwBufferSize  = {.X = sWind_.Size.X, .Y = sWind_.Size.Y};
  757.     COORD dwBufferCoord = {.X = (cHowInsert == INSERT_LEFT_TO_RIGHT) ? iHowMany : 0,
  758.                            .Y = (cHowInsert == INSERT_TOP_TO_BOTTOM) ? iHowMany : 0};
  759.     SMALL_RECT lpReadWriteRegion = {.Left   = (cHowInsert == INSERT_RIGHT_TO_LEFT) ? (sWind_.Min.X + iHowMany) : sWind_.Min.X,
  760.                                     .Top    = (cHowInsert == INSERT_BOTTOM_TO_TOP) ? (sWind_.Min.Y + iHowMany) : sWind_.Min.Y,
  761.                                     .Right  = (cHowInsert == INSERT_LEFT_TO_RIGHT) ? (sWind_.Max.X - iHowMany) : sWind_.Max.X,
  762.                                     .Bottom = (cHowInsert == INSERT_TOP_TO_BOTTOM) ? (sWind_.Max.Y - iHowMany) : sWind_.Max.Y};
  763.     CHAR_INFO (*lpBuffer)[dwBufferSize.X] = (CHAR_INFO(*)[])calloc(dwBufferSize.X * dwBufferSize.Y, sizeof(CHAR_INFO));
  764.     switch(cHowInsert)
  765.     {
  766.         case INSERT_LEFT_TO_RIGHT  :
  767.         case INSERT_RIGHT_TO_LEFT  :
  768.         case INSERT_TOP_TO_BOTTOM  :
  769.         case INSERT_BOTTOM_TO_TOP  :
  770.         case REPLACE_LEFT_TO_RIGHT :
  771.         case REPLACE_TOP_TO_BOTTOM : ReadConsoleOutput(hConsoleOutput, (CHAR_INFO*)lpBuffer, dwBufferSize, dwBufferCoord, &lpReadWriteRegion);
  772.                                      break;
  773.     }
  774.     for(SHORT uiCount = 0; uiCount < iBlockSize; uiCount++)
  775.     {
  776.         SHORT iLine = 0, iColumn = 0;
  777.         switch(cHowInsert)
  778.         {
  779.             case INSERT_RIGHT_TO_LEFT  : iColumn  = (sWind_.Size.X - iHowMany);
  780.             case CLEAR_LEFT_TO_RIGHT   :
  781.             case INSERT_LEFT_TO_RIGHT  :
  782.             case REPLACE_LEFT_TO_RIGHT : iLine    = (uiCount % sWind_.Size.Y);
  783.                                          iColumn += (uiCount / sWind_.Size.Y);
  784.                                          break;
  785.             case INSERT_BOTTOM_TO_TOP  : iLine    = (sWind_.Size.Y - iHowMany);
  786.             case CLEAR_TOP_TO_BOTTOM   :
  787.             case INSERT_TOP_TO_BOTTOM  :
  788.             case REPLACE_TOP_TO_BOTTOM : iLine   += (uiCount / sWind_.Size.X);
  789.                                          iColumn  = (uiCount % sWind_.Size.X);
  790.                                          break;
  791.         }
  792.         switch(cHowInsert)
  793.         {
  794.             default                    : lpBuffer[iLine][iColumn].Char.AsciiChar = ucChar;
  795.             case REPLACE_LEFT_TO_RIGHT :
  796.             case REPLACE_TOP_TO_BOTTOM : lpBuffer[iLine][iColumn].Attributes = wAttributes;
  797.                                          break;
  798.         }
  799.     }
  800.     switch(cHowInsert)
  801.     {
  802.         case INSERT_LEFT_TO_RIGHT : dwBufferCoord.X -= iHowMany;
  803.                                     lpReadWriteRegion.Right += iHowMany;
  804.                                     break;
  805.         case INSERT_RIGHT_TO_LEFT : lpReadWriteRegion.Left -= iHowMany;
  806.                                     break;
  807.         case INSERT_TOP_TO_BOTTOM : dwBufferCoord.Y -= iHowMany;
  808.                                     lpReadWriteRegion.Bottom += iHowMany;
  809.                                     break;
  810.         case INSERT_BOTTOM_TO_TOP : lpReadWriteRegion.Top -= iHowMany;
  811.                                     break;
  812.     }
  813.     WriteConsoleOutput(hConsoleOutput, (CHAR_INFO*)lpBuffer, dwBufferSize, dwBufferCoord, &lpReadWriteRegion);
  814.     free(lpBuffer);
  815. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement