#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
/**
* Here is a scrolly text in C in a command line console for Windows
* Tested using Code::Blocks and Windows 7 Professional SP1
*
* @Author: Shaun B
* @Version: 1.0.0.1 - 2012-09-04
* @Todo: Find out how to print to the entire bottom line of the console
* without forcing a line-feed.
*
**/
int main();
void gotoxy(int, int);
void hideCursor();
void scroll();
void exchangeToDisplay();
// Sets variables for:
// Scrolly message -
static char scrolly [] = "....................This is a scrolly text....................Nah nah nah nah nahhhh....................Like a BAWS....................#";
// Display string -
static char display [78];
// Keyboard input -
static char character [1];
// Running boolean -
static char run = 1;
// Reusable index counters -
static unsigned char i = 0;
static unsigned char c = 0;
// Sleep time -
static signed int sleep = 100;
// Char buffer for scroll function -
static char buffer [1];
int main()
{
// Sets default console size, colours and title:
system("mode CON: COLS=80 LINES=50");
system("color 03");
system("cls");
system("title Scrolly started at %TIME% on %DATE%");
system("echo off");
// Switches off blinking cursor:
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT);
hideCursor();
// Sets up display string:
exchangeToDisplay();
// Main loop:
while(run)
{
// Sets cursor position for each scrolly text:
gotoxy(1,0);
printf("%s",display);
gotoxy(1,49);
printf("%s",display);
// Checks if keyboard has been pressed:
if (_kbhit())
{
character[0]=_getch();
}
// Checks for space...
if(character[0]==32)
{
// If so, stops main loop:
run=0;
}
// Checks for left arrow and arrow down...
if((character[0]==75 || character[0]==80) && sleep<255)
{
// Increases sleep call (higher is slower):
sleep+=10;
}
// Checks for other arrow keys...
if((character[0]==72 || character[0]==77) && sleep>0)
{
// Decreases sleep call:
sleep-=10;
}
// Clears character buffer if set:
if(character[0])
{
character[0]=0;
}
// Checks if time exceeds longest sleep time:
if(sleep>254)
{
sleep=255;
}
// Checks if time exceeds shortest sleep time:
if(sleep<1)
{
sleep=0;
}
// Calls scroll function:
scroll();
// Calls sleep function:
Sleep(sleep+1);
}
// Returns when loop is exited:
return 0;
}
// Sets co-ordinates within the command line:
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// Turns off blinking cursor:
void hideCursor()
{
CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible = 0;
lpCursor.dwSize = 20;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&lpCursor);
}
void scroll()
{
// Sets counter to zero:
c=0;
// The buffer will preserve the first char otherwise the
// scrolling won't wrap around:
buffer[0]=scrolly[c];
// Loop will happen whilst the terminator hasn't been reached:
while(scrolly[c]!='#')
{
// Moves each character one element to the left:
scrolly[c]=scrolly[c+1];
// Increase our counter
c++;
}
// Puts the old first character from the start to the end so
// it wraps around:
scrolly[c-1]=buffer[0];
// Checks to see if the terminator has been over-written, if not
// then it's restored:
if(scrolly[c]!='#')
{
scrolly[c]='#';
}
exchangeToDisplay();
}
void exchangeToDisplay()
{
// Writes the first 78 characters of our [shuffled] scrolly text
// to our display string:
for(c=0;c<78;c++)
{
display[c] = scrolly[c];
}
}