soulik

I love C++

Jun 30th, 2016
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <string.h>
  3.  
  4. bool setConsoleFontSize(HANDLE console, COORD dwFontSize){
  5.     CONSOLE_FONT_INFOEX info{ sizeof(CONSOLE_FONT_INFOEX) };
  6.     if (!GetCurrentConsoleFontEx(console, false, &info))
  7.         return false;
  8.     info.dwFontSize = dwFontSize;
  9.     return SetCurrentConsoleFontEx(console, false, &info);
  10. }
  11.  
  12. void getConsoleSize(HANDLE console, short & width, short & height){
  13.     CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
  14.  
  15.     GetConsoleScreenBufferInfo(console, &bufferInfo);
  16.     width = bufferInfo.srWindow.Right;
  17.     height = bufferInfo.srWindow.Bottom;
  18. }
  19.  
  20. void niceText(const char * text, unsigned short baseColor = 0x07, unsigned short specialColor = 0x07){
  21.     int length = strlen(text);
  22.     unsigned short * attributes = new unsigned short[length];
  23.     unsigned short * highLightAttributes = new unsigned short[length];
  24.  
  25.     for (int i = 0; i < length; i++){
  26.         if (text[i] >= 0x20){
  27.             attributes[i] = baseColor;
  28.             highLightAttributes[i] = baseColor | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY;
  29.         }
  30.         else{
  31.             attributes[i] = specialColor;
  32.             highLightAttributes[i] = specialColor | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY;
  33.         }
  34.     }
  35.  
  36.     HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  37.     setConsoleFontSize(console, {12, 16});
  38.  
  39.     short width, height;
  40.     getConsoleSize(console, width, height);
  41.  
  42.     COORD centeredPosition = {width/2 - length/2, height/2};
  43.     COORD position = { centeredPosition.X, centeredPosition.Y };
  44.  
  45.     unsigned long charactersWritten = 0;
  46.  
  47.     for (int i = 0; i < length; i++){
  48.         position.X = centeredPosition.X + i;
  49.         WriteConsoleOutputCharacterA(console, text + i, 1, position, &charactersWritten);
  50.         WriteConsoleOutputAttribute(console, attributes + i, 1, position, &charactersWritten);
  51.         Sleep(200);
  52.     }
  53.     Sleep(1000);
  54.  
  55.     for (int i = 0; i < length; i++){
  56.         if (i > 0){
  57.             WriteConsoleOutputAttribute(console, attributes + i - 1, 1, position, &charactersWritten);
  58.         }
  59.         position.X = centeredPosition.X + i;
  60.         WriteConsoleOutputAttribute(console, highLightAttributes + i, 1, position, &charactersWritten);
  61.         Sleep(100);
  62.     }
  63.  
  64.     WriteConsoleOutputAttribute(console, attributes + length - 1, 1, position, &charactersWritten);
  65.     Sleep(1000);
  66.  
  67.     delete[] attributes;
  68.     delete[] highLightAttributes;
  69. }
  70.  
  71. int main(int argc, char ** argv, char ** env){
  72.     niceText("I \03 C++", 0x07, 0x04);
  73.     return 0;
  74. }
Advertisement