crtex This chapter describes the CRT unit for Free Pascal, both under DOS LINUX and WINDOWS. The unit was first written for DOS by Florian klämpfl. The unit was ported to LINUX by Mark May1.1, and enhanced by Michaël Van Canneyt and Peter Vreman. It works on the LINUX console, and in xterm and rxvt windows under X-Windows. The functionality for both is the same, except that under LINUX the use of an early implementation (versions 0.9.1 and earlier of the compiler) the crt unit automatically cleared the screen at program startup. This chapter is divided in two sections.
Black = 0; Blue = 1; Green = 2; Cyan = 3; Red = 4; Magenta = 5; Brown = 6; LightGray = 7; DarkGray = 8; LightBlue = 9; LightGreen = 10; LightCyan = 11; LightRed = 12; LightMagenta = 13; Yellow = 14; White = 15; Blink = 128;Miscellaneous constants
TextAttr: Byte = $07; TextChar: Char = ' '; CheckBreak: Boolean = True; CheckEOF: Boolean = False; CheckSnow: Boolean = False; DirectVideo: Boolean = False; LastMode: Word = 3; WindMin: Word = $0; WindMax: Word = $184f; ScreenWidth = 80; ScreenHeight = 25;Some variables for compatibility with Turbo Pascal. However, they're not used by Free Pascal.
var checkbreak : boolean; checkeof : boolean; checksnow : boolean;The following constants define screen modes on a DOS system:
Const bw40 = 0; co40 = 1; bw80 = 2; co80 = 3; mono = 7;The TextAttr variable controls the attributes with which characters are written to screen.
var TextAttr : byte;The DirectVideo variable controls the writing to the screen. If it is True, the the cursor is set via direct port access. If False, then the BIOS is used. This is defined under DOS only.
var DirectVideo : Boolean;The Lastmode variable tells you which mode was last selected for the screen. It is defined on DOS only.
var lastmode : Word;
Program Example1; uses Crt; { Program to demonstrate the AssignCrt function. } var F : Text; begin AssignCrt(F); Rewrite(F); { Don't forget to open for output! } WriteLn(F,'This is written to the Assigned File'); Close(F); end.
Program Example9; uses Crt; { Program to demonstrate the ClrEol function. } begin Write('This line will be cleared from the', ' cursor postion until the right of the screen'); GotoXY(27,WhereY); ReadKey; ClrEol; WriteLn; end.
Program Example8; uses Crt; { Program to demonstrate the ClrScr function. } begin Writeln('Press any key to clear the screen'); ReadKey; ClrScr; Writeln('Have fun with the cleared screen'); end.
Program Example15; uses Crt; { Program to demonstrate the Delay function. } var i : longint; begin WriteLn('Counting Down'); for i:=10 downto 1 do begin WriteLn(i); Delay(1000); {Wait one second} end; WriteLn('BOOM!!!'); end.
Program Example10; uses Crt; { Program to demonstrate the InsLine function. } begin ClrScr; WriteLn; WriteLn('Line 1'); WriteLn('Line 2'); WriteLn('Line 2'); WriteLn('Line 3'); WriteLn; WriteLn('Oops, Line 2 is listed twice,', ' let''s delete the line at the cursor postion'); GotoXY(1,3); ReadKey; DelLine; GotoXY(1,10); end.
Program Example6; uses Crt; { Program to demonstrate the GotoXY function. } begin ClrScr; GotoXY(10,10); Write('10,10'); GotoXY(70,20); Write('70,20'); GotoXY(1,22); end.
Program Example14; uses Crt; { Program to demonstrate the LowVideo, HighVideo, NormVideo functions. } begin LowVideo; WriteLn('This is written with LowVideo'); HighVideo; WriteLn('This is written with HighVideo'); NormVideo; WriteLn('This is written with NormVideo'); end.
Program Example10; uses Crt; { Program to demonstrate the InsLine function. } begin ClrScr; WriteLn; WriteLn('Line 1'); WriteLn('Line 3'); WriteLn; WriteLn('Oops, forgot Line 2, let''s insert at the cursor postion'); GotoXY(1,3); ReadKey; InsLine; Write('Line 2'); GotoXY(1,10); end.
Program Example2; uses Crt; { Program to demonstrate the KeyPressed function. } begin WriteLn('Waiting until a key is pressed'); repeat until KeyPressed; { The key is not Read, so it should also be outputted at the commandline} end.
Stops the speaker sound. This is not supported in LINUX
Program Example16; uses Crt; { Program to demonstrate the Sound and NoSound function. } var i : longint; begin WriteLn('You will hear some tones from your speaker'); while (i<15000) do begin inc(i,500); Sound(i); Delay(100); end; WriteLn('Quiet now!'); NoSound; {Stop noise} end.
The ReadKey function reads 1 key from the keyboard buffer, and returns this. If an extended or function key has been pressed, then the zero ASCII code is returned. You can then read the scan code of the key with a second ReadKey call. Remark. Key mappings under Linux can cause the wrong key to be reported by ReadKey, so caution is needed when using ReadKey.
Program Example3; uses Crt; { Program to demonstrate the ReadKey function. } var ch : char; begin writeln('Press Left/Right, Esc=Quit'); repeat ch:=ReadKey; case ch of #0 : begin ch:=ReadKey; {Read ScanCode} case ch of #75 : WriteLn('Left'); #77 : WriteLn('Right'); end; end; #27 : WriteLn('ESC'); end; until ch=#27 {Esc} end.
TextBackground sets the background color to CL. CL can be one of the predefined color constants.
Program Example13; uses Crt; { Program to demonstrate the TextBackground function. } begin TextColor(White); WriteLn('This is written in with the default background color'); TextBackground(Green); WriteLn('This is written in with a Green background'); TextBackground(Brown); WriteLn('This is written in with a Brown background'); TextBackground(Black); WriteLn('Back with a black background'); end.
TextColor sets the foreground color to CL. CL can be one of the predefined color constants.
Program Example12; uses Crt; { Program to demonstrate the TextColor function. } begin WriteLn('This is written in the default color'); TextColor(Red); WriteLn('This is written in Red'); TextColor(White); WriteLn('This is written in White'); TextColor(LightBlue); WriteLn('This is written in Light Blue'); end.
This procedure is only implemented on DOS.
WhereX returns the current X-coordinate of the cursor, relative to the current window. The origin is (1,1), in the upper-left corner of the window.
Program Example7; uses Crt; { Program to demonstrate the WhereX and WhereY functions. } begin Writeln('Cursor postion: X=',WhereX,' Y=',WhereY); end.
WhereY returns the current Y-coordinate of the cursor, relative to the current window. The origin is (1,1), in the upper-left corner of the window.
Program Example7; uses Crt; { Program to demonstrate the WhereX and WhereY functions. } begin Writeln('Cursor postion: X=',WhereX,' Y=',WhereY); end.
Program Example5; uses Crt; { Program to demonstrate the Window function. } begin ClrScr; WriteLn('Creating a window from 30,10 to 50,20'); Window(30,10,50,20); WriteLn('We are now writing in this small window we just created, we '+ 'can''t get outside it when writing long lines like this one'); Write('Press any key to clear the window'); ReadKey; ClrScr; Write('The window is cleared, press any key to restore to fullscreen'); ReadKey; {Full Screen is 80x25} Window(1,1,80,25); Clrscr; Writeln('Back in Full Screen'); end.