Implementing artistic screen clearing in C
Raising the question: when we write programs, we often need to clear the screen, such as cls under DOS, clrscr() under Turbo
C, and so on. They all have screen-clearing capability, but these are only screen clears in the general sense, and do not show any particular clearing pattern. But sometimes, in order to achieve an artistic and pleasing screen-clearing effect, we often have some specific requirements for clearing the screen, such as: opening screen clear; closing screen clear; upward clear; downward clear; center clear. For this reason, several subfunctions written in C are provided here. When used in a program, they can not only achieve the purpose of clearing the screen, but also add artistic beauty to the display.
Subfunctions and demo program:
#include
#include
#include
void goto_xy(int x,int y);
void dcls(int x1,int x2,int y1,int y2);
void bcls(int x1,int x2,int y1,int y2);
void kcls(int x1,int x2,int y1,int y2);
void recls(int x1,int x2,int y1,int y2);
void zcls(int x1,int x2,int y1,int y2);
void puta(void);
/*--------------Demo program---------------------*/
main()
{
puta();
getch();
dcls(0,4,0,79);
getch();
puta();
getch();
bcls(0,25,0,79);
getch();
puta();
getch();
zcls(0,25,0,79);
getch();
}
/*********center clear screen(center clear screen)***********/
void zcls(int x1,int x2,int y1,int y2)
{
int x00,y00,x0,y0,i,d;
if((y2-y1)>(x2-x1)){
d=(x2-x1)/2;
x0=(x1+x2)/2;
y0=y1+d;
y00=y2-d;
for(i=0;i<(d+1);i++)
recls((x0-i),(x00+i),(y0-i),(y00+i));
delay(10);
}
else{
d=(y2-y1)/2;
y0=(y1+y2)/2;
x0=x1+d;
x00=x2-d;
for(i=0;i<d+1;i++)
recls(x0-i,x00+i,y0-i,y00+i);
delay(10);
}
}
/************* clear rectangle side(rectangle-edge clear)***********************/
void recls(int x1,int x2,int y1,int y2)
{
int i,j;
for(i=y1;i<y2;i++){
goto_xy(x1,i);
putchar(' '
;
goto_xy(x2,i);
putchar(' '
;
delay(10);
}
for(j=x1;j<x2;j++){
goto_xy(i,y1);
putchar(' '
;
goto_xy(j,y2);
putchar(' '
;
delay(10);
}
}
/******************open screen clear(opening-style clear)*********************/
void kcls(int x1,int x2,int y1,int y2)
{
int t,s,i,j;
t=s=(y1+y2)/2;
for(;t<=y2;t++,s--)
for(j=x1;j<x2;j++){
goto_xy(j,t);
putchar(' '
;
goto_xy(j,s);
putchar(' '
;
delay(10);
}
}
/*****************close screen clear*****closing-style clear*******************/
void bcls(int x1,int x2,int y1,int y2)
{
int t,s,j;
t=y1;
s=y2;
for(t=y1;t<(y1+y2)/2;t++,s--)
for(j=x1;jx1;j--)
for(i=y1;i<y2;i++){
goto_xy(j,i);
putchar(' '
;
delay(10);
}
}
/******************Subfunction for setting the cursor******************/
void goto_xy(int x,int y)
{
union REGS r;
r.h.ah=2;
r.h.dl=y;
r.h.dh=x;
r.h.bh=0;
int86(0x10,&r,&r);
}
/**********************Print a string of letter a's on the screen for the demo program******************/
void puta(void)
{
int i,j;
for(i=0;i<24;i++){
for(j=0;j<79;j++){
goto_xy(i,j);
printf("a"
;
}
}
}
The calls in the main function are only for a demo program; for actual use, just make slight modifications.
Copyright © 2002 Computer Room, Tieling Teachers College, Liaoning Province
Raising the question: when we write programs, we often need to clear the screen, such as cls under DOS, clrscr() under Turbo
C, and so on. They all have screen-clearing capability, but these are only screen clears in the general sense, and do not show any particular clearing pattern. But sometimes, in order to achieve an artistic and pleasing screen-clearing effect, we often have some specific requirements for clearing the screen, such as: opening screen clear; closing screen clear; upward clear; downward clear; center clear. For this reason, several subfunctions written in C are provided here. When used in a program, they can not only achieve the purpose of clearing the screen, but also add artistic beauty to the display.
Subfunctions and demo program:
#include
#include
#include
void goto_xy(int x,int y);
void dcls(int x1,int x2,int y1,int y2);
void bcls(int x1,int x2,int y1,int y2);
void kcls(int x1,int x2,int y1,int y2);
void recls(int x1,int x2,int y1,int y2);
void zcls(int x1,int x2,int y1,int y2);
void puta(void);
/*--------------Demo program---------------------*/
main()
{
puta();
getch();
dcls(0,4,0,79);
getch();
puta();
getch();
bcls(0,25,0,79);
getch();
puta();
getch();
zcls(0,25,0,79);
getch();
}
/*********center clear screen(center clear screen)***********/
void zcls(int x1,int x2,int y1,int y2)
{
int x00,y00,x0,y0,i,d;
if((y2-y1)>(x2-x1)){
d=(x2-x1)/2;
x0=(x1+x2)/2;
y0=y1+d;
y00=y2-d;
for(i=0;i<(d+1);i++)
recls((x0-i),(x00+i),(y0-i),(y00+i));
delay(10);
}
else{
d=(y2-y1)/2;
y0=(y1+y2)/2;
x0=x1+d;
x00=x2-d;
for(i=0;i<d+1;i++)
recls(x0-i,x00+i,y0-i,y00+i);
delay(10);
}
}
/************* clear rectangle side(rectangle-edge clear)***********************/
void recls(int x1,int x2,int y1,int y2)
{
int i,j;
for(i=y1;i<y2;i++){
goto_xy(x1,i);
putchar(' '
;goto_xy(x2,i);
putchar(' '
;delay(10);
}
for(j=x1;j<x2;j++){
goto_xy(i,y1);
putchar(' '
;goto_xy(j,y2);
putchar(' '
;delay(10);
}
}
/******************open screen clear(opening-style clear)*********************/
void kcls(int x1,int x2,int y1,int y2)
{
int t,s,i,j;
t=s=(y1+y2)/2;
for(;t<=y2;t++,s--)
for(j=x1;j<x2;j++){
goto_xy(j,t);
putchar(' '
;goto_xy(j,s);
putchar(' '
;delay(10);
}
}
/*****************close screen clear*****closing-style clear*******************/
void bcls(int x1,int x2,int y1,int y2)
{
int t,s,j;
t=y1;
s=y2;
for(t=y1;t<(y1+y2)/2;t++,s--)
for(j=x1;jx1;j--)
for(i=y1;i<y2;i++){
goto_xy(j,i);
putchar(' '
;delay(10);
}
}
/******************Subfunction for setting the cursor******************/
void goto_xy(int x,int y)
{
union REGS r;
r.h.ah=2;
r.h.dl=y;
r.h.dh=x;
r.h.bh=0;
int86(0x10,&r,&r);
}
/**********************Print a string of letter a's on the screen for the demo program******************/
void puta(void)
{
int i,j;
for(i=0;i<24;i++){
for(j=0;j<79;j++){
goto_xy(i,j);
printf("a"
;}
}
}
The calls in the main function are only for a demo program; for actual use, just make slight modifications.
Copyright © 2002 Computer Room, Tieling Teachers College, Liaoning Province
Recent Ratings for This Post
( 1 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| AlexZhang | +2 | 2026-01-30 21:14 |
ko20010214
=================================
大功告成,打个Kiss!
ko20010214@MSN.com
神州优雅Q300C
Intel CeleronM 370处理器 | 256MbDDR内存
40G硬盘 | USB2.0 | IEEE 1394
13.3 ' WXGA 宽屏(16:10) | COMBO光驱
10/100M网卡 | 四合一读卡器
=================================
大功告成,打个Kiss!
ko20010214@MSN.com
神州优雅Q300C
Intel CeleronM 370处理器 | 256MbDDR内存
40G硬盘 | USB2.0 | IEEE 1394
13.3 ' WXGA 宽屏(16:10) | COMBO光驱
10/100M网卡 | 四合一读卡器





