From 9868828cc21aca8ba58e61e688db814dd710e9b4 Mon Sep 17 00:00:00 2001 From: Freeman Date: Tue, 9 Jun 2020 00:44:44 +0300 Subject: [PATCH] CRT unit made more compatible with Turbo/FreePascal one --- .../Console/ConsoleColors/ConsoleColors.dpr | 2 +- Examples/Console/DateTime/DateTime.dpr | 18 ++-- Examples/Console/LoadFile/LoadFile.dpr | 1 - Lib/CRT.pas | 86 ++++++++++++++----- 4 files changed, 72 insertions(+), 35 deletions(-) diff --git a/Examples/Console/ConsoleColors/ConsoleColors.dpr b/Examples/Console/ConsoleColors/ConsoleColors.dpr index e352d26..f50bcc6 100644 --- a/Examples/Console/ConsoleColors/ConsoleColors.dpr +++ b/Examples/Console/ConsoleColors/ConsoleColors.dpr @@ -39,7 +39,7 @@ begin TextBackground(White); WriteLn('White'); - ResetAttributes; + NormVideo; TextColor(Black); WriteLn('Black'); diff --git a/Examples/Console/DateTime/DateTime.dpr b/Examples/Console/DateTime/DateTime.dpr index 744ce3a..f6d1904 100644 --- a/Examples/Console/DateTime/DateTime.dpr +++ b/Examples/Console/DateTime/DateTime.dpr @@ -4,29 +4,25 @@ uses KolibriOS, CRT; var - SystemDate: TSystemDate; - SystemTime: TSystemTime; - CursorPos: TConsolePoint; + CursorXY: TCursorXY; begin InitConsole('Date/Time'); - SetCursorHeight(0); - SetCursorPos(27, 11); + CursorOff; + GotoXY(27, 11); Write( 'System Date and System Time'#10 + ' ' ); - CursorPos := GetCursorPos; + CursorXY := WhereXY; repeat - SystemDate := GetSystemDate; - SystemTime := GetSystemTime; - with SystemDate, SystemTime do + with GetSystemDate, GetSystemTime do begin Write('%02x.%02x.%02x', Day, Month, Year); Write(' - %02x:%02x:%02x', Hours, Minutes, Seconds); end; - SetCursorPos(CursorPos); - Sleep(50); + GotoXY(CursorXY); + Delay(500); until KeyPressed; end. diff --git a/Examples/Console/LoadFile/LoadFile.dpr b/Examples/Console/LoadFile/LoadFile.dpr index 184348c..87a5cff 100644 --- a/Examples/Console/LoadFile/LoadFile.dpr +++ b/Examples/Console/LoadFile/LoadFile.dpr @@ -9,7 +9,6 @@ var begin InitConsole('Load File', False); - Buffer := LoadFile('/sys/example.asm', FileSize); WriteText(Buffer, FileSize); end. diff --git a/Lib/CRT.pas b/Lib/CRT.pas index 1af08b0..5bb42d1 100644 --- a/Lib/CRT.pas +++ b/Lib/CRT.pas @@ -1,3 +1,7 @@ +(* + KolibriOS CRT unit +*) + unit CRT; interface @@ -6,8 +10,8 @@ uses KolibriOS; type - TConsolePoint = record - X, Y: Integer; + TCursorXY = record + X, Y: LongWord; end; const @@ -31,11 +35,13 @@ const procedure InitConsole(Caption: PKolibriChar; CloseWindowOnExit: Boolean = True; WndWidth: LongWord = $FFFFFFFF; WndHeight: LongWord = $FFFFFFFF; ScrWidth: LongWord = $FFFFFFFF; ScrHeight: LongWord = $FFFFFFFF); -function GetCursorPos: TConsolePoint; -procedure SetCursorPos(X, Y: Integer); overload; -procedure SetCursorPos(const Point: TConsolePoint); overload; +procedure GotoXY(X, Y: Integer); overload; +procedure GotoXY(const Point: TCursorXY); overload; +function WhereX: Integer; +function WhereY: Integer; +function WhereXY: TCursorXY; -procedure ResetAttributes; +procedure NormVideo; // reset text attributes procedure TextAttribute(Color, Background: Integer); procedure TextBackground(Color: Integer); procedure TextColor(Color: Integer); @@ -43,10 +49,16 @@ procedure TextColor(Color: Integer); function WriteLn(LineBreaks: Integer = 1): LongInt; overload; function WriteLn(Text: PKolibriChar; LineBreaks: Integer = 1): LongInt; overload; +function CursorBig: LongWord; +function CursorOff: LongWord; +function CursorOn: LongWord; + +procedure Delay(Milliseconds: LongWord); // absolute Sleep(Milliseconds); + var + CursorHeight: function(Height: LongWord): LongWord; stdcall; KeyPressed: function: Boolean; ReadKey: function: KolibriChar; stdcall; - SetCursorHeight: function(Height: Integer): Integer; stdcall; Write: function(const Text: PKolibriChar): LongInt; cdecl varargs; WriteText: procedure(Text: PKolibriChar; Length: LongWord); stdcall; @@ -55,7 +67,7 @@ implementation var CloseWindow: Boolean; -procedure ResetAttributes; +procedure NormVideo; begin Write(#27'[0m'); end; @@ -130,12 +142,17 @@ begin Result := Write(Text) + WriteLn(LineBreaks); end; +procedure Delay(Milliseconds: LongWord); +begin + Sleep(Milliseconds div 10); +end; + var hConsole: Pointer; ConsoleExit: procedure(CloseWindow: Boolean); stdcall; ConsoleInit: procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord; Caption: PKolibriChar); stdcall; - GetCursorPosProc: procedure(var X, Y: Integer); stdcall; - SetCursorPosProc: procedure(X, Y: Integer); stdcall; + GotoXYProc: procedure(X, Y: LongWord); stdcall; + WhereXYProc: procedure(var X, Y: LongWord); stdcall; procedure InitConsole(Caption: PKolibriChar; CloseWindowOnExit: Boolean; WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord); @@ -143,11 +160,11 @@ begin hConsole := LoadLibrary('/sys/lib/console.obj'); ConsoleExit := GetProcAddress(hConsole, 'con_exit'); ConsoleInit := GetProcAddress(hConsole, 'con_init'); - GetCursorPosProc := GetProcAddress(hConsole, 'con_get_cursor_pos'); + CursorHeight := GetProcAddress(hConsole, 'con_set_cursor_height'); KeyPressed := GetProcAddress(hConsole, 'con_kbhit'); ReadKey := GetProcAddress(hConsole, 'con_getch'); - SetCursorHeight := GetProcAddress(hConsole, 'con_set_cursor_height'); - SetCursorPosProc := GetProcAddress(hConsole, 'con_set_cursor_pos'); + GotoXYProc := GetProcAddress(hConsole, 'con_set_cursor_pos'); + WhereXYProc := GetProcAddress(hConsole, 'con_get_cursor_pos'); Write := GetProcAddress(hConsole, 'con_printf'); WriteText := GetProcAddress(hConsole, 'con_write_string'); @@ -155,20 +172,45 @@ begin CloseWindow := CloseWindowOnExit; end; -function GetCursorPos: TConsolePoint; +procedure GotoXY(X, Y: Integer); begin - GetCursorPosProc(Result.X, Result.Y); + GotoXYProc(X, Y); end; -procedure SetCursorPos(X, Y: Integer); -begin - SetCursorPosProc(X, Y); -end; - -procedure SetCursorPos(const Point: TConsolePoint); +procedure GotoXY(const Point: TCursorXY); begin with Point do - SetCursorPosProc(X, Y); + GotoXYProc(X, Y); +end; + +function WhereX: Integer; +begin + Result := WhereXY.X; +end; + +function WhereY: Integer; +begin + Result := WhereXY.Y; +end; + +function WhereXY: TCursorXY; +begin + WhereXYProc(Result.X, Result.Y); +end; + +function CursorBig: LongWord; +begin + Result := CursorHeight(15); +end; + +function CursorOff: LongWord; +begin + Result := CursorHeight(0); +end; + +function CursorOn: LongWord; +begin + Result := CursorHeight(2); end; initialization