1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <windows.h>
  5.  
  6. /**
  7.  * Here is a scrolly text in C in a command line console for Windows
  8.  * Tested using Code::Blocks and Windows 7 Professional SP1
  9.  *
  10.  * @Author: Shaun B
  11.  * @Version:    1.0.0.1 - 2012-09-04
  12.  * @Todo:   Find out how to print to the entire bottom line of the console
  13.  *      without forcing a line-feed.
  14.  *
  15.  **/
  16.  
  17. int main();
  18. void gotoxy(int, int);
  19. void hideCursor();
  20. void scroll();
  21. void exchangeToDisplay();
  22.  
  23. // Sets variables for:
  24. //  Scrolly message -
  25. static char scrolly [] = "....................This is a scrolly text....................Nah nah nah nah nahhhh....................Like a BAWS....................#";
  26. //  Display string -
  27. static char display [78];
  28. //  Keyboard input -
  29. static char character [1];
  30. //  Running boolean -
  31. static char run = 1;
  32. //  Reusable index counters -
  33. static unsigned char i = 0;
  34. static unsigned char c = 0;
  35. //  Sleep time -
  36. static signed int sleep = 100;
  37. //  Char buffer for scroll function -
  38. static char buffer [1];
  39.  
  40. int main()
  41. {
  42.     // Sets default console size, colours and title:
  43.     system("mode CON: COLS=80 LINES=50");
  44.     system("color 03");
  45.     system("cls");
  46.     system("title Scrolly started at %TIME% on %DATE%");
  47.     system("echo off");
  48.     // Switches off blinking cursor:
  49.     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT);
  50.     hideCursor();
  51.     // Sets up display string:
  52.     exchangeToDisplay();
  53.     // Main loop:
  54.     while(run)
  55.     {
  56.         // Sets cursor position for each scrolly text:
  57.         gotoxy(1,0);
  58.         printf("%s",display);
  59.         gotoxy(1,49);
  60.         printf("%s",display);
  61.         // Checks if keyboard has been pressed:
  62.         if (_kbhit())
  63.         {
  64.             character[0]=_getch();
  65.         }
  66.         // Checks for space...
  67.         if(character[0]==32)
  68.         {
  69.             // If so, stops main loop:
  70.             run=0;
  71.         }
  72.         // Checks for left arrow and arrow down...
  73.         if((character[0]==75 || character[0]==80) && sleep<255)
  74.         {
  75.             // Increases sleep call (higher is slower):
  76.             sleep+=10;
  77.         }
  78.         // Checks for other arrow keys...
  79.         if((character[0]==72 || character[0]==77) && sleep>0)
  80.         {
  81.             // Decreases sleep call:
  82.             sleep-=10;
  83.         }
  84.         // Clears character buffer if set:
  85.         if(character[0])
  86.         {
  87.             character[0]=0;
  88.         }
  89.         // Checks if time exceeds longest sleep time:
  90.         if(sleep>254)
  91.         {
  92.             sleep=255;
  93.         }
  94.         // Checks if time exceeds shortest sleep time:
  95.         if(sleep<1)
  96.         {
  97.             sleep=0;
  98.         }
  99.         // Calls scroll function:
  100.         scroll();
  101.         // Calls sleep function:
  102.         Sleep(sleep+1);
  103.     }
  104.     // Returns when loop is exited:
  105.     return 0;
  106. }
  107.  
  108. // Sets co-ordinates within the command line:
  109. void gotoxy(int x, int y)
  110. {
  111.     COORD coord;
  112.     coord.X = x;
  113.     coord.Y = y;
  114.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  115. }
  116.  
  117. // Turns off blinking cursor:
  118. void hideCursor()
  119. {
  120.     CONSOLE_CURSOR_INFO lpCursor;
  121.     lpCursor.bVisible = 0;
  122.     lpCursor.dwSize = 20;
  123.     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&lpCursor);
  124. }
  125.  
  126. void scroll()
  127. {
  128.     // Sets counter to zero:
  129.     c=0;
  130.     // The buffer will preserve the first char otherwise the
  131.     // scrolling won't wrap around:
  132.     buffer[0]=scrolly[c];
  133.     // Loop will happen whilst the terminator hasn't been reached:
  134.     while(scrolly[c]!='#')
  135.     {
  136.         // Moves each character one element to the left:
  137.         scrolly[c]=scrolly[c+1];
  138.         // Increase our counter
  139.         c++;
  140.     }
  141.     // Puts the old first character from the start to the end so
  142.     // it wraps around:
  143.     scrolly[c-1]=buffer[0];
  144.     // Checks to see if the terminator has been over-written, if not
  145.     // then it's restored:
  146.     if(scrolly[c]!='#')
  147.     {
  148.         scrolly[c]='#';
  149.     }
  150.     exchangeToDisplay();
  151. }
  152.  
  153. void exchangeToDisplay()
  154. {
  155.     // Writes the first 78 characters of our [shuffled] scrolly text
  156.     // to our display string:
  157.     for(c=0;c<78;c++)
  158.     {
  159.         display[c] = scrolly[c];
  160.     }
  161. }