CRT_Demo

The CRT unit offers many features for extending the use of the console window. The following procedures and functions are demonstrated in program CRT_Demo:

  • TextColor(TextColour) sets the text colour to TextColour.
  • TextBackground(BackColour) sets the text background to BackColour.
  • Window(x1, y1, x2, y2) creates a window with top left coordinate (x1,y1) and bottom right coordinate (x2, y2). The coordinates are character positions, not pixels. The top left coordinate is (1,1) and the maximum value of x is typically 80.
  • ClrScr clears the current window to the colour of the text background.
  • GoToXY(x, y) moves the cursor to coordinate (x,y).
  • WindMaxX returns the maximum value of x in the current window.
  • WindMaxY returns the maximum value of y in the current window.
  • CursorOff turns the cursor off.
  • CursorOn turns the cursor on.
  • ReadKey returns the char typed at the keyboard.
  • HighVideo turns on bold text.
  • NormVideo returns text back to normal.
program CRT_Demo;
  {$mode objfpc}{$H+}
uses
  CRT;
const
  Colours : array[0..7] of string = ('Black', 'Blue', 'Green', 'Cyan',  'Red',
                                      'Magenta',  'Brown',  'LightGray');
var
  Count, Colour: integer ;
  MaxX, Width, Height, XLeft, XRight, YTop, YBottom : integer;
  InputChar : Char;
  Response : string;

procedure ColouredWindow (X1, Y1, X2, Y2, Colour: integer);
begin
  //Colour the window
  window(X1, Y1 , X2, Y2);
  textBackground(Colour);
  clrScr;
  //Write window's colour half way down
  textColor(LightGreen);
  goToXY(1, Height DIV 2);
  write(Colours[Colour]);
end;

begin
  MaxX := windMaxX;
  //Draw a rectangle for each background colour.
  Width := MaxX DIV 8;
  //Same number of pixels vertically as horizontally
  Height := Width;
  for Count := 0 to 7 do
    begin
      XLeft :=  1 + Width * Count;
      XRight := XLeft + Width;
      YTop := 1;
      YBottom := Height + YTop;
      ColouredWindow(XLeft, YTop, XRight, YBottom, Count);
    end;
  ColouredWindow(1, Height + YTop, 1 + 7 * Width, 2 * Height + YTop, White);
  textColor(Yellow);
  cursorOff;
  goToXY(1, 5);
  writeln('Press return to continue. ');
  InputChar := readKey;
  cursorOn;
  textBackground(Black);
  window(1, 1, MaxX, YTop + 2 * Height);
  clrScr;
  normVideo;
  writeln('Normal text');
  highVideo;
  writeln('Highlighted text');
  normvideo;
  writeln('Text back to normal');
  write('Please type a character. ');
  InputChar := readKey;
  Response := 'Input character: ' + InputChar;
  //Output Response in middle of current window
  goToXY((MaxX - Length(Response))  DIV 2, windMaxY DIV 2 );
  writeln(Response);
  textColor(Yellow);
  write('Press return to exit. ');
  readln;
end.

Student programs using the CRT unit

See this part of the table of programs for many fine examples of the use of the CRT unit. We think that the most impressive are Ascii3D by Peter Hearnshaw and PascalPokemon by Max Foster.

Programming - a skill for life!

Vector Graphics using the CRT, Graph and WinGraph units and output to SVG file